ludic/packages/ludic.effects/verbs.ludic

89 lines
3.3 KiB
Text

# verbs.ludic - the only way the ring changes. The same kind from the same label replaces
# itself; a bonus past the cap pushes out the oldest bonus; a full ring drops the nearest end.
@alloc_ok("an effect's end: a fact per effect that runs out or is replaced")
function ef_end(effects_st: mut EffectsState, i: int, why: int) -> void {
let all = ef_all(effects_st)
var e = q_rec(effects_ended(effects_st))
if e == null {
@alloc_ok("past the ring: more of these in a frame than its records, then kept in it")
e = new EffectEnded
q_ring_add(effects_ended(effects_st), e)
}
e.kind = all[i].kind
e.label = all[i].label
e.why = why
e.bonus = all[i].bonus
q_push(effects_ended(effects_st), e)
ef_drop(effects_st, i)
}
@alloc_ok("an effect gone: the ring rebuilt, a few times a game hour")
function ef_drop(effects_st: mut EffectsState, i: int) -> void {
let all = ef_all(effects_st)
let rest = new []Effect
for k in 0 .. len(all) { if k != i { push(rest, all[k]) } }
effects_st.ef_list = rest
}
# the one with the least time left (of the bonuses only, when asked); -1 when there is none
function ef_nearest_end(effects_st: EffectsState, bonus_only: bool) -> int {
let all = ef_all(effects_st)
var at = -1
for i in 0 .. len(all) {
if bonus_only and not all[i].bonus { continue }
if at < 0 or all[i].left < all[at].left { at = i }
}
return at
}
function ef_make_room(effects_st: mut EffectsState, bonus: bool) -> void {
if bonus {
while effects_bonus_count(effects_st) >= effects_st.ef_bonus_max and ef_nearest_end(effects_st, true) >= 0 { ef_end(effects_st, ef_nearest_end(effects_st, true), EFFECT_PUSHED_OUT) }
}
if len(ef_all(effects_st)) >= effects_st.ef_max and ef_nearest_end(effects_st, false) >= 0 { ef_end(effects_st, ef_nearest_end(effects_st, false), EFFECT_CROWDED_OUT) }
}
# begin one `after` game minutes from now, lasting `minutes` once it has begun
@alloc_ok("an effect begun: food, rest, the weather - an event, not a frame")
export function effects_add_after(effects_st: mut EffectsState, after: float, kind: int, mag: float, minutes: float, bonus: bool, label: string) -> void {
let all = ef_all(effects_st)
for i in 0 .. len(all) {
if all[i].kind == kind and all[i].label == label {
all[i].mag = mag
all[i].left = minutes
return
}
}
ef_make_room(effects_st, bonus)
let e = new Effect
e.kind = kind
e.mag = mag
e.left = minutes
e.delay = after
e.bonus = bonus
e.label = label
push(ef_all(effects_st), e)
}
export function effects_add(effects_st: mut EffectsState, kind: int, mag: float, minutes: float, bonus: bool, label: string) -> void {
effects_add_after(effects_st, 0.0, kind, mag, minutes, bonus, label)
}
# everything stops at once, and says nothing: a new trip or a night's sleep
@alloc_ok("a new trip or a night's sleep")
export function effects_clear(effects_st: mut EffectsState) -> void { effects_st.ef_list = new []Effect }
# run them down by `minutes` of game time; a delayed one waits first
export function effects_run(effects_st: mut EffectsState, minutes: float) -> void {
var i = 0
while i < len(ef_all(effects_st)) {
let e = ef_all(effects_st)[i]
if e.delay > 0.0 {
e.delay = e.delay - minutes
i += 1
continue
}
e.left = e.left - minutes
if e.left < 0.0 { ef_end(effects_st, i, EFFECT_RAN_OUT) } else { i += 1 }
}
}