Skip to content

Commit

Permalink
Script variable: TOTAL_TRAPS (#3391)
Browse files Browse the repository at this point in the history
Also TOTAL_TRAPS, TOTAL_DOORS and TOTAL_CREATURES work on IF_AVAILABLE
  • Loading branch information
Loobinex authored Aug 5, 2024
1 parent 9ade760 commit ecdaf2f
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 17 deletions.
18 changes: 16 additions & 2 deletions src/lvl_script_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@ const struct NamedCommand controls_variable_desc[] = {
{"TOTAL_DIGGERS", SVar_CONTROLS_TOTAL_DIGGERS},
{"TOTAL_CREATURES", SVar_CONTROLS_TOTAL_CREATURES},
{"TOTAL_DOORS", SVar_TOTAL_DOORS},
{"TOTAL_TRAPS", SVar_TOTAL_TRAPS},
{"TOTAL_AREA", SVar_TOTAL_AREA},
{"GOOD_CREATURES", SVar_CONTROLS_GOOD_CREATURES},
{"EVIL_CREATURES", SVar_CONTROLS_EVIL_CREATURES},
{NULL, 0},
};

const struct NamedCommand available_variable_desc[] = {
{"TOTAL_CREATURES", SVar_AVAILABLE_TOTAL_CREATURES},
{"TOTAL_DOORS", SVar_AVAILABLE_TOTAL_DOORS},
{"TOTAL_TRAPS", SVar_AVAILABLE_TOTAL_TRAPS},
{"TOTAL_AREA", SVar_TOTAL_AREA},
{NULL, 0},
};

const struct NamedCommand comparison_desc[] = {
{"==", MOp_EQUAL},
{"!=", MOp_NOT_EQUAL},
Expand Down Expand Up @@ -373,6 +382,7 @@ const struct NamedCommand variable_desc[] = {
{"TOTAL_CREATURES", SVar_TOTAL_CREATURES},
{"TOTAL_RESEARCH", SVar_TOTAL_RESEARCH},
{"TOTAL_DOORS", SVar_TOTAL_DOORS},
{"TOTAL_TRAPS", SVar_TOTAL_TRAPS},
{"TOTAL_AREA", SVar_TOTAL_AREA},
{"TOTAL_CREATURES_LEFT", SVar_TOTAL_CREATURES_LEFT},
{"CREATURES_ANNOYED", SVar_CREATURES_ANNOYED},
Expand Down Expand Up @@ -4273,14 +4283,18 @@ static void if_available_check(const struct ScriptLine *scline)
}
}

long varib_type;
if (gameadd.script.conditions_num >= CONDITIONS_COUNT)
{
SCRPTERRLOG("Too many (over %d) conditions in script", CONDITIONS_COUNT);
return;
}
// Recognize variable
long varib_id = -1;
long varib_id;
long varib_type = get_id(available_variable_desc, varib_name);
if (varib_type == -1)
varib_id = -1;
else
varib_id = 0;
if (varib_id == -1)
{
varib_id = get_id(door_desc, varib_name);
Expand Down
22 changes: 11 additions & 11 deletions src/lvl_script_conditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ long get_condition_value(PlayerNumber plyr_idx, unsigned char valtype, unsigned
case SVar_TOTAL_DOORS:
dungeon = get_dungeon(plyr_idx);
return dungeon->total_doors;
case SVar_TOTAL_TRAPS:
return count_player_deployed_traps_of_model(plyr_idx, -1);
case SVar_TOTAL_AREA:
dungeon = get_dungeon(plyr_idx);
return dungeon->total_area;
Expand Down Expand Up @@ -186,22 +188,20 @@ long get_condition_value(PlayerNumber plyr_idx, unsigned char valtype, unsigned
}
return 0;
case SVar_AVAILABLE_TRAP: // IF_AVAILABLE(TRAP)
dungeon = get_dungeon(plyr_idx);
return dungeon->mnfct_info.trap_amount_stored[validx%game.conf.trapdoor_conf.trap_types_count]
+ dungeon->mnfct_info.trap_amount_offmap[validx%game.conf.trapdoor_conf.trap_types_count];
return count_player_available_traps_of_model(plyr_idx, (validx % game.conf.trapdoor_conf.trap_types_count));
case SVar_AVAILABLE_DOOR: // IF_AVAILABLE(DOOR)
dungeon = get_dungeon(plyr_idx);
return dungeon->mnfct_info.door_amount_stored[validx%game.conf.trapdoor_conf.door_types_count]
+ dungeon->mnfct_info.door_amount_offmap[validx%game.conf.trapdoor_conf.door_types_count];
return count_player_available_doors_of_model(plyr_idx, (validx % game.conf.trapdoor_conf.door_types_count));
case SVar_AVAILABLE_ROOM: // IF_AVAILABLE(ROOM)
dungeon = get_dungeon(plyr_idx);
return (dungeon->room_buildable[validx%game.conf.slab_conf.room_types_count] & 1);
case SVar_AVAILABLE_CREATURE: // IF_AVAILABLE(CREATURE)
dungeon = get_dungeon(plyr_idx);
if (creature_will_generate_for_dungeon(dungeon, validx)) {
return min(game.pool.crtr_kind[validx%game.conf.crtr_conf.model_count],dungeon->max_creatures_attracted - (long)dungeon->num_active_creatrs);
}
return 0;
return count_player_available_creatures_of_model(plyr_idx, (validx % game.conf.crtr_conf.model_count));
case SVar_AVAILABLE_TOTAL_TRAPS:
return count_player_available_traps_of_model(plyr_idx, -1);
case SVar_AVAILABLE_TOTAL_DOORS:
return count_player_available_doors_of_model(plyr_idx, -1);
case SVar_AVAILABLE_TOTAL_CREATURES:
return count_player_available_creatures_of_model(plyr_idx, CREATURE_ANY);
case SVar_SLAB_OWNER: //IF_SLAB_OWNER
{
long varib_id = get_slab_number((unsigned char)plyr_idx, validx);
Expand Down
4 changes: 4 additions & 0 deletions src/lvl_script_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ enum ScriptVariables {
SVar_ALLIED_PLAYER = 78,
SVar_ACTIVE_BATTLES = 79,
SVar_VIEW_TYPE = 80,
SVar_TOTAL_TRAPS = 81,
SVar_AVAILABLE_TOTAL_TRAPS = 82,
SVar_AVAILABLE_TOTAL_DOORS = 83,
SVar_AVAILABLE_TOTAL_CREATURES = 84,
};


Expand Down
17 changes: 17 additions & 0 deletions src/room_entrance.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ static long calculate_excess_attraction_for_creature(ThingModel crmodel, PlayerN
return excess_attraction;
}

long count_player_available_creatures_of_model(PlayerNumber plyr_idx, ThingModel crmodel)
{
struct Dungeon *dungeon = get_dungeon(plyr_idx);
long count = 0;
for (ThingModel i = 0; i < CREATURE_TYPES_MAX; i++)
{
if (!creature_model_matches_model(i, plyr_idx, crmodel))
continue;

if (creature_will_generate_for_dungeon(dungeon, i))
{
count+= game.pool.crtr_kind[i];
}
}
return min(count, dungeon->max_creatures_attracted - (long)dungeon->num_active_creatrs);
}

TbBool creature_will_generate_for_dungeon(const struct Dungeon * dungeon, ThingModel crmodel)
{
SYNCDBG(11, "Starting for creature model %s", creature_code_name(crmodel));
Expand Down
1 change: 1 addition & 0 deletions src/room_entrance.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct Thing *create_creature_at_entrance(struct Room * room, ThingModel crtr_ki

TbBool remove_creature_from_generate_pool(ThingModel crtr_kind);
TbBool creature_will_generate_for_dungeon(const struct Dungeon * dungeon, ThingModel crtr_kind);
long count_player_available_creatures_of_model(PlayerNumber plyr_idx, ThingModel crtr_kind);
/******************************************************************************/
TbBool update_creature_pool_state(void);
/******************************************************************************/
Expand Down
68 changes: 66 additions & 2 deletions src/thing_doors.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,13 @@ TbBool player_has_deployed_door_of_model(PlayerNumber owner, int model, short lo
return false;
}

long count_player_deployed_traps_of_model(PlayerNumber owner, int model)
/**
* Goes through thing list to count the traps of the given model.
* @param owner The owning player to be checked.
* @param model Trap model to count, or -1 for any.
* @return the number of things of class trap with matching model and available shots.
*/
long count_player_deployed_traps_of_model(PlayerNumber owner, ThingModel model)
{
long n = 0;
unsigned long k = 0;
Expand All @@ -624,7 +630,13 @@ long count_player_deployed_traps_of_model(PlayerNumber owner, int model)
return n;
}

TbBool player_has_deployed_trap_of_model(PlayerNumber owner, int model)
/**
* Goes through thing list to find a trap matching the given model.
* @param owner The owning player to be checked.
* @param model Trap model to find, or -1 for any.
* @return true when it finds any trap, false when not.
*/
TbBool player_has_deployed_trap_of_model(PlayerNumber owner, ThingModel model)
{
unsigned long k = 0;
const struct StructureList* slist = get_list_for_thing_class(TCls_Trap);
Expand All @@ -650,6 +662,58 @@ TbBool player_has_deployed_trap_of_model(PlayerNumber owner, int model)
return false;
}

/**
* Checks door crates in workshop and offmap doors available for placement.
* @param plyr_idx The owning player to be checked.
* @param model Door model to count, or -1 for all.
* @return Amount of doors that the player may place.
*/
long count_player_available_doors_of_model(PlayerNumber plyr_idx, ThingModel model)
{
struct Dungeon* dungeon = get_dungeon(plyr_idx);
long count = 0;
if (dungeon_invalid(dungeon))
{
ERRORLOG("Tried to count doors for Player %d which has no dungeon", (int)plyr_idx);
return 0;
}
for (int i = 0; i < game.conf.trapdoor_conf.door_types_count; i++)
{
if ((i == model) || (model == -1))
{
count += dungeon->mnfct_info.door_amount_stored[i];
count += dungeon->mnfct_info.door_amount_offmap[i];
}
}
return count;
}

/**
* Checks trap crates in workshop and offmap trapss available for placement.
* @param plyr_idx The owning player to be checked.
* @param model Trap model to count, or -1 for all.
* @return Amount of traps that the player may place.
*/
long count_player_available_traps_of_model(PlayerNumber plyr_idx, ThingModel model)
{
struct Dungeon* dungeon = get_dungeon(plyr_idx);
long count = 0;
if (dungeon_invalid(dungeon))
{
ERRORLOG("Tried to count traps for Player %d which has no dungeon", (int)plyr_idx);
return 0;
}
for (int i = 0; i < game.conf.trapdoor_conf.trap_types_count; i++)
{
if ((i == model) || (model == -1))
{
count += dungeon->mnfct_info.trap_amount_stored[i];
count += dungeon->mnfct_info.trap_amount_offmap[i];
}
}
return count;
}

// Update all placed doors to new stats
void update_all_door_stats()
{
Expand Down
6 changes: 4 additions & 2 deletions src/thing_doors.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ char determine_door_angle(MapSlabCoord slb_x, MapSlabCoord slb_y);

TbBool player_has_deployed_door_of_model(PlayerNumber owner, int model, short locked);
long count_player_deployed_doors_of_model(PlayerNumber owner, int model);
TbBool player_has_deployed_trap_of_model(PlayerNumber owner, int model);
long count_player_deployed_traps_of_model(PlayerNumber owner, int model);
TbBool player_has_deployed_trap_of_model(PlayerNumber owner, ThingModel model);
long count_player_deployed_traps_of_model(PlayerNumber owner, ThingModel model);
long count_player_available_doors_of_model(PlayerNumber plyr_idx, ThingModel model);
long count_player_available_traps_of_model(PlayerNumber plyr_idx, ThingModel model);

void update_all_door_stats();
/******************************************************************************/
Expand Down

0 comments on commit ecdaf2f

Please sign in to comment.