Combat System¶
The combat module provides a turn-based combat system with extensible actions, targeting, and damage calculation.
Combat Engine¶
Turn-based combat system.
This module provides the core combat management system.
- class barebones_rpg.combat.combat.CombatState(value)[source]¶
Bases:
EnumStates of combat.
- NOT_STARTED = 1¶
- TURN_START = 2¶
- PLAYER_TURN = 3¶
- ENEMY_TURN = 4¶
- TURN_END = 5¶
- VICTORY = 6¶
- DEFEAT = 7¶
- FLED = 8¶
- class barebones_rpg.combat.combat.CombatantGroup(**data)[source]¶
Bases:
BaseModelA group of combatants (party or enemy group).
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- is_defeated()[source]¶
Check if entire group is defeated.
- Return type:
- Returns:
True if all members are dead
- class barebones_rpg.combat.combat.TurnOrder(**data)[source]¶
Bases:
BaseModelManages turn order based on speed.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class barebones_rpg.combat.combat.Combat(player_group, enemy_group, events=None)[source]¶
Bases:
objectTurn-based combat manager.
Manages combat between two groups (players vs enemies), handles turn order, and processes combat actions.
Example
>>> from barebones_rpg.entities import Character, Enemy >>> from barebones_rpg.core import EventManager >>> from barebones_rpg.party import Party >>> >>> hero = Character(name="Hero", stats=Stats(hp=100, atk=15)) >>> goblin = Enemy(name="Goblin", stats=Stats(hp=30, atk=5)) >>> >>> # Using lists (backward compatible) >>> combat = Combat( ... player_group=[hero], ... enemy_group=[goblin], ... events=EventManager() ... ) >>> >>> # Or using Party objects >>> party = Party(name="Heroes", members=[hero]) >>> enemies = Party(name="Monsters", members=[goblin]) >>> combat = Combat( ... player_group=party, ... enemy_group=enemies, ... events=EventManager() ... ) >>> combat.start() >>> # Combat is now active
- execute_action(action, source, targets)[source]¶
Execute a combat action.
- Parameters:
action (
CombatAction) – The action to executesource (
Any) – Entity performing the actiontargets (
List[Any]) – List of target entities (can be empty for self-targeting actions)
- Return type:
- Returns:
Result of the action
- is_player_turn()[source]¶
Check if it’s a player’s turn.
- Return type:
- Returns:
True if current turn is a player
Combat Actions¶
Combat actions and effects.
This module defines the actions that can be taken during combat.
- class barebones_rpg.combat.actions.ActionType(value)[source]¶
Bases:
EnumTypes of combat actions.
- ATTACK = 1¶
- SKILL = 2¶
- ITEM = 3¶
- RUN = 4¶
- CUSTOM = 5¶
- class barebones_rpg.combat.actions.ActionResult(**data)[source]¶
Bases:
BaseModelResult of a combat action.
Contains information about what happened when an action was performed. Supports both single-target and multi-target actions.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class barebones_rpg.combat.actions.CombatAction(action_type=ActionType.CUSTOM)[source]¶
Bases:
ABCBase class for combat actions.
All combat actions inherit from this and implement execute(). Actions now accept a list of targets to support both single-target and multi-target (AOE) abilities.
- class barebones_rpg.combat.actions.AttackAction[source]¶
Bases:
CombatActionBasic physical attack action.
- calculate_damage(source, target, weapon, context)[source]¶
Calculate base damage before crits and defense.
Override this method to add proficiency systems or other damage modifiers.
- class barebones_rpg.combat.actions.SkillAction(name, mp_cost, effect, targets_enemy=True)[source]¶
Bases:
CombatActionSpecial skill/ability action.
This is a flexible action that can be customized.
- class barebones_rpg.combat.actions.ItemAction(item)[source]¶
Bases:
CombatActionUse an item during combat.
- class barebones_rpg.combat.actions.RunAction[source]¶
Bases:
CombatActionAttempt to flee from combat.
- barebones_rpg.combat.actions.create_attack_action()[source]¶
Create a basic attack action.
- Return type:
- barebones_rpg.combat.actions.create_skill_action(name, mp_cost, damage_multiplier=1.5, damage_type='physical', targets_enemy=True, max_targets=1)[source]¶
Create a damage skill.
- Parameters:
name (
str) – Skill namemp_cost (
int) – MP costdamage_multiplier (
float) – Damage multiplier vs normal attackdamage_type (
str) – Type of damage (physical, magic, or custom)targets_enemy (
bool) – Whether skill targets enemiesmax_targets (
int) – Maximum number of targets to hit (1=single target, 2=cleave, None=unlimited AOE)
- Return type:
- Returns:
Skill action
Example
>>> # Single-target skill >>> fireball = create_skill_action("Fireball", mp_cost=10, damage_multiplier=2.0, damage_type="magic") >>> >>> # Cleave attack (hits 2 targets) >>> cleave = create_skill_action("Cleave", mp_cost=5, damage_multiplier=1.2, max_targets=2) >>> >>> # AOE skill that hits all targets >>> meteor = create_skill_action("Meteor", mp_cost=25, damage_multiplier=1.5, damage_type="magic", max_targets=None)
- barebones_rpg.combat.actions.create_heal_skill(name, mp_cost, heal_amount, max_targets=1)[source]¶
Create a healing skill.
- Parameters:
- Return type:
- Returns:
Healing skill action
Example
>>> # Single-target heal >>> cure = create_heal_skill("Cure", mp_cost=5, heal_amount=30) >>> >>> # Group heal (heals 3 targets) >>> group_heal = create_heal_skill("Group Heal", mp_cost=15, heal_amount=20, max_targets=3) >>> >>> # Mass heal (heals all targets) >>> mass_heal = create_heal_skill("Mass Heal", mp_cost=25, heal_amount=15, max_targets=None)
Targeting¶
Targeting and distance calculation utilities for combat.
This module provides optional helper functions for distance calculations and target filtering. These are completely optional - games can use their own distance calculations or ignore positioning entirely.
- barebones_rpg.combat.targeting.manhattan_distance(pos1, pos2)[source]¶
Calculate Manhattan (grid-based) distance between two positions.
Manhattan distance is the sum of horizontal and vertical distances, useful for grid-based movement where diagonal movement isn’t allowed or costs the same as orthogonal movement.
- Parameters:
- Return type:
- Returns:
Manhattan distance as integer
Example
>>> manhattan_distance((0, 0), (3, 4)) 7 >>> manhattan_distance((5, 5), (5, 8)) 3
- barebones_rpg.combat.targeting.euclidean_distance(pos1, pos2)[source]¶
Calculate Euclidean (straight-line) distance between two positions.
Euclidean distance is the straight-line distance between two points, useful for more realistic distance calculations or line-of-sight checks.
- Parameters:
- Return type:
- Returns:
Euclidean distance as float
Example
>>> euclidean_distance((0, 0), (3, 4)) 5.0 >>> euclidean_distance((0, 0), (1, 1)) 1.414...
- barebones_rpg.combat.targeting.chebyshev_distance(pos1, pos2)[source]¶
Calculate Chebyshev distance between two positions.
Chebyshev distance is the maximum of horizontal and vertical distances, useful for grid-based games where diagonal movement costs the same as orthogonal movement (8-directional movement).
- Parameters:
- Return type:
- Returns:
Chebyshev distance as integer
Example
>>> chebyshev_distance((0, 0), (3, 4)) 4 >>> chebyshev_distance((5, 5), (8, 7)) 3
- barebones_rpg.combat.targeting.is_in_range(source, target, range_value, distance_func=<function manhattan_distance>)[source]¶
Check if a target is within range of the source.
This is a helper function that checks if both entities have position attributes and calculates if the target is within the specified range.
- Parameters:
- Return type:
- Returns:
True if target is in range, False otherwise
Example
>>> class Entity: ... def __init__(self, pos): ... self.position = pos >>> player = Entity((0, 0)) >>> enemy = Entity((2, 0)) >>> is_in_range(player, enemy, range_value=3) True >>> is_in_range(player, enemy, range_value=1) False
- barebones_rpg.combat.targeting.filter_targets_by_range(source, targets, range_value, distance_func=<function manhattan_distance>)[source]¶
Filter a list of targets to only those within range of the source.
This is useful for AOE abilities that have a maximum range, or for validating that all selected targets are reachable.
- Parameters:
- Return type:
- Returns:
Filtered list of targets within range
Example
>>> class Entity: ... def __init__(self, pos): ... self.position = pos >>> player = Entity((0, 0)) >>> enemies = [Entity((1, 0)), Entity((5, 0)), Entity((2, 1))] >>> in_range = filter_targets_by_range(player, enemies, range_value=3) >>> len(in_range) 2
- barebones_rpg.combat.targeting.get_targets_in_area(center, radius, all_entities, distance_func=<function manhattan_distance>)[source]¶
Get all entities within a circular area.
This is useful for AOE effects centered on a position rather than an entity (e.g., a fireball explosion at a location).
- Parameters:
- Return type:
- Returns:
List of entities within the area
Example
>>> class Entity: ... def __init__(self, pos): ... self.position = pos >>> entities = [Entity((0, 0)), Entity((1, 1)), Entity((5, 5))] >>> targets = get_targets_in_area((0, 0), radius=2, all_entities=entities) >>> len(targets) 2
- barebones_rpg.combat.targeting.get_targets_in_line(start, end, all_entities, width=0)[source]¶
Get all entities in a line between two points.
This is useful for line-based AOE effects like beam attacks or penetrating shots. Uses Bresenham’s line algorithm.
- Parameters:
- Return type:
- Returns:
List of entities along the line
Example
>>> class Entity: ... def __init__(self, pos): ... self.position = pos >>> entities = [Entity((1, 0)), Entity((2, 0)), Entity((0, 1))] >>> targets = get_targets_in_line((0, 0), (3, 0), entities) >>> len(targets) 2
Damage Types¶
Damage type system with manager for flexible damage types and resistances.
This module provides a singleton manager for damage types, allowing games to register custom damage types with metadata while maintaining flexibility.
- class barebones_rpg.combat.damage_types.DamageTypeMetadata(**data)[source]¶
Bases:
BaseModelMetadata for a damage type.
- model_config: ClassVar[ConfigDict] = {'extra': 'allow'}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class barebones_rpg.combat.damage_types.DamageTypeManager(*args, **kwargs)[source]¶
Bases:
objectGlobal manager for damage types (Singleton).
This manager handles all damage types in the game. It supports: - Pre-registration of common types - Metadata attachment (colors, descriptions, tags) - Lenient mode (auto-registers unknown types) - Query capabilities
Example
>>> # Register a custom damage type >>> DamageTypeManager().register("necrotic", color="green", description="Death magic") >>> >>> # Check if registered >>> DamageTypeManager().is_registered("necrotic") True >>> >>> # Get metadata >>> meta = DamageTypeManager().get_metadata("necrotic") >>> print(meta.color) green >>> >>> # Get all registered types >>> types = DamageTypeManager().get_all() >>> print(types) ['physical', 'magic', 'fire', 'ice', 'poison', 'lightning', 'dark', 'holy', 'necrotic']
- register(damage_type, color=None, description=None, tags=None, **kwargs)[source]¶
Register a damage type with optional metadata.
- get_metadata(damage_type)[source]¶
Get metadata for a damage type.
- Parameters:
damage_type (
str) – Name of the damage type- Return type:
- Returns:
Metadata object or None if not registered
- get_all_with_metadata()[source]¶
Get all damage types with their metadata.
- Return type:
- Returns:
Dictionary mapping damage type names to metadata
- ensure_registered(damage_type)[source]¶
Ensure a damage type is registered, auto-registering if in lenient mode.
This is called internally when an unknown damage type is encountered. In lenient mode, it auto-registers with a warning. In strict mode, it raises an error.
- Parameters:
damage_type (
str) – Name of the damage type- Raises:
ValueError – If damage type not registered and not in lenient mode
- Return type: