180 Commits

Author SHA1 Message Date
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
eaaf524f99
Signs (#1446)
- [x] Rotation (already merged)
- [x] basic GUI
- [x] sign models and textures
- [x] sign blocks
- [x] update the text on the client
- [x] Figure out block entity rendering
- [x] Render the text to a texture on update
- [x] Render the texture in the world in the location of the sign
- [x] Use varint instead of u32 for storing the block data lengths,
(now, while we can still change it)
- [x] Sync the text with the server and all clients
- [x] Figure out block entity storage on the server
- [x] Send the entity data of the initial chunk
- [x] Store the text on the server
- [x] Set the chunk as changed whenever a block entity data update
happens, so we actually store it
- [x] Disable or figure out optimized local chunk transmission
- [x] fix memory leak
- [x] Rethink some of the API (do we need onPlace/onBreak, when there is
unload and updateData?)
- [x] Remove the background shadow from text, it produces too much
aliasing
- [x] Figure out if the default should be black or white
- [x] Correctly center the text
- [x] Why are newlines not working?
- [x] Check if a deadlock is possible on deinit --- it would be possible
only if another thread has a reference to it, which should not be the
case when unload is called.
- [x] Set the text margin and sizes reasonably
- [x] Make sure the GUI fits with the sign width
- [x] Create an issue for configurable sign texture size and
configurable default color

fixes #367

---------

Co-authored-by: Carrie <122191047+careeoki@users.noreply.github.com>
Co-authored-by: OneAvargeCoder193 <85588535+OneAvargeCoder193@users.noreply.github.com>
2025-05-31 10:49:50 +02:00
Krzysztof Wiśniewski
c13d9415d8
Add tool palette (#1494)
## Description

This pull request adds a tool palette to allow using stable indexes for
binary storage.

## Links

Related to: #1290 
Related to: #1478
Related to: #1473
2025-05-23 19:06:24 +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
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
82675434be Always allow localhost connections to join a game
fixes #1415
2025-05-08 22:04:11 +02:00
Krzysztof Wiśniewski
64ce0ed991
Batch block updates (#1313)
* Batch block updates

* Apply review change requests

* Allow blockUpdate to carry multiple block updates in single message

* Read until there is nothing left

* Use mesh_storage.BlockUpdate

* Break instead of boolean

* Restore client side neighbor updates

* Move side check in blockUpdate out of the loop

* Update src/utils.zig

* Fix minor issues

* Reverse ownership logic + change contains into liesInChunk

* Update liesInChunk

* No name for upadeBlock param

* Apply review change requests

* Fix formatting

* Restore onBreakClient where it should be

* Update src/renderer/chunk_meshing.zig

* Update src/renderer/chunk_meshing.zig

* Converge formatting with master

* fix formatting (https://github.com/ziglang/zig-spec/issues/38 is so stupid)

---------

Co-authored-by: IntegratedQuantum <jahe788@gmail.com>
2025-05-01 10:10:03 +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
IntegratedQuantum
6fc7e604e7 Add a delay before after sending the disconnect signal on windows localhost.
fixes #1322
2025-04-26 15:12:24 +02:00
Krzysztof Wiśniewski
62d4bf6700
Use isServerSide instead of conn.user != null (#1346) 2025-04-26 09:40:06 +02:00
Krzysztof Wiśniewski
fde5a69cdc
Add selection wand (#1298)
* Add selection wand

* Update zon file extension

* Apply review change requests

* Simplify receive logic for worldEditPos

* Convert isServerSide to method
2025-04-25 20:49:37 +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
b0af58b21b
Refactor sendKeepAlive and receiveKeepAlive (#1265) 2025-04-02 21:18:17 +02:00
Krzysztof Wiśniewski
92fcd10069
Modernize genericUpdate protocol (#1257)
* Modernize genericUpdate protocol

* Do not complicate sendGamemode

* Apply review change requests

* Yeet cure

* Rename to biomesByIndex

* Update src/network.zig

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

* Use @typeInfo(T).vector

* Update src/network.zig

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

* Undo blind remove of TODO

* Update src/server/terrain/biomes.zig

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

* Remove defaultIndex argument from getByIndex

* Make biomesByIndex a ListUnmanaged

---------

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
2025-04-02 18:05:36 +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
IntegratedQuantum
ac9f00cb32 Add utility functions to make function pointer casting more safe.
It's now only changing the self parameter, everything else will be checked at compile-time.

fixes #1223
2025-03-22 21:24:53 +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
Krzysztof Wiśniewski
f0eb9f4f5a
Add list palettes and item palette (#1190)
* Add list palettes and item palette

* Fix block-item assignments

* Remove initEmpty

* Replace This() with Palette

* Update src/assets.zig

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

* Fix duplicated keys in legacy palette issue

* Remove redundant errdefer

* Some more figuring out order of registering

* Remove assertion breaking migrations 2to1

* Apply review suggestions

* Add error log for block and item ID conflict

* Remove migrations file

* Update src/assets.zig

---------

Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
2025-03-18 21:25:04 +01:00
Krzysztof Wiśniewski
4d7905ce0f
Refactor itemdrop.zig to use BinaryReader (#1189)
* Refactor readPosition to use BinaryReader

* Refactor getPositionAndVelocityData to use BinaryWriter

* Update src/itemdrop.zig
2025-03-11 22:15:23 +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
Krzysztof Wiśniewski
832f5e7e08
Refactor ClientEntityManager.serverUpdate to use BinaryReader (#1171)
* Use ReaderWriter for reading

* Reformat switch

* Apply review suggestions

* Remove redundant comptime

* Remove message length check
2025-03-07 20:49:17 +01:00
IntegratedQuantum
471210f585 Use array @splat
progress towards #1160
2025-03-06 22:08:15 +01:00
IntegratedQuantum
f3b508bf47 Introduce a BinaryReader/Writer to make working with binary protocols easier especially with regards to error handling.
fixes #762
fixes #264
2025-03-05 19:14:49 +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
ffd408bf18 Print the file path when printing a zon error.
fixes #1133
2025-03-01 12:30:06 +01:00
IntegratedQuantum
c343d434cb Limit player names and chat messages, both in terms of visible and total characters.
fixes #1099
fixes #1098
2025-02-23 19:21:11 +01:00
IntegratedQuantum
ca0a37cabb Add an option to allow people to join without being invited (requires a public IP address).
Due to the non-obvious technical requirements, I decided to hide it a bit.

fixes #618
2025-02-16 16:24:51 +01:00
IntegratedQuantum
1f08735177 Only print a warning when the stun server couldn't be reached.
fixes #1052
2025-02-16 09:24:30 +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
b39cfc199c send user id to the client 2025-01-24 22:06:31 +01:00
IntegratedQuantum
1b1f61cae9 Change some warning messages to error messages.
Errors are things that come from actual problems, like incorrect parametrizations.
Warnings are things that hint at potential problems, but could also occur during regular runtime, like for example server lag.

progress towards #911
2025-01-12 15:54:53 +01:00
IntegratedQuantum
de2e4aedb4 Rename ZonElement.free to deinit to follow zig convention. 2025-01-01 20:27:31 +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
0c0ca2ed15 Synchronize block breaking and placing throug the inventory synchronization system.
fixes #751
This allowed reenabling item consumption when placing blocks. Durability and item drops are still work in progress.
Currently all game modes consume items. Temporarily reverts some changes from #739, until the game mode is synchronized to the server as well.

progress towards #670
2024-12-24 23:52:11 +01:00
IntegratedQuantum
0f754c44a0 Fix rare connectivity issues by switching to the minimal IPv4 MTU.
progress towards #819
2024-12-08 20:13:44 +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
b06b4e7224 Use lossy compression on LOD chunks when sending them over the network.
The lossy compression just simply replaces all interior blocks. It should not make any difference visually.

This halves the amount of data that is sent when first joining.

This makes #748 less important
fixes #745
2024-11-30 20:51:36 +01:00
IntegratedQuantum
44bce44ba5 Move inventory operations to a new file.
fixes #755
2024-11-03 15:43:25 +01:00
IntegratedQuantum
f878af9830 Remove the old item stack dropping from the protocol.
fixes #643
2024-10-30 20:06:21 +01:00
IntegratedQuantum
a12a2d0bc9 Send sync operations back to the clients, if another client modified the inventory. In case a client receives sync operations it reverts all local unconfirmed changes, applies the sync and finally reapplies all unconfirmed changes.
A big step towards #643
I also added a testing window that can be used to test this in multiplayer.
2024-10-27 20:26:37 +01:00
IntegratedQuantum
25a69fdacd Send confirmation for successful inventory operations back to the client.
more progress towards #643
2024-10-27 15:33:41 +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