about summary refs log tree commit diff stats
path: root/Archipelago/multiplayer.gd
blob: c2d9875885867cba4120275417d561e8ae1e8697 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
extends "res://scripts/multiplayer.gd"

var queued_messages = []
var queued_messages_mutex = Mutex.new()


func _request_lobby_list():
	var apclient = global.get_node("Archipelago")
	Steam.addRequestLobbyListStringFilter(
		"archipelago", apclient._seed, Steam.LOBBY_COMPARISON_EQUAL
	)

	._request_lobby_list()


func _on_lobby_created(result: int, lobby_id: int) -> void:
	if result != Steam.RESULT_OK:
		return

	var apclient = global.get_node("Archipelago")
	var _ignore = Steam.setLobbyData(lobby_id, "archipelago", apclient._seed)

	._on_lobby_created(result, lobby_id)


func _on_lobby_joined(lobby_id: int, permissions: int, locked: bool, result: int) -> void:
	if result != Steam.RESULT_OK:
		return

	var apclient = global.get_node("Archipelago")
	Steam.setLobbyMemberData(lobby_id, "slot_name", apclient._player_name_by_slot[apclient._slot])

	._on_lobby_joined(lobby_id, permissions, locked, result)


func _update_lobby_members():
	._update_lobby_members()

	if active_lobby_id == 0:
		return
	var lobby_size: int = Steam.getNumLobbyMembers(active_lobby_id)
	for i in range(0, lobby_size):
		var member_id: int = Steam.getLobbyMemberByIndex(active_lobby_id, i)
		if member_id != player_steam_id and member_id in active_lobby_members:
			var slot_name = Steam.getLobbyMemberData(active_lobby_id, member_id, "slot_name")
			active_lobby_members[member_id].steam_name = slot_name


func say(text):
	queued_messages_mutex.lock()
	queued_messages.append(text)
	queued_messages_mutex.unlock()


func _physics_process(_delta):
	if queued_messages_mutex.try_lock() == OK:
		if queued_messages.empty():
			queued_messages_mutex.unlock()
		else:
			var messages = queued_messages.duplicate()
			queued_messages = []
			queued_messages_mutex.unlock()

			var player = get_tree().get_root().get_node("Spatial/player")
			var space_state = get_tree().get_root().get_world().direct_space_state
			var nearby_members = []
			for member_id in active_lobby_members.keys():
				var other_member = active_lobby_members[member_id]
				var ray = space_state.intersect_ray(
					player.global_translation, other_member.global_translation, [player], 0b0101
				)
				if !("collider" in ray) or ray["collider"] == other_member:
					# Visible!
					nearby_members.append(member_id)

			var apclient = global.get_node("Archipelago")
			var player_name = apclient.get_player_name()
			for member_id in nearby_members:
				for msg in messages:
					_send_p2p_packet(
						{"solves": [{"say": msg, "from": player_name}]},
						member_id,
						Steam.P2P_SEND_RELIABLE,
						true
					)


# I'm completely hijacking this callback, since we're in ghost mode and it won't be called normally.
func _receive_solve(data):
	if "say" in data:
		get_tree().get_root().get_node("Spatial/AP_TextClient").parse_printjson(
			"[LOCAL] [color=#fafad2]%s[/color]: %s" % [data["from"], data["say"]]
		)
		messages.showMessage("[color=#fafad2]%s[/color]: %s" % [data["from"], data["say"]])


func _send_hi(_discard):
	pass