Skip to content

Commit

Permalink
Fix some issues with invasions and requests on the scenario editor
Browse files Browse the repository at this point in the history
  • Loading branch information
crudelios committed Oct 5, 2024
1 parent 588eb87 commit f42e0c8
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 48 deletions.
6 changes: 5 additions & 1 deletion src/game/file_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,11 @@ static savegame_load_status savegame_read_file_info(saved_game_info *info, saveg
scenario_description_from_buffer(state->scenario, info->description, version);
info->image_id = scenario_image_id_from_buffer(state->scenario, version);
info->climate = scenario_climate_from_buffer(state->scenario, version);
info->total_invasions = scenario_invasions_from_buffer(state->scenario, version);
if (scenario_version <= SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
info->total_invasions = scenario_invasions_from_buffer(state->scenario, scenario_version);
} else {
info->total_invasions = scenario_invasions_from_buffer(state->invasions, scenario_version);
}
info->player_rank = scenario_rank_from_buffer(state->scenario, version);
info->start_year = scenario_start_year_from_buffer(state->scenario, version);
scenario_open_play_info_from_buffer(state->scenario, version, &info->is_open_play, &info->open_play_id);
Expand Down
15 changes: 15 additions & 0 deletions src/graphics/lang_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y
return lang_text_draw_amount_colored(group, number, amount, x_offset, y_offset, font, COLOR_MASK_NONE);
}

int lang_text_draw_amount_centered(int group, int number, int amount, int x_offset, int y_offset, int box_width,
font_t font)
{
int width;
if (amount >= 0) {
width = text_get_number_width(amount, ' ', " ", font);
} else {
width = text_get_number_width(-amount, '-', " ", font);
}
int text_offset = (amount == 1 || amount == -1) ? 0 : 1;
width += lang_text_get_width(group, number + text_offset, font);
return lang_text_draw_amount_colored(group, number, amount, x_offset + (box_width - width) / 2, y_offset,
font, COLOR_MASK_NONE);
}

int lang_text_draw_amount_colored(int group, int number, int amount, int x_offset, int y_offset,
font_t font, color_t color)
{
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/lang_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void lang_text_draw_centered_colored(
void lang_text_draw_ellipsized(int group, int number, int x_offset, int y_offset, int box_width, font_t font);

int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y_offset, font_t font);
int lang_text_draw_amount_centered(int group, int number, int amount, int x_offset, int y_offset, int box_width,
font_t font);
int lang_text_draw_amount_colored(int group, int number, int amount, int x_offset, int y_offset,
font_t font, color_t color);

Expand Down
6 changes: 0 additions & 6 deletions src/scenario/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <stdint.h>

//#define MAX_REQUESTS 20
#define MAX_INVASIONS 20
#define MAX_DEMAND_CHANGES 20
#define MAX_PRICE_CHANGES 20
#define MAX_CUSTOM_VARIABLES 100
Expand Down Expand Up @@ -168,14 +166,10 @@ extern struct scenario_t {

custom_variable_t custom_variables[MAX_CUSTOM_VARIABLES];

//request_t requests[MAX_REQUESTS];

demand_change_t demand_changes[MAX_DEMAND_CHANGES];

price_change_t price_changes[MAX_DEMAND_CHANGES];

//invasion_t invasions[MAX_INVASIONS];

struct {
int severity;
int year;
Expand Down
4 changes: 2 additions & 2 deletions src/scenario/distant_battle.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void scenario_distant_battle_set_enemy_travel_months(void)

void scenario_distant_battle_process(void)
{
int total_invasions = scenario_invasion_count();
for (int i = 0; i < scenario_invasion_count(); i++) {
int total_invasions = scenario_invasion_count_total();
for (int i = 0; i < total_invasions; i++) {
const invasion_t *invasion = scenario_invasion_get(i);
if (invasion->type == INVASION_TYPE_DISTANT_BATTLE &&
game_time_year() == invasion->year + scenario.start_year &&
Expand Down
11 changes: 8 additions & 3 deletions src/scenario/invasion.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ void scenario_invasion_foreach_warning(void (*callback)(int x, int y, int image_
}
}

int scenario_invasion_count(void)
int scenario_invasion_count_total(void)
{
return data.invasions.size;
}

int scenario_invasion_count_active(void)
{
int num_invasions = 0;
const invasion_t *invasion;
Expand Down Expand Up @@ -715,7 +720,7 @@ void scenario_invasion_load_state(buffer *buf)
array_trim(data.invasions);
}

int scenario_invasion_count_from_buffer(buffer *buf)
int scenario_invasion_count_active_from_buffer(buffer *buf)
{
int size;
buffer_load_dynamic_piece_header_data(buf, 0, 0, &size, 0);
Expand All @@ -742,7 +747,7 @@ void scenario_invasion_load_state_old_version(buffer *buf, invasion_old_state_se
log_error("Error creating invasions array - not enough memory. The game will now crash.", 0, 0);
}
for (size_t i = 0; i < MAX_ORIGINAL_INVASIONS; i++) {
array_next(data.invasions);
array_advance(data.invasions);
}
array_foreach(data.invasions, invasion) {
invasion->year = buffer_read_i16(buf);
Expand Down
5 changes: 3 additions & 2 deletions src/scenario/invasion.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ int scenario_invasion_exists_upcoming(void);

void scenario_invasion_foreach_warning(void (*callback)(int x, int y, int image_id));

int scenario_invasion_count(void);
int scenario_invasion_count_from_buffer(buffer *buf);
int scenario_invasion_count_total(void);
int scenario_invasion_count_active(void);
int scenario_invasion_count_active_from_buffer(buffer *buf);

int scenario_invasion_get_years_remaining(void);

Expand Down
14 changes: 13 additions & 1 deletion src/scenario/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ int scenario_request_count_total(void)
return requests.size;
}

int scenario_request_count_active(void)
{
int num_requests = 0;
const scenario_request *request;
array_foreach(requests, request) {
if (request->resource) {
num_requests++;
}
}
return num_requests;
}

int scenario_request_count_visible(void)
{
int count = 0;
Expand Down Expand Up @@ -391,7 +403,7 @@ void scenario_request_load_state_old_version(buffer *list, requests_old_state_se
log_error("Error creating requests array. The game will probably crash.", 0, 0);
}
for (size_t i = 0; i < MAX_ORIGINAL_REQUESTS; i++) {
array_next(requests);
array_advance(requests);
}
array_foreach(requests, request) {
request->year = buffer_read_i16(list);
Expand Down
2 changes: 2 additions & 0 deletions src/scenario/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void scenario_request_dispatch(int id);

int scenario_request_count_total(void);

int scenario_request_count_active(void);

int scenario_request_count_visible(void);

const scenario_request *scenario_request_get(int id);
Expand Down
25 changes: 2 additions & 23 deletions src/scenario/scenario.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,6 @@ void scenario_save_state(buffer *buf)
buffer_write_i16(buf, 0);
buffer_write_i16(buf, scenario.empire.id);
buffer_skip(buf, 8);
/***
for (int i = 0; i < MAX_INVASIONS; i++) {
buffer_write_i16(buf, scenario.invasions[i].year);
}
for (int i = 0; i < MAX_INVASIONS; i++) {
buffer_write_i16(buf, scenario.invasions[i].type);
}
for (int i = 0; i < MAX_INVASIONS; i++) {
buffer_write_i16(buf, scenario.invasions[i].amount);
}
for (int i = 0; i < MAX_INVASIONS; i++) {
buffer_write_i16(buf, scenario.invasions[i].from);
}
for (int i = 0; i < MAX_INVASIONS; i++) {
buffer_write_i16(buf, scenario.invasions[i].attack_type);
}
***/
buffer_write_i16(buf, 0);
buffer_write_i32(buf, scenario.initial_funds);
buffer_write_i16(buf, scenario.enemy_id);
Expand Down Expand Up @@ -316,11 +299,7 @@ void scenario_save_state(buffer *buf)
for (int i = 0; i < MAX_FISH_POINTS; i++) {
buffer_write_i16(buf, scenario.fishing_points[i].y);
}
/***
for (int i = 0; i < MAX_INVASIONS; i++) {
buffer_write_u8(buf, scenario.invasions[i].month);
}
***/

buffer_write_i32(buf, scenario.rome_supplies_wheat);
/***
for (int i = 0; i < MAX_ALLOWED_BUILDINGS; i++) {
Expand Down Expand Up @@ -663,7 +642,7 @@ int scenario_invasions_from_buffer(buffer *buf, int version)
int num_invasions = 0;

if (version > SCENARIO_LAST_STATIC_ORIGINAL_DATA) {
return scenario_invasion_count_from_buffer(buf);
return scenario_invasion_count_active_from_buffer(buf);
} else {
calculate_buffer_offsets(version);
buffer_set(buf, buffer_offsets.invasions_part1 + (MAX_ORIGINAL_INVASIONS * 2));
Expand Down
4 changes: 4 additions & 0 deletions src/translation/english.c
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,11 @@ static translation_string all_strings[] = {
{TR_FIGURE_TYPE_ARMORY_CARTPUSHER, "Armory deliveryman"},
{TR_TOOLTIP_BUTTON_CAN_GO_TO_ADVISORS, "Display the relevant advisor for this building." },
{TR_FIGURE_ENEMY_CATAPULT, "Catapult"},
{TR_EDITOR_REQUEST, "request"},
{TR_EDITOR_REQUESTS, "requests"},
{TR_EDITOR_NEW_REQUEST, "New request"},
{TR_EDITOR_INVASION, "invasion"},
{TR_EDITOR_INVASIONS, "invasions"},
{TR_EDITOR_NEW_INVASION, "New invasion"},
{TR_TOOLTIP_BUTTON_DELETE_COMMON_MESSAGES, "Delete common messages"}
};
Expand Down
4 changes: 4 additions & 0 deletions src/translation/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,11 @@ typedef enum {
TR_FIGURE_TYPE_ARMORY_CARTPUSHER,
TR_TOOLTIP_BUTTON_CAN_GO_TO_ADVISORS,
TR_FIGURE_ENEMY_CATAPULT,
TR_EDITOR_REQUEST,
TR_EDITOR_REQUESTS,
TR_EDITOR_NEW_REQUEST,
TR_EDITOR_INVASION,
TR_EDITOR_INVASIONS,
TR_EDITOR_NEW_INVASION,
TR_TOOLTIP_BUTTON_DELETE_COMMON_MESSAGES,
TRANSLATION_MAX_KEY
Expand Down
18 changes: 16 additions & 2 deletions src/window/editor/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "graphics/window.h"
#include "input/input.h"
#include "scenario/editor.h"
#include "scenario/invasion.h"
#include "scenario/property.h"
#include "scenario/request.h"
#include "widget/input_box.h"
#include "widget/minimap.h"
#include "widget/sidebar/editor.h"
Expand Down Expand Up @@ -143,7 +145,13 @@ static void draw_foreground(void)

lang_text_draw(44, 40, 32, 165, FONT_NORMAL_BLACK);
button_border_draw(212, 156, 250, 30, data.focus_button_id == 3);
lang_text_draw_centered(44, 14, 212, 165, 250, FONT_NORMAL_BLACK);
int requests = scenario_request_count_active();
if (requests == 0) {
lang_text_draw_centered(44, 19, 212, 165, 250, FONT_NORMAL_BLACK);
} else {
lang_text_draw_amount_centered(CUSTOM_TRANSLATION, TR_EDITOR_REQUEST, requests, 212, 165, 250,
FONT_NORMAL_BLACK);
}

lang_text_draw(44, 41, 32, 205, FONT_NORMAL_BLACK);
button_border_draw(212, 196, 250, 30, data.focus_button_id == 4);
Expand All @@ -152,7 +160,13 @@ static void draw_foreground(void)
lang_text_draw(44, 42, 32, 245, FONT_NORMAL_BLACK);
button_border_draw(212, 236, 250, 30, data.focus_button_id == 5);

lang_text_draw_centered(44, 20, 212, 245, 250, FONT_NORMAL_BLACK);
int invasions = scenario_invasion_count_active();
if (invasions == 0) {
lang_text_draw_centered(44, 20, 212, 245, 250, FONT_NORMAL_BLACK);
} else {
lang_text_draw_amount_centered(CUSTOM_TRANSLATION, TR_EDITOR_INVASION, invasions, 212, 245, 250,
FONT_NORMAL_BLACK);
}

button_border_draw(212, 276, 250, 30, data.focus_button_id == 6);
lang_text_draw_centered(44, 44, 212, 285, 250, FONT_NORMAL_BLACK);
Expand Down
13 changes: 7 additions & 6 deletions src/window/editor/invasions.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void sort_list(void)

static void update_invasion_list(void)
{
int current_invasions = scenario_invasion_count();
int current_invasions = scenario_invasion_count_total();
if (current_invasions != data.total_invasions) {
free(data.invasions);
data.invasions = 0;
Expand All @@ -82,8 +82,8 @@ static void update_invasion_list(void)
for (unsigned int i = 0; i < current_invasions; i++) {
data.invasions[i] = scenario_invasion_get(i);
}
data.total_invasions = current_invasions;
}
data.total_invasions = current_invasions;
}
sort_list();
grid_box_update_total_items(&invasion_buttons, data.invasions_in_use);
Expand All @@ -103,9 +103,10 @@ static void draw_background(void)
lang_text_draw_centered(CUSTOM_TRANSLATION, TR_EDITOR_NEW_INVASION, new_invasion_button.x + 8,
new_invasion_button.y + 8, new_invasion_button.width - 16, FONT_NORMAL_BLACK);


graphics_reset_dialog();

update_invasion_list();

grid_box_request_refresh(&invasion_buttons);
}

Expand All @@ -114,7 +115,7 @@ static void draw_invasion_button(const grid_box_item *item)
button_border_draw(item->x, item->y, item->width, item->height, item->is_focused);
const invasion_t *invasion = data.invasions[item->index];
text_draw_number(invasion->year, '+', " ", item->x + 10, item->y + 7, FONT_NORMAL_BLACK, 0);
lang_text_draw_year(scenario_property_start_year() + invasion->year, item->x + 65, item->y + 7, FONT_NORMAL_BLACK);
lang_text_draw_year(scenario_property_start_year() + invasion->year, item->x + 45, item->y + 7, FONT_NORMAL_BLACK);
int width = text_draw_number(invasion->amount, '@', " ", item->x + 120, item->y + 7, FONT_NORMAL_BLACK, 0);
lang_text_draw(34, invasion->type, item->x + 115 + width, item->y + 7, FONT_NORMAL_BLACK);
}
Expand All @@ -126,7 +127,7 @@ static void draw_foreground(void)
if (data.invasions_in_use) {
grid_box_draw(&invasion_buttons);
} else {
lang_text_draw_centered(44, 19, 0, 165, 640, FONT_LARGE_BLACK);
lang_text_draw_centered(44, 20, 0, 165, 640, FONT_LARGE_BLACK);
}
button_border_draw(new_invasion_button.x, new_invasion_button.y,
new_invasion_button.width, new_invasion_button.height, data.new_invasion_button_focused);
Expand Down Expand Up @@ -167,6 +168,6 @@ void window_editor_invasions_show(void)
draw_foreground,
handle_input
};
grid_box_init(&invasion_buttons, MAX_INVASIONS);
grid_box_init(&invasion_buttons, scenario_invasion_count_active());
window_show(&window);
}
4 changes: 2 additions & 2 deletions src/window/editor/requests.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ static void update_request_list(void)
for (unsigned int i = 0; i < current_requests; i++) {
data.requests[i] = scenario_request_get(i);
}
data.total_requests = current_requests;
}
data.total_requests = current_requests;
}
sort_list();
grid_box_update_total_items(&request_buttons, data.requests_in_use);
Expand Down Expand Up @@ -195,7 +195,7 @@ static void show_window(void (*on_select)(int))
handle_input
};
data.on_select = on_select;
grid_box_init(&request_buttons, scenario_request_count_total());
grid_box_init(&request_buttons, scenario_request_count_active());
window_show(&window);
}

Expand Down

0 comments on commit f42e0c8

Please sign in to comment.