80 Commits

Author SHA1 Message Date
IntegratedQuantum
b882e81c73 Always use explicit direction 2025-07-27 14:48:27 +02:00
IntegratedQuantum
be9069c8df push pop isEmpty 2025-07-26 15:18:30 +02:00
IntegratedQuantum
6baf7fc067
Garbage Collection - The Great Return (#1680)
This is a first attempt to get rid of of reference counting #1413

Basically all threads execute a global sync point, this allows
determining a time when all temporary resources (such as meshes on the
client side used in lighting on other threads) have been freed with 100%
certainty.

Remaining work:
- [x] Implement the sync point
- [x] Implement free lists and free the resources
- [x] Determine if this is worth it performance wise
  - [x] Use this for chunk meshes
- [x] Remove reference counting and the locks on the chunk storage data
structure
- [x] Measure performance of gathering many light samples and compare it
with master → around 15% reduction in time
- [x] Cleanup some unused things (mesh free list)
2025-07-21 22:20:17 +02:00
IntegratedQuantum
aae66ea77f
Update Zig to use the new Writergate version (#1664)
For reference: https://github.com/ziglang/zig/pull/24329

some commits have been extracted from #1583, but the x86_64 backend has
been disabled due to its horrible performance.

Remaining work:
- [x] Wait for official builds on ziglang.org and upload them to our
repository
- [x] Add workaround for https://github.com/ziglang/zig/pull/24466
- [x] Fix TODO comment about ANSI support in stdout
- [x] Check for compile-time performance changes → it went from 13.1 to
11.9 seconds 🎉
2025-07-15 23:00:29 +02:00
IntegratedQuantum
dd506e197e Don't allow two players of the same name to join (except from testing worlds)
fixes #1636
2025-07-01 22:47:36 +02:00
IntegratedQuantum
b1af1be3d2
Cleanup and fix the deposit on close behavior of the workbench. (#1602)
In the process I had to improve how the player inventory is handled,
which required adding an interface to externally load an inventory for
the inventory system.

This new interface is also relevant for block inventories, which is why
I would like you to review this @Argmaster

fixes #978
2025-06-06 19:26:21 +02:00
Krzysztof Wiśniewski
1f4d23464a
Add /mask command (#1337)
* Add masks

* Apply masks to /set

* Change char used for tags

* Remove mask command

* Revert "Remove mask command"

This reverts commit 83e8cdd9044452f11efdd4d515bee330491ea1f6.

* Fix use of BlockTag

* Fix use of BlockTag

* Move Pattern into Blueprint

* Improve error message on capture failure

* Change memory management

* Move EditMask to blueprint.zig

* Polish mask command

* Fix some fixes

* Fix block formatting

* Fix blueprint formatting

* Implement some of the suggestions

* Fix field renames

* Unstupidify Entry.initFromString inverse handling

* Use introspection to create Property enum

* Make or_ and_ inverse etc. private

* Use functions instead of generics

* Rename getBlockById parameter

* Update src/blueprint.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Improve error names

* Add get method to Tag

* Improve error names

* Update src/blueprint.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Change how block dispatch is done

* Add hasTag

* Quick fixes of small bugs

* Fix implementation of hasTag

* Remove redundant try from parseBlockLike

* Add test for inverse masks

* Fix inversed mask check

* Remove brush masks

* Remove unused imports

---------

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
2025-05-18 19:03:41 +02:00
IntegratedQuantum
8bcc00f536 Sort the world list in save selection by least recently used.
Also did some further refactoring to make it easier to deal with different world paths (#606) in the future.
fixes #1311
2025-05-13 20:39:12 +02:00
IntegratedQuantum
1fbf5618b9 Set the placeholder biome before receiving the first biome packet from the server
fixes #1428
2025-05-11 19:54:25 +02:00
IntegratedQuantum
ed99d940d2 Send updated biome information instantly back to the player, but only when the biome actually changed
fixes #478
2025-05-09 22:01:11 +02:00
IntegratedQuantum
74bf4b62c9 Don't print things when canceling an invite.
fixes #1312
2025-05-09 21:32:40 +02:00
IntegratedQuantum
29f06de52f Implement some basic lossy compression for entity and item network data
progress towards #1327
2025-04-26 15:59:59 +02:00
Krzysztof Wiśniewski
05d39a81b8
Use readVec and writeVec where applicable (#1336)
* Use readVec and writeVec where applicable

* Use writeVec in chunkRequest.sendRequest
2025-04-22 23:02:22 +02:00
IntegratedQuantum
5f54d48a73
The new network system (#1297)
* Create the foundations of the new network system.

* Fix memory leak

* Implement a simple additive-increase-multiplicative-decrease congestion control scheme.

Also fixed the locking with the send path

* Fix edge case for mismatch in read and available position

* fix formatting

* Reimplemtent network statistics of the F6 menu

* Fix problem the F6 window size

* Rename size to capacity

* Implement the advanced network debug menu

* Fix a few problems

* Actually call the handlePacketLoss function

* Reduce startup delay and fix possible problems with negative RTTs

* Fix crash in block of partially received packets.

* Use a PriorityQueue for unconfirmed packets.

Also fixed a display issue in the advanced network menu

* Always send confirmations and change how packet loss is handled to avoid killing the network connection by only resending packets outside the receive buffer.

* Use the length instead of the endIndex in the CircularBufferQueue

this just made everything more difficult to work with.

* Optimize CircularBufferQueue.enqueueSlice with memcpy

* Fix problem with zero-length packets, which is a valid use case

* Fix a small edge case

* fix formatting

* Add missing code to increase the capacity in enqueueSlice

* Don't send more data than the receive buffer can fit.

* Use a better data structures that handles the received ranges

* Use memcpy in more circular buffer functions

* Send a keepalive if the connection hasn't been established but the player was invited by the server

* Implement disconnecting for the new protocol

* Reimplement timeout

* Only add new connections when the other side actually sent an init packet.

otherwise the connections stay in the queue, and need keepalives.

also moved the code from the invite window to network.zig, where it belongs

* Don't allow queueing more than 2 GiB of data (i32 limit)

* Reimplement and simplify reconnection

* Use member functions for Socket

* React to packets using a scheme similar to TCP

TCP will send a fast retransmit on first loss discovered by DACKs, shortly after starting the RTO timer.
In essence this means that only second loss of the smallest packets is handled by congestion control.

* Increase scaling after congestion control halves bandwidth.

Now it increases it by at least 1%, isntead of only by the MTU

* Use start and len instead of start and end for the RangeBuffer

* Use ListUnmanaged in RangeBuffer

* Rename flawedReceive to tryReceive

* Add a `ms` constant to make it easier to see what units some of the constants use.

Also added a function alias for microTimestamp to make it potentially easier to change to other timestamps in the future.
2025-04-19 15:05:02 +02:00
Krzysztof Wiśniewski
00c6b29736
Always use big endian (#1274)
* Always use big endian

* Apply review change requests
2025-04-03 21:32:59 +02:00
Krzysztof Wiśniewski
a7052d25f5
Add /redo command (#1238)
* Add undo and redo commands

* Remove unintended change

* Remove redo code

* Revert "Remove redo code"

This reverts commit 80aeca1dc2d0f7b3f3570b95937a8f7fd87cac5d.

* Fix variable name in /redo

* Update import
2025-03-30 17:03:19 +02:00
IntegratedQuantum
c2ef9aba62 Remove the root . . . . . . . . . . import, replacing it with @import("main")
To fully solve the ZLS issues I had to also add the import to the other executables.
2025-03-27 21:16:35 +01:00
IntegratedQuantum
a82ed65dfb Initialize the seed in every thread, also added a helper function for threadlocal (de-)initialization.
fixes #1248
2025-03-27 20:09:09 +01:00
Krzysztof Wiśniewski
12d05a06ff
Add /undo command (#1228)
* Add undo and redo commands

* Remove unintended change

* Remove redo code

* Clean up redo code

* Use CircularBufferQueue

* Fix formatting issues

* Update src/server/server.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Apply review requests

---------

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
2025-03-24 21:47:14 +01:00
Krzysztof Wiśniewski
e815d98b89
Refactor server.zig to use BinaryWriter and BinaryReader (#1213)
* Refactor update() in server.zig

* Refactor User.receiveData() to use BinaryReader

* Use network.networkEndian
2025-03-22 16:12:46 +01:00
Krzysztof Wiśniewski
37eb01ec37
World edit commands for Cubyz (#1141)
* Add basic worldedit commands

* Fix style issues

* Fix style issues and command names

* Fix style issues

* Store worldedit command data in User

* Fix blueprint memory leak

* Add loading from Zon

* Use Block instead of u32

* Add binary storage format

* Add binary blueprint loading

* Fix formatting in copy.zig

* Use BinaryWriter for writing

* Use ReaderWriter for reading

* Add delete command

* Update src/blueprint.zig

* Apply review suggestions

* Fix formatting issues

* Update src/blueprint.zig

* Fix formatting issues

* Fix compilation issue

* make pos1 and pos2 null initially and also show the selection on the client

* fix issue

* Fix formatting issues

* Add deselect command

* Update src/blueprint.zig

* Add clone to Blueprint

* Convert to manual serialization

* Apply review suggestions

* Use Array3D

* Apply suggestions from code review

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Apply review suggestions

* Reorder functions

* Rename

* Apply review suggestions

* Apply review suggestions

* Fix outlines

* Remove append

* Apply review suggestions

* Update src/blueprint.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Replace index with dash

* No green it is

* Update src/server/command/worldedit/pos2.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Update src/server/command/worldedit/pos2.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Update src/server/command/worldedit/pos1.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Apply review suggestions

* Abstract file io to struct

* Revert "Abstract file io to struct"

This reverts commit f0bbe50aad0887d562069cb9ce18085f3de6e4cb.

* Add openBlueprintsDir function

* Apply review suggestions

* Apply review suggestions

* Update src/server/command/worldedit/blueprint.zig

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>

* Apply review suggestions

---------

Co-authored-by: OneAvargeCoder193 <mgiakimenko@outlook.com>
Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
2025-03-20 22:07:26 +01:00
IntegratedQuantum
09fc516828 Move all the allocators to a separate file. They can now be imported with main.heap (analogous to std.heap)
I also removed the unused BufferFallbackAllocator which was from a time where I was still experimenting with different allocators.
2025-03-09 16:26:41 +01:00
IntegratedQuantum
bc02bb0587
Use (an adapted version) of zig fmt to format the source code. (#1140)
* Format all the files

* Fix compilation error.

* The install scripts now patch render.zig from the zig standard library.

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update ci.yml

* Update the formatter, it doesn't need to run on .zig files anymore.

* Update ci.yml

* Update ci.yml

* Add a mistake to check if it works

* IT WORKS!
2025-03-02 16:36:58 +01:00
IntegratedQuantum
22488a28ae CI: Check for trailing spaces and (missing) newlines at the end of the file
fixes #737
2025-02-12 17:36:44 +01:00
IntegratedQuantum
9be4ff1047 Store handShakeComplete only after the player has been added to the player list.
This prevents a race condition between reconnecting and being added to the list.

Should fix #999
2025-02-02 21:13:57 +01:00
OneAvargeCoder193
1de5efbf6e
hopefully save the hand and also when the player leaves or server closes (#943)
* hopefully save the hand and also when the player leaves or server closes

* fixes

* change it to check the user id

* fix

* check pointers

* fix compilation error

* fix other bugs

* put player id in source

* fix error

* remove one line of code

* fix serialization

* revert < 22 to < 14

* change

* fix error where inventory not saved on alt f4

* change connected to a regular bool

* remove isConnected function
2025-01-26 21:30:23 +01:00
OneAvargeCoder193
b39cfc199c send user id to the client 2025-01-24 22:06:31 +01:00
IntegratedQuantum
2c681fbffb Simplify the process of sending formatted messages to the user chat entry, as is done often by commands. 2025-01-12 19:20:38 +01:00
OneAvargeCoder193
2d783c9ece
Let the player set the default gamemode and disable cheats during world creation (#915)
* let the user change the default world gamemode and enable or disable cheats

* changes

* make old worlds not have gamerules

* fixes
2025-01-11 23:40:31 +01:00
IntegratedQuantum
850956b0af Reduce latency of itemdrops by sending item drop updates instantly instead of waiting for the next server tick.
progress towards #868
2025-01-03 23:38:23 +01:00
OneAvargeCoder193
5986c76c45
Add player and inventory saving (#878)
* Save the player's location and inventory, and also added the ability to load the player's location

* remove the comment from utils

* make the player save every 5 seconds

* fix gamemode loading

* remove comment

* added inventory loading

* fix bug

* fix unintentional changes

* fix segfault

* fixes

* Some small fixes.

---------

Co-authored-by: IntegratedQuantum <jahe788@gmail.com>
2025-01-02 15:14:22 +01:00
IntegratedQuantum
de2e4aedb4 Rename ZonElement.free to deinit to follow zig convention. 2025-01-01 20:27:31 +01:00
IntegratedQuantum
9f3c63ebff Split the server mutex into multiple different mutexes that are used in smaller scopes.
fixes #864
Also fixed a data race in the itemdrop manager
2024-12-31 14:53:42 +01:00
IntegratedQuantum
47ed704e49 Refactor the ItemDropManager to reduce the usage of mutexes.
This is done by ensuring all the critical things are done on the server thread, while all remaining things are locked tightly with a mutex or a better data structure.

fixes #863

Also fixes a bug where item drop collection was not collecting the right amount.
2024-12-31 00:53:06 +01:00
IntegratedQuantum
de90b4ede3 Introduce a ConcurrentQueue to reduce the number of raw mutexes used throughout the code to simplify the code reduce the chance of (multithreaded) deadlocks.
For now it's just a queue with a mutex.

this might help with #863 and #864 in the future.
2024-12-30 00:59:45 +01:00
IntegratedQuantum
79c38b8ccc Store the gamemode on the server and add a command to change it. The gamemode is also now used in inventory operations.
fixes #846
2024-12-27 21:36:31 +01:00
IntegratedQuantum
f65c83ee0d Don't send empty entity update packets every server tick. 2024-12-08 11:32:59 +01:00
IntegratedQuantum
8312253e77 Fix deadlock on user reconnect.
Also adds a space before chat messages.
2024-12-07 21:47:53 +01:00
IntegratedQuantum
f0c57d6c13 Fix side-effects of user name color and other formatting hints on other rendered strings.
Introduces § as a formatting specifier to disable all previous specifiers. fixes #729
Also fixed another problem in the advanced network debug menu.
2024-11-15 22:17:15 +01:00
IntegratedQuantum
44bce44ba5 Move inventory operations to a new file.
fixes #755
2024-11-03 15:43:25 +01:00
IntegratedQuantum
79d7393c4e Serialize/Deserialize inventory commands and send them to the server.
More progress towards #643

I had to temporarily disable inventory crafting to make this work for now.
2024-10-26 16:42:00 +02:00
IntegratedQuantum
ad1b8617d2 Always use the position of the last chunk request packet to determine whether a chunk can be removed from the queue. This should prevent any false positives.
fixes #740
2024-10-07 20:22:06 +02:00
IntegratedQuantum
b4f3dfbeca Replace json with zon in the entire game and autoamtically convert all json files when an old version is detected.
progress on #733
2024-09-29 16:28:16 +02:00
IntegratedQuantum
72207c973e Refactor: Use decl literals, a recent addition to Zig. 2024-09-14 14:54:49 +02:00
IntegratedQuantum
3a6f55b8e7 Implement block drop rendering and fix a couple synchronization issues. 2024-08-17 11:01:22 +02:00
IntegratedQuantum
0f654ea5cd Send item drop data to the client.
fixes #622
2024-08-05 10:58:55 +02:00
IntegratedQuantum
16852488b7 Ensure that chunks inside the simulation distance remain loaded on the server.
fixes #76
2024-08-04 13:57:06 +02:00
IntegratedQuantum
783f342ad9 Allow player to reconnect when the server still thinks the old connection is active.
This makes it easier to reconnect to the server when the client crashed.

fixes #369
2024-08-02 11:07:05 +02:00
IntegratedQuantum
10a1322058 Use the default port where possible.
Also make sure that another port is used and communicated correctly when the default port is already in use.
This allows opening multiple singleplayer worlds on the same computer.

fixes #605
2024-08-01 22:05:20 +02:00
IntegratedQuantum
289e3c61f1 Add VSCode section headers to make navigating large files easier. 2024-07-14 11:31:57 +02:00