Skip to content

Commit

Permalink
Fix issues and crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
crudelios committed Jan 25, 2025
1 parent eb121c6 commit a817184
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/game/file_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ static void scenario_load_from_state(scenario_state *file, scenario_version_t ve
}
scenario_load_state(file->scenario, version);
if (version > SCENARIO_LAST_NO_EXTENDED_REQUESTS) {
scenario_request_load_state(file->requests);
scenario_request_load_state(file->requests, version);
}
if (version > SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
scenario_invasion_load_state(file->invasions);
Expand All @@ -690,7 +690,7 @@ static void scenario_load_from_state(scenario_state *file, scenario_version_t ve
}
if (version > SCENARIO_LAST_NO_EVENTS) {
scenario_events_load_state(file->scenario_events, file->scenario_conditions, file->scenario_actions,
version >= SCENARIO_LAST_STATIC_ORIGINAL_DATA);
version > SCENARIO_LAST_STATIC_ORIGINAL_DATA);
} else {
scenario_events_clear();
}
Expand Down Expand Up @@ -764,7 +764,7 @@ static void savegame_load_from_state(savegame_state *state, savegame_version_t v
scenario_load_state(state->scenario, scenario_version);

if (scenario_version > SCENARIO_LAST_NO_EXTENDED_REQUESTS) {
scenario_request_load_state(state->requests);
scenario_request_load_state(state->requests, scenario_version);
}

if (scenario_version > SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
Expand All @@ -777,7 +777,7 @@ static void savegame_load_from_state(savegame_state *state, savegame_version_t v

if (scenario_version > SCENARIO_LAST_NO_EVENTS) {
scenario_events_load_state(state->scenario_events, state->scenario_conditions, state->scenario_actions,
scenario_version >= SCENARIO_LAST_STATIC_ORIGINAL_DATA);
scenario_version > SCENARIO_LAST_STATIC_ORIGINAL_DATA);
} else {
scenario_events_clear();
}
Expand Down
13 changes: 7 additions & 6 deletions src/scenario/action_types/action_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,20 @@ unsigned int scenario_action_type_load_state(buffer *buf, scenario_action_t *act
} else if (action->type == ACTION_TYPE_TRADE_SET_SELL_PRICE_ONLY) {
action->parameter1 = resource_remap(action->parameter1);
} else if (action->type == ACTION_TYPE_CHANGE_ALLOWED_BUILDINGS) {
const building_type *building_list = scenario_allowed_building_get_buildings_from_original_id(action->parameter1);
action->parameter1 = building_list[0];
return building_list[1] != BUILDING_NONE;
if (!is_new_version) {
int original_id = action->parameter1;
return scenario_action_type_load_allowed_building(action, original_id, 0) ? original_id : 0;
}
}
return 0;
}

unsigned int scenario_action_type_load_more(unsigned int index, scenario_action_t *action)
unsigned int scenario_action_type_load_allowed_building(scenario_action_t *action, int original_id, unsigned int index)
{
if (!index || action->type != ACTION_TYPE_CHANGE_ALLOWED_BUILDINGS) {
if (action->type != ACTION_TYPE_CHANGE_ALLOWED_BUILDINGS || !original_id) {
return 0;
}
const building_type *building_list = scenario_allowed_building_get_buildings_from_original_id(action->parameter1);
const building_type *building_list = scenario_allowed_building_get_buildings_from_original_id(original_id);
action->parameter1 = building_list[index];
return building_list[index + 1] != BUILDING_NONE ? index + 1 : 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/scenario/action_types/action_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void scenario_action_type_delete(scenario_action_t *action);
void scenario_action_type_save_state(buffer *buf, const scenario_action_t *action, int link_type, int32_t link_id);
unsigned int scenario_action_type_load_state(buffer *buf, scenario_action_t *action, int *link_type, int32_t *link_id,
int is_new_version);
unsigned int scenario_action_type_load_more(unsigned int index, scenario_action_t *action);
unsigned int scenario_action_type_load_allowed_building(scenario_action_t *action, int original_id, unsigned int index);
int scenario_action_uses_custom_variable(const scenario_action_t *action, int custom_variable_id);

#endif // ACTION_HANDLER_H
49 changes: 48 additions & 1 deletion src/scenario/allowed_building.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,56 @@ int scenario_allowed_building(building_type type)
return !building_properties_for_type(type)->disallowable || allowed_buildings[type];
}

static void set_group(unsigned int id, int allowed)
{
const building_type *group = CONVERSION_FROM_ORIGINAL[id];
unsigned int index = 0;
while (group[index]) {
allowed_buildings[group[index]] = allowed;
index++;
}
}

void scenario_allowed_building_set(building_type type, int allowed)
{
allowed_buildings[type] = allowed;
switch (type) {
case BUILDING_MENU_FARMS:
set_group(1, allowed);
return;
case BUILDING_MENU_RAW_MATERIALS:
set_group(2, allowed);
return;
case BUILDING_MENU_WORKSHOPS:
set_group(3, allowed);
return;
case BUILDING_MENU_SMALL_TEMPLES:
set_group(30, allowed);
return;
case BUILDING_MENU_LARGE_TEMPLES:
set_group(31, allowed);
return;
case BUILDING_MENU_GRAND_TEMPLES:
set_group(48, allowed);
return;
case BUILDING_MENU_TREES:
set_group(16, allowed);
return;
case BUILDING_MENU_PATHS:
set_group(16, allowed);
return;
case BUILDING_MENU_PARKS:
set_group(16, allowed);
return;
case BUILDING_MENU_STATUES:
set_group(18, allowed);
return;
case BUILDING_MENU_GOV_RES:
set_group(38, allowed);
return;
default:
allowed_buildings[type] = allowed;
return;
}
}

void scenario_allowed_building_enable_all(void)
Expand Down
27 changes: 19 additions & 8 deletions src/scenario/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,18 @@ void scenario_request_save_state(buffer *list)
}
}

static void request_load(buffer *list, scenario_request *request)
static void request_load(buffer *list, scenario_request *request, int version)
{
request->year = buffer_read_i16(list);
request->resource = resource_remap(buffer_read_i16(list));
request->amount.min = buffer_read_u16(list);
request->amount.max = buffer_read_u16(list);
request->amount.requested = buffer_read_u16(list);
if (version > SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
request->amount.max = buffer_read_u16(list);
request->amount.requested = buffer_read_u16(list);
} else {
request->amount.max = request->amount.min;
request->amount.requested = request->amount.min;
}
request->deadline_years = buffer_read_i16(list);

request->can_comply_dialog_shown = buffer_read_u8(list);
Expand All @@ -393,12 +398,18 @@ static void request_load(buffer *list, scenario_request *request)
request->extension_disfavor = buffer_read_i16(list);
request->ignored_disfavor = buffer_read_i16(list);

request->repeat.times = buffer_read_i16(list);
request->repeat.interval.min = buffer_read_u16(list);
request->repeat.interval.max = buffer_read_u16(list);
if (version > SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
request->repeat.times = buffer_read_i16(list);
request->repeat.interval.min = buffer_read_u16(list);
request->repeat.interval.max = buffer_read_u16(list);
} else {
request->repeat.times = 0;
request->repeat.interval.min = 0;
request->repeat.interval.max = 0;
}
}

void scenario_request_load_state(buffer *list)
void scenario_request_load_state(buffer *list, int version)
{
unsigned int array_size = buffer_load_dynamic_array(list);

Expand All @@ -409,7 +420,7 @@ void scenario_request_load_state(buffer *list)

for (unsigned int i = 0; i < array_size; i++) {
scenario_request *request = array_next(requests);
request_load(list, request);
request_load(list, request, version);
}

array_trim(requests);
Expand Down
2 changes: 1 addition & 1 deletion src/scenario/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int scenario_request_is_ongoing(int id);
int scenario_request_force_start(int id);

void scenario_request_save_state(buffer *list);
void scenario_request_load_state(buffer *list);
void scenario_request_load_state(buffer *list, int version);

void scenario_request_load_state_old_version(buffer *list, requests_old_state_sections section);

Expand Down
14 changes: 10 additions & 4 deletions src/scenario/scenario.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ void scenario_load_state(buffer *buf, int version)
buffer_skip(buf, 8);

if (version <= SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_TARGET);
if (version <= SCENARIO_LAST_NO_EXTENDED_REQUESTS) {
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_TARGET);
}
scenario_invasion_load_state_old_version(buf, INVASION_OLD_STATE_FIRST_SECTION);
}

Expand All @@ -369,7 +371,7 @@ void scenario_load_state(buffer *buf, int version)
buffer_read_raw(buf, scenario.brief_description, MAX_BRIEF_DESCRIPTION);
buffer_read_raw(buf, scenario.briefing, MAX_BRIEFING);

if (version <= SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
if (version <= SCENARIO_LAST_NO_EXTENDED_REQUESTS) {
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_CAN_COMPLY);
}

Expand Down Expand Up @@ -422,9 +424,13 @@ void scenario_load_state(buffer *buf, int version)
}

if (version <= SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_FAVOR_REWARD);
if (version <= SCENARIO_LAST_NO_EXTENDED_REQUESTS) {
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_FAVOR_REWARD);
}
scenario_invasion_load_state_old_version(buf, INVASION_OLD_STATE_LAST_SECTION);
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_ONGOING_INFO);
if (version <= SCENARIO_LAST_NO_EXTENDED_REQUESTS) {
scenario_request_load_state_old_version(buf, REQUESTS_OLD_STATE_SECTIONS_ONGOING_INFO);
}
}

scenario.rome_supplies_wheat = buffer_read_i32(buf);
Expand Down
10 changes: 6 additions & 4 deletions src/scenario/scenario_events_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ static void conditions_load_state_old_version(buffer *buf)
} else {
group = array_item(event->condition_groups, 0);
}
scenario_condition_t *condition = array_next(group->conditions);
scenario_condition_t *condition;
array_new_item(group->conditions, condition);
scenario_condition_load_state(buf, group, condition);
}
}
Expand Down Expand Up @@ -232,10 +233,11 @@ static void actions_load_state(buffer *buf, int is_new_version)
int32_t link_id = 0;
for (unsigned int i = 0; i < array_size; i++) {
scenario_action_t action = { 0 };
unsigned int next = scenario_action_type_load_state(buf, &action, &link_type, &link_id, is_new_version);
int original_id = scenario_action_type_load_state(buf, &action, &link_type, &link_id, is_new_version);
load_link_action(&action, link_type, link_id);
while (next) {
next = scenario_action_type_load_more(next, &action);
unsigned int index = 1;
while (index) {
index = scenario_action_type_load_allowed_building(&action, original_id, index);
load_link_action(&action, link_type, link_id);
}
}
Expand Down

0 comments on commit a817184

Please sign in to comment.