From d793093925608448104454ff91fbac4b757cbb5e Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Wed, 4 Dec 2024 15:41:36 +0100 Subject: load save file --- src/include/world.h | 2 +- src/scenes/place_detail.c | 8 +- src/scenes/save_state_select.c | 213 +++++++++++++++++++++++++++++++++++++++++ src/world.c | 35 ++++++- 4 files changed, 253 insertions(+), 5 deletions(-) diff --git a/src/include/world.h b/src/include/world.h index c4864fc..5d76991 100644 --- a/src/include/world.h +++ b/src/include/world.h @@ -410,7 +410,7 @@ void add_truck_to_world_location(world* world, world_location* location, truck* void world_update(platform_window* window, world* world); world_update_result world_render(platform_window* window, world* world); company* world_get_company_by_name(world* world, char* str); -company* world_get_product_by_name(world* world, char* str); +product* world_get_product_by_name(world* world, char* str); truck* world_get_truck_by_type(world* world, s32 type); #endif \ No newline at end of file diff --git a/src/scenes/place_detail.c b/src/scenes/place_detail.c index 49973a4..f214347 100644 --- a/src/scenes/place_detail.c +++ b/src/scenes/place_detail.c @@ -984,18 +984,22 @@ static void draw_duration_of_scheduled_job_entry(scheduled_job job, scheduled_jo if (scheduled_time.day == SUNDAY) highlight_y = TILE_Y(MONDAY); if (overflow_to_next_day > 0) { - int extra_days = overflow_to_next_day / TIME_SLOTS_PER_DAY; + int extra_days = ceil(overflow_to_next_day / (float)TIME_SLOTS_PER_DAY); int curr_day = scheduled_time.day; + //printf("%d %d %d\n", extra_days, duration_of_job_measured_in_timeslots_max, overflow_to_next_day); for (int xx = 0; xx < extra_days; xx++) { s32 overflow = overflow_to_next_day % TIME_SLOTS_PER_DAY; if (xx != extra_days-1) overflow = TIME_SLOTS_PER_DAY; + else if (overflow == 0) { + overflow = TIME_SLOTS_PER_DAY; + } if (curr_day == SUNDAY) { curr_day = MONDAY; highlight_y = TILE_Y(MONDAY); } - printf("%d %d %d\n", overflow_to_next_day, extra_days, overflow); + //printf("%d %d %d\n", overflow_to_next_day, extra_days, overflow); renderer->render_rectangle(TILE_X(0), highlight_y, timeslot_w*overflow, timeslot_h, tile_color_bg); overflow_to_next_day -= TIME_SLOTS_PER_DAY; highlight_y += timeslot_h; diff --git a/src/scenes/save_state_select.c b/src/scenes/save_state_select.c index eea022d..af2abe6 100644 --- a/src/scenes/save_state_select.c +++ b/src/scenes/save_state_select.c @@ -567,8 +567,220 @@ static void load_save_file(s32 index) array_push(&existing_loc->schedule.jobs, &existing_job); } + + GET_PROP(insights, location_iter, "insights"); + cJSON* insight_iter; + cJSON_ArrayForEach(insight_iter, insights) + { + money_data_collection year_data; + memset(&year_data, 0, sizeof(money_data_collection)); + + for (int i = 0; i < MONTHS_IN_YEAR; i++) + { + year_data.months[i].total_income = NAN; + } + + GET_PROP(months, insight_iter, "months"); + cJSON* month_iter; + cJSON_ArrayForEach(month_iter, months) + { + money_data month; + + GET_PROP(total_income, month_iter, "total_income"); + month.total_income = (float)total_income->valuedouble; + + GET_PROP(total_expenses, month_iter, "total_expenses"); + month.total_expenses = (float)total_expenses->valuedouble; + + GET_PROP(total_profit, month_iter, "total_profit"); + month.total_profit = (float)total_profit->valuedouble; + + GET_PROP(income_from_trips, month_iter, "income_from_trips"); + month.income_from_trips = (float)income_from_trips->valuedouble; + + GET_PROP(expenses_from_trucks, month_iter, "expenses_from_trucks"); + month.expenses_from_trucks = (float)expenses_from_trucks->valuedouble; + + GET_PROP(expenses_from_utility, month_iter, "expenses_from_utility"); + month.expenses_from_utility = (float)expenses_from_utility->valuedouble; + + GET_PROP(expenses_from_loans, month_iter, "expenses_from_loans"); + month.expenses_from_loans = (float)expenses_from_loans->valuedouble; + + GET_PROP(expenses_from_healthcare, month_iter, "expenses_from_healthcare"); + month.expenses_from_healthcare = (float)expenses_from_healthcare->valuedouble; + + GET_PROP(expenses_from_repairs, month_iter, "expenses_from_repairs"); + month.expenses_from_repairs = (float)expenses_from_repairs->valuedouble; + + GET_PROP(expenses_from_fuel, month_iter, "expenses_from_fuel"); + month.expenses_from_fuel = (float)expenses_from_fuel->valuedouble; + + GET_PROP(expenses_from_employees, month_iter, "expenses_from_employees"); + month.expenses_from_employees = (float)expenses_from_employees->valuedouble; + + GET_PROP(index, month_iter, "index"); + year_data.months[index->valueint] = month; + } + + array_push(&existing_loc->insights, &year_data); + } + } + + GET_PROP(insights, json_object, "insights"); + cJSON* insight_iter; + cJSON_ArrayForEach(insight_iter, insights) + { + money_data_collection year_data; + memset(&year_data, 0, sizeof(money_data_collection)); + + for (int i = 0; i < MONTHS_IN_YEAR; i++) + { + year_data.months[i].total_income = NAN; + } + + GET_PROP(months, insight_iter, "months"); + cJSON* month_iter; + cJSON_ArrayForEach(month_iter, months) + { + money_data month; + + GET_PROP(total_income, month_iter, "total_income"); + month.total_income = (float)total_income->valuedouble; + + GET_PROP(total_expenses, month_iter, "total_expenses"); + month.total_expenses = (float)total_expenses->valuedouble; + + GET_PROP(total_profit, month_iter, "total_profit"); + month.total_profit = (float)total_profit->valuedouble; + + GET_PROP(income_from_trips, month_iter, "income_from_trips"); + month.income_from_trips = (float)income_from_trips->valuedouble; + + GET_PROP(expenses_from_trucks, month_iter, "expenses_from_trucks"); + month.expenses_from_trucks = (float)expenses_from_trucks->valuedouble; + + GET_PROP(expenses_from_utility, month_iter, "expenses_from_utility"); + month.expenses_from_utility = (float)expenses_from_utility->valuedouble; + + GET_PROP(expenses_from_loans, month_iter, "expenses_from_loans"); + month.expenses_from_loans = (float)expenses_from_loans->valuedouble; + + GET_PROP(expenses_from_healthcare, month_iter, "expenses_from_healthcare"); + month.expenses_from_healthcare = (float)expenses_from_healthcare->valuedouble; + + GET_PROP(expenses_from_repairs, month_iter, "expenses_from_repairs"); + month.expenses_from_repairs = (float)expenses_from_repairs->valuedouble; + + GET_PROP(expenses_from_fuel, month_iter, "expenses_from_fuel"); + month.expenses_from_fuel = (float)expenses_from_fuel->valuedouble; + + GET_PROP(expenses_from_employees, month_iter, "expenses_from_employees"); + month.expenses_from_employees = (float)expenses_from_employees->valuedouble; + + GET_PROP(index, month_iter, "index"); + year_data.months[index->valueint] = month; + } + + array_push(&new_world->insights, &year_data); } + GET_PROP(active_jobs, json_object, "active_jobs"); + cJSON* active_job_iter; + cJSON_ArrayForEach(active_job_iter, active_jobs) + { + active_job aj; + + GET_PROP(day, active_job_iter, "day"); + aj.day = (s16)day->valueint; + + GET_PROP(timeslot, active_job_iter, "timeslot"); + aj.timeslot = (s16)timeslot->valueint; + + GET_PROP(stay_at_destination, active_job_iter, "stay_at_destination"); + aj.stay_at_destination = (bool)stay_at_destination->valueint; + + GET_PROP(duration_sec, active_job_iter, "duration_sec"); + aj.duration_sec = (time_t)duration_sec->valuedouble; + + GET_PROP(left_at, active_job_iter, "left_at"); + aj.left_at = (time_t)left_at->valuedouble; + + GET_PROP(done_at, active_job_iter, "done_at"); + aj.done_at = (time_t)done_at->valuedouble; + + GET_PROP(reversed, active_job_iter, "reversed"); + aj.reversed = (bool)reversed->valueint; + + GET_PROP(assignee_id, active_job_iter, "assignee_id"); + u32 emp_id = (u32)assignee_id->valuedouble; + aj.assignee = *get_employee_by_id_global(new_world, emp_id); + + GET_PROP(truck_id, active_job_iter, "truck_id"); + u32 tr_id = (u32)truck_id->valuedouble; + aj.assigned_truck = *get_truck_by_id(new_world, tr_id); + + GET_PROP(offer, active_job_iter, "offer"); + { // Load job offer of existing job. + job_offer new_offer; + new_offer.connections = array_create(sizeof(world_location*)); + + GET_PROP(id, offer, "id"); + new_offer.id = id->valueint; + + GET_PROP(expire_date, offer, "expire_date"); + new_offer.expire_date = (s64)expire_date->valuedouble; + + GET_PROP(company_id, offer, "company_id"); + new_offer.company = world_get_company_by_name(new_world, company_id->valuestring); + + GET_PROP(prod, offer, "product"); + new_offer.product = world_get_product_by_name(new_world, prod->valuestring); + + GET_PROP(shipday_count, offer, "shipday_count"); + new_offer.shipday_count = (s32)shipday_count->valueint; + + GET_PROP(reward, offer, "reward"); + new_offer.reward = (u32)reward->valueint; + + GET_PROP(total_distance, offer, "total_distance"); + new_offer.total_distance = (float)total_distance->valuedouble; + + GET_PROP(boat_distance, offer, "boat_distance"); + new_offer.boat_distance = (float)boat_distance->valuedouble; + + GET_PROP(duration_sec_min, offer, "duration_sec_min"); + new_offer.duration_sec_min = (time_t)duration_sec_min->valuedouble; + + GET_PROP(duration_sec_max, offer, "duration_sec_max"); + new_offer.duration_sec_max = (time_t)duration_sec_max->valuedouble; + + GET_PROP(duration_sec_min_excluding_boat, offer, "duration_sec_min_excluding_boat"); + new_offer.duration_sec_min_excluding_boat = (time_t)duration_sec_min_excluding_boat->valuedouble; + + GET_PROP(shipdays, offer, "shipdays"); + cJSON* shipday_iter; + int shipday_index = 0; + cJSON_ArrayForEach(shipday_iter, shipdays) + { + new_offer.shipdays[shipday_index] = (weekday)shipday_iter->valueint; + shipday_index++; + } + + GET_PROP(connections, offer, "connections"); + cJSON* connection_iter; + cJSON_ArrayForEach(connection_iter, connections) + { + u64 id = (u32)connection_iter->valuedouble; + world_location* loc = get_world_location_by_id(new_world, id); + if (loc) array_push(&new_offer.connections, &loc); + } + + aj.offer = new_offer; + + array_push(&new_world->active_jobs, &aj); + } + } world_map_set_active_world(new_world); enable_insights_for_current_month(new_world); @@ -977,6 +1189,7 @@ static void write_save_file(s32 index) { cJSON_AddItemToObject(offer, "id", cJSON_CreateNumber(aj->offer.id)); cJSON_AddItemToObject(offer, "expire_date", cJSON_CreateNumber(aj->offer.expire_date)); + cJSON_AddItemToObject(offer, "reward", cJSON_CreateNumber(aj->offer.reward)); cJSON_AddItemToObject(offer, "company_id", cJSON_CreateString(aj->offer.company->name)); cJSON_AddItemToObject(offer, "product", cJSON_CreateString(aj->offer.product->name)); cJSON_AddItemToObject(offer, "shipday_count", cJSON_CreateNumber(aj->offer.shipday_count)); diff --git a/src/world.c b/src/world.c index 9130160..83ca6d9 100644 --- a/src/world.c +++ b/src/world.c @@ -6,6 +6,8 @@ static void world_payout_salaries(world* world); static void enable_insights_for_current_month(world* world); static vec2f get_world_location_for_job(platform_window* window, world* world, active_job* job); static employee* get_employee_by_id(world_location* location, u32 id); +static employee* get_employee_by_id_global(world* world, u32 id); +static truck* get_truck_by_id(world* world, u32 id); static void end_contract_with_employee(world* world, employee* emp); vec2f px_to_coords(platform_window* window, double x, double y); static bool world_check_location_accessibility(world_location* orig, world_location* source, int depth, double dist); @@ -405,7 +407,7 @@ static bool world_load_boat_routes_from_file(world* world) return true; } -company* world_get_product_by_name(world* world, char* str) +product* world_get_product_by_name(world* world, char* str) { for (int i = 0; i < world->companies.length; i++) { @@ -415,7 +417,7 @@ company* world_get_product_by_name(world* world, char* str) { product* p = array_at(&c->products, x); - if (strcmp(p->name, str) == 0) return c; + if (strcmp(p->name, str) == 0) return p; } } return 0; @@ -1114,6 +1116,35 @@ static employee* get_global_employee_by_id(world* world, u32 id) return 0; } +static employee* get_employee_by_id_global(world* world, u32 id) +{ + for (s32 x = 0; x < world->locations.length; x++) + { + world_location* location = array_at(&world->locations, x); + if (!location->is_owned) continue; + for (s32 i = 0; i < location->employees.length; i++) + { + employee* em = *(employee**)array_at(&location->employees, i); + if (em->id == id) return em; + } + } + return 0; +} + +static truck* get_truck_by_id(world* world, u32 id) +{ + for (s32 x = 0; x < world->locations.length; x++) + { + world_location* location = array_at(&world->locations, x); + if (!location->is_owned) continue; + for (s32 i = 0; i < location->trucks.length; i++) { + truck* emp = array_at(&location->trucks, i); + if (emp->id == id) return emp; + } + } + return 0; +} + static employee* get_employee_by_id(world_location* location, u32 id) { for (s32 i = 0; i < location->employees.length; i++) -- cgit v1.2.3-70-g09d2