generated from runelite/example-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimulation.py
executable file
·501 lines (430 loc) · 15.6 KB
/
simulation.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python
"""
A script to simulate herb farming yield. This is used to check the output of
the calculator, as a safety check. Not used in production.
"""
import argparse
import json
import math
import random
from prettytable import PrettyTable
from tqdm import tqdm
from typing import NamedTuple
PATCH_ARDOUGNE = "ardougne"
PATCH_CATHERBY = "catherby"
PATCH_FALADOR = "falador"
PATCH_FARMING_GUILD = "farming_guild"
PATCH_HARMONY = "harmony"
PATCH_HOSIDIUS = "hosidius"
PATCH_PORT_PHASMATYS = "port_phasmatys"
PATCH_TROLL_STRONGHOLD = "troll_stronghold"
PATCH_WEISS = "weiss"
COMPOST_NORMAL = "compost"
COMPOST_SUPER = "supercompost"
COMPOST_ULTRA = "ultracompost"
DIARY_EASY = "easy"
DIARY_MEDIUM = "medium"
DIARY_HARD = "hard"
DIARY_ELITE = "elite"
ANIMA_ATTAS = "attas"
ANIMA_IASOR = "iasor"
ANIMA_KRONOS = "kronos"
STATE_HEALTHY = "healthy"
STATE_DISEASED = "diseased"
STATE_DEAD = "dead"
"""
An herb. `min_chance_to_save` is the "chance to save a life while harvesting"
for the herb at level 1. All herbs have a base chance to save of 80% at level
99, and for each herb we linearly interpolate between the two based on the
player's farming level.
Values ripped from:
https://oldschool.runescape.wiki/w/Calculator:Farming/Herbs/Template?action=edit
"""
Herb = NamedTuple(
"Herb",
[
("name", str),
("level", int),
("level_1_chance_to_save", float),
("seed_xp", float),
("harvest_xp", float),
],
)
HERBS = [
Herb("Guam", 9, 25.0, 11.0, 12.5),
Herb("Marrentill", 14, 28.0, 13.5, 15.0),
Herb("Tarromin", 19, 31.0, 16.0, 18.0),
Herb("Harralander", 26, 36.0, 21.5, 24.0),
Herb("Ranarr", 32, 39.0, 27.0, 30.5),
Herb("Toadflax", 38, 43.0, 34.0, 38.5),
Herb("Irit", 44, 46.0, 43.0, 48.5),
Herb("Avantoe", 50, 50.0, 54.5, 61.5),
Herb("Kwuarm", 56, 54.0, 69.0, 78.0),
Herb("Snapdragon", 62, 57.0, 87.5, 98.5),
Herb("Cadantine", 67, 60.0, 106.5, 120.0),
Herb("Lantadyme", 73, 64.0, 134.5, 151.5),
Herb("Dwarf Weed", 79, 67.0, 170.5, 192.0),
Herb("Torstol", 85, 71.0, 199.5, 224.5),
]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("config_path")
parser.add_argument("--trials", "-t", type=int, default=1000)
args = parser.parse_args()
with open(args.config_path) as f:
config = json.load(f)
patch_names = config.pop("patches")
farming_level = config["farming_level"]
# Matrix of patch instances; each row is one herb and each column is a patch
patches_matrix = [
(
herb,
[
HerbPatch(name=patch_name, herb=herb, **config)
for patch_name in patch_names
],
)
for herb in HERBS
# Only show herbs we have the level to grow
if herb.level <= farming_level
]
# Total number of trials to run, used to show progress to the user
pbar = tqdm(total=len(HERBS) * len(patch_names) * args.trials)
# For each cell in the matrix, run n trials and store the aggregate result
results = [
(herb, [patch.simulate(args.trials, pbar) for patch in patches])
for (herb, patches) in patches_matrix
]
pbar.close()
def print_table(name, field_name, formatter, average=False):
table = PrettyTable()
table.field_names = ["Herb", *patch_names, "Total"]
for (herb, row_results) in results:
values = [getattr(cell, field_name) for cell in row_results]
total = sum(values) / len(values) if average else sum(values)
table.add_row(
[
herb.name,
*(formatter(val) for val in values),
formatter(total),
]
)
print(name)
print(table)
print("")
print_table(
"Survival Rate",
"survival_rate",
lambda survival_rate: f"{survival_rate * 100.0:.1f}%",
average=True,
)
print_table(
"Yield",
"herbs_harvested",
lambda herbs_harvested: f"{herbs_harvested:.1f}",
)
print_table("XP", "xp_gained", lambda xp_gained: f"{xp_gained:.1f}")
class HerbPatch:
def __init__(
self,
herb,
name,
farming_level,
magic_level,
compost,
magic_secateurs,
farming_cape,
resurrect_crops,
anima_plant,
falador_diary,
kandarin_diary,
kourend_diary,
hosidius_fifty_favor,
):
self.name = name
self.herb = herb
self.farming_level = farming_level
self.magic_level = magic_level
self.compost = compost
self.magic_secateurs = magic_secateurs
self.farming_cape = farming_cape
self.resurrect_crops = resurrect_crops
self.anima_plant = anima_plant
self.falador_diary = falador_diary
self.kandarin_diary = kandarin_diary
self.kourend_diary = kourend_diary
self.hosidius_fifty_favor = hosidius_fifty_favor
def get_disease_chance_per_cycle(self):
# Check for disease-free patches first
if self.name in [PATCH_TROLL_STRONGHOLD, PATCH_WEISS] or (
self.name == PATCH_HOSIDIUS and self.hosidius_fifty_favor
):
return 0.0
# https://oldschool.runescape.wiki/w/Disease_(Farming)#Reducing_disease_risk
# The disease chance is always expressed as a *proper* fraction of 128,
# i.e. the numerator is always a whole number. So we'll calculate the
# numerator separately, then divide at the end
if self.compost is None:
base_chance = 27.0
if self.compost == COMPOST_NORMAL:
base_chance = 14.0
if self.compost == COMPOST_SUPER:
base_chance = 6.0
if self.compost == COMPOST_ULTRA:
base_chance = 3.0
modifier = 1.0
if self.anima_plant == ANIMA_IASOR:
modifier = 0.2
# Round *down* to the nearest multiple of 1/128, but not to zero
numerator = max(math.floor(base_chance * modifier), 1.0)
return numerator / 128.0
def get_initial_lives(self):
if self.compost == COMPOST_ULTRA:
return 6
if self.compost == COMPOST_SUPER:
return 5
if self.compost == COMPOST_NORMAL:
return 4
if self.compost is None:
return 3
raise ValueError(f"Invalid compost value: {self.compost}")
def get_chance_to_save(self):
item_bonus = self.get_item_chance_to_save_bonus()
diary_bonus = self.get_diary_chance_to_save_bonus()
attas_bonus = self.get_attas_chance_to_save_bonus()
# YES, these chances really are supposed to be this big, they're
# essentially out of 98, not 1.0
chance1 = self.herb.level_1_chance_to_save
chance99 = 80.0 # Max chance to save for all herbs
# This is easier to read in the Wiki's format:
# https://oldschool.runescape.wiki/w/Farming#Variable_crop_yield
# That page doesn't mention anything about the `floor`s though, but it's
# in the Wiki calculator source code
# https://oldschool.runescape.wiki/w/Calculator:Template/Farming/Herbs2?action=edit
return (
math.floor(
math.floor(
math.floor(
(chance1 * (99.0 - self.farming_level) / 98.0)
+ (chance99 * (self.farming_level - 1.0) / 98.0)
)
* (1.0 + item_bonus)
# https://twitter.com/JagexAsh/status/956892754096869376
* (1.0 + diary_bonus)
# https://twitter.com/JagexAsh/status/1245644766328446976
# Note: This conflicts with how the wiki calculator does it,
# but I'm going off of Ash's tweets instead
* (1.0 + attas_bonus)
+ 1.0
)
)
/ 256.0
)
def get_item_chance_to_save_bonus(self):
"""
Get the "chance to save bonus" due to equipped items
"""
bonus = 0.0
if self.magic_secateurs:
bonus += 0.1
if self.farming_cape:
bonus += 0.05
return bonus
def get_diary_chance_to_save_bonus(self):
"""
Get the "chance to save" bonus *local to this patch*, i.e. not including
global buffs.
"""
# Catherby bonus scales based on tier completed
if self.name == PATCH_CATHERBY:
if self.kandarin_diary == DIARY_MEDIUM:
return 0.05
if self.kandarin_diary == DIARY_HARD:
return 0.10
if self.kandarin_diary == DIARY_ELITE:
return 0.15
# Both Hosidius and Farming Guild get +5% from Kourend hard
if (
self.name
in [
PATCH_HOSIDIUS,
PATCH_FARMING_GUILD,
]
and self.kourend_diary in [DIARY_HARD, DIARY_ELITE]
):
return 0.05
return 0.0 # Sad
def get_attas_chance_to_save_bonus(self):
if self.anima_plant == ANIMA_ATTAS:
return 0.05
return 0.0
def get_xp_bonus(self):
# 10% bonus for fally medium
if self.name == PATCH_FALADOR and self.falador_diary in [
DIARY_MEDIUM,
DIARY_HARD,
DIARY_ELITE,
]:
return 0.10
return 0.0
def get_compost_xp(self):
if self.compost == COMPOST_NORMAL:
return 18.0
if self.compost == COMPOST_SUPER:
return 26.0
if self.compost == COMPOST_ULTRA:
return 36.0
return 0.0
def get_resurrect_chance(self):
"""
Resurrection chance scales from 50% at 78 to 75% at 99
"""
min_magic_level = 78
max_magic_level = 99
if not self.resurrect_crops or self.magic_level < min_magic_level:
return 0.0
normalized = (self.magic_level - min_magic_level) / (
max_magic_level - min_magic_level
)
return normalized * (0.75 - 0.50) + 0.50
def simulate(self, trials, pbar):
result = AggregateResult()
for _ in range(trials):
state = HerbPatchState(
herb=self.herb,
disease_chance_per_cycle=self.get_disease_chance_per_cycle(),
resurrect_chance=self.get_resurrect_chance(),
initial_lives=self.get_initial_lives(),
chance_to_save=self.get_chance_to_save(),
xp_bonus=self.get_xp_bonus(),
compost_xp=self.get_compost_xp(),
)
state.grow()
state.harvest()
result.add_trial(state)
pbar.update() # Update progress bar
return result
class HerbPatchState:
# Growth stages vary 0-4, with 4 being fully grown
NUM_GROWTH_STAGES = 5
FINAL_GROWTH_STAGE = NUM_GROWTH_STAGES - 1
def __init__(
self,
herb,
disease_chance_per_cycle,
resurrect_chance,
initial_lives,
chance_to_save,
compost_xp,
xp_bonus,
):
# Constants
self.herb = herb
self.disease_chance_per_cycle = disease_chance_per_cycle
self.resurrect_chance = resurrect_chance
self.chance_to_save = chance_to_save
self.compost_xp = compost_xp
self.xp_bonus = xp_bonus
# Internal game state
self.lives = initial_lives
self.growth_stage = 0
self.state = STATE_HEALTHY
self.has_resurrected = False
# Player outcomes
self.herbs_harvested = 0
self.xp_gained = 0
def grow(self):
"""
Progress herb growth until either death or adulthood
"""
# Grow until we hit a terminal stage
while (
self.growth_stage < self.FINAL_GROWTH_STAGE
and self.state != STATE_DEAD
):
# It's not clear to me why the wiki uses n-1 as the exponent in
# the overall death formula, since disease can occur at any growth
# stage (i.e. all 4), and stops growth meaning death should also be
# able to occur at any growth stage. Needs more investigation.
# https://oldschool.runescape.wiki/w/Disease_(Farming)
# Plant is healthy and still growing, roll for disease
if self.state == STATE_HEALTHY and rand_bool(
self.disease_chance_per_cycle
):
# When disease is applied, the plant stops growing
self.state = STATE_DISEASED
# If we try to grow while already diseased, the plant dies *without*
# progressing to the next state
if self.state == STATE_DISEASED:
self.state = STATE_DEAD
# If the plant is dead, attempt a resurrection
if self.state == STATE_DEAD:
# We can only attempt a resurrection once per crop
if not self.has_resurrected and rand_bool(
self.resurrect_chance
):
self.state = STATE_HEALTHY
self.has_resurrected = True
# Back to the beginning of this stage, we'll need to pass
# disease check again before growing
continue
# If plant is still healthy (either passed disease check or was
# resurrected), then growth progresses and we
if self.state == STATE_HEALTHY:
self.growth_stage += 1
def harvest(self):
"""
Harvest this patch until exhaustion. If not fully grown, does nothing
"""
if self.state != STATE_HEALTHY:
return
if self.growth_stage < self.FINAL_GROWTH_STAGE:
raise ValueError("Cannot harvest plant before fully grown")
xp_factor = 1.0 + self.xp_bonus
def harvest_herb():
self.herbs_harvested += 1
self.xp_gained += self.herb.harvest_xp * xp_factor
if not rand_bool(self.chance_to_save):
self.lives -= 1
# Compost XP is given when it's spread
self.xp_gained += self.compost_xp * xp_factor
# Harvest until we can't
while self.lives > 0:
harvest_herb()
# For some wacky reason, you don't get the "plant" XP until the final
# harvest, which is important because it means dead herbs don't provide
# any XP
# https://oldschool.runescape.wiki/w/Herb_patch/Seeds#cite_ref-3
self.xp_gained += self.herb.seed_xp * xp_factor
class AggregateResult:
def __init__(self):
self._herbs_harvested = 0
self._xp_gained = 0
self._num_survived = 0
self._num_resurrected = 0
self._num_trials = 0
@property
def herbs_harvested(self):
return self._herbs_harvested / self._num_trials
@property
def xp_gained(self):
return self._xp_gained / self._num_trials
@property
def survival_rate(self):
return self._num_survived / self._num_trials
@property
def resurrection_rate(self):
return self._num_resurrected / self._num_trials
def add_trial(self, state):
self._num_trials += 1
self._herbs_harvested += state.herbs_harvested
self._xp_gained += state.xp_gained
if state.state == STATE_HEALTHY:
self._num_survived += 1
def __str__(self):
return f"{self.survival_rate * 100.0}% surv. / \
{self.herbs_harvested} herbs / {self.xp_gained} XP"
def rand_bool(true_weight=0.5):
return random.random() < true_weight
if __name__ == "__main__":
main()