-
Notifications
You must be signed in to change notification settings - Fork 25
/
engine.lua
1747 lines (1620 loc) · 44.9 KB
/
engine.lua
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
local min,max = math.min,math.max
local floor = math.floor
local type = type
Card = class(function(self, id, upgrade_lvl)
self.upgrade_lvl = upgrade_lvl or 0
--print("card init "..id)
if not id_to_canonical_card[id] then
print(id, type(id), "devil card????")
end
for k,v in pairs(id_to_canonical_card[id]) do
self[k]=deepcpy(v)
--print(k,v,self[k])
end
if self.type ~= "character" then
self.active = true
end
--TODO: apply upgrade
end)
function Card:remove_skill(skill_idx)
self.skills[skill_idx] = nil
end
function Card:refresh()
local orig_skills = id_to_canonical_card[self.id].skills
for i=1,3 do
self.skills[i] = orig_skills[i]
end
end
function Card:remove_skill_until_refresh(skill_idx)
self.skills[skill_idx] = 1076
end
function Card:first_empty_skill_slot()
for i=1,3 do
if not self.skills[i] then
return i
end
end
end
function Card:first_skill_idx()
for i=1,3 do
if self.skills[i] then
return i
end
end
end
function Card:gain_skill(skill_id)
local slot = self:first_empty_skill_slot()
if slot then
self.skills[slot] = skill_id
end
end
function Card:squished_skills()
local t = {}
local skills = self.skills or {}
for i=1,3 do
t[#t+1] = skills[i]
end
return t
end
function Card:reset()
for k,v in pairs(id_to_canonical_card[self.id]) do
self[k] = deepcpy(v)
end
self.active = true
--TODO: apply upgrade
return self
end
local grave_mt = {__newindex=function(table, key, value)
if value then
value:reset()
end
rawset(table, key, value)
end}
Player = class(function(self, side, deck)
deck = shallowcpy(deck)
if not deck then
deck = {}
for i=1,30 do
deck[i] = 300019
end
deck[31] = 100089
end
assert(side == "left" or side == "right")
self.side = side
map_inplace(function(x) if type(x) == "number" then return x end return x.id end, deck)
table.sort(deck, function(a,b) return a>b end)
self.character = Card(deck[#deck])
deck[#deck] = nil
self.deck = map(Card, deck)
shuffle(self.deck)
self.hand = {}
self.field = {}
self.field[0] = self.character
self.grave = {}
setmetatable(self.grave, grave_mt)
self.exile = {}
self.shuffles = 2
self.name = self.character.name
self.animation = {}
self.buff_animation = {}
end)
function Player:check_hand()
for i=1,4 do
if self.hand[i+1] and not self.hand[i] then
error("hand is wrong")
end
end
if #self.hand > 5 then
error("The #hand is too damn high.")
end
local ndeck = 0
local ngrave = 0
for _,__ in pairs(self.deck) do
ndeck = ndeck + 1
end
for _,__ in pairs(self.grave) do
ngrave = ngrave + 1
end
local str1 = "ndeck = "..ndeck.." #deck = "..#self.deck
local str2 = "ngrave = "..ngrave.." #grave = "..#self.grave
assert(ndeck == #self.deck, str1)
assert(ngrave == #self.grave, str2)
local unique_things = {}
for _,player in ipairs({self, self.opponent}) do
for _,zone in ipairs({"deck", "field", "hand", "grave", "exile"}) do
for _,card in ipairs(player[zone]) do
if unique_things[card] then
error("no card aliasing pls")
end
unique_things[card] = true
if card.skills then
if unique_things[card.skills] then
error("no skill aliasing pls")
end
unique_things[card] = true
end
end
end
for _,card in ipairs(player.grave) do
if not card.active then
error("messed up grave card active "..card.id..tostring(card.active))
end
end
end
end
function Player:untap_phase()
for i=1,5 do
if self.field[i] then
self.field[i].active = true
end
end
end
function Player:upkeep_phase()
for idx=0,5 do
local card = self.field[idx]
if card and card.skills then
if pred.spell(card) then
--print("Something is terribly wrong, a spell has a skill")
error("asscom.de")
end
for skill_idx = 1,3 do
local skill_id = card.skills[skill_idx]
if skill_id and skill_id_to_type[skill_id] == "start" and
self.field[idx] == card then
print("About to run skill func for id "..skill_id)
self:check_hand()
self.opponent:check_hand()
card.trigger = true
wait(50)
card.trigger = nil
self.game:send_trigger(self.player_index, idx, "start")
local flicker_my_deck
local flicker_opp_deck
local my_deck = {}
local opp_deck = {}
if GO_HARD then
flicker_my_deck = (math.random(10) == 1)
flicker_opp_deck = (math.random(10) == 1)
if flicker_my_deck then
my_deck, self.deck = self.deck, my_deck
end
if flicker_opp_deck then
opp_deck, self.opponent.deck = self.opponent.deck, opp_deck
end
end
if type(skill_id) == "number" and skill_id >= 100000 then
characters_func[skill_id](self, self.opponent, card)
else
skill_func[skill_id](self, idx, card, skill_idx)
end
if GO_HARD then
if flicker_my_deck then
my_deck, self.deck = self.deck, my_deck
end
if flicker_opp_deck then
opp_deck, self.opponent.deck = self.opponent.deck, opp_deck
end
end
if BUFF_COUNTER and BUFF_COUNTER ~= 0 then
error("unresolved buff in skill " .. skill_id)
end
self.game:snapshot(nil, nil, true)
self:check_hand()
self.opponent:check_hand()
end
end
end
end
end
function Player:draw_a_card()
self.hand[#self.hand + 1] = self.deck[#self.deck]
self.hand[#self.hand].active = true
self.deck[#self.deck] = nil
end
function Player:draw_from_bottom_deck()
self.hand[#self.hand + 1] = table.remove(self.deck, 1)
end
function Player:draw_phase()
if #self.hand == 5 then
self:hand_to_grave(1)
end
if #self.deck == 0 then
self.lose = true
--error("I lost the game :(")
end
for i=1,math.min(5-#self.hand, #self.deck) do
self:draw_a_card()
end
end
function Player:to_bottom_deck(card)
table.insert(self.deck, 1, card)
end
function Player:to_top_deck(card)
self.deck[#self.deck + 1] = card
end
function Player:deck_to_bottom_deck(idx)
table.insert(self.deck, 1, (table.remove(self.deck, idx)))
end
function Player:deck_to_top_deck(idx)
table.insert(self.deck, (table.remove(self.deck, idx)))
end
function Player:attempt_shuffle()
if self.shuffles > 0 then
self.shuffles = self.shuffles - 1
local n_cards = #self.hand
for i=1,n_cards do
self:to_bottom_deck(self.hand[i])
self.hand[i] = nil
end
for i=1,n_cards do
self:draw_a_card()
end
self.game:send_shuffle(self.player_index)
self.game:snapshot()
end
end
function Player:grave_to_exile(n)
self.exile[#self.exile+1]=table.remove(self.grave,n)
end
function Player:grave_to_bottom_deck(n)
self:to_bottom_deck(table.remove(self.grave,n))
end
function Player:grave_to_top_deck(n)
self:to_top_deck(table.remove(self.grave,n))
end
function Player:grave_to_field(n)
local slot = self:first_empty_field_slot()
self.field[slot]=table.remove(self.grave,n)
self.field[slot].active = true
end
function Player:field_to_exile(n)
self.exile[#self.exile+1] = self.field[n]
self.field[n] = nil
end
function Player:hand_to_bottom_deck(n)
self:to_bottom_deck(table.remove(self.hand, n))
end
function Player:hand_to_top_deck(n)
self:to_top_deck(table.remove(self.hand, n))
end
function Player:remove_from_hand(n)
local ret = self.hand[n]
for i=n,5 do
self.hand[i] = self.hand[i+1]
end
return ret
end
-- discards the card at index n
function Player:hand_to_grave(n)
self.grave[#self.grave + 1] = self.hand[n]
for i=n,5 do
self.hand[i] = self.hand[i+1]
end
end
function Player:field_to_bottom_deck(n)
self:to_bottom_deck(self.field[n])
self.field[n] = nil
end
function Player:field_to_top_deck(n)
self:to_top_deck(self.field[n])
self.field[n] = nil
end
function Player:deck_to_hand(n)
self.hand[#self.hand + 1] = table.remove(self.deck, n)
end
function Player:deck_to_grave(n)
self.grave[#self.grave + 1] = table.remove(self.deck, n)
end
function Player:deck_to_exile(n)
table.insert(self.exile, table.remove(self.deck, n))
--self.exile[#self.exile+1]=table.remove(self.deck,n)
end
function Player:to_grave(card)
self.grave[#self.grave + 1] = card
end
function Player:to_hand(card)
if not self.hand[5] then
self.hand[#self.hand + 1] = card
end
end
function Player:mill(n)
for i=1,n do
self.grave[#self.grave + 1] = self.deck[#self.deck]
self.deck[#self.deck] = nil
end
end
function Player:deck_to_field(n, slot)
if not slot then
slot = self:first_empty_field_slot()
end
local card = table.remove(self.deck, n)
self.field[slot] = card
card.active = true
assert(self.field[slot] == card, "deck_to_field()")
end
function Player:hand_to_exile(n)
local card = table.remove(self.hand, n)
end
function Player:hand_to_field(n)
local card = table.remove(self.hand, n)
self.field[self:first_empty_field_slot()] = card
card.active = true
end
function Player:field_to_hand(n)
local card = self.field[n]
self.field[n] = nil
table.insert(self.hand, card)
--self.hand[#self.hand + 1] = card
end
function Player:has_follower()
return #self:field_idxs_with_preds(pred.follower) ~= 0
end
function Player:field_to_grave(n)
self.grave[#self.grave + 1] = self.field[n]
self.field[n] = nil
end
function Player:destroy_deck(n)
if self.deck[n].type == "follower" then
self.character.life = self.character.life - self.deck[n].size
end
self:deck_to_grave(n)
end
function Player:destroy_hand(n)
if self.hand[n].type == "follower" then
self.character.life = self.character.life - self.hand[n].size
end
self:hand_to_grave(n)
end
function Player:destroy(n)
if self.field[n].type == "follower" then
self.character.life = self.character.life - self.field[n].size
end
self:field_to_grave(n)
end
function Player:field_size()
local ret = 0
for i=1,5 do
if self.field[i] then
ret = ret + self.field[i].size
end
end
return ret
end
function Player:ncards_in_field()
local ret = 0
for i=1,5 do
if self.field[i] then
ret = ret + 1
end
end
return ret
end
function Player:grave_idxs_with_preds(...)
local preds = {...}
if type(preds[1]) == "table" then
preds = preds[1]
end
local ret = {}
for i=#self.grave,1,-1 do
local incl = true
for j=1,#preds do
if incl and GO_HARD then
assert(type(preds[j](self.grave[i])) == "boolean")
end
incl = incl and preds[j](self.grave[i])
end
if incl then
ret[#ret+1] = i
end
end
return ret
end
function Player:hand_idxs_with_preds(...)
local preds = {...}
if type(preds[1]) == "table" then
preds = preds[1]
end
local ret = {}
for i=1,5 do
if self.hand[i] then
local incl = true
for j=1,#preds do
if incl and GO_HARD then
assert(type(preds[j](self.hand[i])) == "boolean")
end
incl = incl and preds[j](self.hand[i])
end
if incl then
ret[#ret+1] = i
end
end
end
return ret
end
function Player:field_idxs_with_preds(...)
local preds = {...}
if type(preds[1]) == "table" then
preds = preds[1]
end
local ret = {}
for i=1,5 do
if self.field[i] then
local incl = true
for j=1,#preds do
if incl and GO_HARD then
assert(type(preds[j](self.field[i])) == "boolean")
end
incl = incl and preds[j](self.field[i])
end
if incl then
ret[#ret+1] = i
end
end
end
return ret
end
function Player:deck_idxs_with_preds(...)
local preds = {...}
if type(preds[1]) == "table" then
preds = preds[1]
end
local ret = {}
for i=#self.deck,1,-1 do
if self.deck[i] then
local incl = true
for j=1,#preds do
if incl and GO_HARD then
assert(type(preds[j](self.deck[i])) == "boolean")
end
incl = incl and preds[j](self.deck[i])
end
if incl then
ret[#ret+1] = i
end
end
end
return ret
end
function Player:deck_idxs_with_least_and_preds(func, ...)
local idxs = self:deck_idxs_with_preds(...)
local best = 99999
local ret = {}
for i=1,#idxs do
local card = self.deck[idxs[i]]
local score = func(card)
if score < best then
ret = {idxs[i]}
best = score
elseif score == best then
ret[#ret+1] = idxs[i]
end
end
return ret
end
function Player:deck_idxs_with_most_and_preds(func, ...)
return self:deck_idxs_with_least_and_preds(function(...)return -func(...) end, ...)
end
function Player:field_idxs_with_least_and_preds(func, ...)
local idxs = self:field_idxs_with_preds(...)
local best = 99999
local ret = {}
for i=1,#idxs do
local card = self.field[idxs[i]]
local score = func(card)
if score < best then
ret = {idxs[i]}
best = score
elseif score == best then
ret[#ret+1] = idxs[i]
end
end
return ret
end
function Player:field_idxs_with_most_and_preds(func, ...)
return self:field_idxs_with_least_and_preds(function(...)return -func(...) end, ...)
end
function Player:hand_idxs_with_least_and_preds(func, ...)
local idxs = self:hand_idxs_with_preds(...)
local best = 99999
local ret = {}
for i=1,#idxs do
local card = self.hand[idxs[i]]
local score = func(card)
if score < best then
ret = {idxs[i]}
best = score
elseif score == best then
ret[#ret+1] = idxs[i]
end
end
return ret
end
function Player:hand_idxs_with_most_and_preds(func, ...)
return self:hand_idxs_with_least_and_preds(function(...)return -func(...) end, ...)
end
function Player:nth_deck_idx(n)
return #self.deck - n + 1
end
function Player:empty_hand_slots()
local t = {}
for i=1,5 do
if not self.hand[i] then
t[#t+1] = i
end
end
return t
end
function Player:empty_field_slots()
local t = {}
for i=1,5 do
if not self.field[i] then
t[#t+1] = i
end
end
return t
end
function Player:first_empty_field_slot()
for i=1,5 do
if not self.field[i] then return i end
end
return nil
end
function Player:last_empty_field_slot()
for i=5,1,-1 do
if not self.field[i] then return i end
end
return nil
end
function Player:first_empty_hand_slot()
for i=1,5 do
if not self.hand[i] then return i end
end
return nil
end
function Player:grave_idxs_with_least_and_preds(func, preds)
local idxs = self:grave_idxs_with_preds(preds)
local best = 99999
local ret = {}
for i=1,#idxs do
local card = self.grave[idxs[i]]
local score = func(card)
if score < best then
ret = {idxs[i]}
best = score
elseif score == best then
ret[#ret+1] = idxs[i]
end
end
return ret
end
function Player:grave_idxs_with_most_and_preds(func, preds)
return self:grave_idxs_with_least_and_preds(function(...)return -func(...) end, preds)
end
function Player:field_cards_with_preds(...)
return map(function(h) return self.field[h] end, self:field_idxs_with_preds(...))
end
function Player:grave_cards_with_preds(...)
return map(function(h) return self.grave[h] end, self:grave(...))
end
function Player:deck_cards_with_preds(...)
return map(function(h) return self.deck[h] end, self:deck_idxs_with_preds(...))
end
function Player:field_buff_n_random_cards_with_preds(n, b, ...)
local idxs = shuffle(self:field_idxs_with_preds(...))
local buff = OnePlayerBuff(self)
for i = 1, min(n, #idxs) do
buff[idxs[i]] = b
end
buff:apply()
end
function Player:field_buff_n_random_followers_with_preds(n, b, ...)
self:field_buff_n_random_cards_with_preds(n, b, pred.follower, ...)
end
function Player:squish_hand()
local newhand = {}
for i=1,5 do
newhand[#newhand+1] = self.hand[i]
end
for i=1,5 do
self.hand[i] = newhand[i]
end
end
function Player:has_active_cards()
for i=1,5 do
if self.field[i] and self.field[i].active then
return true
end
end
return false
end
function Player:can_play_card(n)
return self.hand[n] and (self:field_size() + self.hand[n].size <= 10) and
self:first_empty_field_slot()
end
function Player:play_card(n)
self.hand[n].active = true
self.field[self:first_empty_field_slot()] = self.hand[n]
for i=n,5 do
self.hand[i] = self.hand[i+1]
end
self.game:snapshot()
end
function Player:ai_act()
if self.character.id == 110181 then
local stuff_to_play = {200178, 300202, 300138, 200030}
for _, id in pairs(stuff_to_play) do
for i=#self.hand,1,-1 do
if self.hand[i].id == id and self:can_play_card(i) then
self.hand[i].hidden = true
self:play_card(i)
end
end
end
end
for i=1,3 do
if #self.hand > 0 then
local idx = math.random(#self.hand)
if self:can_play_card(idx) then
self.hand[idx].hidden = true
self:play_card(idx)
end
end
end
end
function Player:user_act()
self.game.act_buttons = true
local end_time = love.timer.getTime() + 30
self.game.ready = false
while not self.game.ready do
self.game.time_remaining = math.ceil(end_time-love.timer.getTime())
if self.game.time_remaining <= 0 then
self.game.ready = true
end
wait()
end
for i=1,5 do
if self.opponent.field[i] then
self.opponent.field[i].hidden = false
end
end
self.game.act_buttons = false
end
function Player:get_follower_idxs()
local ret = {}
for i=1,5 do
if self.field[i] and self.field[i].type == "follower" then
ret[#ret+1] = i
end
end
return ret
end
function Player:get_atk_target()
local followers = self:get_follower_idxs()
--print(unpack(followers))
if #followers > 0 then
return uniformly(followers)
end
return 0
end
function Player:follower_combat_round(idx, target_idx)
local card = self.field[idx]
local target_card = self.opponent.field[target_idx]
self.game.attacker = {self, idx}
self.game.defender = {self.opponent, target_idx}
if target_card.type == "follower" then
for i=1,2 do
if i==2 then
self.game.attacker, self.game.defender = self.game.defender, self.game.attacker
end
local attack_player, attack_idx = unpack(self.game.attacker)
local defend_player, defend_idx = unpack(self.game.defender)
local attacker = attack_player.field[attack_idx]
local defender = defend_player.field[defend_idx]
for skill_idx=1,3 do
if attacker and defender and defender.type == "follower" then
local skill_id = attacker.skills[skill_idx]
if attack_player.field[attack_idx] == attacker and
skill_id and skill_id_to_type[skill_id] == "attack" then
print("About to run skill func for id "..skill_id)
local other_card = defend_player.field[defend_idx]
self:check_hand()
self.opponent:check_hand()
attacker.trigger = true
wait(50)
attacker.trigger = nil
self.game:send_trigger(attack_player.player_index, attack_idx, "attack")
local flicker_follower = GO_HARD and (math.random(20) == 1)
if flicker_follower then
flicker_follower = defend_player.field[defend_idx]
other_card, defend_player.field[defend_idx] = nil, nil
end
skill_func[skill_id](attack_player, attack_idx, attacker, skill_idx,
defend_idx, other_card)
if BUFF_COUNTER and BUFF_COUNTER ~= 0 then
error("oh no " .. skill_id)
end
if flicker_follower then
defend_player.field[defend_idx] = flicker_follower
end
self.game:snapshot()
self:check_hand()
self.opponent:check_hand()
self.game:clean_dead_followers()
self.game:snapshot()
end
attacker = attack_player.field[attack_idx]
defender = defend_player.field[defend_idx]
end
end
self.game:clean_dead_followers()
self.game:snapshot()
if self.game.combat_round_interrupted then
--print("bail that shouldn't happen 1")
return
end
for skill_idx=1,3 do
if attacker and defender and defender.type == "follower" then
local skill_id = defender.skills[skill_idx]
if defend_player.field[defend_idx] == defender and
skill_id and skill_id_to_type[skill_id] == "defend" then
print("About to run skill func for id "..skill_id)
local other_card = attack_player.field[attack_idx]
self:check_hand()
self.opponent:check_hand()
defender.trigger = true
wait(50)
defender.trigger = nil
self.game:send_trigger(defend_player.player_index, defend_idx, "defend")
local flicker_follower = GO_HARD and (math.random(20) == 1)
if flicker_follower then
flicker_follower = attack_player.field[attack_idx]
other_card, attack_player.field[attack_idx] = nil, nil
end
skill_func[skill_id](defend_player, defend_idx, defender, skill_idx,
attack_idx, other_card)
if BUFF_COUNTER and BUFF_COUNTER ~= 0 then
error("unresolved buff found in skill " .. skill_id)
end
if flicker_follower then
attack_player.field[attack_idx] = flicker_follower
end
self.game:snapshot()
self:check_hand()
self.opponent:check_hand()
self.game:clean_dead_followers()
self.game:snapshot()
end
attacker = attack_player.field[attack_idx]
defender = defend_player.field[defend_idx]
end
end
self.game:clean_dead_followers()
self.game:snapshot()
if self.game.combat_round_interrupted then
--print("bail that shouldn't happen 2")
return
end
self.game:attack_animation()
self.game:defend_animation()
local damage = math.max(0,attacker.atk-defender.def)
defender.sta = defender.sta - damage
self.game:clean_dead_followers()
local atk_msg = {player=attack_player.player_index,
atk_slot=attack_idx, def_slot=defend_idx, damage=damage}
self.game:snapshot(nil, atk_msg)
if self.game.combat_round_interrupted then --print("bail on combat damage")
return
end
end
else
local attack_player, attack_idx = unpack(self.game.attacker)
self.game:attack_animation()
self.game:defend_animation()
target_card.life = target_card.life - card.size
local atk_msg = {player=attack_player.player_index,
atk_slot=attack_idx, def_slot=0, damage=card.size}
self.game:snapshot(nil, atk_msg)
end
end
function Player:combat_round()
--print("NO INTERRUPTION")
self.game.active_player = self.player_index
self.game.combat_round_interrupted = false
local active = {spell={}, follower={}}
for i=1,5 do
local card = self.field[i]
if card and card.active then
table.insert(active[card.type],i)
end
end
if #active.spell>0 then
active = active.spell
else
active = active.follower
end
local idx = uniformly(active)
local card = self.field[idx]
if card.type == "follower" then
local target_idx = self.opponent:get_atk_target()
--print("Got attack target! "..target_idx)
self:follower_combat_round(idx, target_idx)
for i=1,5 do
if self.field[i] == card then
card.active = false
end
end
self.game:snapshot(nil, nil, true)
else
self.send_spell_to_grave = true
print("About to run spell func for id "..card.id)
self:check_hand()
self.opponent:check_hand()
card.trigger = true
wait(50)
card.trigger = nil
self.game:send_trigger(self.player_index, idx, "spell")
spell_func[card.id](self, self.opponent, idx, card)
if BUFF_COUNTER and BUFF_COUNTER ~= 0 then
error("oh no")
end
self.game:snapshot()
self:check_hand()
self.opponent:check_hand()
--print("Just ran spell func for id "..card.id)
local spell_vanish = false
if self.send_spell_to_grave and self.field[idx] == card then
self:field_to_grave(idx)
spell_vanish = true
end
self.game:snapshot(nil,nil,true)
if spell_vanish then
self.game:send_trigger(self.player_index, idx, "vanish")
end
end
end
function Player:is_npc()
if self.character.id > 109999 then
return true
end
return false
end
Game = class(function(self, ld, rd, client, active_character)
self.P1 = Player("left", ld)
self.P2 = Player("right", rd)
self.P1.game = self
self.P2.game = self
self.P1.opponent = self.P2
self.P2.opponent = self.P1
self.turn = 0
self.time_remaining = 0
self.hover_card = Card(active_character or 200099)
if client then
self.client = true
self.P1.client = true
self.P2.client = true
self.coin_flip = false
end
end)
function Game:update()
if self.end_time and self.act_buttons then
self.time_remaining = math.ceil(self.end_time-love.timer.getTime())
end
end
function Game:attack_animation()
self.print_attack_info = true
wait(50)
self.print_attack_info = false