Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
UberWaffe committed Jan 14, 2025
1 parent 2b374a8 commit e34c751
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/building/construction.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "map/tiles.h"
#include "map/water.h"
#include "map/water_supply.h"
#include "scenario/scenario_events_controller.h"

#define MAX_CYCLE_SIZE 10

Expand Down Expand Up @@ -661,6 +662,8 @@ void building_construction_start(int x, int y, int grid_offset)
}
if (!can_start) {
building_construction_cancel();
} else {
scenario_events_full_process(EVENT_TRIGGER_BUILDING_PLACED_BY_PLAYER, 1, data.type);
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/building/construction_clear.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "map/routing_terrain.h"
#include "map/terrain.h"
#include "map/tiles.h"
#include "scenario/scenario_events_controller.h"
#include "translation/translation.h"
#include "window/popup_dialog.h"

Expand All @@ -30,6 +31,12 @@ static struct {
int monument_confirmed;
} confirm;

static void event_process_clearing(int building_type)
{
scenario_events_full_process(EVENT_TRIGGER_BUILDING_CLEARED_BY_PLAYER, 1, BUILDING_AQUEDUCT);
scenario_events_full_process(EVENT_TRIGGER_BUILDING_DESTROYED_BY_ANYTHING, 1, BUILDING_AQUEDUCT);
}

static building *get_deletable_building(int grid_offset)
{
int building_id = map_building_at(grid_offset);
Expand Down Expand Up @@ -144,10 +151,12 @@ static int clear_land_confirmed(int measure_only, int x_start, int y_start, int
game_undo_add_building(space);
space->state = BUILDING_STATE_DELETED_BY_PLAYER;
}
event_process_clearing(b->type);
} else if (map_terrain_is(grid_offset, TERRAIN_AQUEDUCT)) {
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE & ~TERRAIN_HIGHWAY);
items_placed++;
map_aqueduct_remove(grid_offset);
event_process_clearing(BUILDING_AQUEDUCT);
} else if (map_terrain_is(grid_offset, TERRAIN_WATER)) {
if (!measure_only && map_bridge_count_figures(grid_offset) > 0) {
city_warning_show(WARNING_PEOPLE_ON_BRIDGE, NEW_WARNING_SLOT);
Expand All @@ -159,8 +168,17 @@ static int clear_land_confirmed(int measure_only, int x_start, int y_start, int
int next_highways_removed = map_tiles_clear_highway(grid_offset, measure_only);
highways_removed += next_highways_removed;
items_placed += next_highways_removed;
event_process_clearing(BUILDING_HIGHWAY);
} else if (map_terrain_is(grid_offset, TERRAIN_NOT_CLEAR)) {
if (map_terrain_is(grid_offset, TERRAIN_ROAD | TERRAIN_GARDEN)) {
int is_road = map_terrain_is(grid_offset, TERRAIN_ROAD);
int is_garden = map_terrain_is(grid_offset, TERRAIN_GARDEN);
if (is_road) {
event_process_clearing(BUILDING_ROAD);
}
if (is_garden) {
event_process_clearing(BUILDING_GARDENS);
}
if (is_road || is_garden) {
map_property_clear_plaza_earthquake_or_overgrown_garden(grid_offset);
}
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
Expand Down
19 changes: 18 additions & 1 deletion src/building/destruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

#include <string.h>

static void event_process_destruction(event_trigger trigger, int building_type)
{
scenario_events_full_process(trigger, 1, building_type);
scenario_events_full_process(EVENT_TRIGGER_BUILDING_DESTROYED_BY_ANYTHING, 1, building_type);
}

static void destroy_on_fire(building *b, int plagued)
{
game_undo_disable();
Expand Down Expand Up @@ -141,23 +147,31 @@ void building_destroy_by_collapse(building *b)
destroy_linked_parts(b, 0, 0);
}

void building_destroy_by_poor_maintenance(building *b)
{
building_destroy_by_collapse(b);
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_POOR_MAINTENANCE, b->type);
}

void building_destroy_by_fire(building *b)
{
destroy_on_fire(b, 0);
destroy_linked_parts(b, 1, 0);
scenario_events_progress_paused(EVENT_TRIGGER_BUILDING_DESTROYED_BY_FIRE, 1);
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_FIRE, b->type);
}

void building_destroy_by_plague(building *b)
{
destroy_on_fire(b, 1);
destroy_linked_parts(b, 1, 1);
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_DISEASE, b->type);
}

void building_destroy_by_rioter(building *b)
{
destroy_on_fire(b, 0);
destroy_linked_parts(b, 1, 0);
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_COMBAT, b->type);
}

