-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanter.py
More file actions
225 lines (176 loc) · 5.18 KB
/
planter.py
File metadata and controls
225 lines (176 loc) · 5.18 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
import mover
import misc
usedWorldSize = None
isPlantedSet = set() #plant or empty
fertilizedSet = set() #a set containing only fertilized entitites
infectedSet = set() #a set containing only infected entities
treeSet = set() #a set containing all trees planted
def init():
quick_print("planter.init() was called")
global usedWorldSize
global isPlantedSet
global fertilizedSet
global infectedSet
global treeSet
usedWorldSize = get_world_size()
isPlantedSet = set()
fertilizedSet = set()
infectedSet = set()
treeSet = set()
# clear()
# for idx in range( misc.getEntityMaxCount() ):
# x = (idx % get_world_size())
# y = (idx / get_world_size()) // 1
# isPlantedSet.add((x,y))
def canFertilize( entityCount ):
if( num_unlocked(Unlocks.Fertilizer) == 0 ):
return False
return num_items(Items.Fertilizer) >= entityCount
def canUnfertilize( entityCount ):
if( num_unlocked(Unlocks.Fertilizer) == 0 ):
return False
return num_items(Items.Weird_Substance) >= entityCount
def getEntityCost(plantEntity, entityCount):
entityCost = get_cost(plantEntity)
if( entityCost == None or len(entityCost) == 0 ):
return None
for item in entityCost:
multipleCost = entityCost[item] * entityCount
entityCost[item] = multipleCost
return entityCost
def canAffordEntity(plantEntity, entityCount):
entityCost = get_cost(plantEntity)
if( entityCost == None or len(entityCost) == 0 ):
return True #not plantable or free
for item in entityCost:
cost = entityCost[item] * entityCount
if( cost > num_items(item) ):
return False #costs, and can not afford
return True #costs, but can afford
def getIrrigateLevel( entityCount ):
waterItemCount = num_items(Items.Water)
waterRequired = entityCount
if( waterItemCount >= waterRequired*4 ):
irrigateLevel = 1
elif( waterItemCount >= waterRequired*3 ):
irrigateLevel = 0.75
elif( waterItemCount >= waterRequired*2):
irrigateLevel = 0.5
elif( waterItemCount >= waterRequired*1):
irrigateLevel = 0.25
else:
irrigateLevel = 0
return irrigateLevel
def irrigateHere( waterLevel ):
if(waterLevel < 0):
waterLevel = 0
elif(waterLevel > 1):
waterLevel = 1
while(waterLevel > 0 and get_water() < waterLevel and num_items(Items.Water) > 0):
use_item(Items.Water)
def plantHere(entity):
tillHere(Grounds.Soil)
if not( get_entity_type() == None ):
return False
if(entity == Entities.Tree):
thisSet = getSurroundingSet( get_pos_x(),get_pos_y() )
for pos in thisSet:
if pos in treeSet:
return False #trees must be planted sparse
if(plant(entity) == False):
return False
pos = (get_pos_x(),get_pos_y())
isPlantedSet.add(pos)
if(entity == Entities.Tree):
treeSet.add(pos)
return True
def harvestHere():
if( harvest() == False ):
return False
thisGroundType = get_ground_type()
pos = ( get_pos_x(),get_pos_y() )
if( thisGroundType == Grounds.Grassland ):
pass
else:
if pos in isPlantedSet:
isPlantedSet.remove(pos)
if pos in treeSet:
treeSet.remove(pos)
if pos in infectedSet:
infectedSet.remove(pos)
if pos in fertilizedSet:
fertilizedSet.remove(pos)
return True
def tillHere( targetGroundType ):
thisGroundType = get_ground_type()
thisEntity = get_entity_type()
shouldTill = thisGroundType != targetGroundType
if( shouldTill ):
till()
if( shouldTill == False and thisGroundType != Grounds.Grassland and thisEntity != None ):
harvest()
if( shouldTill ):
pos = ( get_pos_x(),get_pos_y() )
if( targetGroundType == Grounds.Grassland ):
isPlantedSet.add(pos)
else:
if pos in isPlantedSet:
isPlantedSet.remove(pos)
return shouldTill
def getSurroundingSet(x,y):
affectedSet = set()
(minX,maxX,minY,maxY) = misc.getWorldMinMaxXXYY()
pos = (x,y) #here
if pos in isPlantedSet:
affectedSet.add(pos)
if(x < maxX):
pos = (x+1,y) #east
if pos in isPlantedSet:
affectedSet.add(pos)
if(x > minX):
pos = (x-1,y) #west
if pos in isPlantedSet:
affectedSet.add(pos)
if(y < maxY):
pos = (x,y+1) #north
if pos in isPlantedSet:
affectedSet.add(pos)
if(y > minY):
pos = (x,y-1) #south
if pos in isPlantedSet:
affectedSet.add(pos)
return affectedSet
def getInfectedInSet(inputSet):
returnSet = set()
for pos in inputSet:
if( pos in infectedSet):
returnSet.add(pos)
return returnSet
def fertilizeHere( ):
if( get_entity_type() == Entities.Bush):
return False #don't fertilize - bushes can't be unfertilized
if( ( get_pos_x(),get_pos_y() ) in fertilizedSet ):
return False #already fertilized
if(num_items(Items.Fertilizer) > 0):
use_item(Items.Fertilizer)
infectedSet.add( (get_pos_x(),get_pos_y()) )
fertilizedSet.add( (get_pos_x(),get_pos_y()) )
return True
def unFertilizeHere( ):
if(get_entity_type() == Entities.Bush):
return False #this would create a maze
affectedSet = getSurroundingSet(get_pos_x(),get_pos_y())
if(len(affectedSet) == 0):
return False
infectedInSet = getInfectedInSet(affectedSet)
nonInfectedCount = len(affectedSet) - len(infectedInSet)
if(nonInfectedCount > 0):
return False #non-infected plants would be affected
if(num_items(Items.Weird_Substance) == 0):
return False
use_item(Items.Weird_Substance)
for pos in affectedSet:
if( pos in infectedInSet ):
infectedSet.remove(pos)
else:
infectedSet.add(pos)