-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
78 lines (64 loc) · 2.64 KB
/
models.py
File metadata and controls
78 lines (64 loc) · 2.64 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
# Resource types
RESOURCE_TYPES = [
"electricity",
"water",
"basic_resources",
"critical_resources",
"heavy_resources",
"personnel",
"data"
]
class Layer:
def __init__(self, name, supported_resources):
self.name = name
self.supported_resources = supported_resources
self.edges = []
def connect_nodes(self, from_node, to_node, attributes):
"""Connect two nodes with an edge having specific attributes"""
edge = Edge(
from_node=from_node,
to_node=to_node,
attributes=attributes
)
self.edges.append(edge)
return edge
class Edge:
def __init__(self, from_node, to_node, attributes):
self.from_node = from_node
self.to_node = to_node
self.attributes = attributes
self.resources_in_transit = {resource: [] for resource in RESOURCE_TYPES}
if "capacity" in attributes:
self.attributes["original_capacity"] = attributes["capacity"]
def send_resource(self, resource_type, amount):
"""Send resources through this edge"""
if self.attributes.get("status") == "destroyed":
return False
if self.from_node.resources.get(resource_type, 0) < amount:
return False
capacity_factor = 1.0
if self.attributes.get("status") == "damaged":
capacity_factor = 0.5
max_amount = amount
if "capacity" in self.attributes:
max_amount = min(amount, self.attributes["capacity"] * capacity_factor)
travel_time = self.attributes.get("travel_time", 1)
if self.attributes.get("status") == "damaged":
travel_time = int(travel_time * 1.5)
self.resources_in_transit[resource_type].append({
'amount': amount,
'ticks_remaining': travel_time
})
return True
def tick(self):
"""Process one time unit of transportation"""
arrived_resources = {resource: 0 for resource in RESOURCE_TYPES}
for resource_type in RESOURCE_TYPES:
for resource in self.resources_in_transit[resource_type][:]:
resource['ticks_remaining'] -= 1
if resource['ticks_remaining'] <= 0:
arrived_resources[resource_type] += resource['amount']
self.resources_in_transit[resource_type].remove(resource)
if arrived_resources[resource_type] > 0:
self.to_node.receive_supplies(resource_type, arrived_resources[resource_type])
return arrived_resources