int building_destroy_first_of_type(building_type type)
Expand Down Expand Up @@ -212,16 +226,19 @@ void building_destroy_by_enemy(int x, int y, int grid_offset)
if (b->state == BUILDING_STATE_IN_USE || b->state == BUILDING_STATE_MOTHBALLED) {
city_ratings_peace_building_destroyed(b->type);
building_destroy_by_collapse(b);
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_COMBAT, b->type);
}
} else {
if (map_terrain_is(grid_offset, TERRAIN_WALL)) {
figure_kill_tower_sentries_at(x, y);
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_COMBAT, BUILDING_WALL);
}
if (map_terrain_is(grid_offset, TERRAIN_GARDEN)) {
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
map_tiles_update_region_empty_land(x, y, x, y);
map_property_clear_plaza_earthquake_or_overgrown_garden(grid_offset);
map_tiles_update_all_gardens();
event_process_destruction(EVENT_TRIGGER_BUILDING_DESTROYED_BY_COMBAT, BUILDING_GARDENS);
} else {
map_building_tiles_set_rubble(0, x, y, 1);
}
Expand Down
2 changes: 2 additions & 0 deletions src/building/destruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

void building_destroy_by_collapse(building *b);

void building_destroy_by_poor_maintenance(building *b);

void building_destroy_by_fire(building *b);

void building_destroy_by_plague(building *b);
Expand Down
2 changes: 1 addition & 1 deletion src/building/maintenance.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static void collapse_building(building *b)
}

game_undo_disable();
building_destroy_by_collapse(b);
building_destroy_by_poor_maintenance(b);
}

static void fire_building(building *b)
Expand Down
4 changes: 2 additions & 2 deletions src/game/tick.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "building/industry.h"
#include "building/lighthouse.h"
#include "building/maintenance.h"
#include "building/type.h"
#include "building/warehouse.h"
#include "city/buildings.h"
#include "city/culture.h"
Expand Down Expand Up @@ -115,8 +116,7 @@ static void advance_month(void)
city_games_decrement_month_counts();
city_gods_update_blessings();
tutorial_on_month_tick();
scenario_events_progress_paused(EVENT_TRIGGER_MONTH_START, 1);
scenario_events_process_by_trigger_type(EVENT_TRIGGER_MONTH_START);
scenario_events_full_process(EVENT_TRIGGER_MONTH_START, 1, BUILDING_NONE);
if (setting_monthly_autosave()) {
game_file_write_saved_game(dir_append_location("autosave.svx", PATH_LOCATION_SAVEGAME));
}
Expand Down
2 changes: 2 additions & 0 deletions src/scenario/condition_types/condition_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ int scenario_condition_type_is_met(scenario_condition_t *condition)
return scenario_condition_type_building_count_area_met(condition);
case CONDITION_TYPE_CITY_POPULATION:
return scenario_condition_type_city_population_met(condition);
case CONDITION_TYPE_CONTEXT_BUILDING_TYPE:
return scenario_condition_type_context_building_type_met(condition);
case CONDITION_TYPE_COUNT_OWN_TROOPS:
return scenario_condition_type_count_own_troops_met(condition);
case CONDITION_TYPE_CUSTOM_VARIABLE_CHECK:
Expand Down
13 changes: 13 additions & 0 deletions src/scenario/condition_types/condition_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "map/grid.h"
#include "scenario/request.h"
#include "scenario/scenario.h"
#include "scenario/scenario_events_controller.h"
#include "scenario/condition_types/comparison_helper.h"

