#include "logic.h" #include #include #include "astronomy.h" #include "util.h" #define CHICK_SURVIVAL_TIME (10 * ONE_MINUTE) bool key_was_down = false; uint8_t update(struct game_state *new, const struct game_state *old) { new->time = time(NULL); new->lamp_on = old->lamp_on; new->start_time = old->start_time; new->lamp_switch_time = old->lamp_switch_time; new->num_days = (new->time - new->start_time) / ONE_DAY; if(new->time < old->time) { dbg_printf("new time < old time"); return STATUS_GAME_OVER_TIME; } if(new->time < new->start_time) { dbg_printf("time < start time"); return STATUS_GAME_OVER_TIME; } if(new->time < new->lamp_switch_time) { dbg_printf("time < lamp switch time"); return STATUS_GAME_OVER_TIME; } if(have_chicks_overheated(new->time, new->lamp_switch_time, new->lamp_on)) return STATUS_GAME_OVER_HOT; if(have_chicks_underheated(new->time, new->lamp_switch_time, new->lamp_on)) return STATUS_GAME_OVER_COLD; for(uint8_t i = 0; i < NUM_CHICKS; i++) { update_chick(&new->chicks[i], &old->chicks[i]); } bool key_is_down = kb_IsDown(kb_KeyEnter) || kb_IsDown(kb_Key2nd); if(key_is_down && !key_was_down) { //dbg_printf("NIGHT_START %lu\n", last_solar_time(NIGHT_START, new->time)); //dbg_printf("NIGHT_END %lu\n", last_solar_time(NIGHT_END, new->time)); //dbg_printf("DAY_START %lu\n", last_solar_time(DAY_START, new->time)); //dbg_printf("DAY_END %lu\n", last_solar_time(DAY_END, new->time)); new->lamp_switch_time = new->time; new->lamp_on = !new->lamp_on; } key_was_down = key_is_down; if(kb_IsDown(kb_KeyClear)) return STATUS_EXIT; return STATUS_IN_PROGRESS; } bool lamp_should_be_on(time_t time) { return last_solar_time(NIGHT_END, time) < last_solar_time(DAY_END, time); } bool are_chicks_hot(time_t time, bool lamp_on) { return lamp_on && last_solar_time(DAY_END, time) < last_solar_time(DAY_START, time); } bool are_chicks_cold(time_t time, bool lamp_on) { return !lamp_on && last_solar_time(NIGHT_END, time) < last_solar_time(NIGHT_START, time); } bool have_chicks_overheated(time_t current_time, time_t lamp_switch_time, bool lamp_on) { if(!lamp_on) return false; time_t day_end = last_solar_time(DAY_END, current_time); time_t day_start = last_solar_time(DAY_START, current_time); // Cast to long as time_t is unsigned if((long)(day_end - lamp_switch_time) > CHICK_SURVIVAL_TIME) { dbg_printf("overheated: day_end - switch_time = %li - %li > %i\n", day_end, lamp_switch_time, CHICK_SURVIVAL_TIME); return true; } if(day_end < day_start && (long)(current_time - lmax(day_start, lamp_switch_time)) > CHICK_SURVIVAL_TIME) { dbg_printf("overheated: current_time - day_start = %li - %li > %i\n", current_time, day_start, CHICK_SURVIVAL_TIME); return true; } return false; } bool have_chicks_underheated(time_t current_time, time_t lamp_switch_time, bool lamp_on) { if(lamp_on) return false; time_t night_end = last_solar_time(NIGHT_END, current_time); time_t night_start = last_solar_time(NIGHT_START, current_time); // Cast to long as time_t is unsigned if((long)(night_end - lamp_switch_time) > CHICK_SURVIVAL_TIME) { dbg_printf("underheated: night_end - switch_time = %li - %li > %i\n", night_end, lamp_switch_time, CHICK_SURVIVAL_TIME); return true; } if(night_end < night_start && (long)(current_time - lmax(night_start, lamp_switch_time)) > CHICK_SURVIVAL_TIME) { dbg_printf("underheated: current_time - night_start = %li - %li > %i\n", current_time, night_start, CHICK_SURVIVAL_TIME); return true; } return false; }