about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2024-12-17 16:01:19 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2024-12-17 16:01:19 -0500
commit1bdfb112b659a6e4447ae8ed02c452bf12d8700a (patch)
tree0285f67a9600b83616edb69d209e3ebaf47cbae8
parent45fa76505dd00bdd6665bae476277371fb1252a2 (diff)
downloadlingo-archipelago-1bdfb112b659a6e4447ae8ed02c452bf12d8700a.tar.gz
lingo-archipelago-1bdfb112b659a6e4447ae8ed02c452bf12d8700a.tar.bz2
lingo-archipelago-1bdfb112b659a6e4447ae8ed02c452bf12d8700a.zip
Add position tracking via IPC
-rw-r--r--Archipelago/client.gd1
-rw-r--r--Archipelago/load.gd6
-rw-r--r--Archipelago/player.gd29
-rw-r--r--Archipelago/settings_screen.gd1
-rw-r--r--Archipelago/tracker.gd84
5 files changed, 121 insertions, 0 deletions
diff --git a/Archipelago/client.gd b/Archipelago/client.gd index 85cfd05..384c907 100644 --- a/Archipelago/client.gd +++ b/Archipelago/client.gd
@@ -9,6 +9,7 @@ var SCRIPT_notifier
9var SCRIPT_panel 9var SCRIPT_panel
10var SCRIPT_pilgrimage_terminator 10var SCRIPT_pilgrimage_terminator
11var SCRIPT_textclient 11var SCRIPT_textclient
12var SCRIPT_tracker
12var SCRIPT_uuid 13var SCRIPT_uuid
13 14
14var ap_server = "" 15var ap_server = ""
diff --git a/Archipelago/load.gd b/Archipelago/load.gd index 651fc39..ba350f6 100644 --- a/Archipelago/load.gd +++ b/Archipelago/load.gd
@@ -692,6 +692,12 @@ func _load():
692 multiplayer_node.ghost_mode = true 692 multiplayer_node.ghost_mode = true
693 add_child(multiplayer_node) 693 add_child(multiplayer_node)
694 694
695 # Create the autotracker node.
696 var autotracker_script = apclient.SCRIPT_tracker
697 var autotracker = autotracker_script.new()
698 autotracker.set_name("AP_Tracker")
699 self.add_child(autotracker)
700
695 # Hook up Geronimo handler. 701 # Hook up Geronimo handler.
696 $player.connect("player_jumped", apclient, "geronimo") 702 $player.connect("player_jumped", apclient, "geronimo")
697 703
diff --git a/Archipelago/player.gd b/Archipelago/player.gd index 52d743a..6638329 100644 --- a/Archipelago/player.gd +++ b/Archipelago/player.gd
@@ -2,12 +2,16 @@ extends "res://scripts/player.gd"
2 2
3 3
4var _oldpos = Vector3(0, -200, 0) 4var _oldpos = Vector3(0, -200, 0)
5var _oldpos_fine = Vector3(0, -200, 0)
5 6
6 7
7func _ready(): 8func _ready():
8 _oldpos = translation 9 _oldpos = translation
9 _oldpos.y = 0 10 _oldpos.y = 0
10 11
12 _oldpos_fine = translation
13 _oldpos_fine.y = 0
14
11 var apclient = global.get_node("Archipelago") 15 var apclient = global.get_node("Archipelago")
12 if apclient.track_player: 16 if apclient.track_player:
13 var tracking_timer = Timer.new() 17 var tracking_timer = Timer.new()
@@ -17,8 +21,19 @@ func _ready():
17 tracking_timer.connect("timeout", self, "_tick_tracking") 21 tracking_timer.connect("timeout", self, "_tick_tracking")
18 tracking_timer.start() 22 tracking_timer.start()
19 23
24 var tracking_timer_fine = Timer.new()
25 tracking_timer_fine.name = "TrackingTimerFine"
26 tracking_timer_fine.wait_time = 0.5
27 add_child(tracking_timer_fine)
28 tracking_timer_fine.connect("timeout", self, "_tick_tracking_fine")
29 tracking_timer_fine.start()
30
20 31
21func _tick_tracking(): 32func _tick_tracking():
33 var tracker = get_tree().get_root().get_node("Spatial/AP_Tracker")
34 if tracker.has_connection():
35 return
36
22 var newpos = translation 37 var newpos = translation
23 newpos.y = 0 38 newpos.y = 0
24 39
@@ -29,6 +44,20 @@ func _tick_tracking():
29 apclient.setValue("PlayerPos", {"x": int(_oldpos.x), "z": int(_oldpos.z)}) 44 apclient.setValue("PlayerPos", {"x": int(_oldpos.x), "z": int(_oldpos.z)})
30 45
31 46
47func _tick_tracking_fine():
48 var tracker = get_tree().get_root().get_node("Spatial/AP_Tracker")
49 if !tracker.has_connection():
50 return
51
52 var newpos = translation
53 newpos.y = 0
54
55 if newpos != _oldpos_fine && newpos.distance_to(_oldpos_fine) > 0.5:
56 _oldpos_fine = newpos
57
58 tracker.update_position(int(_oldpos_fine.x), int(_oldpos_fine.z))
59
60
32func _solving(): 61func _solving():
33 ._solving() 62 ._solving()
34 63
diff --git a/Archipelago/settings_screen.gd b/Archipelago/settings_screen.gd index 2ed8594..d3c654c 100644 --- a/Archipelago/settings_screen.gd +++ b/Archipelago/settings_screen.gd
@@ -33,6 +33,7 @@ func _ready():
33 var pilg_term = load("user://maps/Archipelago/pilgrimage_terminator.gd") 33 var pilg_term = load("user://maps/Archipelago/pilgrimage_terminator.gd")
34 apclient_instance.SCRIPT_pilgrimage_terminator = pilg_term 34 apclient_instance.SCRIPT_pilgrimage_terminator = pilg_term
35 apclient_instance.SCRIPT_textclient = load("user://maps/Archipelago/textclient.gd") 35 apclient_instance.SCRIPT_textclient = load("user://maps/Archipelago/textclient.gd")
36 apclient_instance.SCRIPT_tracker = load("user://maps/Archipelago/tracker.gd")
36 apclient_instance.SCRIPT_uuid = load("user://maps/Archipelago/vendor/uuid.gd") 37 apclient_instance.SCRIPT_uuid = load("user://maps/Archipelago/vendor/uuid.gd")
37 38
38 var apdata = ResourceLoader.load("user://maps/Archipelago/gamedata.gd") 39 var apdata = ResourceLoader.load("user://maps/Archipelago/gamedata.gd")
diff --git a/Archipelago/tracker.gd b/Archipelago/tracker.gd new file mode 100644 index 0000000..798721d --- /dev/null +++ b/Archipelago/tracker.gd
@@ -0,0 +1,84 @@
1extends Node
2
3var autotracker_port = 41253
4
5var _server = WebSocketServer.new()
6var _peers = []
7
8
9func _ready():
10 _server.bind_ip = "127.0.0.1"
11 _server.connect("client_connected", self, "_connection_established")
12 _server.connect("client_disconnected", self, "_connection_dropped")
13 _server.connect("data_received", self, "_data_received")
14 _server.listen(autotracker_port)
15
16
17func _process(_delta):
18 _server.poll()
19
20
21func _connection_established(id, _protocol):
22 _peers.append(id)
23
24 var apclient = global.get_node("Archipelago")
25
26 var msg = {"cmd": "Connect", "slot": {"server": apclient.ap_server, "player": apclient.ap_user}}
27
28 var peer = _server.get_peer(id)
29 peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
30 peer.put_packet(JSON.print(msg).to_utf8())
31
32
33func _connection_dropped(id, _was_clean_close):
34 _peers.erase(id)
35
36
37func _data_received(id):
38 var peer = _server.get_peer(id)
39 peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
40
41 var packet_text = peer.get_packet().get_string_from_utf8()
42 global._print("Got data from tracker: " + packet_text)
43 var msg = JSON.parse(packet_text)
44 if msg.error != OK:
45 global._print("Error parsing packet from Tracker: " + msg.error_string)
46 return
47
48 var apclient = global.get_node("Archipelago")
49
50 if msg["cmd"] == "Sync":
51 var resp = {}
52
53 if apclient.track_player:
54 var player = get_tree().get_root().get_node("Spatial/player")
55 resp = {
56 "cmd": "UpdatePosition",
57 "position": {"x": int(player._oldpos_fine.x), "z": int(player._oldpos_fine.z)}
58 }
59
60 peer.put_packet(JSON.print(resp).to_utf8())
61
62
63func _broadcast(msg):
64 var to_remove = []
65 var serialized = JSON.print(msg).to_utf8()
66
67 for peer_id in _peers:
68 if _server.has_peer(peer_id):
69 var peer = _server.get_peer(peer_id)
70 peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
71 peer.put_packet(serialized)
72 else:
73 to_remove.append(peer_id)
74
75 for peer_id in to_remove:
76 _peers.erase(peer_id)
77
78
79func has_connection():
80 return _peers.size() > 0
81
82
83func update_position(x, z):
84 _broadcast({"cmd": "UpdatePosition", "position": {"x": x, "z": z}})