int scenario_condition_type_building_count_active_met(const scenario_condition_t *condition)
Expand Down Expand Up @@ -210,6 +211,18 @@ int scenario_condition_type_city_population_met(const scenario_condition_t *cond
return comparison_helper_compare_values(comparison, population_value_to_use, value);
}

int scenario_condition_type_context_building_type_met(const scenario_condition_t *condition)
{
int building_type = condition->parameter1;

scenario_event_context_t *context = scenario_events_get_context();
if (!context) {
return 0;
}

return building_type == context->related_building_type;
}

int scenario_condition_type_count_own_troops_met(const scenario_condition_t *condition)
{
int comparison = condition->parameter1;
Expand Down
2 changes: 2 additions & 0 deletions src/scenario/condition_types/condition_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int scenario_condition_type_city_population_met(const scenario_condition_t *cond

int scenario_condition_type_count_own_troops_met(const scenario_condition_t *condition);

int scenario_condition_type_context_building_type_met(const scenario_condition_t *condition);

int scenario_condition_type_custom_variable_check_met(const scenario_condition_t *condition);

int scenario_condition_type_difficulty_met(const scenario_condition_t *condition);
Expand Down
7 changes: 7 additions & 0 deletions src/scenario/scenario_event_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ typedef enum {
EVENT_TRIGGER_UNDEFINED = 0,
EVENT_TRIGGER_MONTH_START = 1,
EVENT_TRIGGER_BUILDING_DESTROYED_BY_FIRE = 2,
EVENT_TRIGGER_BUILDING_DESTROYED_BY_POOR_MAINTENANCE = 3,
EVENT_TRIGGER_BUILDING_DESTROYED_BY_COMBAT = 4,
EVENT_TRIGGER_BUILDING_CLEARED_BY_PLAYER = 5,
EVENT_TRIGGER_BUILDING_DESTROYED_BY_DISEASE = 6,
EVENT_TRIGGER_BUILDING_DESTROYED_BY_ANYTHING = 7,
EVENT_TRIGGER_BUILDING_PLACED_BY_PLAYER = 8,
EVENT_TRIGGER_MAX,
} event_trigger;

Expand Down Expand Up @@ -50,6 +56,7 @@ typedef enum {
CONDITION_TYPE_RESOURCE_STORED_COUNT = 22,
CONDITION_TYPE_RESOURCE_STORAGE_AVAILABLE = 23,
CONDITION_TYPE_BUILDING_COUNT_AREA = 24,
CONDITION_TYPE_CONTEXT_BUILDING_TYPE = 25,
CONDITION_TYPE_MAX,
// helper constants
CONDITION_TYPE_MIN = CONDITION_TYPE_TIME_PASSED,
Expand Down
39 changes: 32 additions & 7 deletions src/scenario/scenario_events_controller.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "scenario_events_controller.h"

#include "building/type.h"
#include "core/log.h"
#include "game/save_version.h"
#include "scenario/action_types/action_handler.h"
Expand All @@ -11,6 +12,8 @@

static array(scenario_event_t) scenario_events;

static scenario_event_context_t scenario_events_context;

static int event_in_use(const scenario_event_t *event)
{
return event->state != EVENT_STATE_UNDEFINED;
Expand All @@ -27,6 +30,9 @@ void scenario_events_init(void)
array_foreach(scenario_events, current) {
scenario_event_init(current);
}

scenario_events_context.cause_of_context = EVENT_TRIGGER_UNDEFINED;
scenario_events_context.related_building_type = BUILDING_NONE;
}

void scenario_events_clear(void)
Expand Down Expand Up @@ -73,6 +79,7 @@ scenario_event_t *scenario_event_create(int repeat_min, int repeat_max, int max_
event->repeat_triggers_min = repeat_min;
event->repeat_triggers_max = repeat_max;
event->max_number_of_repeats = max_repeats;
event->trigger = trigger_type;

return event;
}
Expand Down Expand Up @@ -244,6 +251,16 @@ static void actions_load_state(buffer *buf)
}
}

static void scenario_events_progress_paused(event_trigger type_to_progress, int count)
{
scenario_event_t *current;
array_foreach(scenario_events, current) {
if (current->trigger == type_to_progress) {
scenario_event_decrease_pause_count(current, count);
}
}
}

void scenario_events_load_state(buffer *buf_events, buffer *buf_conditions, buffer *buf_actions)
{
scenario_events_clear();
Expand All @@ -259,6 +276,12 @@ void scenario_events_load_state(buffer *buf_events, buffer *buf_conditions, buff
}
}

void scenario_events_set_context(event_trigger cause, building_type b_type)
{
scenario_events_context.cause_of_context = cause;
scenario_events_context.related_building_type = b_type;
}

void scenario_events_process_by_trigger_type(event_trigger type_to_process)
{
scenario_event_t *current;
Expand All @@ -269,6 +292,13 @@ void scenario_events_process_by_trigger_type(event_trigger type_to_process)
}
}

