diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2025-03-23 22:38:25 +0000 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2025-03-23 22:38:25 +0000 |
commit | c803ecf4213487591e75f66a6f1c1b0ac1ea4f5e (patch) | |
tree | d8d1d3be9b1e57f6ab3d98efbb8a5bf2672b4918 | |
parent | a2feba2d918db861ea85d22887f73993c173e742 (diff) |
Get threading of world working
-rw-r--r-- | main.rhm | 23 | ||||
-rw-r--r-- | src/world.rhm | 35 | ||||
-rw-r--r-- | thread/thread.rhm | 2 | ||||
-rw-r--r-- | thread/thread.rkt | 3 |
4 files changed, 52 insertions, 11 deletions
@@ -5,26 +5,29 @@ import: lib("web-server/servlet.rkt") open lib("web-server/servlet-env.rkt") open "thread/thread.rhm" open + "src/world.rhm" fun start(req): + println(#{request-method}(req)) + println(#{request-post-data/raw}(req)) #{response/xexpr}("Hello world!") -//#{serve/servlet}(start, #{#:launch-browser?}: #false, #{#:servlet-path}: "/game") fun loop(): println("Hello World!") sleep(1) loop() -fun hello(): - let thread_evt = thread_receive_evt() - let sync_res = sync_timeout(1/500, thread_evt) - if !sync_res - | println("No sync result") - | println("Got message " +& thread_receive()) +fun sim_world(): + let wrld = world.World() + wrld.simulate() + println("Creating thread") -def thr: thread(hello) -thread_send(thr, "Hello") +def thr: thread(sim_world) + +// Create webserver +#{serve/servlet}(start, #{#:launch-browser?}: #false, #{#:servlet-path}: "/game") + +// Wait on world simulation thread so we don't kill the world before it has cleanly exited thread_wait(thr) -println("Thread done") diff --git a/src/world.rhm b/src/world.rhm index 9357422..13fe048 100644 --- a/src/world.rhm +++ b/src/world.rhm @@ -3,6 +3,7 @@ import: "chunk.rhm" open "entity.rhm" open "item.rhm" open + "../thread/thread.rhm" open export: Chunk @@ -18,9 +19,43 @@ export: Item Slot +class MessageLogin(user :: String, thr) + class World(chunks :: MutableList.now_of(Chunk), entities :: MutableList.now_of(Entity)): + field thread_evt = thread_receive_evt() + field tick_length = 0.6 // tick length is 600ms + constructor(): super(MutableList(), MutableList()) + method login(user :: String, thr): + // TODO should be accessing some DB to get player + let player = EntityPlayer(0, 0, 0, chunks) + player.equip[Slot.right_hand] := Item.bronze_axe + entities.add(player) + + method process_messages(timestamp :: Real): + let time_passed = current_timestamp() - timestamp + let remaining_time = math.max(tick_length - time_passed, 0) + let sync_res = sync_timeout(remaining_time, thread_evt) + if sync_res + | let msg = thread_receive() + match msg + | MessageLogin(user, thr): login(user, thr) + | ~else: println("Unknown message " +& msg) + process_messages(timestamp) + | #true // Return here + + method tick_loop(): + // TODO step through entities and simulate them + let starting_time = current_timestamp() + process_messages(starting_time) + println("Done tick") + tick_loop() + + + method simulate(): + tick_loop() + diff --git a/thread/thread.rhm b/thread/thread.rhm index a0c1b18..4bf911e 100644 --- a/thread/thread.rhm +++ b/thread/thread.rhm @@ -11,6 +11,7 @@ export: sleep sync_timeout current_thread + current_timestamp def thread_send: #{thread-send} def thread_receive: #{thread-receive} @@ -18,3 +19,4 @@ def thread_receive_evt: #{thread-receive-evt} def thread_wait: #{thread-wait} def sync_timeout: #{sync/timeout} def current_thread: #{current-thread} +def current_timestamp: #{current-inexact-monotonic-milliseconds} diff --git a/thread/thread.rkt b/thread/thread.rkt index bf7d8eb..6fac881 100644 --- a/thread/thread.rkt +++ b/thread/thread.rkt @@ -7,4 +7,5 @@ thread-wait sleep sync/timeout - current-thread) + current-thread + current-inexact-monotonic-milliseconds) |