-
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathTheOrder.lua
More file actions
615 lines (548 loc) · 19.1 KB
/
Copy pathTheOrder.lua
File metadata and controls
615 lines (548 loc) · 19.1 KB
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
-- Credit to @MathIsFun_ for creating TheOrder, which this integration is a modified copy of
-- Patches card creation to not be ante-based and use a single pool for every type/rarity
local cc = create_card
function create_card(_type, area, legendary, _rarity, skip_materialize, soulable, forced_key, key_append)
if MP.should_use_the_order() then
local a = G.GAME.round_resets.ante
G.GAME.round_resets.ante = 0
G.GAME.round_resets.mp_real_ante = a
if _type == "Tarot" or _type == "Planet" or _type == "Spectral" then
if area == G.pack_cards then
key_append = _type .. "_pack"
else
key_append = _type
end
elseif not (_type == "Base" or _type == "Enhanced") then
if key_append == "jud" and G.GAME.modifiers.enable_eternals_in_shop then -- separate judgement rarity queue to avoid jank (and create a little)
_rarity = pseudorandom("order_jud_rarity") -- dumb but should be fine
end
key_append = nil
end
local c = cc(_type, area, legendary, _rarity, skip_materialize, soulable, forced_key, key_append)
G.GAME.round_resets.ante = a
G.GAME.round_resets.mp_real_ante = nil
return c
end
return cc(_type, area, legendary, _rarity, skip_materialize, soulable, forced_key, key_append)
end
-- Patches idol RNG when using the order to sort deck based on count of identical cards instead of default deck order
local original_reset_idol_card = reset_idol_card
function reset_idol_card()
if MP.should_use_the_order() then
G.GAME.current_round.idol_card.rank = "Ace"
G.GAME.current_round.idol_card.suit = "Spades"
-- ----------------------------------------------------------------
-- Step 1: Build count_map keyed by (value, suit)
-- ----------------------------------------------------------------
local count_map = {}
local valid_idol_cards = {}
for _, v in ipairs(G.playing_cards) do
if v.ability.effect ~= "Stone Card" then
local key = v.base.value .. "_" .. v.base.suit
if not count_map[key] then
count_map[key] = {
count = 0,
card = v,
value = v.base.value,
suit = v.base.suit,
}
table.insert(valid_idol_cards, count_map[key])
end
count_map[key].count = count_map[key].count + 1
end
end
if #valid_idol_cards == 0 then return end
-- ----------------------------------------------------------------
-- Step 2: Build rank ordering from SMODS (positional index)
-- ----------------------------------------------------------------
local rank_index = {}
for i, rank_key in ipairs(SMODS.Rank.obj_buffer) do
rank_index[rank_key] = i
end
local suit_index = {}
for i, suit_key in ipairs(SMODS.Suit.obj_buffer) do
suit_index[suit_key] = i
end
-- ----------------------------------------------------------------
-- Step 3: Aggregate per-rank totals (only ranks present in deck)
-- ----------------------------------------------------------------
local rank_totals = {} -- rank_key -> total count across all suits
local distinct_cards = 0 -- number of distinct (rank, suit) entries
for _, entry in ipairs(valid_idol_cards) do
local r = entry.value
rank_totals[r] = (rank_totals[r] or 0) + entry.count
distinct_cards = distinct_cards + 1
end
-- Count of distinct ranks present
local distinct_ranks = 0
for _ in pairs(rank_totals) do
distinct_ranks = distinct_ranks + 1
end
local total_cards = 0
for _, entry in ipairs(valid_idol_cards) do
total_cards = total_cards + entry.count
end
-- ----------------------------------------------------------------
-- Step 4: Compute means, rounded to nearest 0.5
-- (Python: round(x * 2) / 2 — Lua's math.floor with +0.5 trick)
-- ----------------------------------------------------------------
local function round_to_half(x)
return math.floor(x * 2 + 0.5) / 2
end
local function round_to_nearest_05(x)
return math.floor(x * 20 + 0.5) / 20
end
local mean_by_card = round_to_half(total_cards / distinct_cards)
local mean_by_number = round_to_half(total_cards / distinct_ranks)
local raw_mean_by_number = total_cards / distinct_ranks
-- ----------------------------------------------------------------
-- Step 5: Face / low pools and baselines for Generalized score
-- Face = ranks with .face == true in SMODS
-- Low = ranks with nominal <= 5 and nominal >= 2
-- (covers 2,3,4,5 in vanilla; adapts to mods)
-- ----------------------------------------------------------------
local face_pool = 0
local low_pool = 0
local face_ranks_present = 0
local low_ranks_present = 0
for rank_key, total in pairs(rank_totals) do
local rank_obj = SMODS.Ranks[rank_key]
if rank_obj then
if rank_obj.face then
face_pool = face_pool + total
face_ranks_present = face_ranks_present + 1
elseif rank_obj.nominal and rank_obj.nominal >= 2 and rank_obj.nominal <= 5 then
low_pool = low_pool + total
low_ranks_present = low_ranks_present + 1
end
end
end
local face_baseline = round_to_nearest_05(raw_mean_by_number * face_ranks_present)
local low_baseline = round_to_nearest_05(raw_mean_by_number * low_ranks_present)
local W_GEN = 0.05
local GEN_FLOOR = 0.01
-- ----------------------------------------------------------------
-- Step 6: Off Hit per rank (scaled by 0.5)
-- ----------------------------------------------------------------
local off_hit_by_rank = {}
for rank_key, total in pairs(rank_totals) do
off_hit_by_rank[rank_key] = 0.5 * math.max(0.0, total - mean_by_number)
end
-- ----------------------------------------------------------------
-- Step 7: Previous rank (positional wrap: index 1 -> last index)
-- In vanilla obj_buffer: Ace(1) wraps to 2(last), giving
-- the Ace -> King adjacency the Python script intends.
-- ----------------------------------------------------------------
local function previous_rank_key(rank_key)
local idx = rank_index[rank_key]
if not idx then return nil end
if idx == 1 then
-- wrap to last rank in buffer
return SMODS.Rank.obj_buffer[#SMODS.Rank.obj_buffer]
else
return SMODS.Rank.obj_buffer[idx - 1]
end
end
-- ----------------------------------------------------------------
-- Step 8: Generalized score per rank key
-- ----------------------------------------------------------------
local function generalized_for_rank(rank_key)
local rank_obj = SMODS.Ranks[rank_key]
if not rank_obj then return 0.0 end
if rank_obj.face then
return math.max(GEN_FLOOR, W_GEN * math.max(0.0, face_pool - face_baseline))
elseif rank_obj.nominal and rank_obj.nominal >= 2 and rank_obj.nominal <= 5 then
return math.max(GEN_FLOOR, W_GEN * math.max(0.0, low_pool - low_baseline))
end
return 0.0
end
-- ----------------------------------------------------------------
-- Step 9: Compute total score for each distinct (rank, suit) entry
-- ----------------------------------------------------------------
for _, entry in ipairs(valid_idol_cards) do
local rank_key = entry.value
local suit_key = entry.suit
local card_count = entry.count
-- Main Hit: 2 * max(0, card_count - mean_by_card)
local main_hit = 2.0 * math.max(0.0, card_count - mean_by_card)
-- Off Hit: 0.5 * max(0, rank_total - mean_by_number)
local off_hit = off_hit_by_rank[rank_key] or 0.0
-- Rank Adjacent: 0.25 * off_hit of the previous rank
local prev_rank = previous_rank_key(rank_key)
local rank_adj = 0.25 * (off_hit_by_rank[prev_rank] or 0.0)
-- Suit-Matched Adjacent: 0.33 * max(0, neighbor_count - mean_by_card)
-- neighbor = the previous rank of the same suit
local neighbor_count = 0
if prev_rank then
local neighbor_key = prev_rank .. "_" .. suit_key
if count_map[neighbor_key] then
neighbor_count = count_map[neighbor_key].count
end
end
local suit_matched_adj = 0.33 * math.max(0.0, neighbor_count - mean_by_card)
-- Generalized
local generalized = generalized_for_rank(rank_key)
entry.total_score = main_hit + off_hit + suit_matched_adj + rank_adj + generalized
--[[sendDebugMessage(
string.format(
"(Idol) Score for %s of %s: total=%.4f (main=%.4f off=%.4f suit_adj=%.4f rank_adj=%.4f gen=%.4f)",
rank_key, suit_key,
entry.total_score, main_hit, off_hit, suit_matched_adj, rank_adj, generalized
)
)]]
end
-- ----------------------------------------------------------------
-- Step 10: Sort by score, then weighted random selection by count
-- ----------------------------------------------------------------
table.sort(valid_idol_cards, function(a, b)
if a.total_score ~= b.total_score then return a.total_score > b.total_score end
if suit_index[a.suit] ~= suit_index[b.suit] then return suit_index[a.suit] < suit_index[b.suit] end
return (rank_index[a.value] or 0) < (rank_index[b.value] or 0)
end)
local total_weight = 0
for _, entry in ipairs(valid_idol_cards) do
total_weight = total_weight + entry.count
end
if total_weight <= 0 then return end
local raw_random = pseudorandom("idol" .. G.GAME.round_resets.ante)
local threshold = 0
for _, entry in ipairs(valid_idol_cards) do
threshold = threshold + (entry.count / total_weight)
if raw_random < threshold then
local idol_card = entry.card
--[[sendDebugMessage(
string.format(
"(Idol) Selected %s of %s (score=%.4f, count=%d, total_weight=%d)",
idol_card.base.value, idol_card.base.suit,
entry.total_score, entry.count, total_weight
)
)]]
G.GAME.current_round.idol_card.rank = idol_card.base.value
G.GAME.current_round.idol_card.suit = idol_card.base.suit
G.GAME.current_round.idol_card.id = idol_card.base.id
break
end
end
return
end
return original_reset_idol_card()
end
local original_reset_mail_rank = reset_mail_rank
function reset_mail_rank()
if MP.should_use_the_order() then
G.GAME.current_round.mail_card.rank = "Ace"
local count_map = {}
local total_weight = 0
local value_order = {}
for i, rank in ipairs(SMODS.Rank.obj_buffer) do
value_order[rank] = i
end
local valid_ranks = {}
for _, v in ipairs(G.playing_cards) do
if v.ability.effect ~= "Stone Card" then
local val = v.base.value
if not count_map[val] then
count_map[val] = { count = 0, example_card = v }
table.insert(valid_ranks, { value = val, count = 0, example_card = v })
end
count_map[val].count = count_map[val].count + 1
end
end
-- Failsafe: all stone cards
if #valid_ranks == 0 then return end
-- Sort by count desc, then value asc
table.sort(valid_ranks, function(a, b)
if a.count ~= b.count then return a.count > b.count end
return value_order[a.value] < value_order[b.value]
end)
total_weight = 0
for _, entry in ipairs(valid_ranks) do
total_weight = total_weight + count_map[entry.value].count
end
local raw_random = pseudorandom("mail" .. G.GAME.round_resets.ante)
local threshold = 0
for i, entry in ipairs(valid_ranks) do
local count = count_map[entry.value].count
local weight = (count / total_weight)
threshold = threshold + weight
if raw_random < threshold then
--[[ nobody cares
sendDebugMessage(
"(Mail) Selected card "
.. entry.example_card.base.value
.. " with weight "
.. count
.. " of total "
.. total_weight,
"MULTIPLAYER"
)
]]
G.GAME.current_round.mail_card.rank = entry.example_card.base.value
G.GAME.current_round.mail_card.id = entry.example_card.base.id
break
end
end
return
end
return original_reset_mail_rank()
end
-- Take ownership of standard pack card creation
SMODS.Booster:take_ownership_by_kind("Standard", {
create_card = function(self, card, i)
local s_append = "" -- MP.get_booster_append(card)
local b_append = MP.ante_based() .. s_append
local _edition = poll_edition("standard_edition" .. b_append, 2, true)
local _seal = SMODS.poll_seal({ mod = 10 })
return {
set = (pseudorandom(pseudoseed("stdset" .. b_append)) > 0.6) and "Enhanced" or "Base",
edition = _edition,
seal = _seal,
area = G.pack_cards,
skip_materialize = true,
soulable = true,
key_append = "sta" .. s_append,
front = false,
}
end,
}, true)
-- Patch seal queues
local pollseal = SMODS.poll_seal
function SMODS.poll_seal(args)
if MP.should_use_the_order() then
local a = G.GAME.round_resets.ante
G.GAME.round_resets.ante = 0
G.GAME.round_resets.mp_real_ante = a
local ret = pollseal(args)
G.GAME.round_resets.ante = a
G.GAME.round_resets.mp_real_ante = nil
return ret
end
return pollseal(args)
end
-- Make voucher queue less chaotic
-- I don't like the fact that we have to do this twice
local function get_culled(_pool)
local culled = {}
for i = 1, #_pool, 2 do
local first = _pool[i]
local second = _pool[i + 1]
if second == nil then
-- idk if this ever triggers but just to be safe
culled[#culled + 1] = (first ~= "UNAVAILABLE") and first or "UNAVAILABLE"
elseif first ~= "UNAVAILABLE" and second ~= "UNAVAILABLE" then
-- only true in the case of mods adding t3 vouchers
culled[#culled + 1] = first
culled[#culled + 1] = second
elseif first ~= "UNAVAILABLE" then
culled[#culled + 1] = first
elseif second ~= "UNAVAILABLE" then
culled[#culled + 1] = second
else
culled[#culled + 1] = "UNAVAILABLE"
end
end
return culled
end
local nextvouchers = SMODS.get_next_vouchers
function SMODS.get_next_vouchers(vouchers)
if MP.should_use_the_order() or MP.is_major_league_ruleset() then
vouchers = vouchers or { spawn = {} }
local _pool = get_current_pool("Voucher")
local culled = get_culled(_pool)
for i = #vouchers + 1, math.min(
SMODS.size_of_pool(_pool),
G.GAME.starting_params.vouchers_in_shop + (G.GAME.modifiers.extra_vouchers or 0)
) do
local center = pseudorandom_element(culled, pseudoseed("Voucher0"))
local it = 1
while center == "UNAVAILABLE" or vouchers.spawn[center] do
it = it + 1
center = pseudorandom_element(culled, pseudoseed("Voucher0"))
if it > 1000 then -- fallback
center = pseudorandom_element(culled, pseudoseed("Voucher0"..it))
end
end
vouchers[#vouchers + 1] = center
vouchers.spawn[center] = true
end
return vouchers
end
return nextvouchers(vouchers)
end
local nextvoucherkey = get_next_voucher_key
function get_next_voucher_key(_from_tag)
if MP.should_use_the_order() or MP.is_major_league_ruleset() then
local _pool = get_current_pool("Voucher")
local culled = get_culled(_pool)
local center = pseudorandom_element(culled, pseudoseed("Voucher0"))
local it = 1
while center == "UNAVAILABLE" do
it = it + 1
center = pseudorandom_element(culled, pseudoseed("Voucher0"))
if it > 1000 then -- fallback
center = pseudorandom_element(culled, pseudoseed("Voucher0"..it))
end
end
return center
end
return nextvoucherkey(_from_tag)
end
-- Helper function to make code more readable - deal with ante
function MP.ante_based()
if MP.should_use_the_order() then return 0 end
return G.GAME.round_resets.ante
end
-- Handle round based rng with order (avoid desync with skips)
function MP.order_round_based(ante_based)
if MP.should_use_the_order() then
return G.GAME.round_resets.ante .. (G.GAME.blind.config.blind.key or "") .. (G.GAME.blind_on_deck or "")
end
if ante_based then return MP.ante_based() end
return ""
end
-- Helper function for a sorted hand list to fix pairs() jank
function MP.sorted_hand_list(current_hand)
if not current_hand then current_hand = "NULL" end
local _poker_hands = {}
local done = false
local order = 1
while not done do -- messy selection sort
done = true
for k, v in pairs(G.GAME.hands) do
if v.order == order then
order = order + 1
done = false
if v.visible and k ~= current_hand then _poker_hands[#_poker_hands + 1] = k end
end
end
end
return _poker_hands
end
local stdval = {
centers = { -- these are roughly ordered in terms of current meta, doesn't matter toooo much? but they have to be ordered
c_base = 0,
m_stone = 106,
m_bonus = 107,
m_mult = 108,
m_wild = 109,
m_gold = 110,
m_lucky = 111,
m_steel = 112,
m_glass = 113,
},
seals = {
Gold = 122,
Blue = 131,
Purple = 140,
Red = 149,
},
editions = {
foil = 157,
holo = 192,
polychrome = 227,
},
-- no mod compat, but mods aren't too competitive, it won't matter much
}
local function give_stdval(card) -- give each card a value based on current enhancement/seal/edition
card.mp_stdval = 0 + (stdval.centers[card.config.center_key] or 0)
card.mp_stdval = card.mp_stdval + (stdval.seals[card.seal or "nil"] or 0)
card.mp_stdval = card.mp_stdval + (stdval.editions[card.edition and card.edition.type or "nil"] or 0)
end
local function give_shufflevals(tbl, seed, joker)
local tables = {}
for k, v in pairs(tbl) do
local key = nil
if joker then
key = v.config.center.key
else
give_stdval(v)
key = v.config.center.key == "m_stone" and "Stone" or v.base.suit .. v.base.id
end
tables[key] = tables[key] or {}
tables[key][#tables[key] + 1] = v
end
if seed and type(seed) == "string" then seed = pseudoseed(seed) end
local true_seed = pseudorandom(seed)
for k, v in pairs(tables) do
if joker then
table.sort(v, function(a, b)
return a.sort_id < b.sort_id
end) -- oldest joker (of specified key) first
else
table.sort(v, function(a, b)
return a.mp_stdval > b.mp_stdval
end) -- highest value (of specified suit+rank) first
end
local mega_seed = k .. true_seed
for i, card in ipairs(v) do
G._MP_UNSAVED_PRNG = true
card.mp_shuffleval = pseudorandom(mega_seed)
G._MP_UNSAVED_PRNG = false
end
G.GAME.pseudorandom[mega_seed] = nil -- just avoid flooding the table. we don't need to keep this
end
end
-- Rework shuffle rng to be more similar between players
-- This also affects immolate and other uses of pseudoshuffle
local orig_pseudoshuffle = pseudoshuffle
function pseudoshuffle(list, seed)
if MP.should_use_the_order() then
local is_p_card = true
for k, v in pairs(list) do
if
is_p_card
and not (
type(v) == "table"
and v.ability
and (v.ability.set == "Default" or v.ability.set == "Enhanced")
)
then
is_p_card = false
end
end
if is_p_card then
give_shufflevals(list, seed or math.random())
table.sort(list, function(a, b)
return a.mp_shuffleval > b.mp_shuffleval
end)
return
end
end
return orig_pseudoshuffle(list, seed)
end
-- Make pseudorandom_element selecting a joker/playing card less chaotic
local orig_pseudorandom_element = pseudorandom_element
function pseudorandom_element(_t, seed, args)
if MP.should_use_the_order() then
local is_joker = true
local is_p_card = true
for k, v in pairs(_t) do
if is_joker and not (type(v) == "table" and v.ability and v.ability.set == "Joker") then
is_joker = false
end
if
is_p_card
and not (
type(v) == "table"
and v.ability
and (v.ability.set == "Default" or v.ability.set == "Enhanced")
)
then
is_p_card = false
end
end
if is_joker or is_p_card then
local keys = {}
for k, v in pairs(_t) do
keys[#keys + 1] = { k = k, v = v }
end
give_shufflevals(_t, seed or math.random(), is_joker)
table.sort(keys, function(a, b)
return a.v.mp_shuffleval > b.v.mp_shuffleval
end)
local key = keys[1].k
return _t[key], key
end
end
return orig_pseudorandom_element(_t, seed, args)
end