-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.gd
632 lines (493 loc) · 18.3 KB
/
game.gd
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
extends Control
@onready var playersNode = $Players
@onready var rankTrackNode = $RankTrack
@onready var ui = $UI
@onready var actionsNode = $UI/Control/PlayerStatusMarginContainer/MarginContainer/VBoxContainer/Actions
@onready var map : Map = $Map
@onready var sequence : SequenceOfPlay = $SequenceOfPlay
@onready var debug = $Debug
var turn : int = 1
var playerCount = 1
var rankTrack
signal pickedHits(unitNames)
signal test
var skipHell = false
var skipSouls = false
var skipSummoning = false
var skipAction = false
var skipCombat = false
var skipPetitions = false
var skipEnd = false
var skipUnitPlacing = false
var debugTroops = false
var debugSouls : int = 0
var debugFavors : int = 0
var debugDisfavors : int = 0
var loadSaveGame = false
func _ready():
randomize()
# loadSaveGame = true
Signals.proceed.connect(_on_proceed)
Signals.host.connect(_on_host)
Signals.join.connect(_on_join)
Signals.start.connect(_on_start)
Signals.allPlayersReady.connect(_on_allPlayersReady)
Signals.returnToMainMenu.connect(_on_returnToMainMenu)
Signals.returnToLobby.connect(_on_returnToLobby)
Signals.addPlayer.connect(_on_addPlayer)
Signals.updateTurnTrack.connect(_on_updateTurnTrack)
Save.newSavegame()
#debug
Server.playerjoinedRoom.connect(_on_playerjoinedRoom)
#skipHell = true
#skipSouls = true
#skipSummoning = true
#skipAction = true
# skipCombat = true
# skipPetitions = true
# skipEnd = true
#skipUnitPlacing = true
debugTroops = true
# debugSouls = 100
# debugFavors = 1
# debugDisfavors = 1
#
# Settings.tooltips = false
#Settings.skipScreens = true
# Settings.skipSoulsSummary = true
# Settings.skipWaitForPlayers = true
# Settings.skipPhaseReminder = true
# canHitLieutenants = false
# lieutenantBonus = false
if Tutorial.tutorial:
Settings.tooltips = false
if Connection.usedMenuToStartGame:
RpcCalls.peerReady.rpc_id(Connection.host)
ui.start()
Signals.potatoPc.emit(Settings.potatoPc)
func saveGame():
var save_dict = {"game" : {
"rankTrack" : rankTrackNode.rankTrack,
"turn" : turn,
"phase" : Data.phase,
"worldStates" : {},
}}
for aiId in Ai.worldStates:
save_dict.game.worldStates[aiId] = {}
for key in Ai.worldStates[aiId]._state:
save_dict.game.worldStates[aiId][key] = Ai.worldStates[aiId]._state[key]
return save_dict
func loadGame(savegame : Dictionary):
# loading from json makes int a float
var arr : Array = []
for rank in savegame.rankTrack:
arr.append(int(rank))
rankTrackNode.rankTrack = arr
for peer in Connection.peers:
RpcCalls.updateRankTrack.rpc_id(peer, rankTrackNode.rankTrack)
if savegame.has("turn"):
for peer in Connection.peers:
# remember json always stores numbers as float
RpcCalls.updateTurnTrack.rpc_id(peer, int(savegame.turn))
if savegame.has("phase"):
Data.phase = savegame.phase
if savegame.has("worldStates"):
for oldAiId in savegame.worldStates:
var newAiId : int = Connection.oldNewIdDict[int(oldAiId)]
for stateName in savegame.worldStates[oldAiId]:
Ai.worldStates[newAiId].set_state(stateName, savegame.worldStates[oldAiId][stateName])
func _on_allPlayersReady():
setup()
func setup():
var playerIds : Array = Connection.peers.duplicate()
Signals.phaseReminder.emit("Start Phase")
playerIds = setupAiPlayer(playerIds, Connection.aiPlayersId)
addSpawner(playerIds)
if Save.savegame.size() == 0:
setupPlayers(playerIds)
setupSouls()
setupFavors()
else:
setupPlayersFromSavegame(playerIds)
setupSoulsFromSavegame()
setupFavorsFromSavegame()
debug.debugFavors()
debug.debugDisfavors()
setupMouseLights()
if Save.savegame.size() == 0:
Data.chooseDemon = true
if not Tutorial.tutorial:
setupDemons()
await confirmStartDemon()
if not Tutorial.tutorial:
setupSectios()
#debug
debug.debugSectios()
fillAvailableLieutenantsBox()
if not Tutorial.tutorial:
await setupStartLegions()
for peer in Connection.peers:
RpcCalls.updateRankTrack.rpc_id(peer, rankTrackNode.rankTrack)
var phase : int = 0
if Tutorial.tutorial:
phase = Tutorial.chapter
sequenceOfPlay(phase)
else:
Data.chooseDemon = false
setupDemonsFromSavegame()
setupSectiosFromSavegame()
setupLegionsFromSavegame()
setupLieutenantsFromSavegame()
fillAvailableLieutenantsBox()
setupArcanaCardsFromSavegame()
for peer in Connection.peers:
RpcCalls.toogleWaitForPlayer.rpc_id(peer, 66, true)
if not Settings.skipScreens:
for peer in Connection.peers:
await Signals.proceedSignal
for peer in Connection.peers:
RpcCalls.toogleWaitForPlayer.rpc_id(peer, 66, false)
if Save.savegame.game:
loadGame(Save.savegame.game)
sequenceOfPlay(Data.phase)
func setupAiPlayer(playerIds : Array, aiPlayersIds : Array) -> Array:
for aiPlayerId in aiPlayersIds:
Ai.playerIds.append(aiPlayerId)
Ai.addWorldState(aiPlayerId)
playerIds.append(aiPlayerId)
return playerIds
func addSpawner(playerIds : Array) -> void:
for id in playerIds:
for peer in Connection.peers:
map.addSpawner.rpc_id(peer, id)
func setupPlayersFromSavegame(playerIds : Array) -> void:
var colorNamesLeft = Data.colorsNames.duplicate()
colorNamesLeft.remove_at(0) # remove "Random" color
colorNamesLeft.shuffle()
for colorName in Connection.playerIdColorDict.values():
if colorNamesLeft.has(colorName):
colorNamesLeft.erase(colorName)
for id in playerIds:
var newName = Connection.playerIdInfoDict[id]["playerName"]
var loadedName = Connection.playerIdInfoDict[id]["loadedPlayerName"]
var loadedId : int = Connection.playerIdInfoDict[id]["loadedPlayerId"]
# using the new id as and copy savegame to new id key
# json keys are always strings
Connection.oldNewIdDict[loadedId] = id
Save.savegame.players[id] = Save.savegame.players[str(loadedId)]
for peer in Connection.peers:
RpcCalls.addPlayer.rpc_id(peer, id)
var colorName : String = Connection.playerIdColorDict[id]
# if Connection.playerIdColorDict.has(id):
# colorName = Connection.playerIdColorDict[id]
# else:
# colorName = colorNamesLeft.pop_back()
if colorName == Data.colorsNames[0]:
colorName = colorNamesLeft.pop_back()
for peer in Connection.peers:
RpcCalls.changeColor.rpc_id(peer, id, colorName)
RpcCalls.changePlayerName.rpc_id(peer, id, Connection.playerIdNamesDict[id])
func setupPlayers(playerIds : Array) -> void:
var colorNamesLeft = Data.colorsNames.duplicate()
colorNamesLeft.remove_at(0) # remove "Random" color
colorNamesLeft.shuffle()
for colorName in Connection.playerIdColorDict.values():
if colorNamesLeft.has(colorName):
colorNamesLeft.erase(colorName)
for id in playerIds:
for peer in Connection.peers:
RpcCalls.addPlayer.rpc_id(peer, id)
var colorName : String = Connection.playerIdColorDict[id]
if colorName == Data.colorsNames[0]:
colorName = colorNamesLeft.pop_back()
for peer in Connection.peers:
RpcCalls.changeColor.rpc_id(peer, id, colorName)
RpcCalls.changePlayerName.rpc_id(peer, id, Connection.playerIdNamesDict[id])
func setupMouseLights():
for peer in Connection.peers:
RpcCalls.initMouseLights.rpc_id(peer)
func fillAvailableLieutenantsBox():
for i in range(3):
var lieutenantName : String = Decks.getRandomCard("lieutenant")
for peer in Connection.peers:
RpcCalls.fillAvailableLieutenantsBox.rpc_id(peer, lieutenantName)
func setupSoulsFromSavegame():
for playerId in Data.players:
var player = Data.players[playerId]
Signals.changeSouls.emit(playerId, Save.savegame.players[playerId].souls)
func setupSouls():
for playerId in Data.players:
var player = Data.players[playerId]
var souls = player.souls + 10 + debugSouls
Signals.changeSouls.emit(playerId, souls)
func setupFavorsFromSavegame():
for playerId in Data.players:
var player = Data.players[playerId]
Signals.changeFavors.emit(playerId, Save.savegame.players[playerId].favors)
Signals.changeDisfavors.emit(playerId, Save.savegame.players[playerId].disfavors)
func setupFavors():
for playerId in Data.players:
var player = Data.players[playerId]
var favors = player.favors + 1
Signals.changeFavors.emit(playerId, favors)
func setupDemons():
for playerId in Data.players:
for i in range(3):
var nr : String = Decks.getRandomCard("demon")
for peer in Connection.peers:
RpcCalls.addDemon.rpc_id(peer, playerId, nr)
func setupDemonsFromSavegame():
for playerId in Data.players:
# 5 demons
for demonName in Save.savegame.players[playerId].demons:
# for some reason it tries to load the rank values in int
# but the array only contains the names of the demons???
if demonName is String:
for peer in Connection.peers:
RpcCalls.addDemon.rpc_id(peer, playerId, demonName + ".tres", Save.savegame.demons[demonName])
func confirmStartDemon() -> void:
for peer in Connection.peers:
RpcCalls.toogleWaitForPlayer.rpc_id(peer, 0, true)
if not Settings.skipScreens:
for peer in Connection.peers:
ui.confirmStartDemon.rpc_id(peer)
for peer in Connection.peers:
print("waiting for demon")
await Signals.proceedSignal
for peer in Connection.peers:
RpcCalls.toogleWaitForPlayer.rpc_id(peer, 0, false)
func setupSectios():
var assignedCircles = []
var assignedQuarters = []
for playerId in Data.players:
var quarters = [0, 1, 2, 3, 4]
quarters.shuffle()
var playersAssignedCircles = []
var circles = []
for nr in range(9):
circles.append(nr)
for quarterNr in range(4):
var quarter = quarters.pop_back()
var circle = randi_range(0, 8)
while true:
var free = true
for index in assignedCircles.size():
var assignedCircle = assignedCircles[index]
var assignedQuarter = assignedQuarters[index]
if assignedCircle == circle and assignedQuarter == quarter:
free = false
if playersAssignedCircles.has(circle):
free = false
if free:
break
else:
circle = randi_range(0, 8)
playersAssignedCircles.append(circle)
assignedQuarters.append(quarter)
assignedCircles.append(circle)
var sectio : Sectio = Decks.sectios[circle][quarter]
for peer in Connection.peers:
RpcCalls.occupySectio.rpc_id(peer, playerId, sectio.sectioName)
func setupSectiosFromSavegame():
for playerId in Data.players:
for sectioName in Save.savegame.players[playerId].sectios:
for peer in Connection.peers:
RpcCalls.occupySectio.rpc_id(peer, playerId, sectioName)
func setupArcanaCardsFromSavegame():
for playerId in Data.players:
# # 9 arcana cards
for cardName in Save.savegame.players[playerId].arcanaCards:
# remove card from deck
Decks.getSpecificCard("arcana", cardName)
for peer in Connection.peers:
RpcCalls.addArcanaCard.rpc_id(peer, playerId, cardName)
for peer in Connection.peers:
#RpcCalls.showArcanaCardsContainer.rpc_id(peer)
RpcCalls.showStartScreen.rpc_id(peer)
func setupStartLegions():
if not skipUnitPlacing:
var playerIds : Array = Connection.peers.duplicate()
playerIds.append_array(Ai.playerIds.duplicate())
playerIds.shuffle()
for playerId in playerIds:
if playerId > 0:
await playerPlaceStartLegion(playerId)
else:
aiPlaceStartLegion(playerId)
func aiPlaceStartLegion(id : int) -> void:
var bestSectio : Sectio = Ai.getBestStartSectio(id)
map.placeUnit(bestSectio, id, Data.UnitType.Legion)
func playerPlaceStartLegion(playerId : int) -> void:
map.placeFirstLegion.rpc_id(playerId)
for peer in Connection.peers:
RpcCalls.toogleWaitForPlayer.rpc_id(peer, playerId, true)
for peer in Connection.peers:
ui.updateRankTrackCurrentPlayer.rpc_id(peer, playerId)
await map.unitPlacingDone
for peer in Connection.peers:
RpcCalls.toogleWaitForPlayer.rpc_id(peer, playerId, false)
func setupLegionsFromSavegame():
for legion in Save.savegame.legions.values():
# loadUnit.rpc_id(playerNameIdDict[legion.playerName], legion.occupiedSectio)
var sectio = Decks.sectioNodes[legion.occupiedSectio]
# var playerId : int = playerNameIdDict[legion.playerName]
var playerId : int = Connection.oldNewIdDict[int(legion.triumphirate)]
map.spawnUnit(sectio.sectioName, legion.unitNr, playerId, Data.UnitType.Legion)
map.updateTroopInSectio(sectio.sectioName, sectio.troops)
Signals.incomeChanged.emit(playerId)
for peer in Connection.peers:
if not peer == Data.id:
# skip sending to host if its an AI player
if not Connection.peers.has(playerId) and peer == Connection.host:
continue
map.spawnUnit.rpc_id(peer, sectio.sectioName, legion.unitNr, playerId, Data.UnitType.Legion)
map.updateTroopInSectio.rpc_id(peer, sectio.sectioName, sectio.troops)
func setupLieutenantsFromSavegame():
for lieutenant in Save.savegame.lieutenants.values():
# doesnt need to return the name, for its already known
# but it needs to be removed from the deck
Decks.getSpecificCard("lieutenant", lieutenant.unitName)
# need to pass the new triumphirates ID, not the old one
var playerId : int = Connection.oldNewIdDict[int(lieutenant.triumphirate)]
map.spawnUnit(lieutenant.occupiedSectio, lieutenant.unitNr, playerId, Data.UnitType.Lieutenant, lieutenant.unitName)
var sectio = Decks.sectioNodes[lieutenant.occupiedSectio]
map.updateTroopInSectio(sectio.sectioName, sectio.troops)
# var playerId : int = playerNameIdDict[lieutenant.playerName]
Signals.incomeChanged.emit(playerId)
for peer in Connection.peers:
if not peer == Data.id:
# skip sending to host if its an AI player
if not Connection.peers.has(playerId) and peer == Connection.host:
continue
map.spawnUnit.rpc_id(peer, lieutenant.occupiedSectio, lieutenant.unitNr, playerId, Data.UnitType.Lieutenant, lieutenant.unitName)
map.updateTroopInSectio.rpc_id(peer, sectio.sectioName, sectio.troops)
func sequenceOfPlay(phase : int = 0):
if debugTroops:
for playerId in Data.players.keys():
if Connection.peers.has(playerId):
debug.spawnDebugTroops1.rpc_id(playerId)
await get_tree().create_timer(0.5).timeout
else:
debug.spawnDebugTroops1(playerId)
if Tutorial.tutorial and Tutorial.chapter == Tutorial.Chapter.Introduction:
await Tutorial.introduction()
await get_tree().create_timer(0.1).timeout
for peer in Connection.peers:
RpcCalls.updatePhaseLabel.rpc_id(peer, phase, Data.phases.keys()[phase])
await get_tree().create_timer(0.1).timeout
while(true):
#for peer in Connection.peers:
#RpcCalls.showArcanaCardsContainer.rpc_id(peer)
print("hell phase ",phase, " ", Data.phases.Hell)
if phase == Data.phases.Hell and not skipHell:
await $Hell.phase()
phase = await nextPhase(phase, Data.phases.Hell)
print("soul phase ",phase, " ", Data.phases.Soul)
if phase == Data.phases.Soul and not skipSouls:
await $Soul.phase(ui)
phase = await nextPhase(phase, Data.phases.Soul)
print("Summoning phase ",phase, " ", Data.phases.Summoning)
if phase == Data.phases.Summoning and not skipSummoning:
await $Summoning.phase(phase, ui)
phase = await nextPhase(phase, Data.phases.Summoning)
print("Action phase ",phase, " ", Data.phases.Action)
for peer in Connection.peers:
RpcCalls.showRankTrackMarginContainer.rpc_id(peer)
if phase == Data.phases.Action and not skipAction:
var rankTrack : Array = rankTrackNode.rankTrack.duplicate()
await $Action.phase(phase, rankTrack, ui, map, rankTrackNode)
#
phase = await nextPhase(phase, Data.phases.Action)
for peer in Connection.peers:
RpcCalls.combatPhase.rpc_id(peer)
print("Combat phase ",phase, " ", Data.phases.Combat)
$Combat.combatWinner.clear()
if phase == Data.phases.Combat and not skipCombat:
if Tutorial.tutorial:
rankTrack = await Tutorial.combat(rankTrackNode)
# otherwise the "On Earth" Demon doesnt get his Label
for peer in Connection.peers:
RpcCalls.combatPhase.rpc_id(peer)
await $Combat.phase(map)
for peer in Connection.peers:
RpcCalls.combatOver.rpc_id(peer)
phase = await nextPhase(phase, Data.phases.Combat)
print("Petitions phase ",phase, " ", Data.phases.Petitions)
if phase == Data.phases.Petitions and not skipPetitions:
await $Petition.phase($Combat.combatWinner, ui)
phase = await nextPhase(phase, Data.phases.Petitions)
print("End phase ",phase, " ", Data.phases.Petitions)
if phase == Data.phases.End and not skipEnd:
if $End.phase():
break
phase = Data.nextPhase()
for peer in Connection.peers:
RpcCalls.updatePhaseLabel.rpc_id(peer, phase, Data.phases.keys()[phase])
await get_tree().create_timer(0.1).timeout
# save game after each round
turn += 1
for peer in Connection.peers:
RpcCalls.updateTurnTrack.rpc_id(peer, turn)
Save.saveGame()
func nextPhase(currentPhase, expectedPhase):
if currentPhase == expectedPhase:
currentPhase = Data.nextPhase()
for peer in Connection.peers:
RpcCalls.updatePhaseLabel.rpc_id(peer, currentPhase, Data.phases.keys()[currentPhase])
await get_tree().create_timer(0.1).timeout
Save.saveGame()
return currentPhase
func _on_host():
Main.StartServer()
Connection.connectToServer()
await get_tree().create_timer(0.5).timeout
Server.create_room.rpc_id(1, "Debug")
Connection.host = Data.id
await get_tree().create_timer(0.5).timeout
RpcCalls.peerReady.rpc_id(Connection.host)
ui.start()
func peer_connected(id):
playerCount += 1
Connection.peers.append(id)
if Connection.peers.size() >= 2:
await get_tree().create_timer(0.5).timeout
_on_start()
func _on_join():
Connection.connectToServer()
await get_tree().create_timer(0.5).timeout
Server.join_room.rpc_id(1, "Debug")
await get_tree().create_timer(0.5).timeout
RpcCalls.peerReady.rpc_id(Connection.host)
ui.start()
func _on_playerjoinedRoom(roomId : int, room_name : String, player_id : int, playersIdNameDict : Dictionary):
Connection.host = roomId
var peers : Array[int]
for peer in playersIdNameDict.keys():
peers.append(int(peer))
Connection.peers = peers
if roomId == Data.id:
if Connection.peers.size() >= 2:
await get_tree().create_timer(0.5).timeout
_on_start()
func _on_start():
Server.request_start_game.rpc_id(1, Data.id)
func _on_proceed():
Data.chooseDemon = false
RpcCalls.proceed.rpc_id(Connection.host)
func _on_returnToMainMenu():
Signals.resetGame.emit()
get_tree().change_scene_to_file("res://ui/main_menu.tscn")
func _on_returnToLobby():
Server.return_to_lobby.rpc_id(1)
Signals.resetGame.emit()
get_tree().change_scene_to_file("res://ui/lobby.tscn")
func _on_addPlayer(playerScene : Player):
playersNode.add_child(playerScene)
func _on_updateTurnTrack(_turn : int):
turn = _turn
# cheats
func _input(event):
return
if Input.is_action_just_pressed("right_click"):
for peer in Connection.peers:
RpcCalls.addArcanaCard.rpc_id(peer, Data.id, "Blood Money")