@@ -14,7 +14,14 @@ def __init__(self, save_data):
14
14
self .map_name = save_data ["map_name" ]
15
15
self .game_end = save_data ["game_end" ]
16
16
17
- self .players = [Player (self , player_data ) for player_data in save_data ["players" ]]
17
+ self .players = []
18
+ for player_data in save_data ["players" ]:
19
+ if player_data ["control" ] == "human" :
20
+ self .players .append (Player (self , player_data ))
21
+ elif player_data ["control" ] == "computer" :
22
+ self .players .append (ComputerPlayer (self , player_data ))
23
+ else :
24
+ raise Exception ("Invalid player control given: %s" % player_data ["control" ])
18
25
19
26
self .world = World (self , save_data ["world" ]) # assigns settlements to players
20
27
@@ -73,20 +80,19 @@ def cycle_player(self):
73
80
valid_choice = False
74
81
75
82
while not valid_choice : # wont be infinite, as to be called at least two players are left.
76
- if self .players .index (self .current_player ) < len (self .players ) - 1 :
77
- self .current_player = self .players [self .players .index (self .current_player ) + 1 ]
83
+ if self .players .index (self .get_current_player () ) < len (self .players ) - 1 :
84
+ self .current_player_name = self .players [self .players .index (self .get_current_player ()) + 1 ]. get_name ()
78
85
else :
79
- self .current_player = self .players [0 ]
86
+ self .current_player_name = self .players [0 ]. get_name ()
80
87
81
88
# Computer takes go, then we go on to find next human player
82
- if self .current_player .get_control () == "computer" :
83
- self .current_player .take_go (self ) # of current player
84
- if self .current_player .is_dead ():
85
- self .current_player .units = []
86
-
87
- valid_choice = not self .current_player .is_dead () and self .current_player .get_control () == "human"
89
+ current_player = self .get_current_player ()
90
+ if current_player .get_control () == "computer" :
91
+ current_player .take_go () # of current player
92
+ if current_player .is_dead ():
93
+ current_player .units = []
88
94
89
- return self .current_player
95
+ valid_choice = not self .get_current_player (). is_dead () and self . get_current_player (). get_control () == "human"
90
96
91
97
def try_spawn (self , unit_type , position ):
92
98
if not self .get_unit (position ):
@@ -198,6 +204,8 @@ class Player:
198
204
def __init__ (self , model_link , saved_data ):
199
205
self .model_link = model_link
200
206
207
+ self .control = saved_data ["control" ]
208
+
201
209
self .name = saved_data ["name" ]
202
210
self .colour = saved_data ["colour" ]
203
211
self .camera_focus = saved_data ["camera_focus" ]
@@ -216,6 +224,8 @@ def __init__(self, model_link, saved_data):
216
224
217
225
def get_save_data (self ):
218
226
return {
227
+ "control" : self .get_control (),
228
+
219
229
"name" : self .get_name (),
220
230
"colour" : self .get_colour (),
221
231
"camera_focus" : self .get_camera_focus (),
@@ -316,27 +326,31 @@ def set_minimap_status(self, show):
316
326
317
327
318
328
class ComputerPlayer (Player ):
319
- def __init__ (self , name , colour ):
320
- super ().__init__ (name , colour , "computer" )
329
+ def __init__ (self , model_link , save_data ):
330
+ super ().__init__ (model_link , save_data )
321
331
322
- def take_go (self , model ):
323
- model . current_player .start_turn ()
332
+ def take_go (self , ):
333
+ self . model_link . get_current_player () .start_turn ()
324
334
325
- self .handle_cities (model )
326
- self .handle_units (model )
335
+ self .handle_cities ()
336
+ self .handle_units ()
327
337
328
- model . current_player .end_turn ()
338
+ self . model_link . get_current_player () .end_turn ()
329
339
330
- def handle_cities (self , model ):
331
- for city in model .current_player .settlements :
340
+ def handle_cities (self ):
341
+ current_player = self .model_link .get_current_player ()
342
+ for city_position in current_player .settlements :
332
343
# Spawning Units
333
344
affordable_units = [name for name , values in constants .UNIT_SPECS .items ()
334
- if model . current_player .get_ap () - values ["spawn_cost" ] > 0 ]
345
+ if current_player .get_ap () - values ["spawn_cost" ] >= 0 ]
335
346
unit_choice = random .choice (affordable_units )
336
- model .try_spawn (unit_choice , city .get_position ())
337
347
338
- def handle_units (self , model ):
339
- for unit in model .current_player .units :
348
+ city = self .model_link .world .get_tile (city_position )
349
+ self .model_link .try_spawn (unit_choice , city .get_position ())
350
+
351
+ def handle_units (self ):
352
+ current_player = self .model_link .get_current_player ()
353
+ for unit in current_player .units :
340
354
if False : # in city conquer it
341
355
pass
342
356
else :
@@ -351,23 +365,23 @@ def handle_units(self, model):
351
365
# print(path)
352
366
353
367
# See if unit can take a city
354
- if model .check_conquer (unit ):
355
- model .conquer (unit .position )
368
+ if self . model_link .check_conquer (unit ):
369
+ self . model_link .conquer (unit .position )
356
370
else :
357
371
# Make Random Move
358
- all_moves = model .get_moves (unit )
372
+ all_moves = self . model_link .get_moves (unit )
359
373
if len (all_moves ) > 0 :
360
374
move = random .choice (all_moves )
361
- model .move_unit (move , unit )
375
+ self . model_link .move_unit (move , unit )
362
376
363
377
# Make Random Attack (if possible)
364
- all_attacks = model .get_attacks (unit )
378
+ all_attacks = self . model_link .get_attacks (unit )
365
379
if len (all_attacks ) > 0 :
366
380
all_units = sorted (
367
- [model .get_unit (position ) for position in all_attacks ],
381
+ [self . model_link .get_unit (position ) for position in all_attacks ],
368
382
key = lambda x : x .health )
369
383
enemy_unit = all_units [0 ] # target the weakest enemy
370
- model .make_attack (unit , enemy_unit )
384
+ self . model_link .make_attack (unit , enemy_unit )
371
385
372
386
373
387
class Tile :
0 commit comments