-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_server.py
More file actions
735 lines (597 loc) · 24.1 KB
/
game_server.py
File metadata and controls
735 lines (597 loc) · 24.1 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
"""
FastAPI server that wraps the game simulation.
React will communicate with this via HTTP.
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import numpy as np
from typing import Optional
from game_controller import GameState, WorldConfig, SaveSystem
from tribe_system import Tribe, Unit, UnitType, Structure, StructureType
import os
import glob
app = FastAPI()
# Ensure saves directory exists
os.makedirs("saves", exist_ok=True)
# Allow React to connect
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Global game state
current_game: Optional[GameState] = None
class NewGameRequest(BaseModel):
width: int = 100
height: int = 80
sea_level: float = 0.42
herbivore_population: int = 120
predator_population: int = 10
starting_units: dict = {"gatherer": 2, "hunter": 1}
starting_biome: Optional[str] = None
fog_of_war: bool = True
class MoveUnitRequest(BaseModel):
unit_id: str
dx: int
dy: int
class ActionRequest(BaseModel):
unit_id: str
action_type: str # "gather", "hunt", "build", "recruit"
target_resource: Optional[str] = None
target_id: Optional[str] = None # For hunting
structure_type: Optional[str] = None # For building
build_x: Optional[int] = None # For building location
build_y: Optional[int] = None
unit_type: Optional[str] = None # For recruiting
class SaveGameRequest(BaseModel):
filename: str
class LoadGameRequest(BaseModel):
filename: str
@app.get("/")
async def root():
return {"status": "Game server running"}
@app.post("/game/new")
async def new_game(config: NewGameRequest):
"""Create a new game world"""
global current_game
world_config = WorldConfig()
world_config.width = config.width
world_config.height = config.height
world_config.sea_level = config.sea_level
world_config.herbivore_population = config.herbivore_population
world_config.predator_population = config.predator_population
current_game = GameState(world_config)
# Handle "Random" biome selection
biome_pref = config.starting_biome
if biome_pref == "Random":
biome_pref = None
current_game.initialize_world(
starting_units=config.starting_units,
preferred_biome=biome_pref,
fog_of_war=config.fog_of_war
)
return {
"status": "success",
"world_size": [world_config.width, world_config.height]
}
@app.get("/game/world")
async def get_world():
"""Get complete world state for rendering"""
if not current_game:
return {"error": "No active game"}
# Convert numpy arrays to lists for JSON
world_data = {
"width": int(current_game.world.width),
"height": int(current_game.world.height),
"biomes": current_game.world.biomes.tolist(),
"vegetation": current_game.vegetation.density.tolist(),
}
return world_data
@app.get("/game/entities")
async def get_entities():
"""Get all entity positions and stats"""
if not current_game:
return {"error": "No active game"}
entities = {
"herbivores": [],
"predators": [],
"avian": [],
"aquatic": [],
"scavengers": [],
"nomads": []
}
# Herbivores
if current_game.animals:
for animal in current_game.animals.herbivores:
entities["herbivores"].append({
"id": animal.id,
"species": animal.species,
"x": int(animal.x),
"y": int(animal.y),
"hp": int(animal.combat_stats.current_hp),
"max_hp": int(animal.combat_stats.max_hp),
"attack": int(animal.combat_stats.attack),
"defense": int(animal.combat_stats.defense)
})
# Predators
if current_game.predators:
for pred in current_game.predators.predators:
entities["predators"].append({
"id": pred.id,
"species": pred.species,
"x": int(pred.x),
"y": int(pred.y),
"hp": int(pred.combat_stats.current_hp),
"max_hp": int(pred.combat_stats.max_hp),
"attack": int(pred.combat_stats.attack),
"defense": int(pred.combat_stats.defense)
})
# Nomads
if current_game.nomads:
for band in current_game.nomads.bands:
for member in band.members:
entities["nomads"].append({
"id": member.id,
"x": int(member.x),
"y": int(member.y),
"hp": int(member.hp),
"max_hp": int(member.max_hp),
"energy": int(member.energy),
"band_id": band.id
})
# Ecology
if current_game.ecology:
# Avian
for bird in current_game.ecology.avian_creatures:
entities["avian"].append({
"species": bird.species,
"x": int(bird.x),
"y": int(bird.y),
"hp": int(bird.energy * 100),
"max_hp": 100
})
# Aquatic
for aq in current_game.ecology.aquatic_creatures:
entities["aquatic"].append({
"species": aq.species,
"x": int(aq.x),
"y": int(aq.y),
"hp": int(aq.energy * 100),
"max_hp": 100
})
# Scavengers
for scav in current_game.ecology.scavengers:
entities["scavengers"].append({
"species": scav.species,
"x": int(scav.x),
"y": int(scav.y),
"hp": int(scav.energy * 100),
"max_hp": 100
})
return entities
@app.get("/game/stats")
async def get_stats():
"""Get game statistics"""
if not current_game:
return {"error": "No active game"}
stats = current_game.get_current_statistics()
# Add summary for frontend compatibility
stats["population"] = {
"herbivores": sum(stats["populations"].get("herbivores", {}).values()),
"predators": sum(stats["populations"].get("predators", {}).values()),
"scavengers": stats["populations"].get("scavengers", 0),
"avian": stats["populations"].get("avian", 0),
"aquatic": stats["populations"].get("aquatic", 0)
}
# Add tribe stats if available
if current_game.tribe:
stats["tribe"] = {
"population": len(current_game.tribe.units),
"stockpile": current_game.tribe.stockpile
}
# Add history for graphs
stats["history"] = current_game.get_population_history()
# Helper to convert numpy types
def convert_numpy(obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, dict):
return {k: convert_numpy(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy(i) for i in obj]
return obj
return convert_numpy(stats)
@app.get("/game/tribe")
async def get_tribe():
"""Get tribe status and units"""
if not current_game or not current_game.tribe:
return {"error": "No active tribe"}
return current_game.tribe.to_dict()
@app.post("/game/unit/move")
async def move_unit(request: MoveUnitRequest):
"""Move a unit"""
if not current_game or not current_game.tribe:
return {"error": "No active game"}
# Find unit
unit = next((u for u in current_game.tribe.units if u.id == request.unit_id), None)
if not unit:
return {"error": "Unit not found"}
if unit.has_moved:
return {"error": "Unit has already moved this turn"}
# Validate move (Manhattan distance for diamond shape)
distance = abs(request.dx) + abs(request.dy)
if distance > unit.movement_range:
return {"error": f"Move out of range (max {unit.movement_range})"}
# Execute move
unit.move(request.dx, request.dy)
current_game.tribe.update_visibility()
return {"status": "success", "unit": unit.to_dict()}
@app.post("/game/unit/action")
async def unit_action(request: ActionRequest):
"""Perform a unit action (gather, etc)"""
if not current_game or not current_game.tribe:
return {"error": "No active game"}
# Find unit
unit = next((u for u in current_game.tribe.units if u.id == request.unit_id), None)
if not unit:
return {"error": "Unit not found"}
if unit.has_acted:
return {"error": "Unit has already acted this turn"}
if request.action_type == "gather":
if unit.type == "hunter":
return {"error": "Hunters cannot gather resources"}
# Check resources at unit location
tile_res = current_game.resource_map.get((unit.x, unit.y))
if not tile_res:
return {"error": "No resources here"}
target = request.target_resource
# If no target specified, pick the first one
if not target:
target = next(iter(tile_res.keys()))
if target not in tile_res:
return {"error": f"No {target} here"}
# Calculate amount based on unit type
amount = 10 # Base amount
if unit.type == "gatherer":
amount = 20
# Deplete from tile
available = tile_res[target]
gathered = min(available, amount)
tile_res[target] -= gathered
# Cleanup empty resources
if tile_res[target] <= 0:
del tile_res[target]
# Add to stockpile
current_game.tribe.stockpile[target] = current_game.tribe.stockpile.get(target, 0) + gathered
unit.has_acted = True
unit.has_moved = True # Action ends turn (cannot move after)
unit.energy = max(0, unit.energy - 1)
return {
"status": "success",
"gathered": gathered,
"resource": target,
"stockpile": current_game.tribe.stockpile
}
elif request.action_type == "hunt":
# Check for animals in range (Range 2 for hunters, 1 for others)
hunt_range = 2 if unit.type == "hunter" else 1
# Find targets
targets = []
# Check herbivores
if current_game.animals:
for animal in current_game.animals.herbivores:
dist = abs(animal.x - unit.x) + abs(animal.y - unit.y)
if dist <= hunt_range:
targets.append(animal)
# Check predators
if current_game.predators:
for pred in current_game.predators.predators:
dist = abs(pred.x - unit.x) + abs(pred.y - unit.y)
if dist <= hunt_range:
targets.append(pred)
if not targets:
return {"error": "No animals in range"}
# Attack specific target if provided, otherwise first available
target = None
if request.target_id:
target = next((t for t in targets if str(t.id) == request.target_id), None)
if not target:
target = targets[0]
# Calculate damage
damage = 5
if unit.type == "hunter":
damage = 15
# Apply research bonuses
if current_game.tribe:
# Check for completed research structures to determine "tech level"
# For now, just having the structure adds a bonus?
# Or use the research_levels dict we added
weapon_level = current_game.tribe.research_levels.get("weapons", 0)
damage += (weapon_level * 5)
# Also check if we have the structure built, maybe that gives base level 1?
# The user said "derived from the... buildings".
# Let's say: Base 15.
# If Weapon Lab exists: +5.
# Future: Research actions increase level.
has_weapon_lab = any(s.type == StructureType.RESEARCH_WEAPON and s.is_complete for s in current_game.tribe.structures)
if has_weapon_lab:
damage += 5
target.combat_stats.take_damage(damage)
result = {
"status": "success",
"action": "hunt",
"damage": damage,
"target_hp": target.combat_stats.current_hp,
"target_species": target.species,
"target_location": [int(target.x), int(target.y)]
}
if not target.is_alive():
# Kill reward
food_gain = 20
current_game.tribe.stockpile["food"] += food_gain
result["kill"] = True
result["food_gain"] = food_gain
# Remove from game world immediately
if current_game.animals and target in current_game.animals.herbivores:
current_game.animals.herbivores.remove(target)
elif current_game.predators and target in current_game.predators.predators:
current_game.predators.predators.remove(target)
unit.has_acted = True
unit.has_moved = True # Action ends turn (cannot move after)
unit.energy = max(0, unit.energy - 1)
return result
elif request.action_type == "build":
# Determine build location
bx = request.build_x if request.build_x is not None else unit.x
by = request.build_y if request.build_y is not None else unit.y
# Check adjacency
dist = abs(bx - unit.x) + abs(by - unit.y)
if dist > 1:
return {"error": "Can only build on current or adjacent tile"}
# Check if tile is occupied by structure
for s in current_game.tribe.structures:
if s.x == bx and s.y == by:
return {"error": "Tile already has a structure"}
# Define costs and requirements
costs = {}
req_type = request.structure_type
st_enum = None
if req_type == "bonfire":
# Any class can build bonfires
costs = {"wood": 10, "flint": 5}
st_enum = StructureType.BONFIRE
elif req_type == "hut":
if unit.type != "crafter":
return {"error": "Only Crafters can build Huts"}
costs = {"wood": 20, "fiber": 10}
st_enum = StructureType.HUT
elif req_type == "workshop":
if unit.type != "crafter":
return {"error": "Only Crafters can build Workshops"}
costs = {"wood": 20, "stone": 20}
st_enum = StructureType.WORKSHOP
elif req_type == "research_weapon":
if unit.type != "crafter":
return {"error": "Only Crafters can build Research Stations"}
costs = {"wood": 30, "stone": 10}
st_enum = StructureType.RESEARCH_WEAPON
elif req_type == "research_armor":
if unit.type != "crafter":
return {"error": "Only Crafters can build Research Stations"}
costs = {"wood": 30, "fiber": 20}
st_enum = StructureType.RESEARCH_ARMOR
elif req_type == "idol":
if unit.type != "crafter":
return {"error": "Only Crafters can build Idols"}
costs = {"wood": 40, "stone": 40}
st_enum = StructureType.IDOL
else:
return {"error": f"Unknown structure type: {req_type}"}
# Check cost
for res, amount in costs.items():
if current_game.tribe.stockpile.get(res, 0) < amount:
return {"error": f"Not enough {res} (need {amount})"}
# Deduct cost
for res, amount in costs.items():
current_game.tribe.stockpile[res] -= amount
# Determine construction time
build_turns = 0
if st_enum == StructureType.BONFIRE:
build_turns = 1
elif st_enum == StructureType.HUT:
build_turns = 4
elif st_enum == StructureType.WORKSHOP:
build_turns = 6
elif st_enum in [StructureType.RESEARCH_WEAPON, StructureType.RESEARCH_ARMOR]:
build_turns = 8
elif st_enum == StructureType.IDOL:
build_turns = 12
# Create structure
structure = Structure(bx, by, st_enum)
structure.construction_turns_left = build_turns
structure.max_construction_turns = build_turns
structure.is_complete = False
current_game.tribe.add_structure(structure)
unit.has_acted = True
unit.has_moved = True
unit.energy = max(0, unit.energy - 1)
return {
"status": "success",
"action": "build",
"structure": structure.to_dict(),
"stockpile": current_game.tribe.stockpile,
"message": f"Construction started: {req_type} ({build_turns} turns)"
}
elif request.action_type == "recruit":
unit_type = request.unit_type
# Requirements and Costs
food_cost = 0
turns = 0
req_structure = None
if unit_type == "gatherer":
food_cost = 5
turns = 3
elif unit_type == "hunter":
food_cost = 7
turns = 3
elif unit_type == "crafter":
food_cost = 10
turns = 9
req_structure = StructureType.BONFIRE
# Culture Requirement
if current_game.tribe.culture < 5:
return {"error": "Need 5 Culture to recruit Crafter"}
elif unit_type == "shaman":
food_cost = 12
turns = 12
req_structure = StructureType.IDOL
else:
return {"error": f"Unknown unit type: {unit_type}"}
# Check Structure Requirement
if req_structure:
near_structure = False
for s in current_game.tribe.structures:
if s.type == req_structure and s.is_complete:
dist = abs(s.x - unit.x) + abs(s.y - unit.y)
if dist <= 3:
near_structure = True
break
if not near_structure:
return {"error": f"Must be near a completed {req_structure} to recruit {unit_type}"}
# Check Food Cost
if current_game.tribe.stockpile.get("food", 0) < food_cost:
return {"error": f"Not enough food (need {food_cost})"}
# Deduct Cost
current_game.tribe.stockpile["food"] -= food_cost
# Add to Training Queue
current_game.tribe.training_queue.append({
"type": unit_type,
"turns_left": turns,
"x": unit.x,
"y": unit.y
})
unit.has_acted = True
unit.energy = max(0, unit.energy - 1)
return {
"status": "success",
"action": "recruit",
"message": f"Training started for {unit_type} ({turns} turns)",
"stockpile": current_game.tribe.stockpile
}
return {"error": "Unknown action"}
@app.post("/game/step")
async def step_turn():
"""Advance the game by one turn"""
if not current_game:
return {"error": "No active game"}
current_game.advance_turn()
# Process Tribe Queues & Updates
queue_messages = []
# Add general game events (attacks, deaths, etc)
if hasattr(current_game, 'current_turn_log'):
queue_messages.extend(current_game.current_turn_log)
if current_game.tribe:
# Process construction/training queues
queue_messages.extend(current_game.tribe.process_queues())
# Process turn updates (Decay, Culture)
# Check if new year (every 4 turns, assuming season 0 is start of year)
is_new_year = (current_game.climate.season == 0)
update_msgs = current_game.tribe.process_turn_updates(is_new_year)
queue_messages.extend(update_msgs)
return {
"status": "success",
"turn": int(current_game.turn),
"year": int(current_game.climate.year),
"messages": queue_messages
}
@app.get("/game/tile/{x}/{y}")
async def get_tile_info(x: int, y: int):
"""Get detailed info about a specific tile"""
if not current_game:
return {"error": "No active game"}
# Terrain info
biome_names = {
0: "Deep Ocean", 1: "Shallow Ocean", 2: "Beach", 3: "Desert",
4: "Savanna", 5: "Grassland", 6: "Tropical Rainforest",
7: "Temperate Forest", 8: "Taiga", 9: "Tundra", 10: "Snow", 11: "Mountain"
}
try:
biome = int(current_game.world.biomes[y, x])
veg = float(current_game.vegetation.density[y, x])
temp = float(current_game.world.temperature[y, x])
moisture = float(current_game.world.moisture[y, x])
# Find entities at this location
entities_here = []
if current_game.animals:
for animal in current_game.animals.herbivores:
if int(animal.x) == x and int(animal.y) == y:
entities_here.append({
"id": animal.id,
"type": "herbivore",
"species": animal.species,
"hp": f"{int(animal.combat_stats.current_hp)}/{int(animal.combat_stats.max_hp)}",
"stats": f"ATK {int(animal.combat_stats.attack)} | DEF {int(animal.combat_stats.defense)}"
})
if current_game.predators:
for pred in current_game.predators.predators:
if int(pred.x) == x and int(pred.y) == y:
entities_here.append({
"id": pred.id,
"type": "predator",
"species": pred.species,
"hp": f"{int(pred.combat_stats.current_hp)}/{int(pred.combat_stats.max_hp)}",
"stats": f"ATK {int(pred.combat_stats.attack)} | DEF {int(pred.combat_stats.defense)}"
})
# Get resources at this tile
resources = current_game.resource_map.get((x, y), {})
return {
"terrain": biome_names.get(biome, "Unknown"),
"vegetation": f"{veg*100:.1f}%",
"temperature": f"{temp:.2f}",
"moisture": f"{moisture:.2f}",
"entities": entities_here,
"resources": resources
}
except Exception as e:
return {"error": str(e)}
@app.get("/game/saves")
async def list_saves():
"""List available save files"""
files = glob.glob("saves/*.json")
return {"saves": [os.path.basename(f) for f in files]}
@app.post("/game/save")
async def save_game(request: SaveGameRequest):
"""Save current game state"""
if not current_game:
return {"error": "No active game"}
filename = request.filename
if not filename.endswith(".json"):
filename += ".json"
filepath = os.path.join("saves", filename)
try:
SaveSystem.save_game(current_game, filepath)
return {"status": "success", "message": f"Game saved to {filename}"}
except Exception as e:
return {"error": str(e)}
@app.post("/game/load")
async def load_game(request: LoadGameRequest):
"""Load game state"""
global current_game
filepath = os.path.join("saves", request.filename)
if not os.path.exists(filepath):
return {"error": "Save file not found"}
try:
current_game = SaveSystem.load_game(filepath)
return {"status": "success", "message": f"Game loaded from {request.filename}"}
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
print("Starting game server on http://localhost:8000")
uvicorn.run(app, host="0.0.0.0", port=8000)