-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
140 lines (116 loc) · 4.24 KB
/
environment.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
import random
import math
class environment:
"""The environment objects"""
def grid_create(self):
grid = []
x = 0
y = 0
xrow = []
while x < self.size:
xrow.append(location(x, y))
xrow[y].randomise()
y += 1
if y >= self.size:
y = 0
x += 1
grid.append(xrow)
xrow = []
return grid
def __init__(self):
self.size = 10
self.grid = self.grid_create()
self.standard_environment()
self.set_terrain()
def main(self, organisms):
# Anything in environment that needs to change (e.g. plants grow)
# Environment acts on organisms
return
def live(self, organisms):
for num in range(len(organisms.organisms)):
organism = organisms.organisms[num]
organism.health += self.food
self.food -= 1
def standard_environment(self):
for i in range(self.size):
humidity = i / self.size
for j in range(self.size):
temperature = -20 + 60 * j / self.size
self.grid[i][j].humidity = humidity
self.grid[i][j].temperature = temperature
def location(self, pos):
return self.grid[pos[0]][pos[1]]
def list_locations(self):
locations = []
for row in self.grid:
for location in row:
locations.append(location)
return locations
def grow_plants(self):
locations = self.list_locations()
for location in locations:
location.plants_grow()
def count_organisms(self, organisms):
locations = self.list_locations()
for location in locations:
location.organism_count(organisms)
def set_terrain(self):
for location in self.list_locations():
location.set_terrain()
## should correspond to environment
stimuli = {"011": "temperature",
"021": "plant_food",
"031": "meat_food",
"041": "humidity",
"051": "light_level"}
class location():
"""A location object to include position characteristics"""
def __init__(self, xpos, ypos):
self.xpos = xpos
self.ypos = ypos
self.traits = {
"temperature": 0,
"plant_food": 0,
"meat_food": 0,
"humidity": 0,
"light_level": 0
}
self.terrain = 'grass' # terrain type (from a list somewhere)
self.humidity = 0 # water content (between 0 and 1)
self.light_level = 0 # how bright a place is (between 0 and 1)
self.organism_number = 0
self.organisms_list = []
self.organisms_list_after_move = []
def randomise(self):
self.traits["temperature"] = random.randint(-20, 40)
self.traits["plant_food"] = random.randint(0, 1000)
self.traits["meat_food"] = random.randint(0, 200)
self.traits["humidity"] = random.random()
self.traits["light_level"] = random.random()
def plants_grow(self):
self.traits["plant_food"] = (1 + 0.2 * self.traits["light_level"]) * self.traits["plant_food"]
def meat_rots(self):
self.traits["meat_food"] -= (self.traits["humidity"] * max(0, self.traits["temperature"]) / 40) * self.traits["meat_food"]
def get_stimulus(self, index):
stimulus_name = stimuli[index]
return self.traits[stimulus_name]
def organism_count(self, organisms):
self.organism_number = 0
self.checked_location = (self.xpos, self.ypos)
for creature in organisms.organisms:
if creature.current_pos == self.checked_location:
self.organism_number += 1
else:
pass
def set_terrain(self):
if self.temperature > 30:
self.terrain = 'sand'
elif self.temperature < -10:
self.terrain = 'snow'
def set_plant_food(self):
if self.terrain == 'grass':
self.traits["plant_food"] = random.randint(20, 50)
elif self.terrain == 'snow':
self.traits["plant_food"] = random.randint(10, 15)
elif self.terrain == 'sand':
self.traits["plant_food"] = 0 #random.randint(0,5)