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