Gardening.py (view raw)
1import random, time, math, datetime, os
2from Constants import *
3
4water_duration = 3600 * 24
5stage_factors = (1, 3, 10, 20, 30)
6indicator_squares = 6
7mutation_rarity = 20000 # Increase this # to make mutation rarer (chance 1 out of x each second)
8
9class Plant(object):
10 # This is your plant!
11 def __init__(self, owner, generation=1):
12 # Constructor
13 self.points = 0 # one point per second
14 self.life_stages = tuple(st * water_duration for st in stage_factors)
15 self.stage = 0
16 self.mutation = 0
17 self.species = random.randint(0, len(species_list) - 1)
18 self.color = random.randint(0, len(color_list) - 1)
19 self.name = plant_names[random.randint(0, len(plant_names) - 1)]
20 self.rarity = self.rarity_check()
21 self.ticks = 0
22 self.age_formatted = "0"
23 self.generation = generation
24 self.generation_bonus = 1 + (0.2 * (generation - 1))
25 self.dead = False
26 self.owner = owner
27 self.start_time = int(time.time())
28 self.last_time = int(time.time())
29 self.last_update = int(time.time())
30 # must water plant first day
31 self.last_water = int(time.time()) - water_duration - 1
32 self.watered_24h = True
33 self.visitors = []
34
35 def update(self):
36 # find out stage:
37 self.water_check()
38 if self.dead_check(): # updates self.time_delta_watered
39 return
40
41 self.stage = find_stage(self)
42
43 def parse_plant(self):
44 # Converts plant data to human-readable format
45 output = ""
46 if self.stage >= 3:
47 output += rarity_list[self.rarity] + " "
48 if self.mutation != 0:
49 output += mutation_list[self.mutation] + " "
50 if self.stage >= 4:
51 output += color_list[self.color] + " "
52 output += stage_list[self.stage] + " "
53 if self.stage >= 2:
54 output += species_list[self.species] + " "
55 return output.strip()
56
57 def rarity_check(self):
58 # Generate plant rarity
59 CONST_RARITY_MAX = 256.0
60 rare_seed = random.randint(1,CONST_RARITY_MAX)
61 common_range = round((2.0/3)*CONST_RARITY_MAX)
62 uncommon_range = round((2.0/3)*(CONST_RARITY_MAX-common_range))
63 rare_range = round((2.0/3)*(CONST_RARITY_MAX-common_range-uncommon_range))
64 legendary_range = round((2.0/3)*(CONST_RARITY_MAX-common_range-uncommon_range-rare_range))
65
66 common_max = common_range
67 uncommon_max = common_max + uncommon_range
68 rare_max = uncommon_max + rare_range
69 legendary_max = rare_max + legendary_range
70 godly_max = CONST_RARITY_MAX
71
72 if 0 <= rare_seed <= common_max:
73 return 0
74 elif common_max < rare_seed <= uncommon_max:
75 return 1
76 elif uncommon_max < rare_seed <= rare_max:
77 return 2
78 elif rare_max < rare_seed <= legendary_max:
79 return 3
80 elif legendary_max < rare_seed <= godly_max:
81 return 4
82
83 def dead_check(self):
84 # if it has been >5 days since watering, sorry plant is dead :(
85 self.time_delta_watered = int(time.time()) - self.last_water
86 if self.time_delta_watered > (5 * water_duration):
87 self.dead = True
88 return self.dead
89
90 def water_check(self):
91 self.time_delta_watered = int(time.time()) - self.last_water
92 if self.time_delta_watered <= (water_duration):
93 if not self.watered_24h:
94 self.watered_24h = True
95 return True
96 else:
97 self.watered_24h = False
98 return False
99
100 def mutate_check(self, increase):
101 # Create plant mutation
102 mutation_seed = random.randint(increase, mutation_rarity)
103 if mutation_seed == mutation_rarity:
104 # mutation gained!
105 mutation = random.randint(0,len(self.mutation_list)-1)
106 if self.mutation == 0:
107 self.mutation = mutation
108 return True
109 else:
110 return False
111
112 def growth(self):
113 # Increase plant growth stage
114 if self.stage < (len(stage_list)-1):
115 self.stage += 1
116
117 def water(self):
118 # Increase plant growth stage
119 if not self.dead:
120 self.last_water = int(time.time())
121 self.watered_24h = True
122
123 def start_over(self):
124 # After plant reaches final stage, given option to restart
125 # increment generation only if previous stage is final stage and plant
126 # is alive
127 if not self.dead:
128 next_generation = self.generation + 1
129 else:
130 # Should this reset to 1? Seems unfair.. for now generations will
131 # persist through death.
132 next_generation = self.generation
133 self.kill_plant()
134
135 self.__init__(self.owner, next_generation)
136
137 def kill_plant(self):
138 self.dead = True
139
140def find_stage(plant: Plant):
141 now = int(time.time())
142
143 res1 = min(now - plant.last_water, water_duration)
144 res2 = min(plant.last_update - plant.last_water, water_duration)
145 increase = max(0, res1 - res2) # max() not necessary but just in case
146
147 plant.points += increase
148
149 if increase != 0:
150 plant.mutate_check(increase)
151
152 plant.last_update = now
153
154 stages = tuple(ti / plant.generation_bonus for ti in plant.life_stages) # bonus is applied to stage thresholds
155 count = 0
156 closest = None
157 delta = plant.points
158
159 for n in stages:
160 if (n <= delta and (closest is None or (delta - n) < (delta - closest))):
161 closest = n
162 count += 1
163 return count
164
165def get_plant_water(plant: Plant):
166 water_delta = time.time() - plant.last_water
167 water_left_pct = max(0, 1 - (water_delta/water_duration))
168 water_left = int(math.ceil(water_left_pct * indicator_squares))
169 return f"{water_left * '🟦'}{'⬛' * (indicator_squares - water_left)} {str(math.ceil(water_left_pct * 100))}% "
170
171def get_plant_description(plant: Plant):
172 output_text = ""
173 this_species = species_list[plant.species]
174 this_color = color_list[plant.color]
175 this_stage = plant.stage
176
177 if plant.dead:
178 this_stage = 99
179 try:
180 description_num = random.randint(0,len(stage_descriptions[this_stage]) - 1)
181 except KeyError as e:
182 print(e)
183 description_num = 0
184 # If not fully grown
185 if this_stage <= 4:
186 # Growth hint
187 if this_stage >= 1:
188 last_growth_at = plant.life_stages[this_stage - 1]
189 else:
190 last_growth_at = 0
191 ticks_since_last = plant.ticks - last_growth_at
192 ticks_between_stage = plant.life_stages[this_stage] - last_growth_at
193 if ticks_since_last >= ticks_between_stage * 0.8:
194 output_text += "You notice your plant looks different.\n"
195
196 output_text += get_stage_description(this_stage, description_num, this_species, this_color) + "\n"
197
198 # if seedling
199 if this_stage == 1:
200 species_options = [species_list[plant.species],
201 species_list[(plant.species+3) % len(species_list)],
202 species_list[(plant.species-3) % len(species_list)]]
203 random.shuffle(species_options)
204 plant_hint = "It could be a(n) " + species_options[0] + ", " + species_options[1] + ", or " + species_options[2]
205 output_text += plant_hint + ".\n"
206
207 # if young plant
208 if this_stage == 2:
209 if plant.rarity >= 2:
210 rarity_hint = "You feel like your plant is special."
211 output_text += rarity_hint + ".\n"
212
213 # if mature plant
214 if this_stage == 3:
215 color_options = [color_list[plant.color],
216 color_list[(plant.color+3) % len(color_list)],
217 color_list[(plant.color-3) % len(color_list)]]
218 random.shuffle(color_options)
219 return "You can see the first hints of " + color_options[0] + ", " + color_options[1] + ", or " + color_options[2]
220
221 return output_text
222
223def get_plant_art(plant: Plant):
224
225 if plant.dead == True:
226 filename = 'rip.txt'
227 elif datetime.date.today().month == 10 and datetime.date.today().day == 31:
228 filename = 'jackolantern.txt'
229 elif plant.stage == 0:
230 filename = 'seed.txt'
231 elif plant.stage == 1:
232 filename = 'seedling.txt'
233 elif plant.stage == 2:
234 filename = plant_art_list[plant.species]+'1.txt'
235 elif plant.stage == 3 or plant.stage == 5:
236 filename = plant_art_list[plant.species]+'2.txt'
237 elif plant.stage == 4:
238 filename = plant_art_list[plant.species]+'3.txt'
239
240 # Prints ASCII art from file at given coordinates
241 this_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "art")
242 this_filename = os.path.join(this_dir, filename)
243 this_file = open(this_filename,"r")
244 this_string = this_file.read()
245 this_file.close()
246 return this_string
247
248def get_plant_info(plant: Plant):
249
250 return f'''
251{get_plant_description(plant)}
252```{get_plant_art(plant)}```
253{plant.name}, the {plant.parse_plant()}
254
255{get_plant_water(plant)}
256
257Points: {plant.points}
258Bonus: x{plant.generation_bonus - 1}
259Owner: {plant.owner}
260'''