diff options
| author | historia <[not public]> | 2026-06-24 20:58:25 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-24 20:58:25 -0400 |
| commit | 9d4b799db4868bcab291b83599ff088763cd4ed6 (patch) | |
| tree | 252f0fcc779acf35243983d643d6399ee2e0cd0b /internal/game/action.go | |
| parent | d25638d98fe63efdeab570ef77e6a6a97f2d7a60 (diff) | |
| download | thehouseoficarus-9d4b799db4868bcab291b83599ff088763cd4ed6.tar.gz | |
feat: new type of non-violent 'combat': work
Diffstat (limited to 'internal/game/action.go')
| -rw-r--r-- | internal/game/action.go | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/internal/game/action.go b/internal/game/action.go index 9a07419..8d50eea 100644 --- a/internal/game/action.go +++ b/internal/game/action.go @@ -284,22 +284,17 @@ func (g *Game) checkCondition(sess *net.Session, c *action.Condition) bool { } if c.PlayerFlag != "" { - if p == nil || p.Flags == nil { - return c.Not + var val any + present := false + if p != nil && p.Flags != nil { + val, present = p.Flags[c.PlayerFlag] } - val, ok := p.Flags[c.PlayerFlag] - if c.Not { - return !ok || val != c.Value - } - return ok && val == c.Value + return flagMatches(present, val, c.Value, c.Not) } if c.Flag != "" { - val, ok := g.WorldFlags[c.Flag] - if c.Not { - return !ok || val != c.Value - } - return ok && val == c.Value + val, present := g.WorldFlags[c.Flag] + return flagMatches(present, val, c.Value, c.Not) } if c.HasItem != "" { has := p != nil && p.HasItem(c.HasItem) @@ -317,3 +312,41 @@ func (g *Game) checkCondition(sess *net.Session, c *action.Condition) bool { } return true } + +// flagMatches evaluates a flag/player_flag condition. +// +// If the condition specifies no `value`, the flag matches when it is present +// and truthy (so `{player_flag: x}` means "x is set" and +// `{player_flag: x, not: true}` means "x is not set"). If a `value` is given, +// the flag must be present and equal to it. `not` inverts the result. +func flagMatches(present bool, val, want any, not bool) bool { + var match bool + if want == nil { + match = present && isTruthy(val) + } else { + match = present && val == want + } + if not { + return !match + } + return match +} + +func isTruthy(v any) bool { + switch x := v.(type) { + case nil: + return false + case bool: + return x + case int: + return x != 0 + case int64: + return x != 0 + case float64: + return x != 0 + case string: + return x != "" + default: + return true + } +} |
