Battles ~ Design Document - uchicago-cs/chiventure GitHub Wiki
Overview
Team: Alex Sheen, Celia Anderson, Isaaq Khader, Nathaniel Martinez, Sophie Veys
Below is the design document for RPG Battle Systems. Each of the four modules, the Battle Flow, Logic Handler, Battle State, and AI modules, are detailed below with a description of functions, structs, and relevant files. There is an additional section explaining all relevant structs in battle_common.h
and battle_structs.h
.
Though we were able to complete a lot this quarter, there were some things we wished have been able to add which would have enhanced the battle system or allowed for more thorough integration with Chiventure. In order to completely integrate with Chiventure, our team still needs to communicate with CLI or action management give control of the game to our battle flow control loop. As for enhancements, in the future hopefully more intricate AI modes can be used outside of what we currently have (greedy & random). In addition, we would also like to fully implement items being used in battle. We started working on this but found it more important to integrate moves first.
General Structs
Relevant Files:
battle_common.h
battle_structs.h
Structs:
-
battle_item_t
: A doubly-linked list containing all information for an item to be used in battle.int id
: The ID number used to differentiate between items.bool is_weapon
: Clarifies if the item is a weapon.int effectiveness_decrement
: the value to decrement durability with each use.int quantity
: The number of this item in a player's inventory.int durability
: The durability of the item (not implemented yet).char* name
: The name of the item.char* description
: A short description of the item.bool battle
: Clarifies if the item can be used in battle.int attack
: The attack boost the item will give to combatant if used.int defense
: The defense boost the item will give to combatant if used.int hp
: The HP boost the item will give to combatant if used.struct item *next
: The next item in the inventory. NULL if last item.struct item *prev
: The previous item in the inventory. If first item, it points to HEAD.
-
move_t
: A doubly-linked list containing all information for a move that can be used in battle.battle_item_t *item
: The item to be used in a move.int id
: The ID number used to differentiate between moves.char* info
: A short description of the move.bool attack
: Clarifies if the item can be used in battle.int damage
: The power of the move, NOT the damage dealt.int defense
: The defense value of the move.struct move *next
: The next move in the list. NULL if last move.struct move *prev
: The previous move in the list. If first move, it points to HEAD.
-
stat_t
: A struct containing all information related to a combatant's attributes.int speed
: The combatant's speed. Used to determine who goes first in battle.int defense
: The defense value of the player. Used in damage calculations.int dexterity
: The dexterity of the player. Not implemented yet.int hp
: The current HP of the combatant.int max_hp
: The max HP of the combatant.int xp
: The current XP of the combatant.int level
: The current level of the combatant.
-
stat_changes_t
: A struct containing all information related to a combatant's attributes.int turns_left
: the number of turns the status effect has remaining- The rest of the fields are identical to
stat_t
-
enum difficulty_t
: Used as an input ingive_move()
,enum difficulty
specifies the type of move to be returned.BATTLE_AI_NONE
: No AI setting.BATTLE_AI_GREEDY
: Return the move that will deal the most damage.BATTLE_AI_RANDOM
: Return a random move.
-
enum environment_t
: An enum denoting what environment the battle is taking place in. Effects surrounding this struct have not yet been implemented.ENV_NONE
: There is no environment.ENV_GRASS
: It is a grassy environment.ENV_DESERT
: It is a desert environment.ENV_SNOW
: It is a snowy environment.ENV_WATER
: It is an aquatic environment.
-
enum turn_t
: An enum denoting whose turn it is in battle.PLAYER
: It is the player's turn.ENEMY
: It is the enemy's turn.
-
enum battle_status_t
: Specifies the current state of battle; return value ofbattle_over()
.BATTLE_IN_PROGRESS
: The battle is currently ongoing.BATTLE_VICTOR_PLAYER
: The battle has been won by the player.BATTLE_VICTOR_ENEMY
: The battle has been won by the enemy.NO_BATTLE
: There is currently no battle.
-
combatant_t
: A doubly-linked list struct that contains all information about combatants in battle.char* name
: The combatant's name.bool is_friendly
: Boolean differentiating between an enemy and a player.class_t *class
: The combatant's class. Refer toplayerclass/class_structs.h
for more information.stat_t *stats
: The stats of the combatant.move_t *moves
: The available moves to the combatant.battle_item_t *items
: The combatant's inventory.struct combatant *next
: The next combatant; NULL if last combatant. Adds support for battles between more than 1 enemy or player, but this has not been implemented yet.struct combatant *prev
: The previous combatant; if first move, points to HEAD.
-
battle_t
: The struct containing all information about both combatants, environment, and the current turn.combatant_t *player
: The friendly combatant. The user will play as the player.combatant_t *enemy
: The enemy combatant. An AI will control the enemy.environment_t environment
: The environment the battle is taking place in.turn_t turn
: The player whose turn it is.
Battle Flow Module
Relevant Files:
battle_flow.c
battle_flow.h
battle_flow_structs.c
battle_flow_structs.h
battle_print.c
battle_print.h
Description:
The Battle Flow module depends on the rest of the modules to control what happens in the battle in runtime. The functions in Battle Flow are at a higher level, so functions can make use of the more specific functions in the lower modules to carry out each step of the battle. The battle_print
files, though not initially part of our design, were included in this module because they keep track of the battle flow by returning messages relevant to the current battle.
Structs:
-
battle_player_t
: The player struct in the general chiventure game.char* player_id
: A short description of the player.class_t* class
: The class of the player.stat_t* stats
: The stats of the player.move_t* moves
: The moves of the player.battle_item_t* items
: The inventory of the player
-
battle_game_t
: Struct representing the game for game-statebattle_player_t* player
: The player.battle_t* battle
: The battle.
-
chiventure_ctx_battle_t
: Similar to chiventure_ctx_t, but with an additional field for status.battle_game t* game
: The current game.battle_status_t status
: The status of the current battle.
Functions:
start_battle()
: Begins the battle and initializes all required structs usingset_battle()
.set_battle_player()
: Converts a chiventure player struct into a combatant struct.set_enemy()
: Sets up all enemy combatant structs for a new battle.set_battle()
: Initializes all required structs.battle_flow_move()
: Takes in a move and target, and executes the intended move, returns a string with the results of executing the movebattle_flow_item()
: Currently works with consumables, Takes in an item and allows the player to consume it, also return a string containing info about the changes.battle_flow_list()
: Takes in a string currently only works for "moves, and items" and returns a string with the appropriate list.enemy_make_move()
: Helper function for battle_flow.new_ctx_player()
: Allocates memory for and initializes values for a new player struct.new_battle_game()
: Allocates memory for and initializes values for a new game struct.print_start_battle()
: Returns a message to be printed at the start of the battle.print_battle_move()
: Returns a message describing a combatant's move.print_hp()
: Returns a message describing a combatant's HP.print_battle_winner()
: Returns a message describing the combatant who won and the amount of XP gained.
All battle_flow_[] functions currently work by completing actions and and returning strings to be printed to the CLI
Battle State Module
Relevant Files:
battle_state.c
battle_state.h
battle_moves.c
battle_moves.h
Description:
The Battle State Module initializes, allocates, and frees memory for all of the structs needed for battle. This includes things like the battle_t
, move_t
and combatant_t
structs. The moves files were separate from the state files since we had to work in close partnership with player class to create class-specific moves.
Functions:
combatant_new()
: Allocates memory for and callscombatant_init()
to initialize thecombatant_t
struct.combatant_init()
: Initializes values in thecombatant_t
struct.combatant_free()
: Frees the allocated memory for 1 combatant in acombatant_t
struct.combatant_free_all()
: Frees all combatants in the doubly-linkedcombatant_t
struct.stat_changes_new()
: Initialize an empty stat_changes struct.stat_changes_init()
: Creates a new stat_changes struct.stat_changes_free_node()
: Frees a stat_changes struct from memory.stat_changes_add_node()
: Creates an empty stat_changes node at the end of a given stat_changes_t struct.stat_changes_free_all()
: Frees a list of stat_changes structs from memory.stat_changes_remove_node()
: Removes a given stat_changes node from the list it's in.stat_changes_turn_increment()
: Decrements all turn counts in the stat_changes struct by 1.stat_changes_undo()
: Undoes the temporary stat changes.stat_changes_append_node()
: Appends a node to the end of a stat_changes struct.battle_new()
: Allocates memory for and callsbattle_init()
to initialize thebattle_t
struct.battle_init()
: Initializes values in thebattle_t
struct.battle_free()
: Frees allocated memory for abattle_t
struct.move_new()
: Allocates memory for and callsmove_init()
to initialize themove_t
struct.move_init()
: Initializes values in themove_t
struct.move_free()
: Frees allocated memory for amove_t
struct
Logic Handler Module
Relevant Files:
battle_logic.c
battle_logic.h
Description:
The Logic Handler module reads and updates the battle state during battle by managing those functions accordingly. Some functions help keep the battle flow in place by checking if the battle is over or determining what player gets the first move, while other functions use battle logic to manipulate the battle state directly (ex: using an item and applying the item's effects to the stat_t
struct).
Functions:
check_target()
: Checks the targets of a move to see if they exist and are targetablebattle_over()
: Checks if battle is over; returns abattle_status_t
struct.goes_first()
: Determines what player goes first; combatant with the higher speed stat goes first.find_battle_item()
: Finds the desired item in the given combatant's inventory.consume_battle_item()
: Applies the effects of the item to the combatant's stat struct.use_battle_item()
: Callsconsume_item()
and decrements the quantity of the item by 1.remove_battle_item()
: Removes a battle item from a combatant's list of battle items.award_xp()
: Awards XP to a player by increasing the XP stat.stat_changes_add_item_node()
: Adds new temporary status changes from an item.
AI Module
Relevant Files:
battle_ai.c
battle_ai.h
Description:
The AI module deals with functions specific to only the AI player. General functions that deal with move effects and applying damage that can apply to both the player and the enemy AI are in the Logic Handler. The AI module focuses on determining the AI’s moves systematically or randomly, depending on the difficulty level of the game.
Functions:
give_move()
: This is the main function of the AI Module.give_move()
will call the appropriate functions (below) to return a move consistent with the specifications of the difficulty enum input. This function will be called during battle to simulate an AI.find_random()
: Returns a random enemy move; called bygive_move()
.find_greedy()
: Returns the move that will deal the most damage to the player; called bygive_move()
.damage()
: Calculates the damage to be dealt by enemy to player but does NOT apply damage; used byfind_greedy()
to find the greediest move.
Default Module
Relevant Files:
battle_default_objects.h
battle_default_objects.c
Description:
The default module contains five functions that help to generate random default battle_item_t items (consumables or weapons), as well as default moves, and stats. This is in case the developer does not want to customize their own items we can provide a small selection of default ones.
Functions:
randnum()
: This generates a random integer between two boundsget_random_default_consumable()
: Generates a random consumable item from a list of previously-initialized consumables.get_random_default_weapon()
: Generates a random weapon item from a list of previously-initialized weapons.get_random_default_move()
: Generates a random move from a list of previously-initialized moves.get_random_default_stat()
: Generates a random stat from a list of previously-initialized stats.