Fix ability targeting gaps: multi-target, UpTo, ChooseOne, LinearOnly, RequiresLos

- AbilityTarget: add BranchIndex for ChooseOne branch selection
- Action: add ChooseOne? property; when set the action is a branch selector
- AbilitySystem: ExecuteSingleAction handles ChooseOne — reads BranchIndex
  from client targets, executes chosen branch actions (auto-resolved)
- game_screen.gd: count.N > 1 — input handler now accumulates targets until
  N collected before advancing to next action
- game_screen.gd: count.UpTo — show "Done Selecting" confirm button so player
  can finish with fewer than N targets
- game_screen.gd: ChooseOne — detect chooseone key in action dict, show branch
  panel with labelled buttons, record branchIndex in target payload
- game_screen.gd: LinearOnly — _is_linear() filter applied to both tile and
  unit highlights when ability range has linearonly=true
- game_screen.gd: RequiresLos — _client_has_los() supercover DDA (matches
  server LineOfSight.HasLos) applied to highlights when requireslos=true

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-28 16:27:47 -04:00
co-authored by Claude Happy
parent bd59904ce2
commit 2e3e284023
4 changed files with 190 additions and 15 deletions
+166 -13
View File
@@ -140,11 +140,16 @@ var _ability_unit_id : String = ""
var _ability_idx : int = -1
var _ability_panel : Control = null
var _ability_def : Array = [] # Actions array from parsed ability JSON
var _ability_range : Dictionary = {} # Top-level range {min, max} from ability definition
var _ability_range : Dictionary = {} # Top-level range {min, max, linearonly, requireslos}
var _ability_targets : Array = [] # collected AbilityTarget dicts to send
var _ability_cur_action_idx : int = 0 # which Action we are currently collecting for
var _ability_card_id : String = "" # non-empty when targeting for a card's active ability
var _ability_target_conditions: Array = [] # target-side ActivationConditions from card ability
var _ability_target_n : int = 1 # count.N for current action
var _ability_target_up_to : bool = false # count.upTo for current action
var _ability_targets_this_action : int = 0 # targets collected for current action so far
var _ability_confirm_btn : Control = null # "Done" button shown for UpTo selections
var _ability_branch_panel : Control = null # ChooseOne branch selector panel
var _player_panel : Control = null
# ── Unit ownership lookup ─────────────────────────────────────────────────────
@@ -2431,23 +2436,30 @@ func _handle_board_click(mouse_pos: Vector2) -> void:
_confirm_sacrifice(unit_id, unit.get("name", "this spirit"))
Mode.ABILITY_TARGET:
# Collect the clicked board target then advance the targeting flow.
# Collect the clicked board target; advance only when N targets have been gathered.
var _collected := false
if not unit_id.is_empty():
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"instanceId": unit_id
})
_ability_cur_action_idx += 1
_advance_ability_targeting()
_ability_targets_this_action += 1
_collected = true
elif coord.x >= 0:
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"coord": { "x": coord.x, "y": coord.y }
})
_ability_targets_this_action += 1
_collected = true
if not _collected:
_set_mode(Mode.NONE)
elif _ability_targets_this_action >= _ability_target_n:
# All required targets for this action collected — advance.
_ability_cur_action_idx += 1
_advance_ability_targeting()
else:
_set_mode(Mode.NONE)
# else: more targets needed (or UpTo: confirm button lets player finish early)
func _select_unit(unit_id: String) -> void:
@@ -2505,6 +2517,11 @@ func _set_mode(new_mode: Mode) -> void:
_ability_cur_action_idx = 0
_ability_card_id = ""
_ability_target_conditions = []
_ability_target_n = 1
_ability_target_up_to = false
_ability_targets_this_action = 0
_hide_ability_confirm_btn()
_hide_branch_panel()
_clear_highlights()
_hide_ability_panel()
_hide_player_panel()
@@ -2769,12 +2786,24 @@ func _start_ability_targeting() -> void:
if _ability_def[_ability_cur_action_idx] is Dictionary:
action = _ability_def[_ability_cur_action_idx] as Dictionary
# ChooseOne — present branch buttons; no board highlights needed.
if action.has("chooseone"):
_set_mode(Mode.ABILITY_TARGET)
_clear_highlights()
_show_branch_panel(action)
return
var target_sel : Dictionary = action.get("target", {})
var scope : String = target_sel.get("scope", "Battlefield")
var ownership : String = target_sel.get("ownership", "Any")
var count_dict : Dictionary = target_sel.get("count", {})
var count_all : bool = count_dict.get("all", false)
# Read target count for this action (used by the input handler for multi-target collection).
_ability_target_n = maxi(1, int(count_dict.get("n", 1)))
_ability_target_up_to = bool(count_dict.get("upto", false))
_ability_targets_this_action = 0
# Actions auto-resolved by the server need no player input.
# "Target" means "owner of the last targeted card" — resolved server-side from ctx.ResolvedTargets.
var auto_ownerships := ["Self", "ExactSelf", "Random", "Target"]
@@ -2796,11 +2825,18 @@ func _start_ability_targeting() -> void:
_clear_highlights()
_highlight_ability_targets(action)
# When the player may select fewer than N targets, show a confirm button.
if _ability_target_up_to and _ability_target_n > 1:
_show_ability_confirm_btn()
## Called after each target is collected; advances to the next action or sends.
## Called after each target (or set of targets) is collected; advances to the next action or sends.
func _advance_ability_targeting() -> void:
_hide_ability_confirm_btn()
_hide_branch_panel()
_hide_player_panel()
_clear_highlights()
_ability_targets_this_action = 0
_start_ability_targeting()
@@ -2812,16 +2848,29 @@ func _highlight_ability_targets(action: Dictionary) -> void:
var ownership : String = target_sel.get("ownership", "Any")
var filters : Array = target_sel.get("filters", [])
# Tile target — paint tiles that pass spatial filters blue.
var linear_only : bool = _ability_range.get("linearonly", false)
var requires_los : bool = _ability_range.get("requireslos", false)
# Source position — needed for range, linear, and LOS checks.
var src_pos_ab : Dictionary = {}
if not _ability_range.is_empty() or linear_only or requires_los:
src_pos_ab = Game.get_unit_by_id(_ability_unit_id).get("position", {})
# Tile target — paint tiles that pass spatial and range/LOS filters blue.
if type_ == "Tile":
var bw := Game.board_width()
var bh := Game.board_height()
for tx in range(bw):
for ty in range(bh):
if not src_pos_ab.is_empty():
var sx := int(src_pos_ab.get("x", 0))
var sy := int(src_pos_ab.get("y", 0))
if linear_only and not _is_linear(sx, sy, tx, ty): continue
if requires_los and not _client_has_los(sx, sy, tx, ty): continue
if _tile_passes_filters(tx, ty, filters):
_add_highlight(tx, ty, "ability")
return
# Unit-type target — highlight the board position of each matching unit.
var include_enemy : bool = ownership in ["Enemy", "Any"]
var include_friendly : bool = ownership in ["Owner", "Ally", "Any"]
@@ -2838,10 +2887,6 @@ func _highlight_ability_targets(action: Dictionary) -> void:
if not my_arch.is_empty():
targets.append(my_arch)
var src_pos_ab : Dictionary = {}
if not _ability_range.is_empty():
src_pos_ab = Game.get_unit_by_id(_ability_unit_id).get("position", {})
for u: Dictionary in targets:
if type_ == "Spirit" || type_ == "ArchSpirit" || type_ == "SpiritToken" || type_ == "Ruin":
if u.get("cardType", "").to_lower() != type_.to_lower() : continue
@@ -2854,6 +2899,12 @@ func _highlight_ability_targets(action: Dictionary) -> void:
absi(int(pos.get("y", 0)) - int(src_pos_ab.get("y", 0))))
if dist < int(_ability_range.get("min", 0)) or dist > int(_ability_range.get("max", 999)):
continue
# Linear and LOS checks — applied independently of the distance range check.
if not src_pos_ab.is_empty():
var ux := int(pos.get("x", 0)); var uy := int(pos.get("y", 0))
var sx := int(src_pos_ab.get("x", 0)); var sy := int(src_pos_ab.get("y", 0))
if linear_only and not _is_linear(sx, sy, ux, uy): continue
if requires_los and not _client_has_los(sx, sy, ux, uy): continue
_add_highlight(pos.get("x", 0), pos.get("y", 0), "ability")
@@ -2909,6 +2960,108 @@ func _tile_passes_filters(tx: int, ty: int, filters: Array) -> bool:
return true
## Returns true if (tx, ty) lies on the same row, column, or diagonal as (sx, sy).
## Matches the server's AbilityRange.LinearOnly validation.
func _is_linear(sx: int, sy: int, tx: int, ty: int) -> bool:
return sx == tx or sy == ty or absi(tx - sx) == absi(ty - sy)
## Returns true if there is an unobstructed line of sight from (sx, sy) to (tx, ty).
## Uses the same supercover DDA algorithm as the server's LineOfSight.HasLos.
## Any unit occupying an intermediate tile blocks LOS (source and destination excluded).
func _client_has_los(sx: int, sy: int, tx: int, ty: int) -> bool:
if sx == tx and sy == ty: return true
var dx := tx - sx; var dy := ty - sy
var nx := absi(dx); var ny := absi(dy)
var sign_x := 1 if dx > 0 else -1
var sign_y := 1 if dy > 0 else -1
var ix := 0; var iy := 0
var cx := sx; var cy := sy
while ix < nx or iy < ny:
var t_x := (float(ix) + 0.5) / nx if nx > 0 else 1e9
var t_y := (float(iy) + 0.5) / ny if ny > 0 else 1e9
if t_x < t_y:
cx += sign_x; ix += 1
else:
cy += sign_y; iy += 1
if cx == tx and cy == ty: break
var tile := Game.get_tile(cx, cy)
if not tile.is_empty() and str(tile.get("occupantId", "")) != "":
return false
return true
## Show a "Done" button so the player can finish an UpTo selection with fewer than N targets.
func _show_ability_confirm_btn() -> void:
_hide_ability_confirm_btn()
var btn := Button.new()
btn.text = "Done Selecting"
btn.add_theme_font_size_override("font_size", 12)
btn.modulate = Color(0.4, 1.0, 0.5)
btn.position = Vector2(8, 330)
btn.size = Vector2(150, 30)
btn.pressed.connect(func():
_hide_ability_confirm_btn()
_ability_cur_action_idx += 1
_advance_ability_targeting())
_hud.add_child(btn)
_ability_confirm_btn = btn
func _hide_ability_confirm_btn() -> void:
if is_instance_valid(_ability_confirm_btn):
_ability_confirm_btn.queue_free()
_ability_confirm_btn = null
## Show a panel of labelled branch buttons for a ChooseOne action.
func _show_branch_panel(action: Dictionary) -> void:
_hide_branch_panel()
var co : Dictionary = action.get("chooseone", {})
var branches : Array = co.get("branches", [])
if branches.is_empty(): return
var panel := PanelContainer.new()
panel.position = Vector2(8, 120)
var vbox := VBoxContainer.new()
vbox.add_theme_constant_override("separation", 8)
vbox.custom_minimum_size = Vector2(190, 0)
var title := Label.new()
title.text = "Choose One:"
title.add_theme_font_size_override("font_size", 13)
title.add_theme_color_override("font_color", Color(1.0, 0.9, 0.3))
vbox.add_child(title)
vbox.add_child(HSeparator.new())
for i in branches.size():
var branch : Dictionary = branches[i] as Dictionary
var label_text : String = str(branch.get("label", "Option %d" % (i + 1)))
var btn := Button.new()
btn.text = label_text
btn.add_theme_font_size_override("font_size", 12)
var cap_idx : int = i
btn.pressed.connect(func():
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"branchIndex": cap_idx
})
_ability_cur_action_idx += 1
_hide_branch_panel()
_advance_ability_targeting())
vbox.add_child(btn)
panel.add_child(vbox)
_hud.add_child(panel)
_ability_branch_panel = panel
func _hide_branch_panel() -> void:
if is_instance_valid(_ability_branch_panel):
_ability_branch_panel.queue_free()
_ability_branch_panel = null
## Fire the ability request with all collected targets.
func _send_ability() -> void:
if not _ability_card_id.is_empty():
@@ -466,6 +466,18 @@ public static class AbilitySystem
HashSet<string>? restrictTargets = null,
DurationModel? forceDuration = null)
{
// ChooseOne branch selector: execute the client-chosen branch's actions instead.
if (action.ChooseOne is { Branches.Length: > 0 })
{
var branchIdx = clientTargets?
.FirstOrDefault(t => t.ActionIndex == actionIndex)?.BranchIndex ?? 0;
branchIdx = Math.Clamp(branchIdx, 0, action.ChooseOne.Branches.Length - 1);
ExecuteActions(action.ChooseOne.Branches[branchIdx].Actions, ctx,
clientTargets: null, requireClientTargets: false,
forceDuration: forceDuration);
return;
}
// Source-level conditions (Subject != "Target"): gate the whole action.
if (action.Conditions is { Length: > 0 })
foreach (var cond in action.Conditions)
@@ -71,6 +71,14 @@ public sealed class Action
public Condition[]? Conditions { get; set; }
public Effect[] Effects { get; set; } = [];
public DurationModel? Duration { get; set; }
/// <summary>
/// When non-null this Action is a branch selector — <see cref="Effects"/> are ignored.
/// The controlling player picks one <see cref="Branch"/>; its <see cref="Branch.Actions"/>
/// execute instead. Set <see cref="AbilityTarget.BranchIndex"/> in the client payload
/// to indicate which branch was chosen (0-based; defaults to 0 if absent).
/// Branch sub-actions are auto-resolved (no nested client targeting required).
/// </summary>
public ChooseOne? ChooseOne { get; set; }
}
// ── ChooseOne ─────────────────────────────────────────────────────────────────
+4 -2
View File
@@ -748,6 +748,8 @@ public sealed record AbilityTarget(
/// <summary>0-based index of the ability action this target satisfies.</summary>
int ActionIndex,
/// <summary>Set for unit/card targets.</summary>
string? InstanceId = null,
string? InstanceId = null,
/// <summary>Set for tile targets.</summary>
CoordDto? Coord = null);
CoordDto? Coord = null,
/// <summary>Set for ChooseOne actions: the 0-based index of the chosen branch.</summary>
int? BranchIndex = null);