diff --git a/README.md b/README.md index fc3669e..22c511e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Godot 4 tactical RPG prototype inspired by classic turn-based Romance of the Thr - Move, attack, wait, end turn. - Enemy AI with movement, attacks, multiple MP tactic choices, and scenario target priorities. - Hover tile and unit info. +- Enemy threat range overlay with tile-level threat source hints. - Damage forecast for selected-unit attacks. - Terrain defense and avoid affect physical attack damage and hit chance. - Min/max attack ranges and counterattack forecasts. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index dea9409..8516a64 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -38,7 +38,7 @@ 4. Show scenario briefing with the campaign chapter header, battle header, objective summary, chapter overview, manual checkpoint menu, and optional pre-battle shop, Armory, Roster, and Formation setup. 5. Player selects a unit. 6. Briefing completion dispatches battle-begin events, including opening dialogue. -7. Unit moves, attacks, chooses a tactic or consumable item from the side menu, changes equipment, counters, gains EXP, levels up, promotes at class thresholds, or waits. Tactic buttons show MP cost, kind, range, and power, support, debuff, movement modifier, accuracy/evasion modifier, damage-over-time, or action-lock effect. Physical attacks roll hit chance from unit agility and target terrain avoid; misses give reduced EXP. The scene plays placeholder SFX, a short movement slide, low-HP warning rings, hover target preview badges, and floating combat text for core action and UI feedback. +7. Unit moves, attacks, chooses a tactic or consumable item from the side menu, changes equipment, counters, gains EXP, levels up, promotes at class thresholds, or waits. Tactic buttons show MP cost, kind, range, and power, support, debuff, movement modifier, accuracy/evasion modifier, damage-over-time, or action-lock effect. Physical attacks roll hit chance from unit agility and target terrain avoid; misses give reduced EXP. The scene plays placeholder SFX, a short movement slide, low-HP warning rings, hover target preview badges, an enemy threat overlay, and floating combat text for core action and UI feedback. 8. Player ends turn. 9. Enemy AI moves, attacks, and can cast damage, healing, support, debuff, movement-modifier, accuracy/evasion-modifier, damage-over-time, or action-lock tactics through the same combat and skill resolvers. 10. Battle checks scenario-defined defeat conditions first, including commander loss and turn limits, then victory conditions such as enemy defeat or reaching a marked destination tile. @@ -58,6 +58,6 @@ Joined officers gate whether `requires_joined` player deployments are loaded at Inventory and campaign flags are copied from `CampaignState` into `BattleState` when a scenario starts. The pre-battle Chapters overview reads `CampaignState` chapter progress and can load completed or current battles without mutating the save. The pre-battle Save menu can write the current campaign state to `user://campaign_manual_save.json`, and loading that checkpoint restores it into `user://campaign_save.json` before re-entering the same pending-choice, completion, or current-briefing branch as startup. Pre-battle shop stock comes from the loaded scenario's `shop.items` list plus matching `shop.conditional_items` blocks, with optional finite limits from item entries or `stock` maps. Shop purchases and 50% sell-back are campaign transactions: they update saved gold, unequipped inventory, and finite-stock purchase counts immediately, write the save file, and refresh the already-loaded battle inventory before the player begins the battle. Pre-battle Armory equipment changes use the same equipment rules as battle HUD equipment changes, then immediately save merged roster equipment/stat snapshots and inventory stock. Pre-battle Roster uses scenario `roster.max_units`, `roster.required_officers`, and `roster.required_units` to mark optional player deployments as sortie or reserve; reserve units remain in the candidate list but are excluded from board occupancy, drawing, selection, AI targeting, and living-unit victory checks. Non-controllable player units can act as protected escort targets: enemies can attack them and conditions can reference them, but they are skipped by player selection, Armory, Formation, counterattacks, and campaign roster progression snapshots when `persist_progression` is false. Scenario `ai_target_priority` nudges enemy movement and damage-skill scoring toward important targets such as envoys without overriding defeat bonuses or range checks. Pre-battle Formation uses the loaded scenario's `formation.cells` and only mutates current battle unit positions before battle-begin events fire. Destination victory cells from `unit_reaches_tile` conditions are exposed to the scene for objective overlays and tile info. Movement-triggered `unit_reaches_tile` events receive the unit that just moved, so ambushes and pickup events fire on entry rather than from units already standing on a marker. Briefing data can append matching `briefing.conditional_lines`, and scenario events can use `when.campaign_flags` to branch dialogue, objective changes, item pickups, and reinforcements from saved campaign choices. Consumable use can restore HP or MP or cure matching poison/seal/snare/disarm statuses, and in-battle item use, event pickup grants, plus equipment swaps mutate only the battle copy until victory; defeats and restarts do not spend saved items, keep picked-up items, or preserve in-battle gear changes. Post-battle choices write campaign `flags` only after the result-panel button is pressed; if the player reloads after rewards are saved but before selecting, the pending scenario id restores the victory choice panel without replaying rewards. The next battle button stays locked until the campaign choice save succeeds. Equipment rewards share the same `item_id -> count` inventory stock, cover weapon, armor, and accessory slots, can be equipped from the side HUD before the selected unit moves or acts, and do not appear in the consumable action menu. Already completed scenarios can be replayed without granting duplicate rewards, choices, replay inventory consumption, save writes, current-scenario advancement, or finite-stock purchases; pre-battle Shop, Armory, Roster, Formation, and Save are disabled on completed-scenario replays to avoid side effects. -The battle scene owns presentation feedback: briefing and result states use menu BGM, briefing headers combine `CampaignState` chapter ranges, campaign order, and title data with `BattleState` briefing and objective text, active battles use battle BGM, dialogue lines can show optional portrait textures or speaker-initial fallback panels and can place the portrait on the left or right side of the dialogue panel, board units render low-HP warning rings, HP bar color states, and active support/debuff/poison/seal/snare/disarm status pips from battle state fields, hover previews are rendered as target badges from existing `BattleState` forecast APIs, log/result hooks trigger placeholder SFX, support, debuff, poison, seal, snare, disarm, and item pickup effects use distinct floating feedback colors, `BattleState.unit_motion_requested` asks the scene to interpolate a unit's draw position during movement, and `BattleState.combat_feedback_requested` asks the scene to draw transient floating combat text without changing battle rules. +The battle scene owns presentation feedback: briefing and result states use menu BGM, briefing headers combine `CampaignState` chapter ranges, campaign order, and title data with `BattleState` briefing and objective text, active battles use battle BGM, dialogue lines can show optional portrait textures or speaker-initial fallback panels and can place the portrait on the left or right side of the dialogue panel, board units render low-HP warning rings, HP bar color states, and active support/debuff/poison/seal/snare/disarm status pips from battle state fields, hover previews are rendered as target badges from existing `BattleState` forecast APIs, the Threat toggle overlays enemy physical and hostile tactic reach while tile info names threat sources, log/result hooks trigger placeholder SFX, support, debuff, poison, seal, snare, disarm, and item pickup effects use distinct floating feedback colors, `BattleState.unit_motion_requested` asks the scene to interpolate a unit's draw position during movement, and `BattleState.combat_feedback_requested` asks the scene to draw transient floating combat text without changing battle rules. The `New Campaign` button clears `user://campaign_save.json`, resets campaign state to the first scenario, and reloads the opening briefing. diff --git a/docs/GODOT_4_6_MIGRATION.md b/docs/GODOT_4_6_MIGRATION.md index da57729..46066d1 100644 --- a/docs/GODOT_4_6_MIGRATION.md +++ b/docs/GODOT_4_6_MIGRATION.md @@ -47,6 +47,7 @@ powershell -NoProfile -ExecutionPolicy Bypass -File tools\check_godot46_readines - Manual checkpoint save/load restores the expected current briefing without script errors. - A player unit can move, attack, wait, use a tactic, use an item, and equip compatible gear. - Movement-triggered supply cache events grant battle inventory items with pickup feedback, and the items persist only after victory. +- The Threat toggle or `Y` key shows enemy physical and hostile tactic reach, and hovered threatened tiles name the threatening units. - March Order and Hamper Order adjust movement range while active and expire with the normal support/debuff timing. - Focus Order and Feint Order adjust physical hit chance through AGI bonuses or penalties and update attack forecasts while active. - Poison Mist applies poison, poison ticks at the target team's phase start, and poison defeat immediately ends the battle when it satisfies a victory or defeat condition. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index fda7095..e3a3f61 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -13,6 +13,7 @@ - Select player units. - Show move and attack ranges. - Show hover tile and unit information. +- Enemy threat range overlay with tile-level threat source hints. - Show basic damage forecast. - Move, attack, wait, and end turn. - Enemy units take AI turns with movement, attacks, and basic tactic use. @@ -133,7 +134,7 @@ - Portrait pipeline. Officer definitions can provide default portrait paths, dialogue lines can override them, and missing art falls back to speaker initials. - Unit sprites and animations. Board movement now has a short slide animation, and board units show low-HP warning rings plus small status pips for active support, debuff, poison, seal, snare, and disarm effects. -- Battle effects. First floating combat text now covers damage, recovery, misses, support effects, poison ticks, item pickups, level-ups, and promotions. Hover target preview badges summarize attacks, tactics, and items. +- Battle effects. First floating combat text now covers damage, recovery, misses, support effects, poison ticks, item pickups, level-ups, and promotions. Hover target preview badges summarize attacks, tactics, and items, and the board can overlay enemy threat ranges. - Music and sound. Basic placeholder BGM and SFX now play for menus, battle flow, unit actions, tactics, items, and result states. - Chapter UI and visual novel scenes. Pre-battle briefing now uses campaign chapter ranges for chapter-aware numbering, has a chapter progress/replay overview, and dialogue lines can choose left or right portrait placement. diff --git a/scripts/core/battle_state.gd b/scripts/core/battle_state.gd index a9d9001..4df8fd0 100644 --- a/scripts/core/battle_state.gd +++ b/scripts/core/battle_state.gd @@ -1223,6 +1223,28 @@ func get_item_target_cells(unit_id: String, item_id: String) -> Array[Vector2i]: return _item_target_cells_from(unit, item) +func get_threat_cells(source_team := TEAM_ENEMY) -> Array[Vector2i]: + var result: Array[Vector2i] = [] + for unit in get_living_units(source_team): + for cell in _unit_threat_cells(unit): + if not result.has(cell): + result.append(cell) + return result + + +func get_threatening_unit_names(cell: Vector2i, source_team := TEAM_ENEMY) -> Array[String]: + var result: Array[String] = [] + if not is_inside(cell): + return result + for unit in get_living_units(source_team): + if not _unit_threat_cells(unit).has(cell): + continue + var unit_name := str(unit.get("name", unit.get("id", "Unit"))) + if not result.has(unit_name): + result.append(unit_name) + return result + + func get_objective_cells() -> Array[Vector2i]: var result: Array[Vector2i] = [] _collect_objective_cells(battle_conditions.get("victory", {}), result) @@ -1685,6 +1707,93 @@ func _movement_range_for_unit(unit: Dictionary) -> Array[Vector2i]: return result +func _unit_threat_cells(unit: Dictionary) -> Array[Vector2i]: + var result: Array[Vector2i] = [] + if unit.is_empty() or not unit.get("alive", false) or not unit.get("deployed", true): + return result + if not bool(unit.get("controllable", true)): + return result + + for cell in _unit_physical_threat_cells(unit): + if not result.has(cell): + result.append(cell) + for cell in _unit_skill_threat_cells(unit): + if not result.has(cell): + result.append(cell) + return result + + +func _unit_threat_origins(unit: Dictionary) -> Array[Vector2i]: + var result: Array[Vector2i] = [] + var origin: Vector2i = unit.get("pos", Vector2i(-1, -1)) + if not is_inside(origin): + return result + result.append(origin) + for cell in _movement_range_for_unit(unit): + if not result.has(cell): + result.append(cell) + return result + + +func _unit_physical_threat_cells(unit: Dictionary) -> Array[Vector2i]: + var result: Array[Vector2i] = [] + if _unit_has_action_lock(unit, "attack"): + return result + for origin in _unit_threat_origins(unit): + for cell in _attack_cells_from(unit, origin): + if not result.has(cell): + result.append(cell) + return result + + +func _unit_skill_threat_cells(unit: Dictionary) -> Array[Vector2i]: + var result: Array[Vector2i] = [] + if _unit_has_action_lock(unit, "skill"): + return result + if typeof(unit.get("skills", [])) != TYPE_ARRAY: + return result + for skill_id_value in unit.get("skills", []): + var skill_id := str(skill_id_value) + var skill := get_skill_def(skill_id) + if skill.is_empty(): + continue + if int(unit.get("mp", 0)) < int(skill.get("mp_cost", 0)): + continue + if not _skill_threatens_opponents(skill): + continue + for origin in _unit_threat_origins(unit): + for cell in _skill_cells_from_origin(unit, skill, origin): + if not result.has(cell): + result.append(cell) + return result + + +func _skill_threatens_opponents(skill: Dictionary) -> bool: + var target_rule := str(skill.get("target", "enemy")) + if target_rule != "enemy" and target_rule != "any": + return false + var skill_kind := str(skill.get("kind", "damage")) + if skill_kind == "damage": + return true + if skill_kind == "support": + return _skill_has_harmful_support_effects(skill) + return false + + +func _skill_has_harmful_support_effects(skill: Dictionary) -> bool: + for effect in skill.get("effects", []): + if typeof(effect) != TYPE_DICTIONARY: + continue + var effect_type := str(effect.get("type", "")) + if effect_type == "damage_over_time" and int(effect.get("amount", 0)) > 0: + return true + if effect_type == "action_lock" and not str(effect.get("action", "")).strip_edges().is_empty(): + return true + if effect_type == "stat_bonus" and int(effect.get("amount", 0)) < 0: + return true + return false + + func _attack_cells_from(unit: Dictionary, origin: Vector2i) -> Array[Vector2i]: var result: Array[Vector2i] = [] var attack_range := int(unit.get("range", 1)) @@ -1707,11 +1816,14 @@ func _is_in_attack_range(attacker: Dictionary, target_cell: Vector2i) -> bool: func _skill_cells_from(unit: Dictionary, skill: Dictionary) -> Array[Vector2i]: + return _skill_cells_from_origin(unit, skill, unit["pos"]) + + +func _skill_cells_from_origin(_unit: Dictionary, skill: Dictionary, origin: Vector2i) -> Array[Vector2i]: var result: Array[Vector2i] = [] var range_pair := _skill_range_pair(skill) var max_range := int(range_pair["max"]) var min_range := int(range_pair["min"]) - var origin: Vector2i = unit["pos"] for y in range(origin.y - max_range, origin.y + max_range + 1): for x in range(origin.x - max_range, origin.x + max_range + 1): var cell := Vector2i(x, y) diff --git a/scripts/scenes/battle_scene.gd b/scripts/scenes/battle_scene.gd index d0abe55..f376ec6 100644 --- a/scripts/scenes/battle_scene.gd +++ b/scripts/scenes/battle_scene.gd @@ -13,6 +13,8 @@ const SKILL_OVERLAY_COLOR := Color(0.56, 0.28, 0.95, 0.28) const ITEM_OVERLAY_COLOR := Color(0.24, 0.74, 0.34, 0.30) const FORMATION_OVERLAY_COLOR := Color(0.95, 0.76, 0.25, 0.34) const OBJECTIVE_OVERLAY_COLOR := Color(1.0, 0.78, 0.18, 0.30) +const THREAT_OVERLAY_COLOR := Color(1.0, 0.12, 0.10, 0.18) +const THREAT_BORDER_COLOR := Color(1.0, 0.16, 0.12, 0.44) const BGM_MENU_PATH := "res://audio/bgm/menu_theme_placeholder.wav" const BGM_BATTLE_PATH := "res://audio/bgm/battle_loop_placeholder.wav" const SFX_BOW_RELEASE_PATH := "res://audio/sfx/bow_release_01.wav" @@ -48,8 +50,10 @@ var move_cells: Array[Vector2i] = [] var attack_cells: Array[Vector2i] = [] var skill_cells: Array[Vector2i] = [] var item_cells: Array[Vector2i] = [] +var threat_cells: Array[Vector2i] = [] var selected_skill_id := "" var selected_item_id := "" +var show_threat_overlay := false var active_scenario_id := "" var battle_started := false var battle_result_applied := false @@ -124,6 +128,7 @@ var item_list: VBoxContainer var equip_button: Button var equip_menu: VBoxContainer var equip_list: VBoxContainer +var threat_button: Button var restart_button: Button var new_campaign_button: Button var log_box: RichTextLabel @@ -280,6 +285,12 @@ func _create_hud() -> void: equip_button.pressed.connect(_on_equip_pressed) button_row.add_child(equip_button) + threat_button = Button.new() + threat_button.text = "Threat" + threat_button.toggle_mode = true + threat_button.pressed.connect(_on_threat_pressed) + button_row.add_child(threat_button) + end_turn_button = Button.new() end_turn_button.text = "End Turn" end_turn_button.pressed.connect(_on_end_turn_pressed) @@ -875,6 +886,8 @@ func _handle_key(event: InputEventKey) -> void: _on_item_pressed() elif event.keycode == KEY_E and not _is_input_locked(): _on_equip_pressed() + elif event.keycode == KEY_Y and not _is_prep_menu_visible(): + _on_threat_pressed() func _process(delta: float) -> void: @@ -932,6 +945,11 @@ func _draw_overlays() -> void: draw_rect(objective_rect, OBJECTIVE_OVERLAY_COLOR) draw_rect(objective_rect.grow(-5.0), Color(1.0, 0.88, 0.30, 0.82), false, 2.0) + for cell in threat_cells: + var threat_rect := _rect_for_cell(cell) + draw_rect(threat_rect, THREAT_OVERLAY_COLOR) + draw_rect(threat_rect.grow(-6.0), THREAT_BORDER_COLOR, false, 1.5) + for cell in move_cells: draw_rect(_rect_for_cell(cell), Color(0.16, 0.55, 0.95, 0.28)) @@ -1328,6 +1346,9 @@ func _refresh_ranges() -> void: attack_cells.clear() skill_cells.clear() item_cells.clear() + threat_cells.clear() + if show_threat_overlay and battle_started and state.battle_status == BattleState.STATUS_ACTIVE: + threat_cells = state.get_threat_cells(BattleState.TEAM_ENEMY) var selected := state.get_selected_unit() if selected.is_empty(): selected_skill_id = "" @@ -1403,6 +1424,9 @@ func _update_hud() -> void: end_turn_button.disabled = not state.can_player_act() or _is_input_locked() restart_button.disabled = campaign_complete_screen + if threat_button != null: + threat_button.button_pressed = show_threat_overlay + threat_button.disabled = not battle_started or state.battle_status != BattleState.STATUS_ACTIVE or campaign_complete_screen func _update_cell_info() -> void: @@ -1422,6 +1446,10 @@ func _update_cell_info() -> void: ] if state.get_objective_cells().has(cell): text += "\nObjective marker" + if show_threat_overlay: + var threat_names := state.get_threatening_unit_names(cell, BattleState.TEAM_ENEMY) + if not threat_names.is_empty(): + text += "\nThreatened by %s" % _compact_name_list(threat_names, 3) var unit: Dictionary = summary["unit"] if not unit.is_empty(): @@ -1448,6 +1476,16 @@ func _update_cell_info() -> void: cell_info_label.text = text +func _compact_name_list(names: Array[String], limit: int) -> String: + var visible_names: Array[String] = [] + var safe_limit: int = maxi(1, limit) + for index in range(mini(names.size(), safe_limit)): + visible_names.append(names[index]) + if names.size() > safe_limit: + visible_names.append("+%d" % (names.size() - safe_limit)) + return _join_strings(visible_names, ", ") + + func _update_forecast() -> void: var selected := state.get_selected_unit() if selected.is_empty() or not state.is_inside(hover_cell): @@ -1963,6 +2001,17 @@ func _on_equip_pressed() -> void: queue_redraw() +func _on_threat_pressed() -> void: + if not battle_started or state.battle_status != BattleState.STATUS_ACTIVE or campaign_complete_screen: + if threat_button != null: + threat_button.button_pressed = show_threat_overlay + return + show_threat_overlay = not show_threat_overlay + _refresh_ranges() + _update_hud() + queue_redraw() + + func _on_restart_pressed() -> void: if campaign_complete_screen: return