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/world.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'src/world.c') 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