From 2487efee98f77c2450d37622ea3d76fe453e702d Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Mon, 2 Dec 2024 16:14:02 +0100 Subject: close #45 --- src/world.c | 87 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/world.c b/src/world.c index 88c3dac..ae09d79 100644 --- a/src/world.c +++ b/src/world.c @@ -1290,6 +1290,30 @@ static void enable_insights_for_current_month(world* world) } } +static s32 world_count_employees(world* world) +{ + s32 count = 0; + for (s32 i = 0; i < world->locations.length; i++) + { + world_location* location = array_at(&world->locations, i); + if (!location->is_owned) continue; + count+=location->employees.length; + } + return count; +} + +static s32 world_count_owned_locations(world* world) +{ + s32 count = 0; + for (s32 i = 0; i < world->locations.length; i++) + { + world_location* location = array_at(&world->locations, i); + if (!location->is_owned) continue; + count++; + } + return count; +} + static world_location* get_random_owned_location(world* world) { s32 count = 0; @@ -1316,7 +1340,14 @@ static world_location* get_random_owned_location(world* world) static void end_contract_with_random_employee(world* world) { + s32 attempts = 0; + retry:; world_location* location = get_random_owned_location(world); + if (location->employees.length == 0) { + if (attempts == 2) return; + attempts++; + goto retry; + } employee* emp = *(employee**)array_at(&location->employees, get_random_number(0, location->employees.length)); char error_msg[MAX_EVENT_MESSAGE_LENGTH]; @@ -1328,8 +1359,14 @@ static void end_contract_with_random_employee(world* world) static void end_contract_with_random_job(world* world) { + s32 attempts = 0; + retry:; world_location* location = get_random_owned_location(world); - if (location->schedule.jobs.length == 0) return; + if (location->schedule.jobs.length == 0) { + if (attempts == 2) return; + attempts++; + goto retry; + } scheduled_job* scheduled_job = array_at(&location->schedule.jobs, get_random_number(0, location->schedule.jobs.length)); char error_msg[MAX_EVENT_MESSAGE_LENGTH]; @@ -1349,8 +1386,14 @@ static float get_fine_multiplier(world* world) static void give_random_fine(world* world) { + s32 attempts = 0; + retry:; world_location* location = get_random_owned_location(world); - if (location->employees.length == 0) return; + if (location->employees.length == 0) { + if (attempts == 2) return; + attempts++; + goto retry; + } employee* emp = *(employee**)array_at(&location->employees, get_random_number(0, location->employees.length)); s32 fine = get_random_number(world->money / 20, world->money / 10) * get_fine_multiplier(world); // fine is between 5% and 10% of current money. minimum 2k. @@ -1418,28 +1461,28 @@ static void world_start_random_events(world* world) { world->days_since_last_random_event++; - // Minor events - { - #define MIN_DELAY_BETWEEN_EVENTS (60) - float chance_of_random_event = 0.0f; - if (world->days_since_last_random_event > MIN_DELAY_BETWEEN_EVENTS) { - chance_of_random_event = ((world->days_since_last_random_event - MIN_DELAY_BETWEEN_EVENTS)*0.5f)/100.0f; - if (chance_of_random_event > 1.0f) chance_of_random_event = 1.0f; + #define MIN_DELAY_BETWEEN_EVENTS (7) + float chance_of_random_event = 0.0f; + + if (world->days_since_last_random_event > MIN_DELAY_BETWEEN_EVENTS) { - bool run_event = chance_of_random_event >= (get_random_number(0, 100)/100.0f); - - if (run_event) { - s32 rand_event = get_random_number(0, 5); - switch(rand_event) { - case 0: end_contract_with_random_employee(world); break; - case 1: end_contract_with_random_job(world); break; - case 2: give_random_fine(world); break; - case 3: brake_down_random_truck(world); break; - case 4: do_random_inspection(world); break; - } - - world->days_since_last_random_event = 0; + // example: 5 locations with 50 employees = 1.5% chance of random event/day. + chance_of_random_event = (world_count_owned_locations(world)*0.002) + (world_count_employees(world)*0.0001); + if (chance_of_random_event > 0.1f) chance_of_random_event = 0.1f; + + bool run_event = chance_of_random_event >= (get_random_number(0, 100)/100.0f); + + if (run_event) { + s32 rand_event = get_random_number(0, 5); + switch(rand_event) { + case 0: end_contract_with_random_employee(world); break; + case 1: end_contract_with_random_job(world); break; + case 2: give_random_fine(world); break; + case 3: brake_down_random_truck(world); break; + case 4: do_random_inspection(world); break; } + + world->days_since_last_random_event = 0; } } } -- cgit v1.2.3-70-g09d2