forked from Mogara/LuaSkillsForQSGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgs_ex.lua
633 lines (513 loc) · 18.9 KB
/
sgs_ex.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
-- this script file defines all functions written by Lua
-- trigger skills
function sgs.CreateTriggerSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.on_trigger) == "function")
if spec.frequency then assert(type(spec.frequency) == "number") end
if spec.limit_mark then assert(type(spec.limit_mark) == "string") end
local frequency = spec.frequency or sgs.Skill_NotFrequent
local limit_mark = spec.limit_mark or ""
local skill = sgs.LuaTriggerSkill(spec.name, frequency, limit_mark)
if type(spec.guhuo_type) == "string" and spec.guhuo_type ~= "" then skill:setGuhuoDialog(guhuo_type) end
if type(spec.events) == "number" then
skill:addEvent(spec.events)
elseif type(spec.events) == "table" then
for _, event in ipairs(spec.events) do
skill:addEvent(event)
end
end
if type(spec.global) == "boolean" then skill:setGlobal(spec.global) end
skill.on_trigger = spec.on_trigger
if spec.can_trigger then
skill.can_trigger = spec.can_trigger
end
if spec.view_as_skill then
skill:setViewAsSkill(spec.view_as_skill)
end
if type(spec.priority) == "number" then
skill.priority = spec.priority
elseif type(spec.priority) == "table" then
for triggerEvent, priority in pairs(spec.priority) do
skill:insertPriorityTable(triggerEvent, priority)
end
end
if type(dynamic_frequency) == "function" then
skill.dynamic_frequency = spec.dynamic_frequency
end
return skill
end
function sgs.CreateProhibitSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.is_prohibited) == "function")
local skill = sgs.LuaProhibitSkill(spec.name)
skill.is_prohibited = spec.is_prohibited
return skill
end
function sgs.CreateFilterSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.view_filter) == "function")
assert(type(spec.view_as) == "function")
local skill = sgs.LuaFilterSkill(spec.name)
skill.view_filter = spec.view_filter
skill.view_as = spec.view_as
return skill
end
function sgs.CreateDistanceSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.correct_func) == "function")
local skill = sgs.LuaDistanceSkill(spec.name)
skill.correct_func = spec.correct_func
return skill
end
function sgs.CreateMaxCardsSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.extra_func) == "function" or type(spec.fixed_func) == "function")
local skill = sgs.LuaMaxCardsSkill(spec.name)
if spec.extra_func then
skill.extra_func = spec.extra_func
else
skill.fixed_func = spec.fixed_func
end
return skill
end
function sgs.CreateTargetModSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.residue_func) == "function" or type(spec.distance_limit_func) == "function" or type(spec.extra_target_func) == "function")
if spec.pattern then assert(type(spec.pattern) == "string") end
local skill = sgs.LuaTargetModSkill(spec.name, spec.pattern or "Slash")
if spec.residue_func then
skill.residue_func = spec.residue_func
end
if spec.distance_limit_func then
skill.distance_limit_func = spec.distance_limit_func
end
if spec.extra_target_func then
skill.extra_target_func = spec.extra_target_func
end
return skill
end
function sgs.CreateInvaliditySkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.skill_valid) == "function")
local skill = sgs.LuaInvaliditySkill(spec.name)
skill.skill_valid = spec.skill_valid
return skill
end
function sgs.CreateAttackRangeSkill(spec)
assert(type(spec.name) == "string")
assert(type(spec.extra_func) == "function" or type(spec.fixed.func) == "function")
local skill = sgs.LuaAttackRangeSkill(spec.name)
if spec.extra_func then
skill.extra_func = spec.extra_func or 0
end
if spec.fixed_func then
skill.fixed_func = spec.fixed_func or -1
end
return skill
end
function sgs.CreateMasochismSkill(spec)
assert(type(spec.on_damaged) == "function")
spec.events = sgs.Damaged
function spec.on_trigger(skill, event, player, data)
local damage = data:toDamage()
spec.on_damaged(skill, player, damage)
return false
end
return sgs.CreateTriggerSkill(spec)
end
function sgs.CreatePhaseChangeSkill(spec)
assert(type(spec.on_phasechange) == "function")
spec.events = sgs.EventPhaseStart
function spec.on_trigger(skill, event, player, data)
return spec.on_phasechange(skill, player)
end
return sgs.CreateTriggerSkill(spec)
end
function sgs.CreateDrawCardsSkill(spec)
assert(type(spec.draw_num_func) == "function")
if not spec.is_initial then spec.events = sgs.DrawNCards else spec.events = sgs.DrawInitialCards end
function spec.on_trigger(skill, event, player, data)
local n = data:toInt()
local nn = spec.draw_num_func(skill, player, n)
data:setValue(nn)
return false
end
return sgs.CreateTriggerSkill(spec)
end
function sgs.CreateGameStartSkill(spec)
assert(type(spec.on_gamestart) == "function")
spec.events = sgs.GameStart
function spec.on_trigger(skill, event, player, data)
spec.on_gamestart(skill, player)
return false
end
return sgs.CreateTriggerSkill(spec)
end
--------------------------------------------
-- skill cards
function sgs.CreateSkillCard(spec)
assert(spec.name)
if spec.skill_name then assert(type(spec.skill_name) == "string") end
local card = sgs.LuaSkillCard(spec.name, spec.skill_name)
if type(spec.target_fixed) == "boolean" then
card:setTargetFixed(spec.target_fixed)
end
if type(spec.will_throw) == "boolean" then
card:setWillThrow(spec.will_throw)
end
if type(spec.can_recast) == "boolean" then
card:setCanRecast(spec.can_recast)
end
if type(spec.handling_method) == "number" then
card:setHandlingMethod(spec.handling_method)
end
if type(spec.mute) == "boolean" then
card:setMute(spec.mute)
end
if type(spec.filter) == "function" then
function card:filter(...)
local result,vote = spec.filter(self,...)
if type(result) == "boolean" and type(vote) == "number" then
return result,vote
elseif type(result) == "boolean" and vote == nil then
if result then vote = 1 else vote = 0 end
return result,vote
elseif type(result) == "number" then
return result > 0,result
else
return false,0
end
end
end
card.feasible = spec.feasible
card.about_to_use = spec.about_to_use
card.on_use = spec.on_use
card.on_effect = spec.on_effect
card.on_validate = spec.on_validate
card.on_validate_in_response = spec.on_validate_in_response
return card
end
function sgs.CreateBasicCard(spec)
assert(type(spec.name) == "string" or type(spec.class_name) == "string")
if not spec.name then spec.name = spec.class_name
elseif not spec.class_name then spec.class_name = spec.name end
if spec.suit then assert(type(spec.suit) == "number") end
if spec.number then assert(type(spec.number) == "number") end
if spec.subtype then assert(type(spec.subtype) == "string") end
local card = sgs.LuaBasicCard(spec.suit or sgs.Card_NoSuit, spec.number or 0, spec.name, spec.class_name, spec.subtype or "BasicCard")
if type(spec.target_fixed) == "boolean" then
card:setTargetFixed(spec.target_fixed)
end
if type(spec.can_recast) == "boolean" then
card:setCanRecast(spec.can_recast)
end
card.filter = spec.filter
card.feasible = spec.feasible
card.available = spec.available
card.about_to_use = spec.about_to_use
card.on_use = spec.on_use
card.on_effect = spec.on_effect
return card
end
-- ============================================
-- default functions for Trick cards
function isAvailable_AOE(self, player)
local canUse = false
local players = player:getSiblings()
for _, p in sgs.qlist(players) do
if p:isDead() or player:isProhibited(p, self) then continue end
canUse = true
break
end
return canUse and self:cardIsAvailable(player)
end
function onUse_AOE(self, room, card_use)
local source = card_use.from
local targets = sgs.SPlayerList()
local other_players = room:getOtherPlayers(source)
for _, player in sgs.qlist(other_players) do
local skill = room:isProhibited(source, player, self)
if skill ~= nil then
local log_message = sgs.LogMessage()
log_message.type = "#SkillAvoid"
log_message.from = player
log_message.arg = skill:objectName()
log_message.arg2 = self:objectName()
room:broadcastSkillInvoke(skill:objectName())
else
targets:append(player)
end
end
local use = card_use
use.to = targets
self:cardOnUse(room, use)
end
function isAvailable_GlobalEffect(self, player)
local canUse = false
local players = player:getSiblings()
players:append(player)
for _, p in sgs.qlist(players) do
if p:isDead() or player:isProhibited(p, self) then continue end
canUse = true
break
end
return canUse and self:cardIsAvailable(player)
end
function onUse_GlobalEffect(self, room, card_use)
local source = card_use.from
local targets = sgs.SPlayerList()
local all_players = room:getAllPlayers()
for _, player in sgs.qlist(all_players) do
local skill = room:isProhibited(source, player, self)
if skill ~= nil then
local log_message = sgs.LogMessage()
log_message.type = "#SkillAvoid"
log_message.from = player
log_message.arg = skill:objectName()
log_message.arg2 = self:objectName()
room:broadcastSkillInvoke(skill:objectName())
else
targets:append(player)
end
end
local use = card_use
use.to = targets
self:cardOnUse(room, use)
end
function onUse_DelayedTrick(self, room, card_use)
local use = card_use
local wrapped = sgs.Sanguosha:getWrappedCard(self:getEffectiveId())
use.card = wrapped
local data = sgs.QVariant()
data:setValue(use)
local thread = room:getThread()
thread:trigger(sgs.PreCardUsed, room, use.from, data)
use = data:toCardUse()
local logm = sgs.LogMessage()
logm.from = use.from
logm.to = use.to
logm.type = "#UseCard"
logm.card_str = self:toString()
room:sendLog(logm)
local reason = sgs.CardMoveReason(sgs.CardMoveReason_S_REASON_USE, use.from:objectName(), use.to:first():objectName(), self:getSkillName(), "")
room:moveCardTo(self, use.from, use.to:first(), sgs.Player_PlaceDelayedTrick, reason, true)
thread:trigger(sgs.CardUsed, room, use.from, data)
use = data:toCardUse()
thread:trigger(sgs.CardFinished, room, use.from, data)
end
function use_DelayedTrick(self, room, source, targets)
if #targets == 0 then
local reason = sgs.CardMoveReason(sgs.CardMoveReason_S_REASON_USE, source:objectName(), "", self:getSkillName(), "")
room:moveCardTo(self, room:getCardOwner(self:getEffectiveId()), nil, sgs.Player_DiscardPile, reason, true)
end
end
function onNullified_DelayedTrick_movable(self, target)
local room = target:getRoom()
local thread = room:getThread()
local players = room:getOtherPlayers(target)
players:append(target)
local p = nil
for _, player in sgs.qlist(players) do
if player:containsTrick(self:objectName()) then continue end
local skill = room:isProhibited(target, player, self)
if skill then
local logm = sgs.LogMessage()
logm.type = "#SkillAvoid"
logm.from = player
logm.arg = skill:objectName()
logm.arg2 = self:objectName()
room:sendLog(logm)
room:broadcastSkillInvoke(skill:objectName())
continue
end
local reason = sgs.CardMoveReason(sgs.CardMoveReason_S_REASON_TRANSFER, target:objectName(), "", self:getSkillName(), "")
room:moveCardTo(self, target, player, sgs.Player_PlaceDelayedTrick, reason, true)
if target:objectName() == player:objectName() then break end
local use = sgs.CardUseStruct()
use.from = nil
use.to:append(player)
use.card = self
local data = sgs.QVariant()
data:setValue(use)
thread:trigger(sgs.TargetConfirming, room, player, data)
local new_use = data:toCardUse()
if new_use.to:isEmpty() then
p = player
break
end
for _, ps in sgs.qlist(room:getAllPlayers()) do
thread:trigger(sgs.TargetConfirmed, room, ps, data)
end
break
end
if p then self:on_nullified(p) end
end
function onNullified_DelayedTrick_unmovable(self, target)
local reason = sgs.CardMoveReason(sgs.CardMoveReason_S_REASON_NATURAL_ENTER, target:objectName())
target:getRoom():throwCard(self, reason, nil)
end
-- ============================================
function sgs.CreateTrickCard(spec)
assert(type(spec.name) == "string" or type(spec.class_name) == "string")
if not spec.name then spec.name = spec.class_name
elseif not spec.class_name then spec.class_name = spec.name end
if spec.suit then assert(type(spec.suit) == "number") end
if spec.number then assert(type(spec.number) == "number") end
if spec.subtype then
assert(type(spec.subtype) == "string")
else
local subtype_table = { "TrickCard", "single_target_trick", "delayed_trick", "aoe", "global_effect" }
spec.subtype = subtype_table[(spec.subclass or 0) + 1]
end
local card = sgs.LuaTrickCard(spec.suit or sgs.Card_NoSuit, spec.number or 0, spec.name, spec.class_name, spec.subtype)
if type(spec.target_fixed) == "boolean" then
card:setTargetFixed(spec.target_fixed)
end
if type(spec.can_recast) == "boolean" then
card:setCanRecast(spec.can_recast)
end
if type(spec.subclass) == "number" then
card:setSubClass(spec.subclass)
else
card:setSubClass(sgs.LuaTrickCard_TypeNormal)
end
if spec.subclass then
if spec.subclass == sgs.LuaTrickCard_TypeDelayedTrick then
if not spec.about_to_use then spec.about_to_use = onUse_DelayedTrick end
if not spec.on_use then spec.on_use = use_DelayedTrick end
if not spec.on_nullified then
if spec.movable then spec.on_nullified = onNullified_DelayedTrick_movable
else spec.on_nullified = onNullified_DelayedTrick_unmovable
end
end
elseif spec.subclass == sgs.LuaTrickCard_TypeAOE then
if not spec.available then spec.available = isAvailable_AOE end
if not spec.about_to_use then spec.about_to_use = onUse_AOE end
if not spec.target_fixed then card:setTargetFixed(true) end
elseif spec.subclass == sgs.LuaTrickCard_TypeGlobalEffect then
if not spec.available then spec.available = isAvailable_GlobalEffect end
if not spec.about_to_use then spec.about_to_use = onUse_GlobalEffect end
if not spec.target_fixed then card:setTargetFixed(true) end
end
end
card.filter = spec.filter
card.feasible = spec.feasible
card.available = spec.available
card.is_cancelable = spec.is_cancelable
card.on_nullified = spec.on_nullified
card.about_to_use = spec.about_to_use
card.on_use = spec.on_use
card.on_effect = spec.on_effect
return card
end
function sgs.CreateViewAsSkill(spec)
assert(type(spec.name) == "string")
if spec.response_pattern then assert(type(spec.response_pattern) == "string") end
local response_pattern = spec.response_pattern or ""
local response_or_use = spec.response_or_use or false
if spec.expand_pile then assert(type(spec.expand_pile) == "string") end
local expand_pile = spec.expand_pile or ""
local skill = sgs.LuaViewAsSkill(spec.name, response_pattern, response_or_use, expand_pile)
local n = spec.n or 0
function skill:view_as(cards)
return spec.view_as(self, cards)
end
function skill:view_filter(selected, to_select)
if #selected >= n then return false end
return spec.view_filter(self, selected, to_select)
end
if type(spec.guhuo_type) == "string" and spec.guhuo_type ~= "" then skill:setGuhuoDialog(guhuo_type) end
skill.should_be_visible = spec.should_be_visible
skill.enabled_at_play = spec.enabled_at_play
skill.enabled_at_response = spec.enabled_at_response
skill.enabled_at_nullification = spec.enabled_at_nullification
return skill
end
function sgs.CreateOneCardViewAsSkill(spec)
assert(type(spec.name) == "string")
if spec.response_pattern then assert(type(spec.response_pattern) == "string") end
local response_pattern = spec.response_pattern or ""
local response_or_use = spec.response_or_use or false
if spec.filter_pattern then assert(type(spec.filter_pattern) == "string") end
if spec.expand_pile then assert(type(spec.expand_pile) == "string") end
local expand_pile = spec.expand_pile or ""
local skill = sgs.LuaViewAsSkill(spec.name, response_pattern, response_or_use, expand_pile)
if type(spec.guhuo_type) == "string" and spec.guhuo_type ~= "" then skill:setGuhuoDialog(guhuo_type) end
function skill:view_as(cards)
if #cards ~= 1 then return nil end
return spec.view_as(self, cards[1])
end
function skill:view_filter(selected, to_select)
if #selected >= 1 or to_select:hasFlag("using") then return false end
if spec.view_filter then return spec.view_filter(self, to_select) end
if spec.filter_pattern then
local pat = spec.filter_pattern
if string.endsWith(pat, "!") then
if sgs.Self:isJilei(to_select) then return false end
pat = string.sub(pat, 1, -2)
end
return sgs.Sanguosha:matchExpPattern(pat, sgs.Self, to_select)
end
end
skill.enabled_at_play = spec.enabled_at_play
skill.enabled_at_response = spec.enabled_at_response
skill.enabled_at_nullification = spec.enabled_at_nullification
return skill
end
function sgs.CreateZeroCardViewAsSkill(spec)
assert(type(spec.name) == "string")
if spec.response_pattern then assert(type(spec.response_pattern) == "string") end
local response_pattern = spec.response_pattern or ""
local response_or_use = spec.response_or_use or false
local skill = sgs.LuaViewAsSkill(spec.name, response_pattern, response_or_use, "")
if type(spec.guhuo_type) == "string" and spec.guhuo_type ~= "" then skill:setGuhuoDialog(guhuo_type) end
function skill:view_as(cards)
if #cards > 0 then return nil end
return spec.view_as(self)
end
function skill:view_filter(selected, to_select)
return false
end
skill.enabled_at_play = spec.enabled_at_play
skill.enabled_at_response = spec.enabled_at_response
skill.enabled_at_nullification = spec.enabled_at_nullification
return skill
end
function sgs.CreateEquipCard(spec)
assert(type(spec.location) == "number" and spec.location ~= sgs.EquipCard_DefensiveHorseLocation and spec.location ~= sgs.EquipCard_OffensiveHorseLocation)
assert(type(spec.name) == "string" or type(spec.class_name) == "string")
if not spec.name then spec.name = spec.class_name
elseif not spec.class_name then spec.class_name = spec.name end
if spec.suit then assert(type(spec.suit) == "number") end
if spec.number then assert(type(spec.number) == "number") end
if spec.location == sgs.EquipCard_WeaponLocation then assert(type(spec.range) == "number") end
local card = nil
if spec.location == sgs.EquipCard_WeaponLocation then
card = sgs.LuaWeapon(spec.suit or sgs.Card_NoSuit, spec.number or 0, spec.range, spec.name, spec.class_name)
elseif spec.location == sgs.EquipCard_ArmorLocation then
card = sgs.LuaArmor(spec.suit or sgs.Card_NoSuit, spec.number or 0, spec.name, spec.class_name)
elseif spec.location == sgs.EquipCard_TreasureLocation then
card = sgs.LuaTreasure(spec.suit or sgs.Card_NoSuit, spec.number or 0, spec.name, spec.class_name)
end
assert(card ~= nil)
card.on_install = spec.on_install
card.on_uninstall = spec.on_uninstall
return card
end
function sgs.CreateWeapon(spec)
spec.location = sgs.EquipCard_WeaponLocation
return sgs.CreateEquipCard(spec)
end
function sgs.CreateArmor(spec)
spec.location = sgs.EquipCard_ArmorLocation
return sgs.CreateEquipCard(spec)
end
function sgs.CreateTreasure(spec)
spec.location = sgs.EquipCard_TreasureLocation
return sgs.CreateEquipCard(spec)
end
function sgs.LoadTranslationTable(t)
for key, value in pairs(t) do
sgs.AddTranslationEntry(key, value)
end
end