From b8229df5ab46911320e4ed25bba8444a1d1f3574 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Thu, 5 Dec 2024 18:26:47 +0100 Subject: performance fix --- src/include/world.h | 3 +++ src/scenes/place_detail.c | 4 +++- src/scenes/save_state_select.c | 1 + src/scenes/world_map.c | 3 ++- src/world.c | 49 ++++++++++++++++++++++++++++++++++++++---- 5 files changed, 54 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/include/world.h b/src/include/world.h index b11f43c..5daf88b 100644 --- a/src/include/world.h +++ b/src/include/world.h @@ -161,6 +161,9 @@ typedef struct t_employee color hair_color; color face_color; color body_color; + + // not saved + float current_scheduled_weekly_hours; } employee; #define RESUME_FADEOUT_MS 300 diff --git a/src/scenes/place_detail.c b/src/scenes/place_detail.c index f3e1223..39b9df4 100644 --- a/src/scenes/place_detail.c +++ b/src/scenes/place_detail.c @@ -405,7 +405,7 @@ static void place_detail_draw_employees(platform_window* window, tab tab, float // Hours { char buffer[50]; - float total_hours = get_worked_hours_per_week_for_employee(_active_world, emp, 0); + float total_hours = emp->current_scheduled_weekly_hours;//get_worked_hours_per_week_for_employee(_active_world, emp, 0); sprintf(buffer, "Scheduled: %.0f hours", total_hours); s32 text_x = x + item_h + item_part_w*1 + text_pad; @@ -1132,6 +1132,8 @@ static void place_detail_draw_schedule(platform_window* window) else if (_active_schedule_state == RESCHEDULING_JOB) *_active_selected_scheduled_job = _active_scheduling_job; + world_set_weekly_hours_worked(_active_world); + job_offer* offer = get_job_offer_by_id(_active_location, _active_scheduling_job.offer.id); if (offer) array_remove_by(&_active_location->job_offers, offer); diff --git a/src/scenes/save_state_select.c b/src/scenes/save_state_select.c index ae428af..2a2da43 100644 --- a/src/scenes/save_state_select.c +++ b/src/scenes/save_state_select.c @@ -783,6 +783,7 @@ static void load_save_file(s32 index) } world_map_set_active_world(new_world); + world_set_weekly_hours_worked(new_world); //enable_insights_for_current_month(new_world); world_update_location_scores(new_world); diff --git a/src/scenes/world_map.c b/src/scenes/world_map.c index 410e8f8..31eaa1a 100644 --- a/src/scenes/world_map.c +++ b/src/scenes/world_map.c @@ -1290,8 +1290,9 @@ void world_map_scene_render(platform_window* window) break; } - renderer->set_render_depth(3); + renderer->set_render_depth(8); if (currently_viewing_active_job.offerid != INVALID_ID) world_map_draw_viewing_job(window); + renderer->set_render_depth(3); if (_active_world) { world_update_result click_result = world_render(window, _active_world, scene_state == WORLD_SCENE_STATE_IDLE); diff --git a/src/world.c b/src/world.c index a19e924..77748a7 100644 --- a/src/world.c +++ b/src/world.c @@ -319,6 +319,7 @@ static employee* create_employee(world* world, world_location* hired_at) employee1->current_location_id = INVALID_ID; employee1->assigned_truck = 0; employee1->active_job_id = INVALID_ID; + employee1->current_scheduled_weekly_hours = 0.0f; // Portrait generation. { @@ -1019,7 +1020,8 @@ employee* world_get_most_available_employee_for_location(world* world, world_loc { employee* em = *(employee**)array_at(&loc->employees, i); if (em->assigned_truck == 0) continue; - float hours_worked = get_worked_hours_per_week_for_employee(world, em, 0); + float hours_worked = em->current_scheduled_weekly_hours; + //printf("%d %f\n", em->id, hours_worked); if (hours_worked < best_match_hours) { best_match_hours = hours_worked; best_match = em; @@ -1298,11 +1300,49 @@ static float get_worked_hours_per_week_for_employee(world* world, employee* emp, return total / 3600.0f; } +static void world_set_weekly_hours_worked(world* world) +{ + // Reset hours + for (s32 i = 0; i < world->locations.length; i++) + { + world_location* location = array_at(&world->locations, i); + if (!location->is_owned) continue; + + for (s32 r = 0; r < location->employees.length; r++) + { + employee* emp = *(employee**)array_at(&location->employees, r); + emp->current_scheduled_weekly_hours = 0.0f; + } + } + + float total = 0.0f; + for (s32 i = 0; i < world->locations.length; i++) + { + world_location* location = array_at(&world->locations, i); + if (!location->is_owned) continue; + + for (s32 f = 0; f < location->schedule.jobs.length; f++) { + scheduled_job* job = array_at(&location->schedule.jobs, f); + + for (s32 s = 0; s < MAX_SHIPDAYS; s++) { + scheduled_job_time slot = job->timeslots[s]; + if (slot.assignee) { + float hworked = job->offer.duration_sec_min_excluding_boat * get_shiptime_factor(slot.assignee); + if (!slot.stay_at_destination) hworked *= 2; + + slot.assignee->current_scheduled_weekly_hours += (hworked / 3600.0f); + } + } + } + } + return total / 3600.0f; +} + // Run once per day. static void world_update_employee_happiness(world* world, employee* emp) { // Calculate overworking float max_weekly_hours = MAX_WORKED_HOURS_WEEKLY; - float hours_worked = get_worked_hours_per_week_for_employee(world, emp, 0); + float hours_worked = emp->current_scheduled_weekly_hours;//get_worked_hours_per_week_for_employee(world, emp, 0); float hours_overworked = hours_worked - max_weekly_hours; if (hours_overworked > 0) emp->happiness = 1.0f - ((hours_overworked/3.0f)*0.2f); else emp->happiness = 1.0f; @@ -1532,7 +1572,7 @@ static void do_random_inspection(world* world) for (s32 x = 0; x < location->employees.length; x++) { employee* em = *(employee**)array_at(&location->employees, x); - float total_hours = get_worked_hours_per_week_for_employee(world, em, 0); + float total_hours = em->current_scheduled_weekly_hours; if (total_hours > MAX_WORKED_HOURS_WEEKLY) total_employees_overworking++; } @@ -1731,13 +1771,14 @@ static void world_run_simulation_tick(world* world) } if (prev_time->tm_wday != curr_time->tm_wday) { // Run once per day + world_set_weekly_hours_worked(world); world_remove_expired_job_offers(world); world_assign_resumes_to_locations(world); world_start_random_events(world); world_assign_new_job_offers(world, false); world_update_location_scores(world); - if (curr_time->tm_mday <= 28) { + if (curr_time->tm_mday <= 28) { world_payout_salaries(world); // Pay salary first 28 days of month. world_pay_investments(world); world_update_loans(world); -- cgit v1.2.3-70-g09d2