-
Notifications
You must be signed in to change notification settings - Fork 21
/
effects.py
1148 lines (986 loc) · 37.6 KB
/
effects.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys, inspect
import itertools
import numpy as np
import sapai
from sapai.data import data
from sapai.tiers import pet_tier_lookup, pet_tier_lookup_std
from sapai.foods import Food
"""
This module implements all effects in the game. API description is as follows:
Arguments
---------
apet: Pet
Activating Pet. Must include the attached shop or player when necessary.
apet_idx: list
List of two indices for the [team_list_idx, team_idx] from which the
activating pet was called from
teams: list
List of Team objects. Either one or two teams.
te: Pet
Triggering pet. If a pet has trigger the action. For example, if a horse
is on the team and then an animal is summoned. The summoned animal would
be the triggering pet.
te_idx: list
List of two indices for the [team_list_idx, team_idx] from which the
triggering entity exists. This is only used for SummonPet in the case
of a Fly summoning in the position of the fainted pet, which is the
triggering entity. In all other cases, te_idx should not be used as it
is unnecessary.
fixed_targets: list of pets
If a fixed target is desired for the effect. For common use, this is not
required. It is useful in the case that certain outcomees are being
tested. It is also useful in the case that all outcomes due to
randomness are exactly exactly considered rather than having to rely on bootstrapped probabilities. This leads to significantly improved
efficiency for training by database purposes.
Returns
-------
targets: list
List of pets that have been targeted by the effect of the ability
possible: list of lists
List of lists of all possible targets that could also be targeted.
If there is an element of randomness in the outcome, all possible
targets, and potentially all possible combinations of targets, is also
returned.
"""
def get_effect_function(effect_kind):
if isinstance(effect_kind, sapai.Pet):
effect_kind = effect_kind.ability["effect"]["kind"]
elif isinstance(effect_kind, sapai.TeamSlot):
effect_kind = effect_kind.pet.ability["effect"]["kind"]
elif type(effect_kind) == str:
pass
else:
raise Exception(f"Unrecognized input {effect_kind}")
if effect_kind not in func_dict:
raise Exception(
f"Input effect_kind {effect_kind} not found in {list(func_dict.keys())}"
)
return func_dict[effect_kind]
def get_pet(pet_idx, teams, fainted_pet=None, te=None):
"""Helper function with error catching"""
team_idx = pet_idx[0]
team_pet_idx = pet_idx[1]
if len(teams) > 2:
raise Exception("Cannot input more than 2 teams")
if team_idx >= len(teams):
raise Exception("Team idx greater than provided number of teams")
if team_pet_idx >= 5:
raise Exception("Team pet idx greater than 5")
if fainted_pet is None:
pet = teams[team_idx][team_pet_idx].pet
else:
pet = fainted_pet
if te is not None:
pet = te
return pet
def get_teams(pet_idx, teams):
if len(teams) == 1:
return teams[0], []
if pet_idx[0] == 0:
fteam = teams[0]
oteam = teams[1]
elif pet_idx[0] == 1:
fteam = teams[1]
oteam = teams[0]
else:
raise Exception("That's impossible")
return fteam, oteam
def get_target(
apet,
apet_idx,
teams,
te=None,
fixed_targets=None,
get_from=False,
test_kind="",
):
"""
Returns the targets for a given effect. Targets are returned as a list of
pets.
Arguments
---------
apet: Pet or Food
Data to use for ability/effect/etc
apet_idx: list
List of two indices that provide the team index and the pet index
that has requested to obtain target pets
teams: list
List of two teams
fainted_pet: Pet
If the target has been requested due to fainting, the fainted pet should
be input.
get_from: bool
For correting some database inconsistencies
te: Pet
Triggering entity
"""
fixed_targets = fixed_targets or []
if isinstance(apet, sapai.Pet):
effect = apet.ability["effect"]
elif isinstance(apet, Food):
effect = apet.effect
if len(teams) == 1:
teams = [teams[0], []]
### Logic switch because data-dictionary is not consistent
if "target" not in effect:
if "to" in effect:
target = effect["to"]
else:
print(apet, apet_idx, teams, te, fixed_targets, get_from)
raise Exception("Target not found")
else:
target = effect["target"]
if get_from:
if "from" not in effect:
raise Exception("from not found in effect")
else:
target = effect["from"]
if type(target) != dict:
raise Exception("This should not be possible")
kind = target["kind"]
if "n" in target:
n = target["n"]
else:
n = 1
if len(test_kind) != 0:
kind = test_kind
if apet_idx[0] == 0:
fteam = teams[0]
oteam = teams[1]
elif apet_idx[0] == 1:
fteam = teams[1]
oteam = teams[0]
else:
raise Exception("That's impossible")
### Get possible indices for each team
fidx = []
for iter_idx, temp_slot in enumerate(fteam):
if not temp_slot.empty:
### Skiped if health is less than 0
if temp_slot.pet.health > 0:
fidx.append(iter_idx)
oidx = []
for iter_idx, temp_slot in enumerate(oteam):
if not temp_slot.empty:
### Skiped if health is less than 0
if temp_slot.pet.health > 0:
oidx.append(iter_idx)
if kind == "AdjacentAnimals":
all_pets = []
fpet_slot_idx = []
### First add opponent backward
for temp_slot in oteam[::-1]:
all_pets.append(temp_slot)
### Then friendly
for temp_slot in fteam:
fpet_slot_idx.append(len(all_pets))
all_pets.append(temp_slot)
apet_in_all = fpet_slot_idx[apet_idx[1]]
if (apet_in_all - 1) > 0:
left_slot = all_pets[apet_in_all - 1]
else:
left_slot = None
if (apet_in_all + 1) < len(all_pets):
right_slot = all_pets[apet_in_all + 1]
else:
right_slot = None
ret_pets = []
for temp_slot in [left_slot, right_slot]:
if temp_slot is None:
continue
if temp_slot.empty:
continue
else:
ret_pets.append(temp_slot.pet)
return ret_pets, [ret_pets]
elif kind == "AdjacentFriends":
all_pets = []
fpet_slot_idx = []
for temp_slot in fteam:
fpet_slot_idx.append(len(all_pets))
all_pets.append(temp_slot)
apet_in_all = fpet_slot_idx[apet_idx[1]]
if (apet_in_all - 1) > 0:
left_slot = all_pets[apet_in_all - 1]
else:
left_slot = None
if (apet_in_all + 1) < len(all_pets):
right_slot = all_pets[apet_in_all + 1]
else:
right_slot = None
ret_pets = []
for temp_slot in [left_slot, right_slot]:
if temp_slot is None:
continue
if temp_slot.empty:
continue
else:
ret_pets.append(temp_slot.pet)
return ret_pets, [ret_pets]
elif kind == "All":
ret_pets = []
for temp_idx in fidx:
ret_pets.append(fteam[temp_idx].pet)
for temp_idx in oidx:
ret_pets.append(oteam[temp_idx].pet)
return ret_pets, [ret_pets]
elif kind == "DifferentTierAnimals":
pet_tier_lookup = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
for temp_idx in fidx:
temp_tier = fteam[temp_idx].pet.tier
pet_tier_lookup[temp_tier].append(temp_idx)
### Build lookup of all possible pets
idx_list = []
for key, value in pet_tier_lookup.items():
if len(value) > 0:
idx_list.append(value)
grid = np.meshgrid(*idx_list)
ravel_grid = [x.ravel() for x in grid]
all_idx = np.array(ravel_grid).T
all_possible = []
for temp_idx in all_idx:
temp_chosen = [fteam[x].pet for x in temp_idx]
all_possible.append(temp_chosen)
if len(all_possible) == 0:
return [], []
### Choose one to return for current
choice_idx_range = np.arange(0, len(all_possible))
choice_idx = apet.rs.choice(choice_idx_range, (1,))[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
ret_pets = all_possible[choice_idx]
return ret_pets, all_possible
elif kind == "EachEnemy":
ret_pets = []
for temp_idx in oidx:
ret_pets.append(oteam[temp_idx].pet)
return ret_pets, [ret_pets]
elif kind == "EachFriend":
ret_pets = []
for temp_idx in fidx:
if temp_idx != apet_idx[1]:
ret_pets.append(fteam[temp_idx].pet)
return ret_pets, [ret_pets]
elif kind == "EachShopAnimal":
shop = apet.shop
if shop is None:
return [], []
else:
return shop.pets, [shop.pets]
elif kind == "FirstEnemy":
if len(oidx) > 0:
return [oteam[oidx[0]].pet], [[oteam[oidx[0]].pet]]
else:
return [], []
elif kind == "FriendAhead":
chosen_idx = []
for temp_idx in fidx:
if temp_idx < apet_idx[1]:
chosen_idx.append(temp_idx)
ret_pets = []
for temp_idx in chosen_idx[::-1]:
ret_pets.append(fteam[temp_idx].pet)
if len(ret_pets) >= n:
break
return ret_pets, [ret_pets]
elif kind == "FriendBehind":
chosen_idx = []
for temp_idx in fidx:
if temp_idx > apet_idx[1]:
chosen_idx.append(temp_idx)
ret_pets = []
for temp_idx in chosen_idx:
ret_pets.append(fteam[temp_idx].pet)
if len(ret_pets) >= n:
break
return ret_pets, [ret_pets]
elif kind == "HighestHealthEnemy":
health_list = []
for temp_idx in oidx:
health_list.append(oteam[temp_idx].pet.health)
if len(health_list) > 0:
max_health = np.max(health_list)
choice_idx_range = np.where(np.array(health_list) == max_health)[0]
choice_idx = apet.rs.choice(choice_idx_range, (1,), replace=False)[0]
all_possible = [[oteam[oidx[x]].pet] for x in choice_idx_range]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
### Dereference max_idx
return [oteam[oidx[choice_idx]].pet], all_possible
else:
return [], []
elif kind == "LastEnemy":
if len(oidx) > 0:
return [oteam[np.max(oidx)].pet], [[oteam[np.max(oidx)].pet]]
else:
return [], []
elif kind == "LeftMostFriend":
max_idx = np.max(fidx)
return [fteam[max_idx].pet], [[fteam[max_idx].pet]]
elif kind == "Level2And3Friends":
level_list = []
for temp_idx in fidx:
level_list.append(fteam[temp_idx].pet.level)
if len(level_list) > 0:
keep_idx = np.where(np.array(level_list) > 1)[0]
ret_pets = []
for temp_idx in keep_idx:
### Dereference idx
temp_idx = fidx[temp_idx]
ret_pets.append(fteam[temp_idx].pet)
return ret_pets, [ret_pets]
else:
return [], []
elif kind == "LowestHealthEnemy":
health_list = []
for temp_idx in oidx:
health_list.append(oteam[temp_idx].pet.health)
if len(health_list) > 0:
min_health = np.min(health_list)
choice_idx_range = np.where(np.array(health_list) == min_health)[0]
choice_idx = apet.rs.choice(choice_idx_range, (1,), replace=False)[0]
all_possible = [[oteam[oidx[x]].pet] for x in choice_idx_range]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
### Dereference max_idx
return [oteam[oidx[choice_idx]].pet], all_possible
else:
return [], []
elif kind == "RandomEnemy":
ret_pets = []
all_possible = []
if len(oidx) > 0:
if len(oidx) < n:
n = len(oidx)
all_idx = [x for x in itertools.combinations(oidx, n)]
all_possible = []
for temp_idx in all_idx:
temp_chosen = [oteam[x].pet for x in temp_idx]
all_possible.append(temp_chosen)
if len(all_possible) == 0:
return [], []
crange = np.arange(0, len(all_possible))
cidx = apet.rs.choice(crange, (1,), replace=False)[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
ret_pets = all_possible[cidx]
return ret_pets, all_possible
elif kind == "RandomFriend":
ret_pets = []
all_possible = []
if len(fidx) > 0:
if len(fidx) < n:
n = len(fidx)
keep_fidx = []
for temp_entry in fidx:
if temp_entry == apet_idx[1]:
continue
keep_fidx.append(temp_entry)
fidx = keep_fidx
all_idx = [x for x in itertools.combinations(fidx, n)]
all_possible = []
for temp_idx in all_idx:
temp_chosen = [fteam[x].pet for x in temp_idx]
all_possible.append(temp_chosen)
if len(all_possible) == 0:
return [], []
crange = np.arange(0, len(all_possible))
cidx = apet.rs.choice(crange, (1,), replace=False)[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
ret_pets = all_possible[cidx]
return ret_pets, all_possible
elif kind == "RightMostFriend":
return [fteam[fidx[0]].pet], [[fteam[fidx[0]].pet]]
elif kind == "Self":
return [apet], [[apet]]
elif kind == "StrongestFriend":
stat_list = []
for temp_idx in fidx:
temp_stats = fteam[temp_idx].pet.attack + fteam[temp_idx].pet.health
stat_list.append(temp_stats)
stat_list = np.array(stat_list)
max_stats = np.max(stat_list)
max_idx = np.where(stat_list == max_stats)[0]
all_possible = []
for temp_idx in max_idx:
all_possible.append(fteam[temp_idx].pet)
if len(all_possible) == 0:
return [], []
choice = apet.rs.choice(max_idx, (1,), replace=False)[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
ret_pets = [fteam[choice].pet]
return ret_pets, all_possible
elif kind == "HighestHealthFriend":
health_list = []
for temp_idx in fidx:
health_list.append(fteam[temp_idx].pet.health)
if len(health_list) == 0:
return [], []
max_health = np.max(health_list)
max_idx = np.where(health_list == max_health)[0]
all_possible = []
for temp_idx in max_idx:
all_possible.append(fteam[fidx[temp_idx]].pet)
choice = apet.rs.choice(max_idx, (1,), replace=False)[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
ret_pets = [fteam[fidx[choice]].pet]
return ret_pets, all_possible
elif kind == "TriggeringEntity":
if te is not None:
return [te], [[te]]
else:
return [], []
elif kind == "NonWeakEnemy":
possible = []
for temp_idx in oidx:
temp_pet = oteam[temp_idx].pet
if temp_pet.status == "status-weak":
continue
else:
possible.append([temp_pet])
if len(possible) == 0:
return [], []
idx_range = np.arange(0, len(possible))
chosen_idx = apet.rs.choice(idx_range, (1,), replace=False)[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
return possible[chosen_idx], possible
elif kind == "none":
### No targets
return [], []
else:
raise Exception(f"Target {kind} impelementation not found")
def AllOf(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
"""AllOf will return list of lists"""
fixed_targets = fixed_targets or []
original_effect = apet.ability["effect"]
effects = apet.ability["effect"]["effects"]
target = []
possible_targets = []
for iter_idx, temp_effect in enumerate(effects):
effect_kind = temp_effect["kind"]
func = get_effect_function(effect_kind)
apet.ability["effect"] = temp_effect
temp_target, temp_possible = func(apet, apet_idx, teams, te, fixed_targets)
target.append(temp_target)
possible_targets.append(temp_possible)
apet.ability["effect"] = original_effect
return target, possible_targets
def ApplyStatus(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
if len(fixed_targets) == 0:
target, possible = get_target(apet, apet_idx, teams, te=te)
else:
target = fixed_targets
possible = [fixed_targets]
status = apet.ability["effect"]["status"]
for target_pet in target:
target_pet.status = status
return target, possible
def DealDamage(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
if len(fixed_targets) == 0:
target, possible = get_target(apet, apet_idx, teams, te=te)
else:
target = fixed_targets
possible = [fixed_targets]
health_amount = apet.ability["effect"]["amount"]
if type(health_amount) == dict:
if "attackDamagePercent" in health_amount:
health_amount = int(
apet.attack * health_amount["attackDamagePercent"] * 0.01
)
else:
raise Exception()
for target_pet in target:
if target_pet.status == "status-melon-armor":
health_amount = max(0, health_amount - 20)
target_pet.status = "none"
elif target_pet.status == "status-garlic-armor":
health_amount = max(1, health_amount - 2)
elif target_pet.status == "status-coconut-shield":
health_amount = 0
target_pet.status = "none"
elif target_pet.status == "status-weak":
health_amount += 3
target_pet.hurt(health_amount)
return target, possible
def GainExperience(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
if len(fixed_targets) == 0:
target, possible = get_target(apet, apet_idx, teams, te=te)
else:
target = fixed_targets
possible = [fixed_targets]
for target_pet in target:
amount = apet.ability["effect"]["amount"]
level_up = target_pet.gain_experience(amount=amount)
if level_up:
target_pet.levelup_trigger(target_pet)
player = apet.player
if player is not None:
apet.player.shop.levelup()
return target, possible
def GainGold(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
amount = apet.ability["effect"]["amount"]
player = apet.player
if player is not None:
apet.player.gold += amount
return player, [player]
def Evolve(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
if len(fixed_targets) == 0:
target, possible = get_target(apet, apet_idx, teams, te=te)
else:
target = fixed_targets
possible = [fixed_targets]
fteam, oteam = get_teams(apet_idx, teams)
spet = sapai.Pet(target[0].ability["effect"]["into"])
try:
fteam.remove(target[0])
except Exception:
# tiger behind, just does nothing
return target, possible
fteam[apet_idx[1]] = spet
return target, possible
def FoodMultiplier(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
if te is None:
raise Exception("Must input purchased food to FoodMultiplier")
mult = int(apet.ability["effect"]["amount"])
food_list = [te]
for food in food_list:
### Multiplier is not strict multiplier of current value, but additive
### multiplier of base attack and health
mult = mult - 1
food.attack += food.base_attack * mult
food.health += food.base_health * mult
return food_list, [food_list]
def ModifyStats(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
# print("CALLED ModifyStats")
# print("----------------")
# print(apet)
# print(apet_idx)
# print(teams)
# print(te)
fixed_targets = fixed_targets or []
if len(fixed_targets) == 0:
target, possible = get_target(apet, apet_idx, teams, te=te)
else:
target = fixed_targets
possible = [fixed_targets]
attack_amount = 0
health_amount = 0
if "attackAmount" in apet.ability["effect"]:
attack_amount = apet.ability["effect"]["attackAmount"]
if "healthAmount" in apet.ability["effect"]:
health_amount = apet.ability["effect"]["healthAmount"]
if "amount" in apet.ability["effect"]:
if type(apet.ability["effect"]["amount"]) == dict:
if "attackPercent" in apet.ability["effect"]["amount"]:
attack_amount = int(
apet.attack
* apet.ability["effect"]["amount"]["attackPercent"]
* 0.01
)
else:
raise Exception()
for target_pet in target:
if (
"untilEndOfBattle" in apet.ability["effect"]
and apet.ability["effect"]["untilEndOfBattle"] is True
):
target_pet._until_end_of_battle_attack_buff += attack_amount
target_pet._until_end_of_battle_health_buff += health_amount
else:
target_pet._attack += attack_amount
target_pet._health += health_amount
if (
"includingFuture" in apet.ability["effect"]["target"]
and apet.ability["effect"]["target"]["includingFuture"] is True
):
if target_pet.shop is not None:
target_pet.shop.shop_attack += attack_amount
target_pet.shop.shop_health += health_amount
target_pet._attack = min([target_pet._attack, 50])
target_pet._health = min([target_pet._health, 50])
return target, possible
def OneOf(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
"""
Dog is only one with OneOf anyways
However, OneOf current not returning possible correctly because I haven't
decided exactly how possible will be used...
"""
# if len(fixed_targets) == 0:
# target,possible = get_target(apet, apet_idx, teams, te=te)
# else:
# target = fixed_targets
# possible = [fixed_targets]
fixed_targets = fixed_targets or []
possible = [[apet]]
original_effect = apet.ability["effect"]
effects = apet.ability["effect"]["effects"]
chosen_idx = apet.rs.choice(np.arange(0, len(effects)), size=(1,))[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
effect = effects[chosen_idx]
effect_kind = effect["kind"]
apet.ability["effect"] = effect
func = get_effect_function(effect_kind)
target = func(apet, apet_idx, teams, te, fixed_targets)[0]
apet.ability["effect"] = original_effect
return target, possible
def ReduceHealth(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
if len(fixed_targets) == 0:
target, possible = get_target(apet, apet_idx, teams, te=te)
else:
target = fixed_targets
possible = [fixed_targets]
per = apet.ability["effect"]["percentage"] * 0.01
for temp_pet in target:
if temp_pet.health > 0:
temp_pet._health = temp_pet.health - int(np.round(temp_pet.health * per))
if temp_pet._health == 0:
### Floor health of 1
temp_pet._health = 1
return target, possible
def RefillShops(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
"""
Only Cow has refill shop in newest patch anyways...
"""
fixed_targets = fixed_targets or []
if apet.name != "pet-cow":
raise Exception("Only cow implemented for RefillShops")
shop = apet.shop
level = apet.level
targets = []
for slot in shop:
if slot.slot_type == "food":
temp_food = Food("milk")
temp_food.attack *= level
temp_food.health *= level
slot.item = temp_food
targets.append(slot)
return targets, [targets]
def RepeatAbility(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
"""
Tiger implementation.
"""
fixed_targets = fixed_targets or []
### First, modify te by the level of the tiger
original_level = te.level
te.level = apet.level
### Get te ability
func = get_effect_function(te)
### Call will te as apet
if len(fixed_targets) == 0:
targets, possible = func(te, apet_idx, teams, te=None, fixed_targets=None)
else:
### Use fixed_targets if the repeated function is supposed to target
### exactly the same pets again
targets, possible = func(te, apet_idx, teams, te=fixed_targets)
te.level = original_level
return targets, possible
def RespawnPet(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
"""
Only for Mushroom food at the moment
"""
te_idx = te_idx or []
fixed_targets = fixed_targets or []
if len(te_idx) == 0:
raise Exception("Indices of triggering entity must be provided as te_idx")
fteam, _ = get_teams(apet_idx, teams)
spet_name = apet.name
if len(fixed_targets) > 0:
raise NotImplementedError
target_team = fteam
#### First, determine how many pets should be infront
nahead = len(fteam.get_ahead(te_idx[1], n=5))
npets = len(fteam)
### Then move team as far backward as possible
fteam.move_backward()
### Move nahead pets forward which should be infront of the triggering pet
end_idx = (5 - npets) + nahead
fteam.move_forward(start_idx=0, end_idx=end_idx)
target = []
### Check for furthest back open position
empty_idx = []
for iter_idx, temp_slot in enumerate(target_team):
if temp_slot.empty:
empty_idx.append(iter_idx)
if len(empty_idx) == 0:
### Can safely return, cannot summon
return target, [target]
target_slot_idx = np.max(empty_idx)
target_team[target_slot_idx] = spet_name
spet = target_team[target_slot_idx].pet
if "baseAttack" in apet.ability["effect"]:
spet._attack = apet.ability["effect"]["baseAttack"]
if "baseHealth" in apet.ability["effect"]:
spet._health = apet.ability["effect"]["baseHealth"]
spet.level = apet.level
target.append(spet)
### Move back forward
target_team.move_forward()
for temp_slot in target_team:
### Make sure team is assigned correctly to all pets
temp_slot.pet.team = target_team
return target, [target]
def SummonPet(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
""" """
# print("CALLED SUMMON")
# print("----------------")
# print(pet_idx)
# print(teams)
# print(fainted_pet)
# print(te)
te_idx = te_idx or []
fixed_targets = fixed_targets or []
if len(te_idx) == 0:
raise Exception("Indices of triggering entity must be provided as te_idx")
fteam, oteam = get_teams(apet_idx, teams)
spet_name = apet.ability["effect"]["pet"]
team = apet.ability["effect"]["team"]
if len(fixed_targets) > 0:
raise NotImplementedError
if team == "Friendly":
target_team = fteam
#### First, determine how many pets should be infront
nahead = len(fteam.get_ahead(te_idx[1], n=5))
npets = len(fteam)
### Then move team as far backward as possible
fteam.move_backward()
### Move nahead pets forward which should be infront of the triggering pet
end_idx = (5 - npets) + nahead
fteam.move_forward(start_idx=0, end_idx=end_idx)
elif team == "Enemy":
target_team = oteam
if type(target_team).__name__ != "Team":
### Assume that oteam was not input, for example if Rat was pilled
### during the shop phase
return [], []
target_team.move_backward()
else:
raise Exception(apet.ability["effect"]["team"])
n = 1
if apet.name == "pet-sheep":
n = 2
elif apet.name == "pet-rooster":
n = apet.level
target = []
for _ in range(n):
### Check for furthest back open position
empty_idx = []
for iter_idx, temp_slot in enumerate(target_team):
if temp_slot.empty:
empty_idx.append(iter_idx)
if len(empty_idx) == 0:
### Can safely return, cannot summon
return target, [target]
target_slot_idx = np.max(empty_idx)
target_team[target_slot_idx] = spet_name
spet = target_team[target_slot_idx].pet
if "withAttack" in apet.ability["effect"]:
spet._attack = apet.ability["effect"]["withAttack"]
if "withHealth" in apet.ability["effect"]:
spet._health = apet.ability["effect"]["withHealth"]
if "withLevel" in apet.ability["effect"]:
spet.level = apet.ability["effect"]["withLevel"]
if apet.name == "pet-rooster":
spet._attack = int(apet.attack * 0.5)
target.append(spet)
### Move back forward
target_team.move_forward()
for temp_slot in target_team:
### Make sure team is assigned correctly to all pets
temp_slot.pet.team = target_team
return target, [target]
def SummonRandomPet(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
""" """
te_idx = te_idx or []
fixed_targets = fixed_targets or []
fteam, oteam = get_teams(apet_idx, teams)
if len(fixed_targets) > 0:
chosen = fixed_targets[0]
if len(fixed_targets) > 1:
raise Exception("Only 1 fixed_targets input allowed for SummonRandomPet")
else:
tier = apet.ability["effect"]["tier"]
if fteam.pack == "StandardPack":
possible = pet_tier_lookup_std[tier]
elif fteam.pack == "ExpansionPack1":
possible = pet_tier_lookup[tier]
else:
raise Exception()
chosen = apet.rs.choice(possible, (1,))[0]
### Update internal state of random generator
apet.seed_state = apet.rs.get_state()
#### Perform team movement to ensure that the pet is summoned in the
#### correct position
#### First, determine how many pets should be infront
nahead = len(fteam.get_ahead(te_idx[1], n=5))
npets = len(fteam)
### Then move team as far backward as possible
fteam.move_backward()
### Move nahead pets forward which should be infront of the triggering pet
end_idx = (5 - npets) + nahead
fteam.move_forward(start_idx=0, end_idx=end_idx)
### Check for furthest back open position
empty_idx = []
for iter_idx, temp_slot in enumerate(fteam):
if temp_slot.empty:
empty_idx.append(iter_idx)
if len(empty_idx) == 0:
### Can safely return, cannot summon
return []
target_slot_idx = np.max(empty_idx)
fteam[target_slot_idx] = str(chosen)
spet = fteam[target_slot_idx].pet
if "baseAttack" in apet.ability["effect"]:
sattack = apet.ability["effect"]["baseAttack"]
else:
sattack = data["pets"][spet.name]["baseAttack"]
if "baseHealth" in apet.ability["effect"]:
shealth = apet.ability["effect"]["baseHealth"]
else:
shealth = data["pets"][spet.name]["baseHealth"]
if "level" in apet.ability["effect"]:
spet.level = apet.ability["effect"]["level"]
if "statsModifier" in apet.ability["effect"]:
sattack *= apet.ability["effect"]["statsModifier"]
shealth *= apet.ability["effect"]["statsModifier"]
spet._attack = sattack
spet._health = shealth
fteam.move_forward()
for temp_slot in fteam:
temp_slot.pet.team = fteam
return [spet], [[x] for x in possible]
def Swallow(apet, apet_idx, teams, te=None, te_idx=None, fixed_targets=None):
fixed_targets = fixed_targets or []
fteam, oteam = get_teams(apet_idx, teams)