interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / mobhub / lib.om

1236 lines · 27919 bytes Raw Blame History
1note core/mobhub/lib.om: pure-Omega MobHub faction operations engine (converted from mobhub).
2note Composable OM-2 library unit (no om2 line, no fn main).
3note Compose with: si/directives/om2.om core/mobhub/lib.om <main.om|test.om>
4note
5note Implements all core domains from upstream mobhub (Rust + Mojo):
6note - MobPay: deterministic FlatRate & PoolShare war payout engine with zero-drift integer
7note reconciliation, leadership buyout/shared-pool rules, and war-hitter flat bonuses
8note - MobWorth: faction net worth valuation and vault net spendable cash calculation
9note - MobGym: Vladar Training Formula V2.0 (pure IEEE-754 f64 ln/log10/exp/pow, 50M soft cap,
10note happy taper & decay, single-train and multi-train session simulation)
11note - Reconcile: Torn money-log HTML parser with automatic OC-cut exclusion, 2-pass real-money
12note safe disbursement matcher, war-window check, and ambiguous-give closeout picker
13note - Vault: key= query parameter secret redaction guard
14
15const MH_OK ulong 0
16const MH_ERR_NEG_CACHE ulong 1
17const MH_ERR_BAD_MODEL ulong 2
18const MH_ERR_BAD_SPLITS ulong 3
19const MH_ERR_CAPACITY ulong 4
20const MH_ERR_DUP_ID ulong 5
21const MH_ERR_BAD_STAT ulong 6
22const MH_ERR_BAD_ENERGY ulong 7
23const MH_ERR_PARSE ulong 8
24const MH_ERR_OC_EXCLUDED ulong 9
25const MH_ERR_NO_MATCH ulong 10
26
27const MH_MODEL_FLAT_RATE ulong 0
28const MH_MODEL_POOL_SHARE ulong 1
29
30const MH_KIND_MEMBER ulong 0
31const MH_KIND_LEADERSHIP ulong 1
32
33const MH_WARN_SPLIT_SUM ulong 1
34const MH_WARN_NO_LEADER ulong 2
35const MH_WARN_BONUS_EXCEEDS ulong 4
36
37const MH_STAT_STR ulong 0
38const MH_STAT_SPD ulong 1
39const MH_STAT_DEX ulong 2
40const MH_STAT_DEF ulong 3
41const MH_STAT_INVALID ulong 255
42
43const MH_PICK_NONE ulong 0
44const MH_PICK_ONE ulong 1
45const MH_PICK_AMBIGUOUS ulong 2
46
47const MH_MAX_MEMBERS ulong 128
48const MH_MAX_REG ulong 128
49const MH_MAX_GIVES ulong 128
50
51note --- MobPay Policy & Member Globals ---
52global mh_g_pol_model ulong 1
53global mh_g_pol_rate_resp f64 1
54global mh_g_pol_rate_out f64 1
55global mh_g_pol_rate_ast f64 1
56global mh_g_pol_w_war f64 1
57global mh_g_pol_w_ast f64 1
58global mh_g_pol_w_out f64 1
59global mh_g_pol_split_m f64 1
60global mh_g_pol_split_f f64 1
61global mh_g_pol_split_l f64 1
62global mh_g_pol_has_ldr ulong 1
63global mh_g_pol_ldr_id long 1
64global mh_g_pol_fixed_rate ulong 1
65global mh_g_pol_ldr_shares ulong 1
66global mh_g_pol_flat_bonus long 1
67global mh_g_pol_rec_mode ulong 1
68
69global mh_g_in_cnt ulong 1
70global mh_g_in_tid long 128
71global mh_g_in_resp f64 128
72global mh_g_in_war long 128
73global mh_g_in_ast long 128
74global mh_g_in_out long 128
75
76global mh_g_out_cnt ulong 1
77global mh_g_out_tid long 128
78global mh_g_out_kind ulong 128
79global mh_g_out_resp f64 128
80global mh_g_out_war long 128
81global mh_g_out_ast long 128
82global mh_g_out_out long 128
83global mh_g_out_ph f64 128
84global mh_g_out_share f64 128
85global mh_g_out_base long 128
86global mh_g_out_lbonus long 128
87global mh_g_out_fbonus long 128
88global mh_g_out_amt long 128
89global mh_g_sort_idx ulong 128
90
91global mh_g_res_cache long 1
92global mh_g_res_mpool long 1
93global mh_g_res_ffund long 1
94global mh_g_res_lfund long 1
95global mh_g_res_rate f64 1
96global mh_g_res_has_rate ulong 1
97global mh_g_res_tot_resp f64 1
98global mh_g_res_tot_ph f64 1
99global mh_g_res_bonus_tot long 1
100global mh_g_res_fbonus_tot long 1
101global mh_g_res_mpay_tot long 1
102global mh_g_res_lpaid long 1
103global mh_g_res_grand long 1
104global mh_g_res_unalloc long 1
105global mh_g_res_warns ulong 1
106
107note --- MobWorth Valuation Globals ---
108global mh_g_val_resp long 1
109global mh_g_val_cash long 1
110global mh_g_val_armory long 1
111global mh_g_val_total long 1
112
113note --- MobGym Config & Result Globals ---
114global mh_g_gym_perk_mult f64 1
115global mh_g_gym_use_loss ulong 1
116global mh_g_gym_loss f64 1
117global mh_g_gym_min_h f64 1
118
119global mh_g_gym_trains ulong 1
120global mh_g_gym_espent f64 1
121global mh_g_gym_tgain f64 1
122global mh_g_gym_estat f64 1
123global mh_g_gym_ehappy f64 1
124global mh_g_gym_avg f64 1
125
126note --- Reconcile Globals ---
127global mh_g_parse_tid long 1
128global mh_g_parse_amt long 1
129global mh_g_parse_name uchar 128
130global mh_g_parse_nlen ulong 1
131
132global mh_g_reg_cnt ulong 1
133global mh_g_reg_id long 128
134global mh_g_reg_tid long 128
135global mh_g_reg_owed long 128
136global mh_g_reg_paid ulong 128
137global mh_g_reg_claimed ulong 128
138
139global mh_g_give_cnt ulong 1
140global mh_g_give_id long 128
141global mh_g_give_tid long 128
142global mh_g_give_amt long 128
143global mh_g_give_match long 128
144
145global mh_g_rec_matched ulong 1
146global mh_g_rec_unmatched ulong 1
147global mh_g_pick_id long 1
148global mh_g_pick_amt long 1
149
150note --- Vault Redaction & General Output Buffer ---
151global mh_g_buf uchar 8192
152global mh_g_buf_len ulong 1
153
154note --- Text Constants ---
155text mh_t_str "strength"
156text mh_t_str2 "str"
157text mh_t_spd "speed"
158text mh_t_spd2 "spd"
159text mh_t_dex "dexterity"
160text mh_t_dex2 "dex"
161text mh_t_def "defense"
162text mh_t_def2 "def"
163
164text mh_t_oc "cut for their role"
165text mh_t_inc "increased"
166text mh_t_xid "xid="
167text mh_t_mbal "money balance by $"
168text mh_t_keyeq "key="
169text mh_t_redacted "[REDACTED_BY_ATLAS_VAULT]"
170
171note ====================================================================
172note Section 1: Pure IEEE-754 f64 Math Primitives (no external libm)
173note ====================================================================
174
175fn mh_round_i64 x f64 to long
176if ge x 0.0f64
177ret long add x 0.5f64
178end
179ret long sub x 0.5f64
180end
181
182fn mh_round_to4 x f64 to f64
183p = f64 10000.0f64
184ret div f64 mh_round_i64 mul x p p
185end
186
187fn mh_clamp01 x f64 to f64
188if lt x 0.0f64
189ret 0.0f64
190end
191if gt x 1.0f64
192ret 1.0f64
193end
194ret x
195end
196
197fn mh_fmax0 x f64 to f64
198if lt x 0.0f64
199ret 0.0f64
200end
201ret x
202end
203
204fn mh_imax0 x long to long
205if lt x 0
206ret 0
207end
208ret x
209end
210
211fn mh_iabs x long to long
212if lt x 0
213ret neg x
214end
215ret x
216end
217
218note Natural logarithm ln(x) for x > 0 in full IEEE-754 f64 precision (< 1e-15 error).
219fn mh_ln x f64 to f64
220if le x 0.0f64
221ret 0.0f64
222end
223bits = bitcast ulong x
224eb = and shr bits 52 0x7ff
225e = sub long eb 1023
226mbits = or and bits 0x000fffffffffffff 0x3ff0000000000000
227m = bitcast f64 mbits
228if gt m 1.4142135623730951f64
229m = mul m 0.5f64
230e = add e 1
231end
232t = div sub m 1.0f64 add m 1.0f64
233s = mul t t
234acc = div 1.0f64 19.0f64
235acc = add div 1.0f64 17.0f64 mul s acc
236acc = add div 1.0f64 15.0f64 mul s acc
237acc = add div 1.0f64 13.0f64 mul s acc
238acc = add div 1.0f64 11.0f64 mul s acc
239acc = add div 1.0f64 9.0f64 mul s acc
240acc = add div 1.0f64 7.0f64 mul s acc
241acc = add div 1.0f64 5.0f64 mul s acc
242acc = add div 1.0f64 3.0f64 mul s acc
243acc = add 1.0f64 mul s acc
244lnm = mul mul 2.0f64 t acc
245ret add lnm mul f64 e 0.6931471805599453f64
246end
247
248fn mh_log10 x f64 to f64
249ret div mh_ln x 2.302585092994046f64
250end
251
252note Exponential exp(y) in full IEEE-754 f64 precision.
253fn mh_exp y f64 to f64
254if lt y -700.0f64
255ret 0.0f64
256end
257if gt y 700.0f64
258ret 1.0e300f64
259end
260ln2 = f64 0.6931471805599453f64
261k = mh_round_i64 div y ln2
262r = sub y mul f64 k ln2
263acc = f64 1.0f64
264i = long 13
265while gt i 0 bound 16
266acc = add 1.0f64 div mul acc r f64 i
267i = sub i 1
268end
269eb = ulong add k 1023
270scale = bitcast f64 shl eb 52
271ret mul acc scale
272end
273
274note Compute h^1.05 for h >= 0 cleanly via h * exp(0.05 * ln(h)).
275fn mh_pow_105 h f64 to f64
276if le h 0.0f64
277ret 0.0f64
278end
279ret mul h mh_exp mul 0.05f64 mh_ln h
280end
281
282note ====================================================================
283note Section 2: MobPay Deterministic Payout Engine (upstream src/payout.rs)
284note ====================================================================
285
286fn mh_policy_default_flat to void
287put mh_g_pol_model 0 MH_MODEL_FLAT_RATE
288put mh_g_pol_rate_resp 0 180284.0f64
289put mh_g_pol_rate_out 0 500000.0f64
290put mh_g_pol_rate_ast 0 250000.0f64
291put mh_g_pol_w_war 0 1.0f64
292put mh_g_pol_w_ast 0 0.5f64
293put mh_g_pol_w_out 0 0.5f64
294put mh_g_pol_split_m 0 0.8f64
295put mh_g_pol_split_f 0 0.2f64
296put mh_g_pol_split_l 0 0.0f64
297put mh_g_pol_has_ldr 0 0
298put mh_g_pol_ldr_id 0 0
299put mh_g_pol_fixed_rate 0 0
300put mh_g_pol_ldr_shares 0 0
301put mh_g_pol_flat_bonus 0 0
302put mh_g_pol_rec_mode 0 0
303end
304
305fn mh_members_reset to void
306put mh_g_in_cnt 0 0
307end
308
309fn mh_member_add tid long resp f64 war long ast long outh long to ulong
310cnt = get mh_g_in_cnt 0
311if ge cnt MH_MAX_MEMBERS
312ret MH_ERR_CAPACITY
313end
314i = ulong 0
315while lt i cnt bound 128
316if eq get mh_g_in_tid i tid
317ret MH_ERR_DUP_ID
318end
319i = add i 1
320end
321put mh_g_in_tid cnt tid
322put mh_g_in_resp cnt resp
323put mh_g_in_war cnt war
324put mh_g_in_ast cnt ast
325put mh_g_in_out cnt outh
326put mh_g_in_cnt 0 add cnt 1
327ret MH_OK
328end
329
330fn mh_payable_hits_in idx ulong to f64
331w1 = mul f64 mh_imax0 get mh_g_in_war idx mh_fmax0 get mh_g_pol_w_war 0
332w2 = mul f64 mh_imax0 get mh_g_in_ast idx mh_fmax0 get mh_g_pol_w_ast 0
333w3 = mul f64 mh_imax0 get mh_g_in_out idx mh_fmax0 get mh_g_pol_w_out 0
334ret add add w1 w2 w3
335end
336
337fn mh_is_bought_out tid long to bool
338if eq get mh_g_pol_has_ldr 0 0
339ret false
340end
341if le mh_clamp01 get mh_g_pol_split_l 0 0.0f64
342ret false
343end
344if ne get mh_g_pol_ldr_shares 0 0
345ret false
346end
347ret eq tid get mh_g_pol_ldr_id 0
348end
349
350fn mh_push_out tid long kind ulong idx ulong has_in bool to ulong
351o = get mh_g_out_cnt 0
352if ge o MH_MAX_MEMBERS
353ret o
354end
355put mh_g_out_tid o tid
356put mh_g_out_kind o kind
357if has_in
358put mh_g_out_resp o get mh_g_in_resp idx
359put mh_g_out_war o get mh_g_in_war idx
360put mh_g_out_ast o get mh_g_in_ast idx
361put mh_g_out_out o get mh_g_in_out idx
362else
363put mh_g_out_resp o 0.0f64
364put mh_g_out_war o 0
365put mh_g_out_ast o 0
366put mh_g_out_out o 0
367end
368put mh_g_out_ph o 0.0f64
369put mh_g_out_share o 0.0f64
370put mh_g_out_base o 0
371put mh_g_out_lbonus o 0
372put mh_g_out_fbonus o 0
373put mh_g_out_amt o 0
374put mh_g_out_cnt 0 add o 1
375ret o
376end
377
378note Deterministic insertion sort of mh_g_sort_idx[0..n]:
379note payable_hits descending, tie-break torn_id ascending (exact upstream order).
380fn mh_sort_for_drift n ulong to void
381i = ulong 0
382while lt i n bound 128
383put mh_g_sort_idx i i
384i = add i 1
385end
386i = ulong 1
387while lt i n bound 128
388key = get mh_g_sort_idx i
389kph = get mh_g_out_ph key
390ktid = get mh_g_out_tid key
391j = i
392while gt j 0 bound 128
393prev = get mh_g_sort_idx sub j 1
394pph = get mh_g_out_ph prev
395ptid = get mh_g_out_tid prev
396swap = false
397if gt kph pph
398swap = true
399else
400if and eq kph pph lt ktid ptid
401swap = true
402end
403end
404if swap
405put mh_g_sort_idx j prev
406j = sub j 1
407else
408break
409end
410end
411put mh_g_sort_idx j key
412i = add i 1
413end
414end
415
416fn mh_apply_drift_reconcile pool_target long to void
417n = get mh_g_out_cnt 0
418if eq n 0
419ret
420end
421cur_sum = long 0
422i = ulong 0
423while lt i n bound 128
424cur_sum = add cur_sum get mh_g_out_base i
425i = add i 1
426end
427drift = sub pool_target cur_sum
428if eq drift 0
429ret
430end
431mh_sort_for_drift n
432if gt drift 0
433cnt = ulong drift
434k = ulong 0
435while lt k cnt bound 100000
436idx = get mh_g_sort_idx rem k n
437put mh_g_out_base idx add get mh_g_out_base idx 1
438put mh_g_out_amt idx add get mh_g_out_amt idx 1
439k = add k 1
440end
441else
442cnt = ulong neg drift
443k = ulong 0
444while lt k cnt bound 100000
445idx = get mh_g_sort_idx rem k n
446if gt get mh_g_out_base idx 0
447put mh_g_out_base idx sub get mh_g_out_base idx 1
448put mh_g_out_amt idx sub get mh_g_out_amt idx 1
449end
450k = add k 1
451end
452end
453end
454
455fn mh_emit_flat_member i ulong rate f64 to void
456tid = get mh_g_in_tid i
457if mh_is_bought_out tid
458ret
459end
460o = mh_push_out tid MH_KIND_MEMBER i true
461put mh_g_out_ph o mh_payable_hits_in i
462v1 = mul mh_fmax0 get mh_g_in_resp i rate
463v2 = mul f64 mh_imax0 get mh_g_in_out i get mh_g_pol_rate_out 0
464v3 = mul f64 mh_imax0 get mh_g_in_ast i get mh_g_pol_rate_ast 0
465base = mh_round_i64 add add v1 v2 v3
466put mh_g_out_base o base
467put mh_g_out_amt o base
468end
469
470fn mh_member_bonus_f64 i ulong to f64
471o_h = f64 mh_imax0 get mh_g_in_out i
472a_h = f64 mh_imax0 get mh_g_in_ast i
473b1 = mul o_h get mh_g_pol_rate_out 0
474b2 = mul a_h get mh_g_pol_rate_ast 0
475ret add b1 b2
476end
477
478fn mh_compute_flat_rate mpool f64 to void
479icnt = get mh_g_in_cnt 0
480btot = f64 0.0f64
481tot_resp = f64 0.0f64
482tot_ph = f64 0.0f64
483i = ulong 0
484while lt i icnt bound 128
485if not mh_is_bought_out get mh_g_in_tid i
486btot = add btot mh_member_bonus_f64 i
487tot_resp = add tot_resp mh_fmax0 get mh_g_in_resp i
488tot_ph = add tot_ph mh_payable_hits_in i
489end
490i = add i 1
491end
492bonus_i = mh_round_i64 btot
493put mh_g_res_bonus_tot 0 bonus_i
494put mh_g_res_tot_resp 0 tot_resp
495put mh_g_res_tot_ph 0 tot_ph
496rate = f64 0.0f64
497if ne get mh_g_pol_fixed_rate 0 0
498rate = get mh_g_pol_rate_resp 0
499else
500rpool = sub mpool f64 bonus_i
501if lt rpool 0.0f64
502put mh_g_res_warns 0 or get mh_g_res_warns 0 MH_WARN_BONUS_EXCEEDS
503rate = 0.0f64
504else
505if gt tot_resp 0.0f64
506rate = div rpool tot_resp
507end
508end
509end
510put mh_g_res_rate 0 rate
511put mh_g_res_has_rate 0 1
512j = ulong 0
513while lt j icnt bound 128
514mh_emit_flat_member j rate
515j = add j 1
516end
517end
518
519fn mh_compute_pool_share mpool f64 to void
520icnt = get mh_g_in_cnt 0
521tot_ph = f64 0.0f64
522i = ulong 0
523while lt i icnt bound 128
524if not mh_is_bought_out get mh_g_in_tid i
525tot_ph = add tot_ph mh_payable_hits_in i
526end
527i = add i 1
528end
529put mh_g_res_tot_ph 0 tot_ph
530put mh_g_res_has_rate 0 0
531put mh_g_res_bonus_tot 0 0
532put mh_g_res_tot_resp 0 0.0f64
533i = ulong 0
534while lt i icnt bound 128
535tid = get mh_g_in_tid i
536if not mh_is_bought_out tid
537o = mh_push_out tid MH_KIND_MEMBER i true
538ph = mh_payable_hits_in i
539sh = f64 0.0f64
540if gt tot_ph 0.0f64
541sh = div ph tot_ph
542end
543base = mh_round_i64 mul mpool sh
544put mh_g_out_ph o ph
545put mh_g_out_share o sh
546put mh_g_out_base o base
547put mh_g_out_amt o base
548end
549i = add i 1
550end
551end
552
553fn mh_find_in_by_tid tid long to ulong
554icnt = get mh_g_in_cnt 0
555i = ulong 0
556while lt i icnt bound 128
557if eq get mh_g_in_tid i tid
558ret i
559end
560i = add i 1
561end
562ret MH_MAX_MEMBERS
563end
564
565fn mh_find_out_by_tid tid long to ulong
566ocnt = get mh_g_out_cnt 0
567i = ulong 0
568while lt i ocnt bound 128
569if eq get mh_g_out_tid i tid
570ret i
571end
572i = add i 1
573end
574ret MH_MAX_MEMBERS
575end
576
577fn mh_apply_leadership lfund long to void
578sl = mh_clamp01 get mh_g_pol_split_l 0
579if le sl 0.0f64
580put mh_g_res_lpaid 0 0
581ret
582end
583if eq get mh_g_pol_has_ldr 0 0
584put mh_g_res_warns 0 or get mh_g_res_warns 0 MH_WARN_NO_LEADER
585put mh_g_res_lpaid 0 0
586ret
587end
588rid = get mh_g_pol_ldr_id 0
589put mh_g_res_lpaid 0 lfund
590if ne get mh_g_pol_ldr_shares 0 0
591ex = mh_find_out_by_tid rid
592if lt ex MH_MAX_MEMBERS
593put mh_g_out_kind ex MH_KIND_LEADERSHIP
594put mh_g_out_lbonus ex lfund
595put mh_g_out_amt ex add get mh_g_out_amt ex lfund
596ret
597end
598end
599in_idx = mh_find_in_by_tid rid
600has_in = lt in_idx MH_MAX_MEMBERS
601o = mh_push_out rid MH_KIND_LEADERSHIP in_idx has_in
602put mh_g_out_lbonus o lfund
603put mh_g_out_amt o lfund
604end
605
606fn mh_finalize_payout_totals mpool_i long ffund long to void
607fbon = mh_imax0 get mh_g_pol_flat_bonus 0
608fbon_tot = long 0
609ocnt = get mh_g_out_cnt 0
610if gt fbon 0
611i = ulong 0
612while lt i ocnt bound 128
613if gt get mh_g_out_war i 0
614put mh_g_out_fbonus i fbon
615put mh_g_out_amt i add get mh_g_out_amt i fbon
616fbon_tot = add fbon_tot fbon
617end
618i = add i 1
619end
620end
621put mh_g_res_fbonus_tot 0 fbon_tot
622mpay = long 0
623pdist = long 0
624all_out = long 0
625j = ulong 0
626while lt j ocnt bound 128
627amt = get mh_g_out_amt j
628if eq get mh_g_out_kind j MH_KIND_MEMBER
629mpay = add mpay amt
630end
631pdist = add pdist get mh_g_out_base j
632all_out = add all_out amt
633j = add j 1
634end
635put mh_g_res_mpay_tot 0 mpay
636put mh_g_res_grand 0 add all_out ffund
637put mh_g_res_unalloc 0 sub mpool_i pdist
638end
639
640fn mh_validate_payout_policy to ulong
641model = get mh_g_pol_model 0
642if and ne model MH_MODEL_FLAT_RATE ne model MH_MODEL_POOL_SHARE
643ret MH_ERR_BAD_MODEL
644end
645sm = get mh_g_pol_split_m 0
646sf = get mh_g_pol_split_f 0
647sl = get mh_g_pol_split_l 0
648if or lt sm 0.0f64 or lt sf 0.0f64 lt sl 0.0f64
649ret MH_ERR_BAD_SPLITS
650end
651if or gt sm 1.0f64 or gt sf 1.0f64 gt sl 1.0f64
652ret MH_ERR_BAD_SPLITS
653end
654ssum = add add sm sf sl
655if gt fabs sub ssum 1.0f64 0.0001f64
656put mh_g_res_warns 0 or get mh_g_res_warns 0 MH_WARN_SPLIT_SUM
657end
658ret MH_OK
659end
660
661fn mh_should_reconcile model ulong to bool
662rmode = get mh_g_pol_rec_mode 0
663if eq rmode 1
664ret true
665end
666if and eq rmode 0 eq model MH_MODEL_POOL_SHARE
667ret true
668end
669ret false
670end
671
672note Main MobPay entry point: validates inputs fail-closed, runs payout & zero-drift reconciliation.
673fn mh_compute_payout cache_value long to ulong
674if lt cache_value 0
675ret MH_ERR_NEG_CACHE
676end
677put mh_g_out_cnt 0 0
678put mh_g_res_warns 0 0
679verr = mh_validate_payout_policy
680if ne verr MH_OK
681ret verr
682end
683put mh_g_res_cache 0 cache_value
684cf = f64 cache_value
685ffund = mh_round_i64 mul cf get mh_g_pol_split_f 0
686lfund = mh_round_i64 mul cf get mh_g_pol_split_l 0
687mpool = mul cf get mh_g_pol_split_m 0
688mpool_i = mh_round_i64 mpool
689put mh_g_res_ffund 0 ffund
690put mh_g_res_lfund 0 lfund
691put mh_g_res_mpool 0 mpool_i
692model = get mh_g_pol_model 0
693if eq model MH_MODEL_FLAT_RATE
694mh_compute_flat_rate mpool
695else
696mh_compute_pool_share mpool
697end
698if mh_should_reconcile model
699if or gt get mh_g_res_tot_ph 0 0.0f64 eq model MH_MODEL_FLAT_RATE
700mh_apply_drift_reconcile mpool_i
701end
702end
703mh_apply_leadership lfund
704mh_finalize_payout_totals mpool_i ffund
705ret MH_OK
706end
707
708note ====================================================================
709note Section 3: MobWorth Valuation Engine (upstream src/valuation.rs)
710note ====================================================================
711
712fn mh_compute_valuation respect f64 rate f64 armory f64 cash f64 to ulong
713if or lt respect 0.0f64 or lt rate 0.0f64 or lt armory 0.0f64 lt cash 0.0f64
714ret MH_ERR_PARSE
715end
716rv = mh_round_i64 mul respect rate
717av = mh_round_i64 armory
718cv = mh_round_i64 cash
719put mh_g_val_resp 0 rv
720put mh_g_val_armory 0 av
721put mh_g_val_cash 0 cv
722put mh_g_val_total 0 add add rv av cv
723ret MH_OK
724end
725
726note Vault net spendable cash = max(0, round(vault_money - sum(member_deposits)))
727fn mh_net_faction_cash vault_money f64 deps view f64 * ndep ulong to long
728tot_dep = f64 0.0f64
729i = ulong 0
730while lt i ndep bound 1024
731tot_dep = add tot_dep get deps i
732i = add i 1
733end
734net = mh_fmax0 sub vault_money tot_dep
735ret mh_round_i64 net
736end
737
738note ====================================================================
739note Section 4: MobGym Vladar Training Formula V2.0 (src/gym.rs & mojo)
740note ====================================================================
741
742fn mh_ascii_lower b uchar to uchar
743if and ge b 65 le b 90
744ret add b 32
745end
746ret b
747end
748
749fn mh_eq_ci a view uchar * aoff ulong alen ulong b view uchar * to bool
750if ne alen len b
751ret false
752end
753i = ulong 0
754while lt i alen bound 64
755if ne mh_ascii_lower get a add aoff i mh_ascii_lower get b i
756ret false
757end
758i = add i 1
759end
760ret true
761end
762
763fn mh_stat_parse buf view uchar * off ulong slen ulong to ulong
764if or mh_eq_ci buf off slen mh_t_str mh_eq_ci buf off slen mh_t_str2
765ret MH_STAT_STR
766end
767if or mh_eq_ci buf off slen mh_t_spd mh_eq_ci buf off slen mh_t_spd2
768ret MH_STAT_SPD
769end
770if or mh_eq_ci buf off slen mh_t_dex mh_eq_ci buf off slen mh_t_dex2
771ret MH_STAT_DEX
772end
773if or mh_eq_ci buf off slen mh_t_def mh_eq_ci buf off slen mh_t_def2
774ret MH_STAT_DEF
775end
776ret MH_STAT_INVALID
777end
778
779fn mh_stat_const_a sid ulong to f64
780if eq sid MH_STAT_STR
781ret 1600.0f64
782end
783if eq sid MH_STAT_SPD
784ret 1600.0f64
785end
786if eq sid MH_STAT_DEX
787ret 1800.0f64
788end
789ret 2100.0f64
790end
791
792fn mh_stat_const_b sid ulong to f64
793if eq sid MH_STAT_STR
794ret 1700.0f64
795end
796if eq sid MH_STAT_SPD
797ret 2000.0f64
798end
799if eq sid MH_STAT_DEX
800ret 1500.0f64
801end
802ret -600.0f64
803end
804
805fn mh_soft_cap_stat sval f64 to f64
806cap = f64 50000000.0f64
807if le sval cap
808ret sval
809end
810denom = mul 8.77635f64 mh_log10 sval
811ret add cap div sub sval cap denom
812end
813
814fn mh_gym_reset_opts to void
815put mh_g_gym_perk_mult 0 1.0f64
816put mh_g_gym_use_loss 0 0
817put mh_g_gym_loss 0 0.0f64
818put mh_g_gym_min_h 0 0.0f64
819end
820
821fn mh_gym_inner sid ulong sval f64 happy f64 to f64
822h = mh_fmax0 happy
823seff = mh_soft_cap_stat sval
824log_t = mh_round_to4 mh_ln add 1.0f64 div h 250.0f64
825h_mult = mh_round_to4 add 1.0f64 mul 0.07f64 log_t
826acc = mul seff h_mult
827acc = add acc mul 8.0f64 mh_pow_105 h
828hr = div h 99999.0f64
829acc = add acc mul sub 1.0f64 mul hr hr mh_stat_const_a sid
830ret add acc mh_stat_const_b sid
831end
832
833note Exact Vladar Training Formula V2.0 single-train expected stat gain.
834fn mh_gym_expected_gain sid ulong sval f64 happy f64 e_train f64 dots f64 pmult f64 to f64
835inner = mh_gym_inner sid sval happy
836r = mul div inner 200000.0f64 dots
837r = mul r e_train
838ret mul r pmult
839end
840
841note Multi-train session simulator with decaying happy and min_happy floor.
842fn mh_gym_simulate sid ulong sv f64 hv f64 ev f64 e_train f64 dots f64 to ulong
843if gt sid MH_STAT_DEF
844ret MH_ERR_BAD_STAT
845end
846if or le e_train 0.0f64 lt ev 0.0f64
847ret MH_ERR_BAD_ENERGY
848end
849if or le dots 0.0f64 lt sv 0.0f64
850ret MH_ERR_BAD_ENERGY
851end
852pmult = get mh_g_gym_perk_mult 0
853if le pmult 0.0f64
854pmult = 1.0f64
855end
856loss = mul 0.5f64 e_train
857if ne get mh_g_gym_use_loss 0 0
858loss = get mh_g_gym_loss 0
859end
860hv = mh_fmax0 hv
861trains = ulong 0
862tgain = f64 0.0f64
863while ge ev e_train bound 100000
864g = mh_gym_expected_gain sid sv hv e_train dots pmult
865sv = add sv g
866tgain = add tgain g
867hv = sub hv loss
868if lt hv get mh_g_gym_min_h 0
869hv = get mh_g_gym_min_h 0
870end
871ev = sub ev e_train
872trains = add trains 1
873end
874put mh_g_gym_trains 0 trains
875put mh_g_gym_espent 0 mul f64 trains e_train
876put mh_g_gym_tgain 0 tgain
877put mh_g_gym_estat 0 sv
878put mh_g_gym_ehappy 0 hv
879if gt trains 0
880put mh_g_gym_avg 0 div tgain f64 trains
881else
882put mh_g_gym_avg 0 0.0f64
883end
884ret MH_OK
885end
886
887note ====================================================================
888note Section 5: Money-Log Parser & 2-Pass Reconciler (upstream src/reconcile.rs)
889note ====================================================================
890
891fn mh_find_ci hay view uchar * start ulong hlen ulong ndl view uchar * to ulong
892nlen = len ndl
893if or eq nlen 0 gt add start nlen hlen
894ret add hlen 1
895end
896lim = add sub hlen nlen 1
897i = start
898while lt i lim bound 8192
899if mh_eq_ci hay i nlen ndl
900ret i
901end
902i = add i 1
903end
904ret add hlen 1
905end
906
907fn mh_is_digit b uchar to bool
908ret and ge b 48 le b 57
909end
910
911fn mh_parse_dollar_amt txt view uchar * pos ulong tlen ulong to ulong
912p_bal = mh_find_ci txt pos tlen mh_t_mbal
913if gt p_bal tlen
914ret MH_ERR_NO_MATCH
915end
916q = add p_bal len mh_t_mbal
917amt = long 0
918d_amt = ulong 0
919while lt q tlen bound 64
920c = get txt q
921if eq c 44
922q = add q 1
923continue
924end
925if not mh_is_digit c
926break
927end
928amt = add mul amt 10 long sub c 48
929d_amt = add d_amt 1
930q = add q 1
931end
932if eq d_amt 0
933ret MH_ERR_NO_MATCH
934end
935put mh_g_parse_amt 0 amt
936ret MH_OK
937end
938
939note Parse Torn depositFunds money-log HTML entry.
940note Excludes OC cuts ("cut for their role") with MH_ERR_OC_EXCLUDED.
941note Extracts recipient_torn_id, recipient_name, and comma-stripped dollar amount.
942fn mh_parse_money_log txt view uchar * tlen ulong to ulong
943put mh_g_parse_tid 0 0
944put mh_g_parse_amt 0 0
945put mh_g_parse_nlen 0 0
946if eq tlen 0
947ret MH_ERR_NO_MATCH
948end
949if le mh_find_ci txt 0 tlen mh_t_oc tlen
950ret MH_ERR_OC_EXCLUDED
951end
952p_inc = mh_find_ci txt 0 tlen mh_t_inc
953if gt p_inc tlen
954ret MH_ERR_NO_MATCH
955end
956p_xid = mh_find_ci txt add p_inc 9 tlen mh_t_xid
957if gt p_xid tlen
958ret MH_ERR_NO_MATCH
959end
960p = add p_xid 4
961tid = long 0
962d_tid = ulong 0
963while lt p tlen bound 32
964c = get txt p
965if not mh_is_digit c
966break
967end
968tid = add mul tid 10 long sub c 48
969d_tid = add d_tid 1
970p = add p 1
971end
972if eq d_tid 0
973ret MH_ERR_NO_MATCH
974end
975put mh_g_parse_tid 0 tid
976while lt p tlen bound 256
977if eq get txt p 62
978p = add p 1
979break
980end
981p = add p 1
982end
983nlen = ulong 0
984while lt p tlen bound 128
985c = get txt p
986if eq c 60
987break
988end
989put mh_g_parse_name nlen c
990nlen = add nlen 1
991p = add p 1
992end
993put mh_g_parse_nlen 0 nlen
994ret mh_parse_dollar_amt txt p tlen
995end
996
997fn mh_rec_reset to void
998put mh_g_reg_cnt 0 0
999put mh_g_give_cnt 0 0
1000put mh_g_rec_matched 0 0
1001put mh_g_rec_unmatched 0 0
1002end
1003
1004fn mh_rec_add_reg reg_id long tid long owed long paid ulong to ulong
1005c = get mh_g_reg_cnt 0
1006if ge c MH_MAX_REG
1007ret MH_ERR_CAPACITY
1008end
1009put mh_g_reg_id c reg_id
1010put mh_g_reg_tid c tid
1011put mh_g_reg_owed c owed
1012put mh_g_reg_paid c paid
1013put mh_g_reg_claimed c paid
1014put mh_g_reg_cnt 0 add c 1
1015ret MH_OK
1016end
1017
1018fn mh_rec_add_give give_id long tid long amt long to ulong
1019c = get mh_g_give_cnt 0
1020if ge c MH_MAX_GIVES
1021ret MH_ERR_CAPACITY
1022end
1023put mh_g_give_id c give_id
1024put mh_g_give_tid c tid
1025put mh_g_give_amt c amt
1026put mh_g_give_match c 0
1027put mh_g_give_cnt 0 add c 1
1028ret MH_OK
1029end
1030
1031fn mh_find_reg_by_tid tid long to ulong
1032rc = get mh_g_reg_cnt 0
1033i = ulong 0
1034while lt i rc bound 128
1035if eq get mh_g_reg_tid i tid
1036ret i
1037end
1038i = add i 1
1039end
1040ret MH_MAX_REG
1041end
1042
1043note 2-pass real-money-safe reconciliation:
1044note Rule 1: never match an already-paid register row.
1045note Rule 2: each register row claimed by at most one give.
1046note Pass 1: exact amount match. Pass 2: within tolerance_pct.
1047fn mh_match_disbursements tol_pct f64 to void
1048rc = get mh_g_reg_cnt 0
1049ri = ulong 0
1050while lt ri rc bound 128
1051put mh_g_reg_claimed ri get mh_g_reg_paid ri
1052ri = add ri 1
1053end
1054gc = get mh_g_give_cnt 0
1055gi = ulong 0
1056while lt gi gc bound 128
1057put mh_g_give_match gi 0
1058gi = add gi 1
1059end
1060note Pass 1: exact amount match
1061gi = ulong 0
1062while lt gi gc bound 128
1063gamt = get mh_g_give_amt gi
1064if gt gamt 0
1065ridx = mh_find_reg_by_tid get mh_g_give_tid gi
1066if lt ridx MH_MAX_REG
1067if eq get mh_g_reg_claimed ridx 0
1068owed = get mh_g_reg_owed ridx
1069if and gt owed 0 eq gamt owed
1070put mh_g_give_match gi get mh_g_reg_id ridx
1071put mh_g_reg_claimed ridx 1
1072end
1073end
1074end
1075end
1076gi = add gi 1
1077end
1078note Pass 2: within tolerance percentage
1079gi = ulong 0
1080while lt gi gc bound 128
1081if eq get mh_g_give_match gi 0
1082gamt = get mh_g_give_amt gi
1083if gt gamt 0
1084ridx = mh_find_reg_by_tid get mh_g_give_tid gi
1085if lt ridx MH_MAX_REG
1086if eq get mh_g_reg_claimed ridx 0
1087owed = get mh_g_reg_owed ridx
1088if gt owed 0
1089mdiff = mh_round_i64 mul f64 owed tol_pct
1090if lt mdiff 1
1091mdiff = 1
1092end
1093if le mh_iabs sub gamt owed mdiff
1094put mh_g_give_match gi get mh_g_reg_id ridx
1095put mh_g_reg_claimed ridx 1
1096end
1097end
1098end
1099end
1100end
1101end
1102gi = add gi 1
1103end
1104m = ulong 0
1105u = ulong 0
1106gi = ulong 0
1107while lt gi gc bound 128
1108if ne get mh_g_give_match gi 0
1109m = add m 1
1110else
1111u = add u 1
1112end
1113gi = add gi 1
1114end
1115put mh_g_rec_matched 0 m
1116put mh_g_rec_unmatched 0 u
1117end
1118
1119note Verify if give timestamp falls in half-open war payout window [from_ms, to_ms).
1120fn mh_is_in_war_window has_ts bool ts long has_from bool from_ms long has_to bool to_ms long to bool
1121if not has_ts
1122ret not has_to
1123end
1124if has_from
1125if lt ts from_ms
1126ret false
1127end
1128end
1129if has_to
1130if ge ts to_ms
1131ret false
1132end
1133end
1134ret true
1135end
1136
1137note Pick single exact give within [round(owed*(1-tol)), round(owed*(1+tol))] from loaded gives.
1138fn mh_pick_payout_give owed long tol f64 to ulong
1139put mh_g_pick_id 0 0
1140put mh_g_pick_amt 0 0
1141if le owed 0
1142ret MH_PICK_NONE
1143end
1144lo = mh_round_i64 mul f64 owed sub 1.0f64 tol
1145hi = mh_round_i64 mul f64 owed add 1.0f64 tol
1146found = ulong 0
1147gc = get mh_g_give_cnt 0
1148i = ulong 0
1149while lt i gc bound 128
1150amt = get mh_g_give_amt i
1151if and gt amt 0 and ge amt lo le amt hi
1152found = add found 1
1153if eq found 1
1154put mh_g_pick_id 0 get mh_g_give_id i
1155put mh_g_pick_amt 0 amt
1156else
1157put mh_g_pick_id 0 0
1158put mh_g_pick_amt 0 0
1159ret MH_PICK_AMBIGUOUS
1160end
1161end
1162i = add i 1
1163end
1164if eq found 1
1165ret MH_PICK_ONE
1166end
1167ret MH_PICK_NONE
1168end
1169
1170note ====================================================================
1171note Section 6: Vault Secret Redaction Guard (upstream src/vault.rs)
1172note ====================================================================
1173
1174fn mh_is_ws b uchar to bool
1175if eq b 32
1176ret true
1177end
1178if eq b 9
1179ret true
1180end
1181if eq b 10
1182ret true
1183end
1184ret eq b 13
1185end
1186
1187note Redacts every key=<secret> query param in inp[0..ilen] to key=[REDACTED_BY_ATLAS_VAULT].
1188note Writes result to mh_g_buf and stores length in mh_g_buf_len[0].
1189fn mh_redact_secrets inp view uchar * ilen ulong to ulong
1190w = ulong 0
1191i = ulong 0
1192while lt i ilen bound 8192
1193matched = false
1194if le add i 4 ilen
1195if mh_eq_ci inp i 4 mh_t_keyeq
1196matched = true
1197end
1198end
1199if matched
1200note Emit "key=[REDACTED_BY_ATLAS_VAULT]"
1201k = ulong 0
1202while lt k 4 bound 4
1203if lt w 8192
1204put mh_g_buf w get mh_t_keyeq k
1205w = add w 1
1206end
1207k = add k 1
1208end
1209rlen = len mh_t_redacted
1210r = ulong 0
1211while lt r rlen bound 64
1212if lt w 8192
1213put mh_g_buf w get mh_t_redacted r
1214w = add w 1
1215end
1216r = add r 1
1217end
1218i = add i 4
1219while lt i ilen bound 1024
1220c = get inp i
1221if or eq c 38 mh_is_ws c
1222break
1223end
1224i = add i 1
1225end
1226else
1227if lt w 8192
1228put mh_g_buf w get inp i
1229w = add w 1
1230end
1231i = add i 1
1232end
1233end
1234put mh_g_buf_len 0 w
1235ret w
1236end