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