-
Notifications
You must be signed in to change notification settings - Fork 12
/
cell.py
143 lines (120 loc) · 5.14 KB
/
cell.py
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
import math
class Cell:
def __init__(self, x, y, environment, maxSugar=0, maxSpice=0, growbackRate=0):
self.x = x
self.y = y
self.environment = environment
self.maxSugar = maxSugar
self.maxSpice = maxSpice
self.agent = None
self.hemisphere = "north" if self.x >= self.environment.equator else "south"
self.neighbors = {}
self.pollution = 0
self.pollutionFlux = 0
self.ranges = {}
self.season = None
self.spice = maxSpice
self.spiceLastProduced = 0
self.sugar = maxSugar
self.sugarLastProduced = 0
self.timestep = 0
def doPollutionDiffusion(self):
self.pollution = self.pollutionFlux
def doSpiceConsumptionPollution(self, spiceConsumed):
consumptionPollutionFactor = self.environment.spiceConsumptionPollutionFactor
self.pollution += consumptionPollutionFactor * spiceConsumed
def doSpiceProductionPollution(self, spiceProduced):
productionPollutionFactor = self.environment.spiceProductionPollutionFactor
self.pollution += productionPollutionFactor * spiceProduced
def doSugarConsumptionPollution(self, sugarConsumed):
consumptionPollutionFactor = self.environment.sugarConsumptionPollutionFactor
self.pollution += consumptionPollutionFactor * sugarConsumed
def doSugarProductionPollution(self, sugarProduced):
productionPollutionFactor = self.environment.sugarProductionPollutionFactor
self.pollution += productionPollutionFactor * sugarProduced
def findEastNeighbor(self):
if self.environment.wraparound == False and self.x + 1 > self.environment.width - 1:
return None
eastNeighbor = self.environment.findCell((self.x + 1 + self.environment.width) % self.environment.width, self.y)
return eastNeighbor
def findNeighborAgents(self):
agents = []
for neighbor in self.neighbors.values():
agent = neighbor.agent
if agent != None:
agents.append(agent)
return agents
def findNeighbors(self, mode):
self.neighbors = {}
north = self.findNorthNeighbor()
south = self.findSouthNeighbor()
east = self.findEastNeighbor()
west = self.findWestNeighbor()
if north is not None:
self.neighbors["north"] = north
if south is not None:
self.neighbors["south"] = south
if east is not None:
self.neighbors["east"] = east
if west is not None:
self.neighbors["west"] = west
if mode == "moore":
northeast = north.findEastNeighbor() if north is not None else None
northwest = north.findWestNeighbor() if north is not None else None
southeast = south.findEastNeighbor() if south is not None else None
southwest = south.findWestNeighbor() if south is not None else None
if northeast is not None:
self.neighbors["northeast"] = northeast
if northwest is not None:
self.neighbors["northwest"] = northwest
if southeast is not None:
self.neighbors["southeast"] = southeast
if southwest is not None:
self.neighbors["southwest"] = southwest
def findNeighborWealth(self):
neighborWealth = 0
for neighbor in self.neighbors.values():
if neighbor != None:
neighborWealth += neighbor.sugar + neighbor.spice
return neighborWealth
def findNorthNeighbor(self):
if self.environment.wraparound == False and self.y - 1 < 0:
return None
northNeighbor = self.environment.findCell(self.x, (self.y - 1 + self.environment.height) % self.environment.height)
return northNeighbor
def findPollutionFlux(self):
meanPollution = 0
for neighbor in self.neighbors.values():
meanPollution += neighbor.pollution
meanPollution = meanPollution / (len(self.neighbors))
self.pollutionFlux = meanPollution
def findSouthNeighbor(self):
if self.environment.wraparound == False and self.y + 1 < self.environment.height - 1:
return None
southNeighbor = self.environment.findCell(self.x, (self.y + 1 + self.environment.height) % self.environment.height)
return southNeighbor
def findWestNeighbor(self):
if self.environment.wraparound == False and self.x - 1 < 0:
return None
westNeighbor = self.environment.findCell((self.x - 1 + self.environment.width) % self.environment.width, self.y)
return westNeighbor
def isOccupied(self):
return self.agent != None
def resetAgent(self):
self.agent = None
def resetSpice(self):
self.spice = 0
def resetSugar(self):
self.sugar = 0
def updateSeason(self):
if self.season == "wet":
self.season = "dry"
else:
self.season = "wet"
def __str__(self):
string = ""
if self.agent != None:
string = "-A-"
else:
string = f"{str(self.sugar)}/{str(self.spice)}"
return string