void scenario_events_full_process(event_trigger cause, int progress_count, building_type b_type)
{
scenario_events_set_context(cause, b_type);
scenario_events_progress_paused(cause, progress_count);
scenario_events_process_by_trigger_type(cause);
}

scenario_event_t *scenario_events_get_using_custom_variable(int custom_variable_id)
{
scenario_event_t *current;
Expand All @@ -280,12 +310,7 @@ scenario_event_t *scenario_events_get_using_custom_variable(int custom_variable_
return 0;
}

void scenario_events_progress_paused(event_trigger type_to_progress, int count)
scenario_event_context_t *scenario_events_get_context(void)
{
scenario_event_t *current;
array_foreach(scenario_events, current) {
if (current->trigger == type_to_progress) {
scenario_event_decrease_pause_count(current, count);
}
}
return &scenario_events_context;
}
11 changes: 10 additions & 1 deletion src/scenario/scenario_events_controller.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SCENARIO_EVENTS_CONTOLLER_H
#define SCENARIO_EVENTS_CONTOLLER_H

#include "building/building.h"
#include "core/buffer.h"
#include "scenario/scenario_event_data.h"

Expand All @@ -12,6 +13,11 @@ typedef enum {
SCENARIO_EVENTS_VERSION_TRIGGERS = 2,
} scenario_events_version;

typedef struct {
event_trigger cause_of_context;
building_type related_building_type;
} scenario_event_context_t;

void scenario_events_init(void);
void scenario_events_clear(void);
scenario_event_t *scenario_event_get(int event_id);
Expand All @@ -22,8 +28,11 @@ int scenario_events_get_count(void);
void scenario_events_save_state(buffer *buf_events, buffer *buf_conditions, buffer *buf_actions);
void scenario_events_load_state(buffer *buf_events, buffer *buf_conditions, buffer *buf_actions);

void scenario_events_set_context(event_trigger cause, building_type b_type);
void scenario_events_process_by_trigger_type(event_trigger type_to_process);
void scenario_events_progress_paused(event_trigger type_to_progress, int count);
void scenario_events_full_process(event_trigger cause, int progress_count, building_type b_type);
scenario_event_t *scenario_events_get_using_custom_variable(int custom_variable_id);

scenario_event_context_t *scenario_events_get_context(void);

#endif // SCENARIO_EVENTS_CONTOLLER_H
Loading

0 comments on commit e34c751

Please sign in to comment.