code:
# next_day

*p '	'
!Next Day
!Overhauled by crushboss

gs '#var_base'
gs '#txt' &! after #var_base
gs '#txt_2' &! after #var_base

killvar 'play_sound'
killvar '$text'
killvar 'sex'
killvar 'sex_num'
killvar 'sex_done_today'
killvar 'sex_done_thisperson'
killvar 'assistant_already_sex_today'
killvar 'slave_already_sex_today'
killvar 'суперудар_сделан'

base_score += master_mood &! this should happen before daily vars are reset

$reset_daily_master_vars = {
	!Reset mood rate daily (persistent effects go in moodlets)
	master_mood_rate = 0 &! this var is adjusted later in next_day

	!Following vars are not used anywhere else in next_day
	master_half_action = 0
	master_massaged = 0
	master_spaed = 0
	master_erected = 0
	master_styled = 0

	!Update daily counters (alone_count = days with no slave and no assistant)
	if slave_state < CONST_INT['slave_exist'] and assistant_state < CONST_INT['slave_exist']:
		!check < exist instead of = no_slave: living with dead slaves counts as being alone - ImperatorAugustus
		alone_count += 1
	end
}
dynamic $reset_daily_master_vars &! TODO: determine best placement of this

$next_day_update_daily_slave_vars = {
	if dynslave_state ! CONST_INT['no_slave']:
		! state check includes dead, conscious and fainted

		!Reset mood rate daily (persistent effects go in moodlets)
		dynslave_rate['mood'] = 0
		!note mood is not reset yet, so the ending mood for the day remains unchanged and can be referenced until the end of next_day - ImperatorAugustus
		!Lower rehabilitation time by one each day or reset it to zero if negative
		if dynslave['rehabilitation'] ! 0: dynslave['rehabilitation'] = max(0, dynslave['rehabilitation'] - 1)
		!Reset active spells (sententia veritas is intentionally omitted here so it can persist overnight)
		dynslave['domini_dictum'] = 0
		!Reset daily activity flags
		dynslave['beaten'] = 0
		dynslave['massaged'] = 0
		dynslave['tattoo_timeup'] = 0
		!Update style
		if dynslave['makeup'] = 1: dynslave['makeup'] = 0
		if dynslave['perfume'] = 1: dynslave['perfume'] = 0
		if dynslave['epilation'] > 0: dynslave['epilation'] -= 1
		if dynslave['manicure'] > 0: dynslave['manicure'] -= 1
		if dynslave['hairstyle'] > 0: dynslave['hairstyle'] -= 1
		!removed hair hardcode state, hairfix
		!if dynslave['hairstyle'] > 0: dynslave['hairstyle_demo'] = 1
		! fixed the typo, by Lyazar
		if dynslave_inventory['perfume'] > 0: dynslave['perfume'] = 1
		!Reset counters
		dynslave['half_action'] = 0
	end
}

dynamic $do_for_each_slave_in_list, '$next_day_update_daily_slave_vars', '$unfrozen_slave_characters' &! all except iced (see #dynamic and #static_base)

!Debug
!msg 'total_food_cost = <<total_food_cost>>'
if search['active'] = 1: gs '$expert_search'
npc = 0

debt_count -= 1
if debt_count < 0 and debt > 0:
	dynamic $death, 'As in any other city, in the Eternal Rome it is reckless to forget to repay money you have borrowed. You died a disgraceful death at the hand of the moneylender’s henchmen...'
end

stock_limit = rand(5,8)
stock_sold = 0
auction_phase = 0
haggling_stage = 0
sl_txt = 0

if gladi_count > 0:
	gladi_count -= 1
else
	gladi_count = 10
end

!Testing Code - every day is a Gladiatrix contest, Race contest, or auction day - crushboss
!gladi_count = 0
!ride_count = 1
!auction_count = 1
!End of Testing Code - crushboss

if ride_count > 0:
	ride_count -= 1
else
	ride_count = 10
	!Creating a route to the next race
	$trace[1] = 'ride_start'

	i = 1
	:loop_trace
		i += 1
		a = rand(1,12)
		$trace[i] = $trace_type[a]
	if i < 11: jump 'loop_trace'

	$trace[12] = 'ride_finish'
end

if auction_count > 0:
	auction_count -= 1
else
	auction_count = 10
end

!training contract completion check or elapsed days counter increment
if contract['active'] = 2: contract['active'] = 0
if contract['active'] = 1: contract['time'] += 1

!Daily Expenses (charged per-decade in billing)
if stasis_tank > 0: decade_cryo_costs += 2
if iced_1_state ! CONST_INT['no_slave']: decade_cryo_costs += 1
if iced_2_state ! CONST_INT['no_slave']: decade_cryo_costs += 1
if iced_3_state ! CONST_INT['no_slave']: decade_cryo_costs += 1
if iced_4_state ! CONST_INT['no_slave']: decade_cryo_costs += 1
if iced_5_state ! CONST_INT['no_slave']: decade_cryo_costs += 1
if iced_6_state ! CONST_INT['no_slave']: decade_cryo_costs += 1

!NPC state
gs 'npc_progress'
farid_debt += farid_potential_debt
farid_potential_debt = 0

!Beginning of assistant checks - no calculations if no assistant exists - crushboss
if assistant_state > CONST_INT['no_slave']:
	!assistant part
	assistant['calories'] = 3 + assistant['stamina']
	assistant_food_cost = 2
	total_food_cost += assistant_food_cost

	!chance of dying at zero value health - assistant
	!base - 14% - min 6% max 20%
	if assistant_state = CONST_INT['slave_exist'] and assistant['stamina'] = 0 and master_medic < 5:
		dprob_max = 7
		! psy status affect the chance
		if $assistant_psy_status = 'servile' or $assistant_psy_status = 'obedient': dprob_max = 16
		if $assistant_psy_status = 'soft' or $assistant_psy_status = 'optimistic': dprob_max = 9
		if $assistant_psy_status = 'broken': dprob_max = 5
		death_prob = 1 + ((97 * day) mod dprob_max) &! pseudo-rand(1,dprob_max)
		if death_prob = 5:
			assistant_died = 1
		end
	end

	!Training bonuses for easy obedience difficulty - crushboss
	if difficulty_setting = 1 and assistant_state = CONST_INT['slave_exist']:
		moral_basis = ((master_str + master_domination + master_fighter + master_cha)/5)
		if moral_basis < 1: moral_basis = 1
		assistant_rate['moral'] += moral_basis
		assistant_rate['custom'] += 1
		assistant_rate['rational'] += 1
		assistant_rate['instinct'] += 1
		assistant_rate['spoil'] -= 5
	end

	!brand effect - assistant
	if assistant_state = CONST_INT['slave_exist'] and assistant['brand'] > 0: assistant_rate['custom'] += 1
	if assistant_state = CONST_INT['slave_exist'] and assistant['brand'] = 3: assistant_rate['custom'] += 1

	!Normalize mood and arousal and clear merit and guilt for assistant - crushboss (simplified - ImperatorAugustus)
	assistant_rate['mood'] = 10 &! +1 mood for being trusted with assistant role - ImperatorAugustus

	!reset all that does not affect the assistant
	assistant['virtue'] = 0
	assistant['sin'] = 0

end &! End of assistant checks - do not remove this end statement - crushboss

!independent slave actions
slave_attack = 0
slave_escape = 0
slave_suicide = 0
!Slave cannot attack, escape or kill herself if she is mind-broken or locked in a cell at night (-2)
if slave['sleep_condition'] ! -2 and $slave_psy_status ! 'broken':

	!added check for health and unconsciousness, if health is zero or if she is lying unconscious, the slave will not be able to escape or attack - the_vm
	if slave['stamina'] > 0 and slave_state = CONST_INT['slave_exist']:

		if slave['mood'] < 0 and slave['temper'] > slave['obedience']:
			n = 7 + master_cha - slave['angst']
			if assistant_state = CONST_INT['slave_exist']:
				n += assistant['intellect'] + assistant['moral']
				if assistant['supervise_rule'] = 1: n += assistant['ego'] + assistant['gladiatrix']
			end

			if (97 * day) mod n = 1:
				if slave['angst'] > 0 and (slave['gladiatrix'] + slave['angst']) > (master_supermacy - master_allure):
					slave_attack = 1
				elseif slave['slave_collar'] = 0:
					!Slave without collar will escape successfully unless she has a magical brand
					if slave['brand'] = 3:
						slave_escape = 2
					else
						slave_escape = 1
					end
				elseif slave['brand'] = 1 or slave['brand'] = 2:
					!Slave is returned if wearing a slave collar and has your brand - crushboss
					slave_escape = 3
				elseif slave['brand'] = 4:
					!Slave is returned if wearing a slave collar and the guild brand - crushboss
					slave_escape = 4
				end
			end
		end

		!Added an extra chance for the slave to turn on her master and attack - crushboss (relaxed conditions - ImperatorAugustus)
		if slave['angst'] > 2 and slave['stamina'] > 3 and slave['gladiatrix'] > 0 and slave['moral'] = 0 and slave['mood'] < 0 and (master_supermacy - master_allure) < 2 and exam_in_progress = 0:
			slave_attack = 1
			slave_escape = 0
		end

		!Slave will not escape or attack with high obedience or devotion - crushboss (removed instinct condition - ImperatorAugustus)
		if slave['obedience'] > 7 or slave['moral'] > 1:
			slave_escape = 0
			slave_attack = 0
		end

		if slave['mood'] = -5 and slave['angst'] > max(slave['ego'], slave['temper']) and ((97 * day) mod 11) < slave['angst']: slave_suicide = 1 &! pseudo-rand(0, 10)

	end &! end health and consciousness check

	!slave cannot escape during exam in training mode
	!slaves will heal faster from wounds in tutorial mode since there is no medical center to visit - crushboss
	!slaves will recover from negative energy in tutorial - crushboss
	if exam_in_progress = 1:
		slave_escape = 0
		if slave_rate['wounds'] > 0: slave_rate['wounds'] -= slave['stamina'] + 2
		! fixed reversed sign (was adding stamina to wounds rate instead of subtracting) - ImperatorAugustus
		if slave['energy'] < 0: slave['energy'] += 1
		if slave['energy'] < 0 and slave['stamina'] > 3: slave['energy'] += 1
	end

end &! end of not-sleeping-in-dungeon check

!Slave Cook rule
if kitchen > 0 and cooked_food <= 0 and slave_state = CONST_INT['slave_exist'] and slave['cooks_rule'] = 1 and slave['energy'] >= 0 and slave['obedience'] >= -6 + IIF(slave['pride'] > 2, -2, 0) + IIF(slave['ego'] < 3, 1, 0) + IIF(slave['intellect'] < 3, 1, 0):
	slave_auto_cook = 1 &! flag for end_of_day_screen
	cooked_food = slave['cook']
	house_mess_rate += 5
	$misc_bonus = 'learn_bonus["cook"]'
	$target_skill = 'cook'
	$target_affinity = 'cook_affinity'
	tutor_modifier = 0
	dynamic $replace($dyn_diligence, 'dyn'+'slave', 'slave')
	dynamic $replace($dyn_skill_rise, 'dyn'+'slave', 'slave')
	slave['energy'] -= 1 &! not using energy_drop here so end of day calorie display is reliable
end

!Assistant Cooks Rule (or if slave was supposed to do it but did not)
if kitchen > 0 and cooked_food <= 0 and assistant_state = CONST_INT['slave_exist'] and (assistant['cooks_rule'] = 1 or slave['cooks_rule'] = 1) and assistant['energy'] >= 0:
	assistant_auto_cook = 1 &! flag for end_of_day_screen
	cooked_food = min(assistant['cook'], assistant['moral'])
	house_mess_rate += 5
	$misc_bonus = 'learn_bonus["cook"]'
	$target_skill = 'cook'
	$target_affinity = 'cook_affinity'
	tutor_modifier = 0
	dynamic $replace($dyn_diligence, 'dyn'+'slave', 'assistant')
	dynamic $replace($dyn_skill_rise, 'dyn'+'slave', 'assistant')
	assistant['energy'] -= 1 &! not using energy_drop here so end of day calorie display is reliable
end

!Master will make a meal if no one else does so he eats well - crushboss
if master_energy >= 0 and cooked_food <= 0 and kitchen > 0:
	master_auto_cook = 1 &! flag for end_of_day_screen
	$interaction_type = 'special'
	master_butler_rate += (skill_adv_mul * 1)
	master_moodlet['neg_cook'] = max(0, 3 + brand_reputation - master_butler)
	dynamic $master_energy_drop
	if cooked_food < master_butler: cooked_food = min(5, master_butler)
	house_mess_rate += 5
	master_hygiene_rate += 5
end

!Decrease Fiend Milking count by one each day - crushboss
if spawn_milked >= tentacle_size: spawn_milked = max(1, tentacle_size - 1) &! ensure it is hungry soon enough to continue growing if milked regularly until final stage -ImperatorAugustus
if spawn_milked > 0: spawn_milked -= 1

!Slave Tentacle rule - crushboss
if pet_tentacle > 0 and slave_state = CONST_INT['slave_exist'] and slave['tentacle_rule'] = 1 and slave['energy'] >= 0:
	interaction_slave['repulse'] = IIF(slave['virginity'] < 1, 25, 24) &! same as milk the fiend interaction
	stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
	orgastic = 1
	disgusting = 1
	vermin_based = 1
	dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
	dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
	if tentacle_state = 1 or spawn_milked = 0 and interaction_slave['res'] >= 0:
		slave_auto_tentacle = IIF(slave['energy'] > 0, 1, 2) &! flag for end_of_day_screen
		slave_rate['hygiene'] += 30
		tentacle_hp += 1
		if slave['sub_tentacles'] > tentacle_size:
			tentacle_hp += 2
			spawn_semen += semen_gain[max(0, tentacle_size - spawn_milked)]
		end
		if slave['energy'] > 0:
			slave['energy'] -= 1 &! not using energy_drop here so end of day calorie display is reliable
		else
			slave['half_action'] += 2 &! queue up energy drop for next morning
		end
	end
end
if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')

!Assistant Tentacle rule - crushboss
if pet_tentacle > 0 and assistant_state = CONST_INT['slave_exist'] and assistant['tentacle_rule'] = 1 and assistant['energy'] >= 0:
	if tentacle_state = 1 or spawn_milked = 0:
		assistant_auto_tentacle = IIF(assistant['energy'] > 0, 1, 2) &! flag for end_of_day_screen
		assistant_rate['hygiene'] += 30
		tentacle_hp += 1
		if assistant['sub_tentacles'] > tentacle_size:
			tentacle_hp += 2
			spawn_semen += semen_gain[max(0, tentacle_size - spawn_milked)]
		end
		if assistant['energy'] > 0:
			assistant['energy'] -= 1 &! not using energy_drop here so end of day calorie display is reliable
		else
			assistant['half_action'] += 2 &! queue up energy drop for next morning
		end
	end
end

!Tentacle state - milked or needs milking - crushboss
if pet_tentacle = 0:
	tentacle_state = 0
else
	if spawn_milked = 0: tentacle_state = 1
	if spawn_milked > tentacle_size: tentacle_state = 2
end

!Slave cleans rule
if house_mess > 1 and slave_state = CONST_INT['slave_exist'] and slave['cleans_rule'] = 1 and slave['energy'] >= 0 and slave['obedience'] >= -5 + IIF(slave['stamina'] < 3, 1, -1) + IIF(slave['intellect'] > 2, 1, -1) + IIF(slave['pride'] > 2, slave['pride'] - 2, 3 - slave['pride']) + IIF(slave['pride'] > 2, -2, 0) + IIF(slave['ego'] < 3, 1, 0) + IIF(slave['intellect'] < 3, 1, 0):
	slave_auto_clean = IIF(slave['energy'] > 0 or house_mess > 3, 1, 2) &! flag for end_of_day_screen
	slave_rate['hygiene'] += house_mess + 2
	slave_skill = min(slave['mood'] + 2, slave['service'])
	house_mess_rate -= max(8, slave_skill*16) &! added missing formula to clean based on ability instead of full cleaning - ImperatorAugustus
	dynamic $house_cleaning &! never worked as intended because $house_cleaning was enclosed in quotes - ImperatorAugustus
	if slave_skill < 3 and house_mess = 0: house_mess_rate = 10 &! B+ skill or better is needed to reach perfect cleanliness
	$misc_bonus = 'learn_bonus["service"]'
	$target_skill = 'service'
	$target_affinity = 'service_affinity'
	tutor_modifier = 0
	dynamic $replace($dyn_diligence, 'dyn'+'slave', 'slave')
	dynamic $replace($dyn_skill_rise, 'dyn'+'slave', 'slave')
	if slave['energy'] > 0 or house_mess > 3:
		slave['energy'] -= 1 &! not using energy_drop here so end of day calorie display is reliable
	else
		slave['half_action'] += 2 &! queue up energy drop for next morning
	end
end

!Assistant cleans rule (or without the rule, if it is messy)
if house_mess > 1 and assistant_state = CONST_INT['slave_exist'] and (assistant['cleans_rule'] = 1 or house_mess > 2) and assistant['energy'] >= 0:
	assistant_auto_clean = IIF(assistant['energy'] > 0 or house_mess > 3, 1, 2) &! flag for end_of_day_screen
	assistant_rate['hygiene'] += house_mess + 2
	house_mess_rate -= max(8, assistant['service']*16) &! added missing formula to clean based on ability instead of full cleaning - ImperatorAugustus
	dynamic $house_cleaning &! never worked as intended because $house_cleaning was enclosed in quotes - ImperatorAugustus
	if assistant['service'] < 3 and house_mess = 0: house_mess_rate = 10 &! B+ skill or better is needed to reach perfect cleanliness
	$misc_bonus = 'learn_bonus["service"]'
	$target_skill = 'service'
	$target_affinity = 'service_affinity'
	tutor_modifier = 0
	dynamic $replace($dyn_diligence, 'dyn'+'slave', 'assistant')
	dynamic $replace($dyn_skill_rise, 'dyn'+'slave', 'assistant')
	if assistant['energy'] > 0 or house_mess > 3:
		assistant['energy'] -= 1 &! not using energy_drop here so end of day calorie display is reliable
	else
		assistant['half_action'] += 2 &! queue up energy drop for next morning
	end
end

!Master will clean the house if no one else does and it is really messy - crushboss
if master_energy >= 0 and house_mess > 3:
	master_auto_clean = 1 &! flag for end_of_day_screen
	$interaction_type = 'special'
	master_butler_rate += (skill_adv_mul * 1)
	master_moodlet['neg_cleaning'] = 1 + brand_reputation
	dynamic $master_energy_drop
	master_hygiene_rate += house_mess + 2
	house_mess_rate -= max(8, master_butler*16)
	dynamic $house_cleaning
end

!Slave washes master rule
if bath > 0 and master_hygiene > 0 and slave_state = CONST_INT['slave_exist'] and slave['washes_master_rule'] = 1 and slave['energy'] >= 0:
	interaction_slave['repulse'] = 0 &! same tier as seduction, masturbation lessons
	stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
	impotent = 1 &! does not require the master to achieve an erection - ImperatorAugustus
	if slave['moral'] < 1: shameful = 1 &! interacts with slave exhibitionism trait when non-devoted slave is exposed to master
	dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
	dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
	if interaction_slave['res'] >= 0:
		slave_auto_wash = IIF(slave['energy'] > 0, 1, 2) &! flag for end_of_day_screen
		slave_rate['arousal'] -= master_libido
		master_hygiene_rate = 0
		master_hygiene = 0
		master_moodlet['pos_self_clean'] = 2
		master_mood_rate += min (slave['petting'], slave['oral'])
		dynamic $replace($dyn_slave_bathed, 'dyn'+'slave', 'slave')
		house_mess_rate += 3
		slave['half_action'] += 1
	end
	if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')
end

!Assistant washes master rule
if bath > 0 and master_hygiene > 0 and assistant_state = CONST_INT['slave_exist'] and assistant['washes_master_rule'] = 1 and assistant['energy'] >= 0:
	assistant_auto_wash = IIF(assistant['energy'] > 0, 1, 2) &! flag for end_of_day_screen
	dynamic $replace($dyn_slave_bathed, 'dyn'+'slave', 'assistant')
	assistant_rate['arousal'] -= master_libido
	master_hygiene_rate = 0
	master_hygiene = 0
	master_moodlet['pos_self_clean'] = 2
	master_mood_rate += min (assistant['petting'], assistant['oral'])
	house_mess_rate += 3
	assistant['half_action'] += 1
end

!Assistant washes slave rule (only if slave did not wash master)
if bath > 0 and slave_state = CONST_INT['slave_exist'] and assistant_state = CONST_INT['slave_exist'] and slave['hygiene'] > 0 and assistant['washes_slave_rule'] = 1 and assistant['energy'] >= 0:
	assistant_washed_slave = IIF(assistant['energy'] > 0, 1, 2) &! flag for end_of_day_screen
	slave_auto_bathing = 0 &! flag for end_of_day_screen
	dynamic $replace($dyn_slave_bathed, 'dyn'+'slave', 'slave')
	dynamic $replace($dyn_slave_bathed, 'dyn'+'slave', 'assistant')
	house_mess_rate += 3
	assistant['half_action'] += 1
end

!Slave will wash herself if not already washed and has unused energy
if bath > 0 and slave_state = CONST_INT['slave_exist'] and slave['hygiene'] > 0 and (slave['energy'] > 0 or slave['half_action'] < 0):
	slave_auto_bathing = 1 &! flag for end_of_day_screen
	dynamic $replace($dyn_slave_bathed, 'dyn'+'slave', 'slave')
end

!Refund slave energy unused or spent on relaxation for the following day
previous_energy = slave['energy']
if previous_energy > 0:
	if slave_state = CONST_INT['slave_exist'] and slave['mood'] > -2:
		slave_auto_rest = 1 &! flag for end_of_day_screen
		slave_rate['mood'] += previous_energy
		slave['well_rested'] += previous_energy
	end
	slave['energy'] = 0
end
slave['energy'] += slave['well_rested'] &! refund energy for use next day
if slave['energy'] >= 0 and slave['well_rested'] > 0: slave['pos_well_rested'] = 2
if slave['well_rested'] >= slave['stamina'] and slave['stamina'] < 3: slave_rate['stamina'] += 2
slave['well_rested'] = 0

show_food = 1 &! flag for end_day_screen
gs '$food_choice'

!buying food
food_buy_volume = 5
dynamic $get_fridge_space
!assistant will not buy food if accumulated billing costs for food purchased this decade would leave less than 50 sparks in reserve - ImperatorAugustus
if assistant['buy_vegetables'] = 1 and fridge['vegetables'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['vegetables'] += 5
	autobuy_cost += 5
	dynamic $get_fridge_space
end
if assistant['buy_flour'] = 1 and fridge['flower'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['flower'] += 5
	autobuy_cost += 5
	dynamic $get_fridge_space
end
if assistant['buy_spice'] = 1 and fridge['spice'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['spice'] += 5
	autobuy_cost += 5
	dynamic $get_fridge_space
end
if assistant['buy_milk'] = 1 and fridge['milk'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['milk'] += 5
	autobuy_cost += 20
	dynamic $get_fridge_space
end
if assistant['buy_cheese'] = 1 and fridge['cheese'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['cheese'] += 5
	autobuy_cost += 5
	dynamic $get_fridge_space
end
dynamic $get_fridge_space
if assistant['buy_alcohol'] = 1 and fridge['alcohol'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['alcohol'] += 5
	autobuy_cost += 10
	dynamic $get_fridge_space
end
if assistant['buy_eggs'] = 1 and fridge['eggs'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['eggs'] += 5
	autobuy_cost += 10
	dynamic $get_fridge_space
end
if assistant['buy_cream'] = 1 and fridge['cream'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['cream'] += 5
	autobuy_cost += 20
	dynamic $get_fridge_space
end
if assistant['buy_mince'] = 1 and fridge['mince'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['mince'] += 5
	autobuy_cost += 7
	dynamic $get_fridge_space
end
if assistant['buy_meat'] = 1 and fridge['meat'] = 0 and fridge_space >= 5 and autobuy_cost < sparks + 50:
	fridge['meat'] += 5
	autobuy_cost += 20
	dynamic $get_fridge_space
end

if fridge_space < 5 and fridge['milk'] > 10: fridge['milk'] = 10
dynamic $get_fridge_space

if fridge_space < 5 and fridge['eggs'] > 15: fridge['eggs'] = 10
dynamic $get_fridge_space

if fridge_space < 5 and fridge['cream'] > 15: fridge['cream'] = 10
dynamic $get_fridge_space

if fridge_space < 5 and fridge['mince'] > 10: fridge['mince'] = 10
dynamic $get_fridge_space

!Daily libido and excitement changes - crushboss (revised to not rise passively from lowest levels - ImperatorAugustus)
if master_mood > 0 and master_str > 2 and master_excitement > -5 and master_excitement < 5 and master_energy >= 0 and master_libido > 1: master_libido_rate += master_libido
if master_energy < 0: master_libido_rate -= 3
if master_mood < 0: master_libido_rate -= 2
if master_ill > 6: master_libido_rate -= 5
master_libido_rate -= master_wounds
if master_excitement = 5 and chimera_gem_on = 0: master_libido_rate -= 2
if chimera_gem_on = 1:
	if master_libido < 2: master_libido = 2 & master_libido_rate = 40
	if master_libido_rate < 5: master_libido_rate = 5
	if master_excitement < 5: master_excitement = 5 & master_excitement_rate = 200
	if master_wounds > 0 or master_wounds_rate > 0: master_wounds_rate -= 1
end

! Uncle Tom likes lolis and his impotence cannot be cured by the Chimera gem (though it does boost him)
if is_perv ! 0 and no_loli ! 1:
	if slave['age'] ! CONST_INT['loli_age']: master_perv_rate -= master_libido + 2 + slave['age']
	tmp1 = 0
	tmp2 = 0
	if slave_state = CONST_INT['slave_exist'] and slave['age'] ! CONST_INT['loli_age']: tmp1 = slave['boobs']
	if assistant_state = CONST_INT['slave_exist'] and slave['age'] ! CONST_INT['loli_age']: tmp2 = assistant['boobs']
	master_perv_rate -= (tmp1 * tmp2) / 2
	master_excitement_rate = min(master_excitement_rate, master_libido_rate)
	if assistant['age'] = CONST_INT['loli_age']: master_perv_rate += 1
end

dogortoilet = 0
if slave['sub_zoosex'] > 0: dogortoilet = rand(0,3)/3

!reseting and lowering vars
day += 1
decadecount += 1

!decrement positive and negative moodlets
gs '$mod_master_drop', 1
gs '$mod_master_rise', 1
gs '$mood_drop', 'slave', 1
gs '$mood_rise', 'slave', 1
gs '$mod_global_drop', 1

!Slave age count daily increase for keeping track of how long you’ve owned the slave
!Revised by ImperatorAugustus to not handle resets (killvar elsewhere takes care of it) and to increase count for slaves that are fainted or in cryo
$next_day_age_count_update = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus
		dynslave['age_count'] += 1
	end
}

dynamic $do_for_each_slave_in_list, '$next_day_age_count_update', '$all_slave_characters'

$next_day_stamina_update = {
	!Revised by ImperatorAugustus (moved everything inside slave_faint condition, removed skill_adv_mul - this is recovery, not training)
	!Conscious slaves no longer gain endurance passively from here (they can still recover endurance up to B+ passively from diet if they have excess calories at day-end)
	!A slave who is passed out with F- endurance recovers endurance at a faster rate than usual but loses physique points (fat reserves) to compensate
	if dynslave_state = CONST_INT['slave_faint']:
		if dynslave['stamina'] > 0 and dynslave['fat'] > 0:
			! Slave wakes up if endurance and physique are both above F-
			dynslave_state = CONST_INT['slave_exist']
			dynslave_rate['stamina'] -= slave['stamina']*10 &! endurance above S+ will drop by one level; S+ endurance will drop more than half-way to A+
			dynslave['energy'] = -4
		elseif max(master_medic, assistant['nurse']) > 3:
			dynslave_rate['stamina'] += 1 &! A+ or S+ medical skill assists recovery (otherwise, it is up to diet alone)
		end
	end
}
dynamic $do_for_each_slave_in_list, '$next_day_stamina_update', '$unfrozen_slave_characters'

$next_day_forgotten_rewards = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		!Forgotten rewards, merits, and virtue - improved - crushboss
		if dynslave['virtue'] > 0:
			if dynslave['moral'] ! 5 and dynslave['abuse_attitude'] ! 2:
				! fully devoted slaves or psi-masochists do not feel bad about not getting rewards - ImperatorAugustus
				dynslave_rate['mood'] -= (dynslave['virtue']*2)
				if dynslave['moral'] < 3: dynslave_rate['angst'] += dynslave['virtue']
				if dynslave['virtue'] > dynslave['neg_no_praise']: dynslave['neg_no_praise'] = dynslave['virtue']
				dynslave_rate['rational'] -= dynslave['virtue'] &! drain awareness instead of increasing it - ImperatorAugustus (not being rewarded undermines rational understanding/acceptance of slavery)
			end
			dynslave['virtue'] = 0
		end

	end
}

dynamic $do_for_each_slave_in_list, '$next_day_forgotten_rewards', '$unfrozen_slave_characters' &! all except iced

$next_day_forgotten_sins = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		!Forgotten sins - improved - crushboss (revised - ImperatorAugustus)
		!ImperatorAugustus: leaving minor guilt unpunished at end of day can be beneficial, especially at the beginning of training, because it raises mood and lowers angst
		!Guilt can be reduced by giving a slave tasks they are willing to complete with diligence; merit gained cancels guilt (but will not lower guilt below 1)
		!These effects do not apply to prisoners or restrained cows or slaves whose sleep condition rule is sleeping in the cells (TODO: or on the floor) (those are considered punishments)
		if dynslave['sin'] > 0 and dynslave['sleep_condition'] >= -1 and ('dynslave' ! 'cow' or cow_locked = 0):
			! positive consequences (proportional to fear: moodlet and mood rate increase, fear and angst rate down)
			if dynslave['fear'] > 0: dynslave['pos_no_punish'] += dynslave['fear'] &! changed moodlet magnitude to depend on fear, not guilt - ImperatorAugustus
			if dynslave['mood'] < 0: dynslave_rate['mood'] -= 2*dynslave['mood']*dynslave['fear'] &! elevate mood (up to +5 at 5 fear and -5 mood, capped by $moodlets_account) - ImperatorAugustus
			dynslave_rate['fear'] -= (dynslave['fear']*4) &! increased fear rate reduction but removed direct fear drop when sin > fear - ImperatorAugustus
			dynslave_rate['angst'] -= dynslave['angst']*2 &! angst rate reduction usually inferior to rest rewards but still helpful - ImperatorAugustus
			! negative consequences (proportional to guilt: spoil rate up, rational rate down, and proportional to fear: taming rate down)
			dynslave_rate['spoil'] += (dynslave['sin']*4) &! spoil 0 rests at -10 rate and rises at +10, so 5 guilt raises 0 spoil to 1 - ImperatorAugustus
			dynslave_rate['rational'] -= (dynslave['sin']*2) &! rational drain proportional to guilt (rational 1 drops to 0 at -10 rate) - ImperatorAugustus (more surprising not to be punished than not to be rewarded)
			dynslave_rate['instinct'] -= (dynslave['fear']*2) &! added taming drain proportional to fear (taming 1 drops to 0 at -10 rate) - ImperatorAugustus
			! guilt decays nightly instead of being reset - ImperatorAugustus
			dynslave['sin'] -= 1
		end

	end
}

dynamic $do_for_each_slave_in_list, '$next_day_forgotten_sins', '$unconfined_slave_characters' &! all except iced, livestock and prisoner (whose imprisonment is itself a punishment)
dynamic $do_for_each_slave_in_list, '$next_day_forgotten_sins', '$livestock_slave_characters' &! also apply to livestock

$next_day_impregnation_update = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus
		dynslave['impregnation'] = 0
		dynslave['cum_load'] -= rand(4,8)
		if dynslave['cum_load'] < 0: dynslave['cum_load'] = 0
		if dynslave['lactation'] > 0 and dynslave['cum_load'] > 0:
			if dynslave['cum_load'] >= 1: dynslave['impregnation'] = 1
			if dynslave['cum_load'] > 10: dynslave['impregnation'] = 2
			if dynslave['cum_load'] > 25: dynslave['impregnation'] = 3
			if dynslave['cum_load'] > 40: dynslave['impregnation'] = 4
			if dynslave['cum_load'] > 50: dynslave['impregnation'] = 5
			if dynslave['cum_load'] > 60: dynslave['impregnation'] = 6
			if dynslave['cum_load'] > 70: dynslave['impregnation'] = 7
			if dynslave['cum_load'] > 80: dynslave['impregnation'] = 8
			if dynslave['cum_load'] > 90: dynslave['impregnation'] = 9
			if dynslave['cum_load'] > 100: dynslave['impregnation'] = 10
		end
	end
}

!slaves impregnation (linked to lactation) - converted to dynamic function by ImperatorAugustus
dynamic $do_for_each_slave_in_list, '$next_day_impregnation_update', '$main_screen_slave_characters' &! slave, assistant, prisoner, cow - TODO: add others?

$next_day_lactation_update = {
	if dynslave_state > CONST_INT['no_slave']:
		!slave lactation - fixed by crushboss - updated by Pianocat - converted to dynamic function and limited to conscious state by ImperatorAugustus

		dynslave['milk_gain'] = 0
		if dynslave['lactation'] > 0:
			dynslave['milk_gain'] = dynslave['boobs'] + dynslave['cowSK'] + dynslave['impregnation'] + min((dynslave['custom']*3)/2, 5) - dynslave['angst']*2
			if dynslave['mood'] < 6: dynslave['milk_gain'] += dynslave['mood']
			if dynslave['age'] = CONST_INT['milf_age']: dynslave['milk_gain'] += 2
			dynslave['milk_gain'] -= dynslave['rehabilitation']/2
			if dynslave['fat'] = 2: dynslave['milk_gain'] += 1
			if dynslave['fat'] = 3: dynslave['milk_gain'] += 2
			if dynslave['fat'] > 3: dynslave['milk_gain'] += 4
			if dynslave['fat'] = 0: dynslave['milk_gain'] = dynslave['milk_gain']/2
			if dynslave['ill'] > 0: dynslave['milk_gain'] = (dynslave['milk_gain']/3)*2
			if $dynslave_psy_status = 'broken': dynslave['milk_gain'] = (dynslave['milk_gain']/3)*2
			dynslave['lactation'] = 1
			if dynslave['milk_gain'] > 10: dynslave['lactation'] = 2
			if dynslave['milk_gain'] > 16: dynslave['lactation'] = 3
			if dynslave['milk_gain'] > 22: dynslave['lactation'] = 4
			if dynslave['milk_gain'] > 35: dynslave['lactation'] = 5
			if dynslave['cowSK'] < 5 and dynslave['cowSK_affinity'] <= 0:
				dynslave['neg_cow'] = 1
			else
				dynslave['neg_cow'] = 0
			end
			if dynslave['cowSK'] > 2 and dynslave['cowSK_affinity'] = 1:
				dynslave['pos_cow'] = 1
			else
				dynslave['pos_cow'] = 0
			end
			if dynslave['cowSK'] = 5 and dynslave['cowSK_affinity'] <= 0:
				dynslave['pos_cow'] = 1
			else
				dynslave['pos_cow'] = 0
			end

			dynslave['energy'] -= 1 &! producing milk consumes energy (this also qualifies the cow for nightly item bonuses) - ImperatorAugustus
		end

		!Milk summ calculation + random boost - Pianocat
		if dynslave['milk_gain'] < 0: dynslave['milk_gain'] = 0
		if dynslave['milk_summ'] > 130: dynslave['milk_summ'] = 130

		if dynslave['milk_summ'] >= 0 and dynslave['milk_summ'] < 20: dynslave['milk_summ']+= rand(0,1)
		if dynslave['milk_summ'] >= 20 and dynslave['milk_summ'] < 40: dynslave['milk_summ']+= rand(2,4)
		if dynslave['milk_summ'] >= 40 and dynslave['milk_summ'] < 60: dynslave['milk_summ']+= rand(5,6)
		if dynslave['milk_summ'] >= 60 and dynslave['milk_summ'] < 80: dynslave['milk_summ']+= rand(2,4)
		if dynslave['milk_summ'] >= 80 and dynslave['milk_summ'] < 90: dynslave['milk_summ']+= rand(0,1)
		if dynslave['milk_summ'] >= 90 and dynslave['milk_summ'] < 100: dynslave['milk_summ']-= rand(1,3)
		if dynslave['milk_summ'] >= 100 and dynslave['milk_summ'] < 110: dynslave['milk_summ']-= rand(2,6)
		if dynslave['milk_summ'] >= 110 and dynslave['milk_summ'] < 120: dynslave['milk_summ']-= rand(4,8)
		if dynslave['milk_summ'] >= 120: dynslave['milk_summ'] -= rand(5,10)

		!Transfer the milk gained this day to the total amount of milk
		dynslave['milk_summ'] += dynslave['milk_gain']
	end
}

!slave lactation - fixed by crushboss - updated by Pianocat - converted to dynamic function by ImperatorAugustus
dynamic $do_for_each_slave_in_list, '$next_day_lactation_update', '$main_screen_slave_characters' &! slave, assistant, prisoner, cow - TODO: add others?

!Milk demand adjustment
dynamic $milk_price
milk_tip = 0
tip_asked = 0

if code_defined['next_day_starvation_effects'] = 0:
	code_defined['next_day_starvation_effects'] = 1 &! do-once flag

	$next_day_starvation_effects = {
		! expects to be called with dynamic $replace($next_day_starvation_effects, 'dyn'+'slave', 'a slave instance variable'), BOOL - where BOOL is zero if the slave was not fed today and non-zero if the slave was fed today
		! example:  dynamic $replace($next_day_starvation_effects, 'dyn'+'slave', 'slave'), slave['diet_portion']
		! expects the 'starvation' property of the slave instance variable to be set before calling, if the slave was fed today but there was a sufficient caloric deficit to qualify as starvation
		if dynslave_state > CONST_INT['no_slave']:

			if cheat_no_starve = 1:
				dynslave['days_without_food'] = 0
				dynslave['starvation'] = 0
			end

			if args[0] = 0 and dynslave['days_without_food'] >= 0: dynslave['days_without_food'] += 1 &! counting consecutive days without food
			if args[0] = 0 and dynslave['days_without_food'] < 0:  dynslave['days_without_food'] *= -1 &! invert counter to trigger mood and angst penalties if starved again mid-recovery
			if args[0] ! 0 and dynslave['days_without_food'] > 0:  dynslave['days_without_food'] -= 1 &! reduce consecutive days-without-food counter when being fed again
			if args[0] ! 0 and dynslave['days_without_food'] >= 3: dynslave['days_without_food'] = -10 &! apply starvation penalties for 10 days after feeding resumes (refeeding syndrome)
			if args[0] ! 0 and dynslave['days_without_food'] < 0:  dynslave['days_without_food'] += 1 & dynslave['starvation'] = 16 &! counting up to zero; apply maximum starvation penalty

			if dynslave['starvation'] > 0:
				dynslave_rate['arousal'] /= 2
				if dynslave['days_without_food'] > 1:
					dynslave_rate['mood'] -= 25 * dynslave['days_without_food']
					dynslave_rate['angst'] += dynslave['sensitivity']
				end
				dynslave[IIF(dynslave['days_without_food'] < 0 or dynslave['days_without_food'] > 3, 'neg_starvation', 'neg_hungry')] = 100
				dynslave_rate['rational'] += min(dynslave['intellect'], dynslave['starvation']/2)
				dynslave_rate['stamina'] -= min(4, dynslave['starvation']/4) &! starvation drains one fourth of the negative calorie amount from endurance, rounded down, at most -4 per night
				dynslave['half_action'] += 2 &! lethargy effect - next day starts with less energy and 100 fewer calories
				if dynslave['starvation_warning'] = 0: dynslave['starvation_warning'] = 1 &! flag for notification message
				dynslave['starvation'] = 0 &! starvation resets daily
			else
				dynslave['starvation_warning'] = 0 &! reset so it can trigger again
				dynslave['neg_starvation'] /= 10
				dynslave['neg_hungry'] = 0
			end

		end &! end of conscious or fainted check
	}
end

if slave_state > CONST_INT['no_slave']:
	!Calories counting - overhauled by crushboss
	food_need = 1
	if slave['pregnant'] > 9: slave['calories'] -= 1 & food_need += 1 &! > 9 corresponds to when Technosphere medical center can detect pregnancy
	if slave['fertility'] = 1: slave['calories'] -= 1 & food_need += 1 &! egglaying
	if slave['lactation'] > 0: slave['calories'] -= 1 & food_need += 1
	if slave['parasite'] > 0: slave['calories'] -= slave['parasite']

	if slave['diet_portion'] = 4 and (difficulty_setting < 2 or (slave['calories'] + food_need) > -6):
		! on hardcore obedience difficulty, adjust remaining calories only if day ended above starvation threshold (-6)
		! nutritionist/calculated portion size fixes underweight and overweight physique, then maintains physique
		if slave['fat'] > 3:
			! optimal least-endurance-drain non-starvation slimming diet for overweight slaves (duration depends on metabolism, min: 14 days = 70 sparks, max: 27 days = 135 sparks)
			slave['calories'] = -6 + slave['metabolism']
		elseif slave['fat'] = 0:
			! recovery diet for underweight slaves (max: 5 days = 25 sparks, min: 1 day = 5 sparks)
			slave['calories'] = max(slave['calories'], 4 + slave['metabolism'])
		else
			! maintenance diet (no weight change)
			slave['calories'] = 0
		end
	end

	if slave['calories'] < 0:
		if slave['calories'] < -5 + slave['metabolism']:
			! weight loss if calories are at or below -5 + metabolism level [metabolism ranges from [1,5] with 1 being weakest (lose fat slowest)]
			slave_rate['fat'] -= rand(3, 2 + (1+slave['metabolism'])/2) &! weak metabolism (1) loses 3 physique points per night, powerful metabolism (5) loses 3-5 physique points per night
			! rational motivation to obey increases when a slave notices that she is losing weight (this supports the help text for the "restricted" diet portion) - ImperatorAugustus
			slave_rate['rational'] += 1
		end
		if slave['calories'] < -5:
			! below -5 calories = starvation
			slave['starvation'] -= slave['calories'] &! calories are negative, so subtraction increases the starvation value
		else
			! minimal endurance loss without starvation occurs when calories are between -1 and -5, representing loss of muscle mass due to caloric deficit, offsetting exercise during the day (if any)
			slave_rate['stamina'] -= 1
		end
		! a slave with no fat reserves will pass out immediately if not given any food
		if cheat_no_starve = 0 and slave['fat'] = 0 and slave_rate['fat'] < 0 and slave['diet_portion'] = 0: slave_state = CONST_INT['slave_faint'] & slave_rate['stamina'] -= 16
	elseif slave['calories'] > 0:
		! metabolized calories raise endurance until B+ (benefit with higher metabolism = easier to maintain weight)
		if slave['stamina'] < 3: slave_rate['stamina'] += min(slave['calories'], slave['metabolism'])
		! excess calories not consumed by metabolism go to fat
		slave_rate['fat'] += max(0, slave['calories'] - slave['metabolism']) &! metabolism ranges from [1,5] with 1 being weakest (gain fat easiest)
		if slave['fat'] = 5 and slave['stamina'] > 0: slave_rate['stamina'] -= max(0, slave['calories'] - slave['metabolism']) &! with max physique, excess calories lower endurance but not below D-
	end

	dynamic $replace($next_day_starvation_effects, 'dyn'+'slave', 'slave'), slave['diet_portion']

	!reveal hidden traits
	if slave['weight_before'] ! slave['fat'] and slave['metabolism_revealed'] = 0: slave['metabolism_revealed'] = 1
	slave['weight_before'] = slave['fat']
	!if slave['memory_revealed'] = 0: slave['memory_revealed'] = 1
end

!Eventual death due to no stamina and no stamina_rate - crushboss (TODO: is slave_faint state handled elsewhere? - ImperatorAugustus)
if prisoner_state > CONST_INT['no_slave']:
	if prisoner['stamina'] = 0 and prisoner_rate['stamina'] <= indep_atr_min_threshold[0]: prisoner_died = 1
	if cheat_no_starve = 0 and prisoner['fat'] = 0 and prisoner_rate['fat'] <= indep_atr_min_threshold[0] and prisoner['prison_food_type'] = -1: prisoner_died = 1
end

!Eventual death due to no stamina and no stamina_rate for cow (TODO: is slave_faint state handled elsewhere? - ImperatorAugustus)
if cow_state > CONST_INT['no_slave']:
	if cow['stamina'] = 0 and cow_rate['stamina'] <= indep_atr_min_threshold[0]: cow_died = 1
	if cheat_no_starve = 0 and cow['fat'] = 0 and cow_rate['fat'] <= indep_atr_min_threshold[0] and cow_ration = -1: cow_died = 1
end

!if special_food = 1: - eliminated need for special_food, main_slave_ration, and slave_ration = 100 - ImperatorAugustus
!	main_slave_ration = slave_ration
!	slave_ration = 100
!	special_food = 0
!end

!lillimerge: added positive mood change of scraps to all food types
!modified requirement for positive mood when given no food
!and fixed indentation for better readability
!ImperatorAugustus: removed pos_cookie moodlet when eating fiend cum (the text for that moodlet mentions eating from table instead of bowl)
if slave['diet_food_type'] = 0 or slave['diet_food_type'] = 2:
	if slave['diet_food_type'] = 2 and slave['diet_portion'] > 0:
		slave['neg_slave_food'] = 1
		if slave['diet_leftovers'] = 1 and cooked_food > slave['pride']*2: slave['pos_cookie'] = 1
		if slave['fiend_tattoo'] = 0: slave['neg_disgust'] = 1 + slave['pride'] - slave['perversion']
		if slave['perversion'] = 2: slave['neg_slave_food'] = 0
		if slave['fiend_tattoo'] = 1: slave['neg_slave_food'] = 0
		slave['perversion_xp'] += 1
	end
	if slave['diet_food_type'] = 0 and slave['diet_portion'] > 0:
		slave['neg_slave_food'] = 1
		if slave['diet_leftovers'] = 1 and cooked_food > slave['pride']*2: slave['pos_cookie'] = 1
	end
	if slave['diet_portion'] = 0:
		if slave['diet_leftovers'] = 1 and cooked_food > slave['pride']*3: slave['pos_cookie'] = 1
	end
else
	if slave['diet_portion'] = 0:
		if slave['diet_leftovers'] = 1 and cooked_food > slave['pride']*3: slave['pos_cookie'] = 1
	else
		if slave['diet_leftovers'] = 1 and cooked_food > slave['pride']: slave['pos_cookie'] = 1
	end
end

!Added benefits and penalties to Eat my leftovers given to slaves - crushboss
if slave['diet_leftovers'] = 1:
	!slave_rate['mood'] += slave['moral'] &! disabled: moodlet is enough of a boost, extra mood rate is overkill - ImperatorAugustus
	slave_rate['mood'] -= slave['pride'] &! negative mood rate can stay - ImperatorAugustus
	if slave['moral'] = 0:
		slave_rate['spoil'] += slave['sensitivity']*5
	elseif slave['moral'] = 1:
		slave_rate['spoil'] += slave['sensitivity']*2
	elseif slave['moral'] >= 2 and slave['moral'] < 5:
		slave_rate['spoil'] += 1
	end
	if slave['pride'] < 2 and slave['angst'] <= 0 and slave['mood'] > 3 and slave['moral'] >= 2:
		slave_rate['moral'] += 1
		slave_rate['instinct'] += 1
		slave['pos_cookie'] = 1
	end
end

eaten_food = 0
if slave['diet_portion'] = 1: eaten_food = food_need + slave['stamina'] - 6 &! restricted portion: weight loss without starvation if endurance-level energy is spent without any exercise
if slave['diet_portion'] = 2: eaten_food = food_need + slave['stamina'] + 2 &! moderate portion: calibrated to require exercising daily only with below-average metabolism to avoid gaining weight, while not requiring daily exercise for average or better metabolism, with passive endurance gains making up to an extent for a lack of exercise... but only up to B+ endurance
if slave['diet_portion'] > 2 or slave_already_done_today[1515] > 0:
	! generous portion or nutritionist/calculated provide same number of calories
	!replaced slave_ration = 100 with slave_already_done_today[1515] (went to restaurant) - ImperatorAugustus
	eaten_food = food_need + slave['stamina']*3 + slave['fat'] - 1
	if slave_state = CONST_INT['slave_exist']:
		! fainted slaves eat less - ImperatorAugustus
		if slave['passion_sweets'] > 0: eaten_food += 2
		if slave['mood'] < 0: eaten_food -= slave['mood'] + slave['angst'] &! slaves in a bad mood eat more, offset by angst
		if slave['diet_portion'] = 3 and slave['diet_food_type'] ! 2: slave_rate['custom'] -= 1 &! eating as much as she wants drains habit (but not with nutritionist)
	end
end
!if slave_ration = 100: slave['pos_cookie'] = 2 - moved this code to interaction 1515 - AugustusImperator
!slave_ration = 0 - no longer needed - AugustusImperator

if slave['diet_portion'] = 0:
	slave['calories'] = -22 + slave['fat']*2 &! if no food, she starts the day with negative calories sufficient to cause starvation, calibrated so that overweight physique takes 15 days longer to starve from S+ to F- endurance
else
	slave['calories'] = eaten_food
end

slave_food_cost = 0
if slave['diet_food_type'] = 0: slave_food_cost = eaten_food

!Added benefits and penalties to Fresh Food given to slaves - crushboss
if slave['diet_food_type'] = 1:
	slave_food_cost = eaten_food*2
	slave['half_action'] -= 1
	if slave['stamina'] < 3: slave_rate['stamina'] += 1
	if slave['angst'] < 3: slave_rate['mood'] += (1 + (slave['sensitivity']/2)) &! no moodlet for this, adds 0.1 to 0.35 mood - ImperatorAugustus
	if slave['moral'] = 0:
		slave_rate['spoil'] += slave['sensitivity']*3
	elseif slave['moral'] >= 1 and slave['moral'] < 4:
		slave_rate['spoil'] += 1
	end
end

if slave['diet_food_type'] = 2:
	if spawn_semen > eaten_food:
		spawn_semen -= eaten_food
	else
		slave_food_cost += eaten_food - spawn_semen
		spawn_semen = 0
	end
end
if slave['diet_leftovers'] = 1: slave['calories'] += (97 * day + slave['calories']) mod 4 &! pseudo-rand(0,3)
food_cost = slave_food_cost/3

if slave['diet_portion'] = 4 and max(master_medic, assistant['nurse']) < 4: food_cost += 5 &! nutritionist adds 5 sparks/day to food cost billed at decade end (waived if master or assistant has A+ or S+ medical skill)

if slave['diet_supplements'] = 1 and slave['stamina'] < 3:
	slave_rate['stamina'] += 1
	food_cost += 1

	!Added stamina bonus for supplements with nutritionist - crushboss (added S+ master/assistant medical skill as alternative - ImperatorAugustus)
	if slave['diet_portion'] = 4 or max(master_medic, assistant['nurse']) > 3: slave_rate['stamina'] += 1
end

if slave_state > CONST_INT['no_slave']: total_food_cost += food_cost

!healing and dependence (converted to dynamic function, merged variations) - ImperatorAugustus
! cow variant of this was lowering addictions daily without any other effect, now it is the same as other slaves
$next_day_healing_and_dependence = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus
		if dynslave['koffed'] > 0:
			dynslave['koffed'] = 0
			if dynslave['koffe_addiction'] > dynslave['stamina']*10: dynslave['energy'] -= 1 & dynslave['neg_slave_koffe'] = 1
			dynslave['koffe_addiction'] += 1
		elseif dynslave['koffe_addiction'] > 0 and rand(1,3) = 1:
			dynslave['koffe_addiction'] -= 1
		end

		if dynslave['on_alco'] > 0:
			dynslave['on_alco'] = 0
			dynslave['dipsomania'] += 1
			dynslave['pos_slave_alco'] = 0
			dynslave['neg_slave_alco'] = dynslave['dipsomania']
		elseif dynslave['dipsomania'] > 0:
			dynslave['dipsomania'] -= 1
		end
		if dynslave['dipsomania'] >= dynslave['stamina']: dynslave_rate['stamina'] -= dynslave['dipsomania']

		if dynslave['on_meth'] > 0:
			dynslave['on_meth'] = 0
			dynslave['pos_slave_meth'] = 0
			if dynslave['meth_addiction'] > dynslave['stamina']: dynslave['neg_slave_meth'] = dynslave['meth_addiction']
			dynslave['meth_addiction'] += 1
			dynslave_rate['stamina'] -= 1
		else
			dynslave_rate['stamina'] -= dynslave['meth_addiction']
			if rand(1,3) = 1 and dynslave['meth_addiction'] > 0: dynslave['meth_addiction'] -= 1
		end

		if dynslave['on_opium'] > 0:
			dynslave['on_opium'] = 0
			dynslave['pos_slave_opium'] = 0
			if dynslave['opium_addiction'] > dynslave['stamina']: dynslave['neg_slave_opium'] = dynslave['opium_addiction']
			dynslave['opium_addiction'] += 1
			dynslave_rate['stamina'] -= 1
		else
			dynslave_rate['stamina'] -= dynslave['opium_addiction']
			if rand(1,3) = 1 and dynslave['opium_addiction'] > 0: dynslave['opium_addiction'] -= 1
		end

		if arrpos('$livestock_slave_characters', 'dynslave') ! -1:
			if farmhand_state = CONST_INT['slave_exist']:
				slave_healing_bonus = farmhand['nurse'] &! for livestock, only farmhand skill is considered
			else
				slave_healing_bonus = 0
			end
		elseif assistant_state = CONST_INT['slave_exist']:
			slave_healing_bonus = max(master_medic, assistant['nurse']) &! ImperatorAugustus: changed to use best skill of master or assistant instead of sum
		else
			slave_healing_bonus = master_medic
		end
		! added mood factor (was only in prisoner variation of the code) to offset above change and make healing in prison less effective (if negative mood)
		if dynslave_rate['bruises'] > 0: dynslave_rate['bruises'] -= dynslave['stamina'] + dynslave['mood'] + slave_healing_bonus

		if dynslave_rate['wounds'] > 0:
			dynslave_rate['stamina'] -= dynslave['wounds']
			dynslave_rate['wounds'] -= dynslave['stamina'] + slave_healing_bonus - dynslave['hygiene']
			dynslave_rate['scars'] += dynslave['wounds']
			dynslave['neg_slave_wounded'] = 1
		end
		if dynslave_rate['wounds'] < 0: dynslave_rate['wounds'] = 0

		!Added chance for scarring for slave - crushboss
		a = 1 + ((97 * day + dynslave_rate['wounds']) mod 10) &! pseudo-rand(1,10)
		if (dynslave_rate['wounds'] >= 40 and dynslave_rate['wounds'] < 100) or dynslave['wounds'] = 2 or dynslave['wounds'] = 3:
			if a > 4: dynslave['scars'] += 1
		elseif (dynslave_rate['wounds'] >= 100 and dynslave_rate['wounds'] < 120) or dynslave['wounds'] = 4:
			if a > 4: dynslave['scars'] += 1
			if a > 7: dynslave['scars'] += 1
		elseif dynslave_rate['wounds'] >= 120 or dynslave['wounds'] = 5:
			if a > 4: dynslave['scars'] += 1
			if a > 6: dynslave['scars'] += 1
			if a > 8: dynslave['scars'] += 1
		end
		killvar 'a'

		if dynslave['ill'] > 0:
			dynslave['ill'] += 1
			if dynslave['ill'] > 10 and ((97 * day + dynslave['ill'] + dynslave['stamina']) mod 60) < dynslave['stamina']: dynslave['ill'] = 0 &! chance of natural recovery after a decade, 5% at B+ endurance, 8.3% at S+ endurance - ImperatorAugustus
			if dynslave['ill'] > dynslave['stamina']: dynslave_rate['stamina'] -= 1
			if dynslave['ill'] > 6: dynslave['neg_slave_ill'] = 1000 &! large magnitude to survive mood rise or Lethe potions - ImperatorAugustus
		end

		if dynslave['parasite'] > 0:
			dynslave_rate['arousal'] += dynslave['parasite'] * 10 &! this effect was not applied for prisoners before - ImperatorAugustus
			dynslave['parasite'] += 1
		end
	end
}

dynamic $do_for_each_slave_in_list, '$next_day_healing_and_dependence', '$unfrozen_slave_characters' &! all except iced

!ImperatorAugustus: assistants with addictions simply take what they want (added effects, compare interaction 5506, 5509, 5510)
$next_day_addiction_pilfering = {
	if dynslave_state = CONST_INT['slave_exist']:
		! state check here is only if conscious - ImperatorAugustus
		if dynslave['koffe_addiction'] > 20 and dynslave['koffed'] = 0 and lab_store['koffe'] > 0:
			lab_store['koffe'] -= 1
			dynslave['koffed'] = 1
			dynslave['neg_slave_koffe'] = 0
			dynslave['pos_slave_koffe'] = 1
			dynslave['half_action'] -= 2
		end
		if dynslave['meth_addiction'] > 4 and dynslave['on_meth'] = 0 and lab_store['meth'] > 0:
			lab_store['meth'] -= 1
			dynslave['on_meth'] = 1
			dynslave['neg_slave_meth'] = 0
			dynslave['pos_slave_meth'] = 1
			dynslave['half_action'] -= 10
		end
		if dynslave['opium_addiction'] > 4 and dynslave['on_opium'] = 0 and lab_store['opium'] > 0:
			lab_store['opium'] -= 1
			dynslave['on_opium'] = 1
			dynslave['neg_slave_opium'] = 0
			dynslave['pos_slave_opium'] = 1
			gs '$mood_rise', 'dynslave', 3
		end
	end
}

dynamic $do_for_each_slave_in_list, '$next_day_addiction_pilfering', '$assistant_slave_characters' &! assistant and farmhand

$next_day_menses_preg_egglaying_update = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted (cycle continues even when unconscious) - ImperatorAugustus
		!Menstrual cycle and pregnancy - improved and fixed - crushboss (converted to dynamic and overhauled by ImperatorAugustus)
		if dynslave['vaginal_modification'] = 4: dynslave['menstruation'] = 0 & dynslave['neg_menstruation'] = 0 & dynslave['parasite'] = 0 &! egglayers cannot menstruate or host parasites

		if dynslave['inseminated'] > 0: dynslave['inseminated'] -= 1 &! inseminated represents remaining lifespan of live sperm (should be set to 3-5 when insemination occurs)

		if dynslave['menstruation'] > 0:
			if dynslave['fertility_revealed'] = 0: dynslave['fertility_revealed'] = 1 &! menstruation reveals fertility
			dynslave['pregnant'] = 0 &! menstruation while pregnant ends the pregnancy (miscarriage, not implemented) - ImperatorAugustus
			dynslave['menstruation'] += 1 &! tracking duration of menstruation and suppressing end_of_day_screen notification (shown when = 1 only)
			!TODO: sex while menstruation > 0 should impact hygiene more, and perhaps give neg moodlets (depending on master / slave perversion)
			if dynslave['menstruation'] > 3 + ((97 * day) mod 4):
				! end menstruation after pseudo-rand(3,6) days, remove moodlet
				dynslave['menstruation'] = 0
				dynslave['neg_menstruation'] = 0
			end
		elseif dynslave['pregnant'] > 0:
			dynslave['pregnant'] += 1 &! counting days pregnant (early term is 37w0d to 38w6d [259-272 days], full term is 39w0d - 40w6d [273 to 286 days], after is late)
			dynslave_rate['stamina'] -= 1 + (dynslave['pregnant'] / 30) &! pregnancy drains stamina at an increasing daily rate as it progresses to term - ImperatorAugustus
			!Pregnancy Revealed - overhauled entire pregnancy system - added $pregnancy and $pregnancy_assistant modules - crushboss (overhauled again, modules removed - ImperatorAugustus)
			!Overhauled code to work with Oviparity (egglaying) and sjm_UI_UIadds module - modified pregnancy activities to point to the above modules - crushboss (replaced with $inseminate - ImperatorAugustus)
			if dynslave['pregnant_revealed'] = 0:
				!Normal and early detection chance - crushboss (but not too early - ImperatorAugustus)
				if dynslave['pregnant'] >= 140 or (dynslave['pregnant'] > 90 and inventory['stethoscope'] ! 0 and (master_medic > 3 or assistant['nurse'] > 3) and rand(1,21) = 8):
					dynslave['pregnant_revealed'] = 1
					if dynslave['fertility_revealed'] = 0: dynslave['fertility_revealed'] = 1 &! detected pregnancy reveals fertility
				end
			end
		end
		if dynslave['pregnant'] = 0 and dynslave['fertility'] = 0 and dynslave['vaginal_modification'] ! 4:
			! not pregnant, not immature, and not egglaying -> can menstruate (surgical sterilization prevents pregnancy but does not stop cycles) - ImperatorAugustus
			dynslave['cycle_day'] -= 1 &! countdown to beginning of the next cycle
			if dynslave['cycle_day'] <= 0:
				dynslave['menstruation'] = 1 &! today is first day of menstruation (triggers end_of_day_screen notifications)
				dynslave['neg_menstruation'] = 1000 &! large magnitude to survive any mod_drop including the -100 from Water of Lethe
				dynslave['cycle_day'] = rand(21,35) &! duration of next cycle varies from 21 to 35 days, averaging 28 (synchronization of women living together is a myth)
			elseif dynslave['ovulation'] > 0:
				dynslave['ovulation'] -= 1 &! countdown to ovulation day
				if dynslave['ovulation'] = 0 and dynslave['fertility'] = 0 and dynslave['inseminated'] > rand(1,5):
					dynslave['pregnant'] = 1 &! pregnancy starts on ovulation day if live sperm can reach the egg (max 80% chance, reduced by time since insemination)
					dynslave['pregnant_revealed'] = 0
				end
			elseif dynslave['cycle_day'] > 16:
				dynslave['ovulation'] = dynslave['cycle_day'] - rand(13,15) &! ovulation occurs on average 14 days before menstruation
			end
		end
		!Egglaying
		dynslave['egg_time'] = 0
		if dynslave['fertility'] = 1 and dynslave['rehabilitation'] = 0:
			if dynslave['eggs_to_lay'] = 0:
				dynslave['eggs_to_lay'] = dynslave['age'] + dynslave['stamina'] + dynslave['fat'] + rand(3,6)
				dynslave['pregnant'] = 1 &! begin counting days pregnant with eggs
			else
				dynslave['pregnant'] += 1 &! increment days pregnant with eggs
				if dynslave['pregnant'] > 3:
					dynslave['eggs_to_lay'] -= 1 &! lay one egg each day after the third day until all eggs are laid
					dynslave['egg_time'] = 1 &! notifies end_of_day_screen that an egg was laid today by this slave
					total_eggs_laid_today += 1 &! global counter of eggs laid today by all slaves in the household
					dynslave_rate['custom'] += 1
					dynslave['virginity'] = 3 &! relaxed
				end
			end
		end
	end
}

!Menstrual cycle and pregnancy/egglaying - slave - improved and fixed - crushboss (converted to dynamic function and revised -ImperatorAugustus)
dynamic $do_for_each_slave_in_list, '$next_day_menses_preg_egglaying_update', '$main_screen_slave_characters' &! slave, assistant, prisoner, cow

!Menstrual cycle and pregnancy/egglaying - add farmhand, pig (TODO: harpy is handled elsewhere for now) -ImperatorAugustus
dynamic $replace($next_day_menses_preg_egglaying_update,'dyn'+'slave','farmhand')
dynamic $replace($next_day_menses_preg_egglaying_update,'dyn'+'slave','pig')

!Change stats under the influence of the established rules

!Clear the rules if no slave exists - crushboss
!no longer needed, all rules are now properties of slave array so they are cleaned up automatically - ImperatorAugustus

!Ensure all variables are set to zero - sanity check - probably unnecessary - crushboss
!rules_broken = 0 - legacy variable (globally replaced by slave['rules_broken']) - ImperatorAugustus
slave['num_rules_broken'] = 0
slave['num_rules_wanttobreak'] = 0
! TODO: are above lines unnecessary here? ($end_day_variable_reset clears them) - ImperatorAugustus
!removed unnecessary rule_res variable clearing (they were reset before each use) and later eliminated those variables entirely - ImperatorAugustus
overnight_rules_count = 0
$slave['rules_explanations'] = ''
!the two lines above are necessary daily resets - ImperatorAugustus

!Rewrote most of the rule code - was a total mess - crushboss - yeah me too:) - Slaver (ditto -ImperatorAugustus)
!rule_masturbation is not handled here but later in next_day and end_day_screen - crushboss
!orgasm_prohibited rule is not handled here but in #dynamic, sexy_time_engine, and end_day_screen - crushboss
!All the other rules that require special handling are handled in this section - crushboss

!Changed condition from ! no_slave to slave_exist (a fainted slave cannot follow rules) - ImperatorAugustus
if slave_state = CONST_INT['slave_exist']:

	if slave['cooks_rule'] = 1:
		overnight_rules_count += 1
		! Cooking lesson repulse (-6) as baseline, offset by -2 for high pride (pride prefers less interaction with slaver) and +1 each for low intellect, low nature due to discomfort with lack of guidance
		if slave['obedience'] < -6 + IIF(slave['pride'] > 2, -2, 0) + IIF(slave['ego'] < 3, 1, 0) + IIF(slave['intellect'] < 3, 1, 0):
			slave['neg_rules'] = 1
			slave['num_rules_broken'] += 1
			$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Act as cook</u>: '
			$slave['rules_explanations'] += '<<$slave_name>> refuses to prepare meals for you.'
		end
	end

	if slave['cleans_rule'] = 1:
		overnight_rules_count += 1
		! Maid lesson repulse as baseline, offset by -2 for high pride (pride prefers less interaction with slaver) and +1 each for low intellect, low nature due to discomfort with lack of guidance
		if slave['obedience'] < -5 + IIF(slave['stamina'] < 3, 1, -1) + IIF(slave['intellect'] > 2, 1, -1) + IIF(slave['pride'] > 2, slave['pride'] - 2, 3 - slave['pride']) + _
																			IIF(slave['pride'] > 2, -2, 0) + IIF(slave['ego'] < 3, 1, 0) + IIF(slave['intellect'] < 3, 1, 0):
			slave['neg_rules'] = 1
			slave['num_rules_broken'] += 1
			$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Act as maid</u>: '
			$slave['rules_explanations'] += '<<$slave_name>> refuses to clean the house.'
		end
	end

	if slave['washes_master_rule'] = 1:
		overnight_rules_count += 1
		interaction_slave['repulse'] = 0 &! same tier as seduction, masturbation lessons
		stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
		impotent = 1 &! does not require the master to achieve an erection - ImperatorAugustus
		if slave['moral'] < 1: shameful = 1 &! interacts with slave exhibitionism trait when non-devoted slave is exposed to master
		dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
		dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
		if interaction_slave['res'] < 0:
			slave['neg_rules'] = 1
			slave['num_rules_broken'] += 1
			$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Bath slave</u>: '
			$slave['rules_explanations'] += '<<$slave_name>> refuses to bathe you.'
		end
		if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')
	end

	if slave['blowjob_rule'] = 1:
		overnight_rules_count += 1
		stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
		interaction_slave['repulse'] = 6 &! same as blowjob lesson
		dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
		dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Behave as alarm</u>: '
		if interaction_slave['res'] < 0:
			if slave['force_rules'] = 1 and assistant['supervise_rule'] = 1 and assistant['gladiatrix'] > slave['gladiatrix'] and assistant['stamina'] > slave['stamina']:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> does not want to provide your morning blowjob, so your assistant forces her.'
				damage = 3 & bruising = 1 & dynamic $replace($dyn_slave_damaged, 'dyn'+'slave', 'slave')
				slave_rate['angst'] += slave['sensitivity']
			else
				slave['blowjob_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> does not come to your room to provide your morning blowjob.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> enters your room in the morning and wakes you up with a blowjob.'
		end
		if slave['blowjob_rule_broken'] = 0:
			slave_rate['mood'] -= 5 + slave['temper'] + slave['pride'] + slave['ego'] + slave['angst'] - _
								slave['moral'] - slave['sub_bj'] - slave['instinct'] - slave['custom'] - _
								slave['nymphomania'] - slave['perversion'] - slave['meekness']
			if 5 + slave['temper'] + slave['pride'] + slave['ego'] + slave['angst'] - _
								slave['moral'] - slave['sub_bj'] - slave['instinct'] - slave['custom'] - _
								slave['nymphomania'] - slave['perversion'] - slave['meekness'] > 0:
				if max(0, min(master_excitement + 1, slave['sub_bj'])) < 1:
					$slave['rules_explanations'] += ' Though she tried, she could not ' + IIF(master_excitement < -2, 'get you hard.', 'make you cum.')
					$slave['rules_explanations'] += ' Disgust, distress and guilt is visible on her face.'
				else
					$slave['rules_explanations'] += ' Disgust and distress is visible on her face.'
				end
			elseif max(0, min(master_excitement + 1, slave['sub_bj'])) > 0:
				$slave['rules_explanations'] += ' You leave her with a mouthful of cum and a satisfied smile on her face.'
			elseif master_excitement < -2:
				$slave['rules_explanations'] += ' Though she tried diligently, she could not get you hard. She looks guilty.'
			else
				$slave['rules_explanations'] += ' Though she tried diligently, she could not make you cum. She looks guilty.'
			end
		end
		if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')
	end

	if slave['humility_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Behave with humility</u>: '
		if slave['obedience'] < 1:
			if slave['force_rules'] = 1 and inventory['shock_collar'] > 0:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> calls you ''Master'', and it seems to hurt her mouth every time she does.'
			else
				slave['humility_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> refuses to call you ''Master''.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> calls you ''Master''.'
		end
		if slave['humility_rule_broken'] = 0:
			slave_rate['mood'] -= slave['ego'] + slave['pride'] - slave['moral']
			if slave['ego'] + slave['pride'] - slave['moral'] > 0:
				$slave['rules_explanations'] += ' But she is clearly too willful or proud to enjoy it.'
			else
				$slave['rules_explanations'] += ' And she seems to derive some satisfaction from it.'
			end
		end
	end

	if slave['pet_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Behave as pet</u>: '
		if slave['obedience'] < 0 - 5*slave['pet_affinity']:
			if slave['force_rules'] = 1 and (slave_inventory['petsuit'] > 0 or inventory['petsuit'] > 0 or $slave['cloth'] = 'petsuit'):
				if slave_inventory['petsuit'] = 0 and $slave['cloth'] ! 'petsuit':
					if $slave['cloth'] ! '': slave_inventory[$slave['cloth']] += 1 &! return whatever the slave was wearing before to slave inventory
					inventory['petsuit'] -= 1 &! transfer from master inventory; becomes aura-bound on first use
					$slave['cloth'] = 'petsuit' &! equip the petsuit
				elseif $slave['cloth'] ! 'petsuit':
					if $slave['cloth'] ! '': slave_inventory[$slave['cloth']] += 1 &! return whatever the slave was wearing before to slave inventory
					slave_inventory['petsuit'] -= 1 &! use petsuit from slave inventory
					$slave['cloth'] = 'petsuit' &! equip the petsuit
				end
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> cannot break out of her pet suit, maintaining her on all fours all day long.'
			else
				slave['pet_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> refuses to behave like a pet.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> is a good pet.'
		end
		if slave['pet_rule_broken'] = 0:
			slave_rate['mood'] -= slave['ego'] + slave['pride'] - slave['moral']
			if slave['ego'] + slave['pride'] - slave['moral'] > 0:
				$slave['rules_explanations'] += ' She seems to resent this.'
			else
				$slave['rules_explanations'] += ' She seems to enjoy it.'
			end
		end
	end

	if slave['silence_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Remain silent</u>: '
		if slave['obedience'] < slave['temper']:
			if slave['force_rules'] = 1 and inventory['secure_gag'] > 0:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> regularly moans behind her gag.'
			else
				slave['silence_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> speaks as if she was allowed to.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> remains silent unless you ask her a question.'
		end
		if slave['silence_rule_broken'] = 0:
			slave_rate['mood'] -= slave['temper'] - slave['moral']
			if slave['temper'] - slave['moral'] > 0:
				$slave['rules_explanations'] += ' She is clearly unhappy with this suppression of her temperament.'
			else
				$slave['rules_explanations'] += ' She seems happy to attend to you without words.'
			end
		end
	end

	if slave['no_shit_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Deny toileting</u>: '
		if slave['obedience'] < slave['ego'] + slave['pride'] - master_supermacy:
			if slave['force_rules'] = 1 and inventory['anal_plug'] > 0:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> regularly tries to remove her anal plug, unsuccessfully.'
			else
				slave['no_shit_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> uses the toilet as if it was allowed. You recall one of the slaver maxims: an anal pear each day keeps the shitting at bay.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> refrains from using the toilet unsupervised.'
		end
		if slave['no_shit_rule_broken'] = 0:
			slave_rate['mood'] -= slave['ego'] - slave['moral']
			if slave['ego'] - slave['moral'] > 0:
				$slave['rules_explanations'] += ' She seems resentful.'
			else
				$slave['rules_explanations'] += ' She seems pleased to have more time with you.'
			end
		end
	end

	if slave['toilet_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Behave as toilet</u>: '
		stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
		impotent = 1 &! does not require the master to achieve an erection - ImperatorAugustus
		disgusting = 1
		interaction_slave['repulse'] = 26 &! same as coprophagia lesson
		dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
		dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
		if interaction_slave['res'] < 0:
			if slave['force_rules'] = 1 and inventory['toilet_stable'] = 1:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> would keep her mouth sealed or run away if not for the gag that force-spreads her lips and the toilet rack to which she is steadily tied.'
				slave_rate['angst'] -= interaction_slave['res'] &! additional angst increase proportional to how much she does not want to do this - ImperatorAugustus
			else
				slave['toilet_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> refuses to take your shit. Using a special toilet seat could solve the problem.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> lies down and opens her mouth whenever you mention your need.'
		end
		if slave['toilet_rule_broken'] = 0:
			slave_rate['mood'] -= 6 + slave['ego'] + slave['pride'] - slave['moral'] - slave['sub_kopro']
			if 6 + slave['ego'] + slave['pride'] - slave['moral'] - slave['sub_kopro'] > 0:
				$slave['rules_explanations'] += ' Being befouled with your waste seems to upset her.'
			else
				$slave['rules_explanations'] += ' Coprophagia seems to be a pleasant experience for her.'
			end
		end
		if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')
	end

	if slave['urinal_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Behave as urinal</u>: '
		stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
		impotent = 1 &! does not require the master to achieve an erection - ImperatorAugustus
		disgusting = 1
		interaction_slave['repulse'] = 18 &! same as golden shower lesson
		dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
		dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
		if interaction_slave['res'] < 0:
			if slave['force_rules'] = 1 and inventory['pissuar_stable'] = 1:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> would flee if not restrained on the urinal rack.'
				slave_rate['angst'] -= interaction_slave['res'] &! additional angst increase proportional to how much she does not want to do this - ImperatorAugustus
			else
				slave['urinal_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> refuses to take your urine. Tying her with a urinal rack could solve the problem.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> kneels and opens her mouth whenever you mention your need.'
		end
		if slave['urinal_rule_broken'] = 0:
			slave_rate['mood'] -= 3 + slave['ego'] + slave['pride'] - slave['moral'] - slave['sub_piss']
			if 3 + slave['ego'] + slave['pride'] - slave['moral'] - slave['sub_piss'] > 0:
				$slave['rules_explanations'] += ' Being befouled with your urine seems to upset her.'
			else
				$slave['rules_explanations'] += ' Urophagia seems to be a pleasant experience for her.'
			end
		end
		if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')
	end

	if slave['tentacle_rule'] = 1:
		overnight_rules_count += 1
		stimulating = 1 &! sex acceptance will be added to repulse - ImperatorAugustus
		interaction_slave['repulse'] = IIF(slave['virginity'] < 1, 25, 24) &! same as milk the fiend interaction
		orgastic = 1
		disgusting = 1
		vermin_based = 1
		dynamic $replace($dyn_sex_acceptance, 'dyn'+'slave', 'slave')
		dynamic $replace($dyn_repulse_check, 'dyn'+'slave', 'slave')
		if interaction_slave['res'] < 0:
			slave['neg_rules'] = 1
			slave['num_rules_broken'] += 1
			$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Milk the fiend</u>: '
			$slave['rules_explanations'] += '<<$slave_name>> refuses to milk the fiend.'
			if slave['sub_tentacles'] < 1: $slave['rules_explanations'] += ' She is not familiar with the procedure.'
		end
		if $reset_interaction_variables ! '': dynamic $replace($reset_interaction_variables, 'dyn'+'slave', 'slave')
	end

	if slave['v_balls_rule'] = 1:
		overnight_rules_count += 1
		$slave['rules_explanations'] += IIF($slave['rules_explanations'] ! '', '<br>', '') + '<u>Use vaginal beads</u>: '
		if slave['obedience'] < 2 + slave['pride'] + slave['ego'] - slave['sub_vaginal'] - slave['nymphomania'] - slave['perversion']:
			! added chastity belt as requirement for enforcing the rule - ImperatorAugustus
			if slave['force_rules'] = 1 and inventory['chastity_belt'] = 1:
				slave['neg_rules'] = 1
				slave['num_rules_wanttobreak'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> only uses the vaginal beads because the chastity belt prevents her from removing them.'
			else
				slave['v_balls_rule_broken'] = 1
				slave['num_rules_broken'] += 1
				$slave['rules_explanations'] += '<<$slave_name>> refuses to use the vaginal beads.'
			end
		else
			$slave['rules_explanations'] += '<<$slave_name>> uses the vaginal beads all day long without you having to remind her.'
		end
		if slave['v_balls_rule_broken'] = 0:
			slave_rate['mood'] -= 4 + slave['pride'] + slave['ego'] - slave['moral'] - slave['sub_vaginal']
			if 4 + slave['pride'] + slave['ego'] - slave['moral'] - slave['sub_vaginal'] > 0:
				$slave['rules_explanations'] += ' Too much self-esteem or a lack of experience makes this painful to her.'
			else
				$slave['rules_explanations'] += ' She seems to enjoy the sensations.'
			end
		end
	end

	if slave['masturbation_rule'] = 1: overnight_rules_count += 1
	if slave['no_orgasm_rule'] = 1: overnight_rules_count += 1
	!if slave['force_rules'] = 1: overnight_rules_count += 1 &! force rules no longer counts as a rule - ImperatorAugustus

end
!15 rules total - crushboss

!Handle results when slave breaks one or more rules - crushboss
if slave['num_rules_broken'] > 0:
	slave['rules_broken'] = 1
	rules_broken_message = 1
else
	slave['rules_broken'] = 0
	rules_broken_message = 0
end

if slave['rules_broken'] = 1:
	dynamic $sin_gained_now, 'slave'
	master_moodlet['neg_rebell'] = 1
	master_moodlet['pos_nice_slave'] = 0
	if slave['sin'] > 1:
		slave_rate['custom'] -= slave['num_rules_broken'] &! Habitual obedience is reduced when the slave refuses to do something - ImperatorAugustus
		slave_rate['instinct'] -= slave['ego'] &! Instinctual obedience is reduced when the slave successfully disobeys, undermining the perception that the master is superior - ImperatorAugustus
		slave_rate['rational'] -= slave['intellect'] &! Rational obedience is reduced here not for lack of trust that obedience would be rewarded but because the slave is not willing to fulfill their side of that equation - ImperatorAugustus
	end
	if slave['silence_rule'] = 1 and slave['silence_rule_broken'] = 1:
		!slave_rate['expression'] += 1
		slave_rate['temper'] += 1
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['humility_rule'] = 1 and slave['humility_rule_broken'] = 1:
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['no_shit_rule'] = 1 and slave['no_shit_rule_broken'] = 1:
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['pet_rule'] = 1 and slave['pet_rule_broken'] = 1:
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['blowjob_rule'] = 1 and slave['blowjob_rule_broken'] = 1:
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['urinal_rule'] = 1 and slave['urinal_rule_broken'] = 1:
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['toilet_rule'] = 1 and slave['toilet_rule_broken'] = 1:
		slave_rate['pride'] += 5
		slave_rate['ego'] += 1
	end
	if slave['v_balls_rule'] = 1 and slave['v_balls_rule_broken'] = 1:
		slave_rate['pride'] += 2
		slave_rate['ego'] += 1
	end
end

!Bonuses and adjustments for Rules followed - crushboss
!Awake check (not fainted) and interactions check (replacement for crushboss energy check) for applying rule effects - ImperatorAugustus
if slave_state = CONST_INT['slave_exist'] and arrsize('slave_already_done_today') > 0:

	if slave['silence_rule'] = 1 and slave['silence_rule_broken'] = 0:
		slave_rate['custom'] += 1
		slave_rate['sensitivity'] += 1
		slave_rate['temper'] -= 1
		slave_rate['pride'] -= 1
		if slave['expression'] < master_artdirector: slave_rate['expression'] += 1
	end
	if slave['humility_rule'] = 1 and slave['humility_rule_broken'] = 0:
		if master_supermacy > 2:
			slave_rate['instinct'] += (master_supermacy/2)
			slave_rate['rational'] += (master_supermacy/2)
		else
			slave_rate['instinct'] += 1
			slave_rate['rational'] += 1
		end
		slave_rate['pride'] -= 1
	end
	if slave['no_shit_rule'] = 1 and slave['no_shit_rule_broken'] = 0:
		slave_rate['custom'] += 2
		slave_rate['pride'] -= 2
	end
	if slave['pet_rule'] = 1 and slave['pet_rule_broken'] = 0:
		slave_rate['custom'] += 1
		slave_rate['ego'] -= 1
		if $slave['cloth'] ! 'petsuit': slave_rate['calisthenics'] += 1 &! petsuit inhibits free movement - ImperatorAugustus
		slave_rate['pet'] += 2
		slave_rate['pride'] -= 1
		slave_rate['hygiene'] += 10
		if slave['pet'] >= max(3, brand_reputation): master_moodlet['pos_good_pet'] = 1
	end
	if slave['blowjob_rule'] = 1 and slave['blowjob_rule_broken'] = 0:
		ejaculation_intensity = max(0, min(master_excitement + 1, slave['sub_bj'])) &! keep within [0,5] range because excitement can be negative - ImperatorAugustus
		if ejaculation_intensity > 0:
			dynamic $master_ejaculate
			master_moodlet['pos_good_morning'] = 1
			if ejaculation_intensity = 5 and master_moodlet['pos_satisfied'] < 3: master_moodlet['pos_satisfied'] = 3
			slave['sub_bj_xp'] += 1
		else
			dynamic $sin_gained_now, 'slave', 1 &! slave feels guilty (capped at 1)
		end
		slave_rate['custom'] += 1
		slave_rate['rational'] += 1
		slave_rate['instinct'] += 1
		slave_rate['pride'] -= 1
		slave_rate['hygiene'] += 5
	end
	if slave['urinal_rule'] = 1 and slave['urinal_rule_broken'] = 0:
		master_mood_rate += slave['sub_piss'] - 3
		if slave['perversion'] < 1: slave['neg_disgust'] = 10
		if slave['sub_piss'] > 2: master_moodlet['pos_toilet'] = 1
		slave['sub_piss_xp'] += 1
		slave['perversion_xp'] += 1
		slave_rate['mood'] -= max(0, 4 - slave['sub_piss'] - slave['moral'] - slave['perversion']) &! fixed sign of perversion, zero as lower bound so mood effect cannot be positive - ImperatorAugustus
		if slave['sub_piss'] < 4 and slave['perversion'] < 1: slave['neg_rules'] = 1
		slave_rate['custom'] += 2
		if slave['sub_piss'] < 3: slave['sub_piss'] += 1
		slave_rate['pride'] -= 3
		slave_rate['hygiene'] += 15
		slave_rate['angst'] += (slave['ego'] + slave['pride'] + slave['temper']) - (slave['perversion'] + slave['sub_piss'] + slave['moral'] + slave['instinct'])
	end
	if slave['toilet_rule'] = 1 and slave['toilet_rule_broken'] = 0:
		if slave['perversion'] < 2: slave['neg_disgust'] = 10
		master_mood_rate += slave['sub_kopro'] - 3
		slave['perversion_xp'] += 1
		if slave['sub_kopro'] > 3: master_moodlet['pos_toilet'] += 1
		slave_rate['mood'] -= max(0, 5 - slave['sub_kopro'] - slave['moral'] - slave['perversion']) &! fixed sign of perversion, zero as lower bound so mood effect cannot be positive - ImperatorAugustus
		if slave['sub_kopro'] < 4 and slave['perversion'] < 2: slave['neg_rules'] = 1
		slave_rate['custom'] += 3
		if slave['sub_kopro'] < 3: slave['sub_kopro'] += 1
		slave_rate['pride'] -= 4
		slave_rate['hygiene'] += 40
		slave_rate['angst'] += (slave['ego'] + slave['pride'] + slave['temper']) - (slave['perversion'] + slave['sub_kopro'] + slave['moral'] + slave['instinct'])
		if master_ill > 0 and rand(1,5) = 1: slave['ill'] += 1 &! added 20% chance to become ill (consuming faeces bacteria orally represents a vector of infection) - ImperatorAugustus
	end
	if slave['v_balls_rule'] = 1 and slave['v_balls_rule_broken'] = 0:
		slave['sub_vaginal_xp'] += 1
		slave_rate['arousal'] += 3
		slave_rate['sensitivity'] += 1
		slave_rate['pride'] -= 1
		slave_rate['hygiene'] += 5
	end

	!Penalties and Bonuses for forcing rules that the slave hates - crushboss (revised to reflect stat differences -ImperatorAugustus)
	if slave['num_rules_wanttobreak'] > 0:
		slave_rate['angst'] += min(slave['sensitivity'], slave['num_rules_wanttobreak'])*5 &! forcing rules raises despair slower if the slave has low sensitivity
		slave_rate['moral'] -= slave['num_rules_wanttobreak']*5
		slave_rate['rational'] += min(slave['intellect'], slave['num_rules_wanttobreak']) &! forcing rules raises awareness slower if the slave has low intellect
		slave_rate['pride'] -= slave['num_rules_wanttobreak']*min(IIF(difficulty_setting = 2, 2, 4), max(1, slave['pride']-1)) &! easy/normal: -4, -3, -2, -1, -1; hardcore: -2, -2, -2, -1, -1
		slave_rate['custom'] += slave['num_rules_wanttobreak']
	end

end &! end of not fainted and interactions check for rule following - ImperatorAugustus

!rest conditions
if code_defined['next_day_prison_sleep_effects'] = 0:
	code_defined['next_day_prison_sleep_effects'] = 1 &! do-once

	$next_day_prison_sleep_effects = {
		if dynslave_state > CONST_INT['no_slave'] and (dynslave['sleep_condition'] = -2 or 'dynslave' = 'prisoner'):

			dynslave['energy'] -= 1
			dynslave_rate['stamina'] -= 3
			if dynslave['sensitivity'] > 0: dynslave['neg_bad_sleep'] = 1
			dynslave['neg_spoil'] = 0 &! no spoil moodlet after overnight in prison (the text is incongruous with the situation) - ImperatorAugustus
			!SLEEP IN CELLS (applies to prisoner or slave with corresponding sleep rule): Always apply physical effects (above), check not fainted for mental impacts - ImperatorAugustus
			if slave_state ! CONST_INT['slave_faint']:
				if dynslave['deprivation_attitude'] = -1:
					if dynslave['sin'] < 3: dynslave_rate['angst'] += dynslave['sensitivity']*4
					if dynslave['sin'] > 0: dynslave_rate['rational'] += dynslave['intellect']
					dynslave_rate['fear'] += 20
					gs '$mood_drop', 'slave', 5
				elseif dynslave['deprivation_attitude'] = 0:
					if dynslave['sin'] < 3: dynslave_rate['angst'] += dynslave['sensitivity']*2
					if dynslave['sin'] > 0: dynslave_rate['rational'] += dynslave['intellect']
					dynslave_rate['fear'] += 5
					gs '$mood_drop', 'slave', 2
				elseif dynslave['deprivation_attitude'] = 1:
					if dynslave['sin'] < 1: dynslave_rate['angst'] += dynslave['sensitivity']
					if dynslave['sin'] > 0: dynslave_rate['rational'] += dynslave['intellect']
					if dynslave['fear'] < 1: dynslave_rate['fear'] += 1
				elseif dynslave['deprivation_attitude'] = 2:
					if dynslave['fear'] > 0: dynslave['fear'] -= 1
					dynslave_rate['mood'] += slave['temper']
				end
				if dynslave['deprivation_attitude'] ! 2:
					dynslave_rate['mood'] -= 5
					dynslave['neg_prisoner'] = dynslave['sensitivity']
					dynslave_rate['instinct'] += 3 &! changed from custom to instinct (imprisonment builds reflexive fear-based obedience) - ImperatorAugustus
				end
				dynslave['deprivation_xp'] += 1
				dynslave['sin'] = 0 &! clear guilt carried over from the previous day
			end

		end &! end of conscious or fainted state check
	}
end

dynamic $replace($next_day_prison_sleep_effects, 'dyn'+'slave', 'slave')
dynamic $replace($next_day_prison_sleep_effects, 'dyn'+'slave', 'prisoner')

!existence and interactions check for sleep condition effects (replacement for crushboss energy check) - ImperatorAugustus
if slave_state > CONST_INT['no_slave'] and arrsize('slave_already_done_today') > 0:

	if slave['sleep_condition'] = -1:
		slave['energy'] -= 1
		slave_rate['stamina'] -= 1
		if slave['sensitivity'] > 1: slave['neg_bad_sleep'] = 1
		slave['neg_spoil'] = 0 &! no spoil moodlet after overnight on floor (the text is incongruous with the situation) - ImperatorAugustus
		!SLEEP ON FLOOR: Always apply physical effects (above), check not fainted for mental impacts - ImperatorAugustus
		if slave_state ! CONST_INT['slave_faint']:
			slave_rate['mood'] -= slave['sensitivity'] &! effect ranges from -0 to -0.5 mood, in addition to -1 from the moodlet - ImperatorAugustus
			slave_rate['custom'] += 2 &! slave grows accustomed to the situation
			slave_rate['instinct'] += 2 &! slave feels superiority of master
			!TODO: proposed revision (Sleeping on the floor is now considered a punishment similar to sleeping in the cells and will resolve guilt) - ImperatorAugustus
			!TODO: proposed revision (If the slave does not feel any guilt, sleeping on the floor will drain awareness and increase angst)
			!slave_rate['instinct'] += 1 &! slave feels superiority of master
			!if slave['sin'] = 0:
			!	slave_rate['angst'] += (slave['sensitivity']*2 + slave['temper'])/3 &! angst increases by weighted average of sensitivity (x2) and temperament (x1)
			!	slave_rate['rational'] -= 2 &! drain awareness due to punishment without guilt
			!	!If the slaver behaves in an arbitrary way and punishes a slave who is not feeling guilty, it weakens her motivation to obey the slaver to avoid punishments, because the slaver has shown that he may punish regardless
			!else
			!	slave_rate['rational'] += 2 &! increase awareness due to punishment with guilt
			!	slave['sin'] /= 2 &! reduce guilt carried over from the previous day by half (between -= 1 for unpunished guilt and = 0 for sleeping in prison)
			!end
		end
	end

	if slave['sleep_condition'] = 0:
		if slave['sensitivity'] > 3: slave['neg_bad_sleep'] = 1
		!BEDROLL: Always apply physical effects (above), check not fainted for mental impacts - ImperatorAugustus
		if slave_state ! CONST_INT['slave_faint']:
			slave_rate['custom'] += 1 &! slave grows accustomed to the situation
		end
	end

	!Sleep (in my bed) with slaver in master bedroom - improved - crushboss
	if slave['sleep_condition'] = 1:
		!For a 'positive' sleeping condition, even if slave is unconscious, they would find out where they slept when they wake up, so apply all effects even if unconscious (energy gain only if the slave wanted to be there, as otherwise they would not sleep well) - ImperatorAugustus
		if slave['moral'] = 0:
			slave_rate['angst'] += slave['sensitivity']
		elseif slave['moral'] = 1:
			slave_rate['pride'] += slave['ego']
			slave_rate['spoil'] += slave['temper']
		elseif slave['moral'] >= 2:
			slave_rate['moral'] += 1
		end
		!slave_rate['mood'] += (slave['moral']*2) &! disabled: moodlet is enough of a boost, extra mood rate is overkill - ImperatorAugustus
		slave_rate['mood'] -= slave['angst'] + slave['pride'] &! negative mood rate can stay - ImperatorAugustus
		if master_supermacy > 0: slave_rate['instinct'] += 1 &! added master_supermacy check, taming occurs regardless of attitude - ImperatorAugustus
		if slave['pride'] < 3 and slave['angst'] <= 0 and slave['moral'] >= 2:
			slave['half_action'] -= 1
			slave_rate['moral'] += 1
			slave['pos_slept_with_master'] = 1
			!added new moodlets for sleeping with master and liking or disliking it and removed mood condition (maybe she just had a rough day) - ImperatorAugustus
		else
			! added inverse effects for a slave who does not want to sleep with master - ImperatorAugustus
			slave['energy'] -= 1 &! restless sleep due to not wanting to be there
			slave_rate['moral'] -= 1
			slave['neg_slept_with_master'] = 1
		end
	end

	!Slave sleeps in boudoir - improved - crushboss
	if slave['sleep_condition'] = 2:
		!For a 'positive' sleeping condition, even if slave is unconscious, they would find out where they slept when they wake up, so apply all effects even if unconscious - ImperatorAugustus
		slave['half_action'] -= 2
		if slave['moral'] = 0:
			slave_rate['spoil'] += slave['sensitivity']*5
		elseif slave['moral'] >= 1 and slave['moral'] < 4:
			slave_rate['spoil'] += 1
		end
		if slave['sensitivity'] > 0: slave['pos_sleep'] = 1
		if slave['angst'] < 3: slave_rate['mood'] += slave['sensitivity'] &! there is already a moodlet, but sleeping in the boudoir is a privilege: this adds up to +0.5 mood - ImperatorAugustus
		if master_supermacy > 0: slave_rate['instinct'] += 1 &! added master_supermacy check - ImperatorAugustus
		slave_rate['custom'] += 1 &! slave grows accustomed to the situation
		if slave['ego'] < 3: slave_rate['ego'] += 1 &! nature increases passively up to B+ (Independent) - ImperatorAugustus and utterlyjunky
	end

end &! end of existence and interactions check for slave sleep conditions

$next_day_angst_effects = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		!Obtain/reduce Angst - improved - crushboss
		if dynslave['mood'] < 0:
			angst_rise = 6 - dynslave['sensitivity'] + dynslave['mood']
			if angst_rise < 0: dynslave_rate['angst'] += dynslave['sensitivity']
		else
			dynslave_rate['angst'] -= dynslave['moral']*4 + dynslave['instinct']*3 + dynslave['rational']*2 + dynslave['custom']*2 + dynslave['mood']
			! discarded prisoner formula, which was (if positive mood in prison): prisoner_rate['angst'] -= prisoner['mood'] * prisoner['angst'] - ImperatorAugustus
		end

		!Angst does the dirty work
		if dynslave['angst'] > 0:
			dynslave_rate['temper'] -= dynslave['angst']
			dynslave_rate['ego'] -= dynslave['angst']

			gs '$mood_drop', 'dynslave', dynslave['angst']
			if dynslave['angst'] > dynslave['temper']: dynslave['neg_angst'] = dynslave['angst']
		end
	end
}

dynamic $do_for_each_slave_in_list, '$next_day_angst_effects', '$unfrozen_slave_characters' &! all except iced

$next_day_fear_effects = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		!Lowering fear - improved - crushboss
		if dynslave['moral'] > dynslave['fear'] and dynslave['fear'] > 0 and dynslave['mood'] > 0:
			dynslave['fear'] -= 1
			dynslave_rate['fear'] = 0
		elseif dynslave['moral'] > 0 and dynslave['fear'] > 0 and dynslave['mood'] > 0:
			dynslave_rate['fear'] -= dynslave['moral']*5
		end

		!Reduce the mood because of fear
		if dynslave['fear'] > dynslave['moral'] and dynslave['fear'] > (dynslave['ego'] - dynslave['sensitivity']):
			! replaced the conditions for suppressing this moodlet - ImperatorAugustus
			! slave morality counters fear by reframing everything as "what master wants is what I want"
			! nature/willpower (ego) increases resistance to fear, but is countered by sensitivity
			dynslave['neg_fear'] = 1
		end

		if master_supermacy < 0: dynslave_rate['instinct'] -= dynslave['ego'] - (1 + dynslave['fear'])/2 &! intimidation/submission-based obedience fades if the master has a weaker aura than the slave, offset by fear (a slave with below-average nature can gain taming instead, when fear is high enough) - ImperatorAugustus
	end
}

dynamic $do_for_each_slave_in_list, '$next_day_fear_effects', '$unfrozen_slave_characters'

gs '$difficulty_setting' &! among other things, this caps daily reward count at 2 on lower difficulties, so call it before $next_day_spoil_effects

$next_day_spoil_effects = {
	if dynslave_state = CONST_INT['slave_exist']:
		! state check does not include fainted - ImperatorAugustus

		!Spoil increase if too many rewards are given out per day - slaves should not expect a reward every time they obey - crushboss
		if dynslave['daily_reward_count'] > 2: dynslave_rate['spoil'] += (dynslave['daily_reward_count'] * 5)

		if 'dynslave' = 'slave' and dynslave['fear'] = 0 and dynslave['days_without_food'] = 0:
			!Spoil increase if not enough rules are enabled per day - slaves do not respect a master who does not expect them to follow any rules - crushboss
			!Only for current "training" slave, other roles are considered busy enough to avoid these spoil increases -ImperatorAugustus
			!Slaves will not become spoiled due to insufficient rules unless they are also without fear -ImperatorAugustus
			if overnight_rules_count < 3: dynslave_rate['spoil'] += (dynslave['pride'] + dynslave['ego'] + dynslave['temper'])
		end

		!Too high of spoiling causes obedience to fade - crushboss
		!Lowered threshold from > 3 to > 0 and made effect scale with spoil level (was flat -2,-2,-2,-4) - ImperatorAugustus
		if dynslave['spoil'] > 0:
			dynslave_rate['rational'] -= dynslave['spoil']
			dynslave_rate['instinct'] -= dynslave['spoil']
			dynslave_rate['custom'] -= dynslave['spoil']
			dynslave_rate['moral'] -= dynslave['spoil']
		end

	end

	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus
		dynslave['daily_reward_count'] = 0 &! reset here instead of in $end_day_variable_reset so you cannot freeze slaves to avoid spoiling them with too many rewards (but you can still avoid it by making them faint) - ImperatorAugustus

		!lowering spoil - improved - crushboss (restructured and also applied to prisoner, sleeping in cells, sleeping on floor, not being fed and being more afraid than devoted - ImperatorAugustus)
		if (overnight_rules_count >= IIF(difficulty_setting = 2, 3, 2) or dynslave['sleep_condition'] < 0 or dynslave['days_without_food'] ! 0 or dynslave['fear'] > dynslave['moral']) and (dynslave_rate['spoil'] > 0 or dynslave['spoil'] > 0):
			dynslave_rate['spoil'] -= 1 + dynslave['fear'] + dynslave['moral'] + dynslave['angst'] + max(0, dynslave['days_without_food'])
			if dynslave['mood'] < 0: dynslave_rate['spoil'] -= dynslave['sensitivity']
		end

	end
}

dynamic $do_for_each_slave_in_list, '$next_day_spoil_effects', '$unfrozen_slave_characters'

$next_day_slave_mood = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		if dynslave_state = CONST_INT['slave_exist']:
			! state check is conscious only - ImperatorAugustus
			dynslave_rate['mood'] += dynslave['moral'] + (dynslave['stamina'] - 3) + dynslave['mood'] _
				- dynslave['arousal'] - dynslave['fear'] - dynslave['spoil'] - dynslave['angst']*2 _
				- dynslave['hygiene'] - (house_mess - 1) &! removed energy from this formula (handled elsewhere) and doubled angst impact on mood - ImperatorAugustus

			if dynslave['spoil'] > 0 and dynslave['spoil'] > dynslave['mood'] and dynslave['sleep_condition'] >= -1: dynslave['neg_spoil'] = 1 &! not for prisoners or slaves sleeping in cells (TODO: or on the floor)
		end

		! effects for both conscious and fainted slaves (for when they wake up):
		if dynslave['moral'] > 0: dynslave['pos_moral'] = dynslave['moral']
		if dynslave['angst'] > 0: dynslave['neg_angst'] = dynslave['angst']

		!Lower slave mood when obedience, devotion, habit, instinct, and awareness are low and mood is high - crushboss (mood caps are now handled by $moodlets_account - ImperatorAugustus)
		!if dynslave['mood'] > 2 and (dynslave['moral'] < 1 or (dynslave['moral'] < 2 and max(dynslave['rational'], dynslave['instinct'], dynslave['custom']) <= 3)):
		!	dynslave_rate['mood'] -= (dynslave['pride'] + dynslave['temper'] + dynslave['ego'])
		!end

	end
}

dynamic $do_for_each_slave_in_list, '$next_day_slave_mood', '$unfrozen_slave_characters'

$next_day_slave_arousal = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		!Overhauled arousal calculation and modifiers - crushboss
		arousal_modifier = 0
		if dynslave['nymphomania'] > 0: arousal_modifier += ((dynslave['temper'] + dynslave['sensitivity'] + dynslave['mood'])/2)
		if dynslave['nymphomania'] < 0: arousal_modifier -= ((dynslave['temper'] + dynslave['sensitivity'] + dynslave['mood'])/2)
		dynslave_rate['arousal'] += dynslave['stamina'] + daily_bonus['arousal'] + dynslave['sensitivity'] + dynslave['mood'] + dynslave['moral'] + arousal_modifier - (6 + dynslave['angst'] + dynslave['pride'] + dynslave['fear'] + dynslave['spoil'])

		if dynslave['pos_orgasm'] > 0: dynslave['pos_orgasm'] -= 1
		if dynslave['pos_orgasm'] < 0: dynslave['pos_orgasm'] = 0

	end &! end of conscious or fainted state check
}

dynamic $do_for_each_slave_in_list, '$next_day_slave_arousal', '$unfrozen_slave_characters'

$next_day_slave_moral = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		!Growing moral - i.e. Daily Devotion Bonus - improved - crushboss
		moral_app = min (dynslave['moral'], dynslave['custom'], dynslave['rational'], dynslave['instinct'])
		if max(dynslave['angst'], dynslave['spoil']) = 0 and dynslave['mood'] > 0 and moral_app = dynslave['moral'] and dynslave['fear'] < 5 and dynslave['pride'] < 5 and overnight_rules_count >= IIF(difficulty_setting = 2, 3, 2):
			moral_basis = (dynslave['custom'] + dynslave['rational'] + dynslave['instinct'])/3
			moral_boost = min (dynslave['mood'], moral_basis)
			if moral_boost < 1: moral_boost = 1
			dynslave_rate['moral'] += moral_boost
		end

		!Lowering moral progress if the slave is severely mishandled - crushboss
		if dynslave['angst'] > 4 and dynslave['fear'] > 4 and dynslave['mood'] < 0:
			dynslave_rate['moral'] -= (dynslave['pride'] + dynslave['ego'] + dynslave['temper'])
		end

		!Love begins with true devotion - crushboss
		if dynslave['moral'] >= 5 and master_str >= 5 and master_domination >= 5 and master_cha >= 5: dynslave['pos_slave_love'] = 100

	end &! end of conscious or fainted state check
}

dynamic $do_for_each_slave_in_list, '$next_day_slave_moral', '$unfrozen_slave_characters'

!Training bonuses for easy obedience difficulty - crushboss
if difficulty_setting = 1:
	moral_basis = ((master_str + master_domination + master_fighter + master_cha)/5)
	if moral_basis < 1: moral_basis = 1
	slave_rate['moral'] += moral_basis
	slave_rate['custom'] += 1
	slave_rate['rational'] += 1
	slave_rate['instinct'] += 1
	slave_rate['spoil'] -= 5
	slave_rate['angst'] -= 5
end

!daily_reward_count = 0 - unnecessary, not used again before it is cleared by $end_day_variable_reset - ImperatorAugustus

!brand effect
if slave['brand'] > 0: slave_rate['custom'] += 1
if slave['brand'] = 3: slave_rate['custom'] += 1

!chance of dying at zero value health
!base - 14% - min 6% max 20% (0% if master or assistant has S+ medical skill)
if slave_state = CONST_INT['slave_exist'] and slave['stamina'] = 0 and max(master_medic, assistant['nurse']) < 5:
	dprob_max = 7
! psy status affect the chance
	if $slave_psy_status = 'servile' or $slave_psy_status = 'obedient': dprob_max = 16
	if $slave_psy_status = 'soft' or $slave_psy_status = 'optimistic': dprob_max = 9
	if $slave_psy_status = 'broken': dprob_max = 5
	death_prob = 1 + ((97 * day) mod dprob_max) &! pseudo-rand(1,dprob_max)
	if death_prob = 5:
		slave_died = 1
	end
end

!Slave energy refresh for following day (no changes to slave energy after this) - ImperatorAugustus
$next_day_energy_refresh = {
	if dynslave_state > CONST_INT['no_slave']:
		! state check includes fainted - ImperatorAugustus

		a = dynslave['stamina'] - dynslave['wounds']
		if a < 1:
			if farmhand_state = CONST_INT['slave_exist'] and arrpos('$livestock_slave_characters', 'dynslave') ! -1 and farmhand['nurse'] > dynslave['wounds']:
				a = 1
			elseif assistant_state = CONST_INT['slave_exist'] and assistant['nurse'] > dynslave['wounds']:
				a = 1
			elseif master_medic > dynslave['wounds']:
				a = 1
			end
		end
		dynslave['energy'] = min(dynslave['stamina'], max(IIF(dynslave['stamina'] > 0, -4, -5), dynslave['energy'] + a)) &! if stamina is zero, [-5, slave stamina level], else [-4, slave stamina level]

	end &! end of conscious or fainted state check
}

dynamic $do_for_each_slave_in_list, '$next_day_energy_refresh', '$unfrozen_slave_characters'

!master part
estate_quality = estate_prestige[$estate] + interior_prestige[$study] + interior_prestige[$bath] + interior_prestige[$bed] - house_mess
estate_quality_modifier = estate_quality - brand_reputation
if estate_quality_modifier > master_cha: master_cha_rate += 1
if brand_reputation > master_cha: master_cha_rate += 1
if master_visage > 0: master_visage -= 1
if master_haircut > 0: master_haircut -= 1

!effect of food quality on master mood - added by wisarddd 2020-02-28
if cooked_food >= brand_reputation and cooked_food >= 3:
	master_moodlet['pos_self_food'] = 1
elseif cooked_food < brand_reputation - 1 or cooked_food < 3:
	master_moodlet['neg_food'] = 1
end

!mood change - fixed by wisarddd 2020-02-28
if master_moodlet['pos_show'] < brand_reputation - 1 - master_mood:
	master_moodlet['neg_boring'] = 1
	master_moodlet['pos_show'] = 0
	gs '$mod_master_drop', 1
end

!treat wounds and illness effects
if assistant_state = CONST_INT['slave_exist']:
	healing = master_medic + assistant['nurse']
else
	healing = master_medic
end
if master_wounds_rate > 0: master_wounds_rate -= master_str + master_mood + healing - master_hygiene + 1
if master_wounds_rate < 0: master_wounds_rate = 0
killvar 'healing'

if master_ill > 0:
	master_ill += 1
	if master_ill > 10 and ((89 * day + master_ill + master_str) mod 60) < master_str: master_ill = 0 &! chance of natural recovery after a decade, 5% at B+ strength, 8.3% at S+ strength - ImperatorAugustus
	if master_ill > master_str: master_str_rate -= 1
	if master_ill > 6: master_moodlet['neg_master_ill'] = master_ill
end

!next day energy for master
master_energy += min(5, max(-5, master_str + 1 - master_wounds))

!raise or lower health
if ate_already = 0 and cooked_food < 3 and master_str > cooked_food + 1:
	master_str_rate -= 1 &! if the slaver did not eat out during the day, eating F- food will decline strength to D-; eating D- food will decline strength to C-; eating C- food will decline health to B+
elseif (cooked_food - 1) > master_str:
	master_str_rate += 1 &! eating high quality food can raise master health up to one level below the food quality grade (max of A+ health with S+ food)
end
if snake_amulet_on = 1 and master_str < 3: master_str_rate += 1 &! quest artifact passively raises health up to B+, countering the penalty for low quality food

!Addiction
if master_str < master_alco_addiction: master_str_rate -= 2

if master_drunk > 0:
	master_moodlet['neg_drunk'] = 1 + master_alco_addiction
	master_drunk = 0
end
if master_koffed > 0:
	master_moodlet['neg_no_koffe'] = master_koffe_addiction - 30
	master_koffed = 0
	master_koffe_addiction += 1
elseif master_koffe_addiction > 0:
	if master_koffe_addiction > 40: master_str_rate -= 1
	if rand(1,3) = 1: master_koffe_addiction -= 1
end
if kannabis_pressure > 0:
	if kannabis_pressure > 30: master_cha_rate -= 1
	if rand(1,4) = 1: kannabis_pressure -= 1
end
if master_moodlet['neg_no_meth'] > 1: master_str_rate -= 1
if master_on_meth > 0:
	master_moodlet['neg_no_meth'] = master_meth_addiction - rand(2,6)
	if master_moodlet['neg_no_meth'] > 1: master_energy -= 1
	master_meth_addiction += 1
	master_on_meth = 0
elseif master_meth_addiction > 0 and rand(1,5) = 1:
	master_meth_addiction -= 1
end
if master_moodlet['neg_no_opium'] > 1: master_str_rate -= 1
if master_on_opium > 0:
	master_moodlet['neg_no_opium'] = master_opium_addiction - rand(2,6)
	if master_moodlet['neg_no_opium'] > 1: gs '$mod_master_drop',100
	master_opium_addiction += 1
	master_on_opium = 0
elseif master_opium_addiction > 0 and rand(1,8) = 1:
	master_opium_addiction -= 1
end
master_energised = 0

if master_moodlet['neg_wounded'] < master_wounds: master_moodlet['neg_wounded'] = master_wounds

!rise arousal
master_excitement_rate += master_libido*20
if master_excitement = 5 and chimera_gem_on ! 1:
	blazing_counter += 1
else
	blazing_counter = 0
end

!Beginning of prisoner check - no calculations if no prisoner exists - crushboss
if prisoner_state ! CONST_INT['no_slave']:
	prisoner_rate['hygiene'] += 7 + prisoner['wounds']

	if prisoner['prison_food_type'] = -1:
		! no food
		if cheat_no_starve = 0: prisoner['starvation'] += 22 - prisoner['fat']*2 &! starvation effects (highest magnitude) in addition to the endurance drop for sleeping in prison
		prisoner_rate['fat'] -= rand(3, 2 + (1+prisoner['metabolism'])/2) &! should be same formula used for slave
		prisoner_rate['fear'] += 5
		prisoner_rate['angst'] += 5
		prisoner_rate['mood'] -= 5
		prisoner['calories'] = -22 + prisoner['fat']*2
		prisoner_food_cost = 0
	end

	if prisoner['prison_food_type'] = 1:
		! insufficient diet
		prisoner['calories'] = -6
		if cheat_no_starve = 0: prisoner['starvation'] += 4 &! starvation effects (lowest magnitude) in addition to the endurance drop for sleeping in prison
		prisoner_rate['fat'] -= rand(3, 2 + (1+prisoner['metabolism'])/2) &! should be same formula used for slave
		prisoner_rate['fear'] += 2
		prisoner_rate['angst'] += 2
		prisoner_rate['mood'] -= 2
		prisoner_food_cost = 1
	end

	if prisoner['prison_food_type'] = 0:
		! sufficient diet
		prisoner['calories'] = 3
		prisoner_food_cost = 3
	end

	if prisoner['prison_food_type'] = 2:
		! fiend cum
		prisoner['neg_slave_food'] = 1
		prisoner['neg_disgust'] = 1 + prisoner['pride'] - prisoner['perversion']
		if prisoner['perversion'] = 2: prisoner['pos_cookie'] = 1 & prisoner['neg_slave_food'] = 0
		if prisoner['fiend_tattoo'] = 0: prisoner['neg_disgust'] = 1 + prisoner['pride'] - prisoner['perversion']
		if prisoner['fiend_tattoo'] = 1: prisoner['pos_cookie'] = 1 & prisoner['neg_slave_food'] = 0
		prisoner['perversion_xp'] += 1
		prisoner['calories'] = 3
		if spawn_semen > 2:
			spawn_semen -= 3
			prisoner_food_cost = 0
		else
			prisoner_food_cost = (3 - spawn_semen)
			spawn_semen = 0
		end
	end

	if prisoner['prison_food_type'] = 3:
		! cookies
		prisoner_eats = (11 - prisoner['ego'] - prisoner['pride'] - prisoner['mood']) / 2
		if prisoner['age'] = CONST_INT['milf_age']: prisoner_eats += 1
		if prisoner['age'] = CONST_INT['loli_age']: prisoner_eats -= 1
		if prisoner_eats < 0 or prisoner['fat'] = 5: prisoner_eats = 1
		prisoner_food_cost = prisoner_eats
		prisoner['neg_disgust'] = 1 + prisoner['pride'] - prisoner['perversion']
		prisoner['neg_slave_food'] = 0
		prisoner['perversion_xp'] += 2
		prisoner_rate['mood'] -= rand(1,prisoner_eats+2)
		prisoner_rate['fat'] += max(1, prisoner_eats - prisoner['metabolism']) &! needs to be non-zero
		prisoner['calories'] = prisoner_eats
		if prisoner['fat'] > 3: prisoner_rate['stamina'] -= min(prisoner_eats, prisoner['metabolism']) &! overweight - excess calories are unhealthy (this is in addition to the endurance drop for sleeping in prison)
		if prisoner['fat'] < 5 and prisoner_rate['fat'] >= indep_atr_max_threshold[prisoner['fat']]:
			prisoner['ego'] -= rand(0,1)
			prisoner['pride'] -= rand(0,1)
		elseif prisoner['fat'] = 5 and prisoner_rate['fat'] > 6:
			prisoner_rate['fat'] = 0
			prisoner['ego'] -= rand(0,1)
			if prisoner['ego'] >= 0: prisoner['pride'] -= rand(0,1)
		end
		if prisoner['ego'] < 0: prisoner['ego'] = 0
		if prisoner['pride'] < 0: prisoner['pride'] = 0
	end

	dynamic $replace($next_day_starvation_effects, 'dyn'+'slave', 'prisoner'), (prisoner['prison_food_type'] + 1) &! -1 represents no food, so +1 to shift it to zero for boolean

	total_food_cost += prisoner_food_cost
!End of prisoner checks - do not remove this end statement - crushboss
end

!Beginning of cow check - no calculations if no cow exists - crushboss - updated Pianocat
if cow_state > CONST_INT['no_slave']:
	cow_rate['hygiene'] += 12 + cow['wounds']

	if cow_ration = -1:
		! no food
		if cheat_no_starve = 0: cow['starvation'] += 22 - cow['fat']*2 &! starvation effects (highest magnitude)
		cow_rate['fat'] -= rand(3, 2 + (1+cow['metabolism'])/2) &! should be same formula used for slave
		cow_rate['fear'] += 5
		cow_rate['angst'] += 5
		cow_rate['mood'] -= 5
		barn_cow_food += 0
		cow['calories'] = -22 + cow['fat']*2
	end

	if cow_ration = 1:
		! standard food
		if cow['time_in_barn'] < 10: cow['neg_slave_food'] = 1
		barn_cow_food += cow['lactation'] + 5
		cow['calories'] = barn_cow_food
	end

	if cow_ration = 0:
		! fattening food
		cow_rate['fat'] += 8 - cow['metabolism'] &! metabolism ranges [1,5] so this results in +5 gain with average metabolism (3)
		barn_cow_food += cow['lactation']*2 + 5
		cow['calories'] = barn_cow_food
	end

	if cow_ration = 2:
		! fiend cum
		cow['neg_slave_food'] = 1
		cow['neg_disgust'] = 1 + cow['pride'] - cow['perversion']
		if cow['perversion'] = 2: cow['pos_cookie'] = 1 & cow['neg_slave_food'] = 0
		cow['perversion_xp'] += 1
		cow['calories'] = 3
		if spawn_semen > 5:
			spawn_semen -= 5
			barn_cow_food = 0
		else
			barn_cow_food += (5 - spawn_semen)
			spawn_semen = 0
		end
	end

	dynamic $replace($next_day_starvation_effects, 'dyn'+'slave', 'cow'), (cow_ration + 1) &! -1 represents no food, so +1 to shift it to zero for boolean

	!Risks for slave to escape the barn or commit suicide
	cow_escape = 0
	cow_suicide = 0
	if $cow_psy_status ! 'broken' and cow_locked = 0:
		if cow['cowSK'] < 5 and rand(8,12) > cow['cowSK'] + cow['obedience'] + cow['fear']*2: cow_escape = 1
		if cow['angst'] > cow['ego'] + cow['custom'] and rand(0,10) < cow['angst']: cow_suicide = 1
	end

	!Time in barn influence slave - Pianocat
	!fixed state check: time_in_barn also increases for fainted cow - ImperatorAugustus
	if cow_escape = 0: cow['time_in_barn'] += 1
	if cow['time_in_barn'] = 1: barn_first_night = 1
	if cow['time_in_barn'] = 10: barn_first_decade = 1

	!barn_custom determines how slaves react at time spent in barn - Pianocat
	no_more_barn_update = 0
	if cow['cowSK'] = 5:
		cow['barn_custom'] = 6
		no_more_barn_update = 1
	else
		cow_sanity = cow['ego'] + cow['temper'] + cow['pride']
		if cow['cowSK'] = 4: cow['barn_custom'] = 5
		if cow['cowSK'] = 4 and cow_sanity < 5: cow['barn_custom'] = 4
		if cow['cowSK'] = 3: cow['barn_custom'] = 3
		if cow['cowSK'] = 3 and cow_sanity < 5: cow['barn_custom'] = 2
		if cow['cowSK'] < 3: cow['barn_custom'] = 1
		if cow_sanity < 3: cow['barn_custom'] = 0
		killvar 'cow_sanity'
	end
	if cow['cowSK_affinity'] ! 0 and cow['cowSK_affinity_revealed'] = 0: cow['cowSK_affinity_revealed'] = 1

	!impacts on the cow - less negative impact and quicker learning if a farmhand is appointed - Pianocat
	!condition escape_cow=0 added, to avoid an escaping cow malus/bonus, excepted moral ones
	if cow['barn_custom'] = 6:
		if farmhand_state = CONST_INT['slave_exist']:
			cow['pos_cow'] = 1
			cow['pos_barn'] = 1
			cow_rate['intellect'] -= 2
		else
			if cow['deprivation_attitude'] > 0: cow['pos_barn'] = 1
			cow_rate['intellect'] -= 3
		end
	elseif cow['barn_custom'] = 5:
		if cow_escape = 0:
			cow_rate['cowSK'] += 10
			cow_rate['pride'] -= 2
			if farmhand_state = CONST_INT['slave_exist']:
				cow['pos_barn'] = 1
				cow_rate['temper'] -= 1
				cow_rate['intellect'] -= 2
				cow_rate['ego'] -= 1
			else
				if cow['deprivation_attitude'] > 0: cow['pos_barn'] = 1
				cow_rate['temper'] -= 2
				cow_rate['intellect'] -= 3
				cow_rate['ego'] -= 2
			end
		end
	elseif cow['barn_custom'] = 4:
		if cow_escape = 0:
			cow_rate['cowSK'] += 8
			cow_rate['pride'] -= 2
			if farmhand_state = CONST_INT['slave_exist']:
				cow['pos_barn'] = 1
				cow_rate['temper'] -= 1
				cow_rate['intellect'] -= 2
				cow_rate['ego'] -= 1
			else
				if cow['deprivation_attitude'] > 0: cow['pos_barn'] = 1
				cow_rate['temper'] -= 2
				cow_rate['intellect'] -= 3
				cow_rate['ego'] -= 2
			end
		end
	elseif cow['barn_custom'] = 3:
		if cow_escape = 0:
			cow_rate['cowSK'] += 5
			cow_rate['pride'] -= 2
		end
		if farmhand_state = CONST_INT['slave_exist']:
			if cow_escape = 0:
				cow_rate['temper'] -= 1
				cow_rate['intellect'] -= 2
				cow_rate['ego'] -= 1
			end
		else
			if cow['deprivation_attitude'] < 1: cow['neg_barn'] = 2
			if cow_escape = 0:
				cow_rate['temper'] -= 2
				cow_rate['intellect'] -= 3
				cow_rate['ego'] -= 2
			end
		end
	elseif cow['barn_custom'] = 2:
		cow['neg_barn'] = 3
		if cow_escape = 0:
			cow_rate['cowSK'] += 3
			cow_rate['pride'] -= 2
		end
		if farmhand_state = CONST_INT['slave_exist']:
			if cow_escape = 0:
				cow_rate['temper'] -= 1
				cow_rate['intellect'] -= 2
				cow_rate['ego'] -= 1
			end
		else
			if cow['deprivation_attitude'] < 1: cow['neg_barn_plus'] = 2
			if cow_escape = 0:
				cow_rate['temper'] -= 2
				cow_rate['intellect'] -= 3
				cow_rate['ego'] -= 2
			end
		end
	elseif cow['barn_custom'] = 1:
		cow['neg_barn'] = 4
		if cow['deprivation_attitude'] < 2: cow['neg_barn_plus'] = 2
		if cow_escape = 0:
			cow_rate['cowSK'] += 2
			cow_rate['pride'] -= 4
		end
		if farmhand_state = CONST_INT['slave_exist']:
			if cow_escape = 0:
				cow_rate['temper'] -= 3
				cow_rate['intellect']-= 3
				cow_rate['ego'] -= 3
			end
		else
			cow['neg_disgust'] = 1
			if cow_escape = 0:
				cow_rate['temper'] -= 4
				cow_rate['intellect'] -= 3
				cow_rate['ego'] -= 4
			end
		end
	end

	cow['deprivation_xp'] += 1

	!Broken cows do not care if farmhand or not - Pianocat
	if cow['barn_custom'] = 0:
		cow_rate['cowSK'] += 6
		cow_rate['intellect'] -= 3
	end

	!Adding additional penalties if cow is locked
	if cow_locked = 1 and cow['barn_custom'] ! 0:
		if cow['deprivation_attitude'] = -1:
			cow_rate['fear'] += 20
			cow_rate['angst'] += 20
			cow_rate['mood'] -= 20
			cow_rate['rational'] += 5
			if cow['fear'] < cow['sensitivity']: cow['fear'] = cow['sensitivity']
		end
		if cow['deprivation_attitude'] = 0:
			cow_rate['fear'] += 5
			cow_rate['angst'] += 10
			cow_rate['mood'] -= 10
			cow_rate['rational'] += 5
			a = cow['sensitivity'] - 1
			if cow['fear'] < a: cow['fear'] = a
		end
		if cow['deprivation_attitude'] = 1:
			cow_rate['fear'] += 3
			cow_rate['angst'] += 5
			cow_rate['mood'] -= 5
			cow_rate['rational'] += 3
			a = cow['sensitivity'] - cow['ego']
			if cow['fear'] < a: cow['fear'] = a
		end
		if cow['deprivation_attitude'] = 2:
			cow_rate['fear'] += 0
			cow_rate['mood'] += 2
		end
		cow['energy'] -= 1
		cow_rate['stamina'] -= 1
		cow['deprivation_xp'] += 1
		cow['neg_prisoner'] = 3
	end

	!cow stamina slowly trends to Weakened
	if cow['stamina'] < 2:
		cow_rate['stamina'] += 2
	elseif cow['stamina'] > 2:
		cow_rate['stamina'] -= 2
	elseif cow_rate['stamina'] > 0:
		cow_rate['stamina'] -= 1
	elseif cow_rate['stamina'] < 0:
		cow_rate['stamina'] += 1
	end

!End of cow checks - do not remove this end statement
end

!Dirt and Hygiene from sleep, time, and normal house use - master and slave - crushboss
house_mess_rate += 5
master_hygiene_rate += 5 + master_wounds
slave_rate['hygiene'] += 5 + slave['wounds']
assistant_rate['hygiene'] += 5 + assistant['wounds']

!food reset
master_food_cost = IIF(cooked_food < 1, 2, 0) &! cans cost 2 sparks a day, otherwise cost is paid in ingredients

total_food_cost += master_food_cost
if slave_inventory['pony'] > 0: total_food_cost += 1
if assistant_inventory['pony'] > 0: total_food_cost += 1
if prisoner_inventory['pony'] > 0: total_food_cost += 1
cooked_food = -1

$next_day_apply_overnight_item_bonuses = {
	!daily bonuses - improved - crushboss
	!Slave energy check - no bonuses if the player just clicks 'End of Day' to advance time - crushboss (replaced energy check with interactions counter check - ImperatorAugustus)
	!Converted to dynamic and applied to prisoner and cow in addition to slave and assistant - ImperatorAugustus
	if dynslave_state = CONST_INT['slave_exist'] and (arrsize('dynslave_already_done_today') > 0 or 'dynslave' = 'cow' or 'dynslave' = 'prisoner'):
		! state check is conscious only - ImperatorAugustus

		dynamic $replace($recalc_equipment_bonuses, 'dyn'+'slave', 'dynslave') &! split first to protect from earlier $replace, see #item_base and #static_base
		dynslave_rate['stamina'] += daily_bonus['stamina']
		dynslave_rate['sensitivity'] += daily_bonus['sensitivity']
		dynslave_rate['temper'] += daily_bonus['temper']
		dynslave_rate['intellect'] += daily_bonus['intellect']
		dynslave_rate['ego'] += daily_bonus['ego']
		dynslave_rate['pride'] += daily_bonus['pride']
		dynslave_rate['instinct'] += daily_bonus['instinct']
		dynslave_rate['rational'] += daily_bonus['rational']
		dynslave_rate['custom'] += daily_bonus['custom']
		dynslave_rate['angst'] += daily_bonus['angst']
		dynslave['half_action'] += daily_bonus['half_action'] &! items can provide bonus energy by adding negative half_action count - ImperatorAugustus

		! Heavy rings bonus
		tmp = 0
		!if $slave['earrings'] = 'heavy_gauge_rings': tmp += 0
		if $slave['tongue'] = 'heavy_gauge_rings': tmp += 3
		if $slave['nipplerings'] = 'heavy_gauge_rings': tmp += 2
		if $slave['navel'] = 'heavy_gauge_rings': tmp += 1
		if $slave['clitring'] = 'heavy_gauge_rings': tmp += 3
		if slave['nose_piercing'] = 1: tmp += 3
		slave_rate['instinct'] += tmp / 3
		slave_rate['rational'] += tmp / 4
		killvar 'tmp'

		!Raise interest to the slave
		gs '$slave_price', 'dynslave' &! get charm value for current slave
		dynslave['newness'] += 1 + charm/6

	end &! end of consciousness and interactions check
}

dynamic $do_for_each_slave_in_list, '$next_day_apply_overnight_item_bonuses', '$main_screen_slave_characters' &! slave, assistant, prisoner, cow

!Define the action at the end of the day
!added check for slave status
!does she want to masturbate
!Added check for energy level of slave and increased the minimum arousal level to 3 - crushboss
slave_will_masturbate = 0
slave_masturbating_anyway = 0
if slave_state = CONST_INT['slave_exist'] and previous_energy >= 0 and slave['arousal'] > slave['pride'] and slave['arousal'] > 2:
	if slave['masturbation_rule'] = 1:
		!is there a rule against it
		if slave['force_rules'] = 1 and inventory['chastity_belt'] = 1:
			!is she wearing a chastity belt
			slave['neg_rules'] = 1
			slave_masturbating_anyway = 0
		elseif (slave['instinct'] > slave['arousal'] or slave['obedience'] > slave['arousal'] * 3 or slave['caught_masturbating'] > 0) and (slave['arousal'] < 5 or slave['moral'] >= slave['arousal']):
			!if theres a rule against it, will her taming or obedience or devotion or being caught recently stop her (only possible if arousal is less than 5 or devotion equals or exceeds arousal)
			slave_rate['instinct'] += 1
			slave_masturbating_anyway = 0
		else
			slave_masturbating_anyway = 1
		end
	else
		slave_will_masturbate = 1
	end
else
	slave_will_masturbate = 0
end

!pet tentacle
if pet_tentacle > 0:
	tentacle_hp -= 1
	if tentacle_size >= 4: tentacle_hp -= 4
	if tentacle_hp >= tentacle_grow[tentacle_size]:
		if tentacle_size < 4: tentacle_grow_event = 1
	end
	if tentacle_hp <= tentacle_death[tentacle_size]:
		tentacle_death_event = 1
	end
end

!Arousal check for end_day_screen event - crushboss
if slave['arousal'] > 4 and slave_state = CONST_INT['slave_exist'] and slave['obedience'] > 7 and master_supermacy > 0:
	slave_beg_for_sex = 1
else
	slave_beg_for_sex = 0
end
if assistant['arousal'] > 4 and assistant_state = CONST_INT['slave_exist'] and assistant['obedience'] > 7 and master_supermacy > 0:
	assistant_beg_for_sex = 1
else
	assistant_beg_for_sex = 0
end

txt = 1
menu_form = 0

!Magic Spell Reset - moved out of $end_day_variable_reset to work with auto casting rules - crushboss
auspex = raven_crown_on
magna_magnifika = 0

!loss condition in tutorial if slave is killed
if exam_in_progress = 1:
	if slave['stamina'] = 0:
		lecture = 12
		gt 'demo_intro'
	end
end

!autosave condition
if autosave_day = 1:
	autosave = 1
end

!Barn stuff goes here
!farmhand lesbian xp accumulation
!bonus calculation
if farmhand_state = CONST_INT['slave_exist']:
	if cow_state = CONST_INT['slave_exist'] and farmhand['homosexualism'] = 0: farmhand['lesbian_xp'] + 1
	if harpy_state = CONST_INT['slave_exist'] and farmhand['homosexualism'] = 0: farmhand['lesbian_xp'] + 1
	if farmhand['lesbian_xp'] > 50 and farmhand['homosexualism'] = 0: farmhand['homosexualism'] = 1 &! bi-sexual
	barn_farmhand_bonus = 120
	if farmhand['homosexualism'] = -1: barn_farmhand_bonus -= 10
	if farmhand['homosexualism'] = 1: barn_farmhand_bonus += 20
	if farmhand['homosexualism'] = 2: barn_farmhand_bonus += 30
else
	barn_farmhand_bonus = 100
end

!farmhand food calc
if farmhand_state = CONST_INT['slave_exist']:
	farmhand['calories'] = 3 + farmhand['stamina']
	total_food_cost += 2 &! goes to household expenses, not barn food costs - ImperatorAugustus
end

!eggs
if harpy_state = CONST_INT['slave_exist']:
	!calculate egg amount
	a = rand(3,6)
	barn_harpy_eggs = harpy['age'] + harpy['stamina'] + a + harpy['fat']
	harpy['egglaying'] = 1
	!set egglaying rating
	if barn_harpy_eggs > 5: harpy['egglaying'] = 2
	if barn_harpy_eggs > 7: harpy['egglaying'] = 3
	if barn_harpy_eggs > 9: harpy['egglaying'] = 4
	if barn_harpy_eggs > 12: harpy['egglaying'] = 5
	barn_harpy_eggs *= barn_farmhand_bonus
	barn_harpy_eggs /= 100
	!check to see if harpy’s recovered from surgery
	if harpy['rehabilitation'] > 0:
		barn_harpy_eggs = 0
		harpy['egglaying'] = 0
	end
	decade_eggs += barn_harpy_eggs
	!refill the fridge
	consume_eggs = barn_harpy_eggs
	if fridge_space >= 5:
		barn_harpy_eggs += fridge['eggs']
		fridge['eggs'] = 0
		if barn_harpy_eggs < 5:
			fridge['eggs'] = barn_harpy_eggs
			barn_harpy_eggs = 0
		else
			fridge['eggs'] = 5
			barn_harpy_eggs -= 5
		end
	end
	consume_eggs -= barn_harpy_eggs
	!sell any leftover eggs
	barn_egg_profit += barn_harpy_eggs/2
	if consume_eggs > 0: decade_consume += consume_eggs*2
end

!harpy food calc and weight gain
if harpy_state = CONST_INT['slave_exist']:
	barn_harpy_food += 1 + harpy['egglaying'] / 2
	if barn_farmhand_fattens = 1 and farmhand_state = CONST_INT['slave_exist']:
		barn_harpy_food += 3
		harpy_rate['fat'] += 5
	end
	harpy['calories'] = barn_harpy_food
end

!pig food calc and weight gain
! food consumption and weight gain based on a Weight-gain cookies diet vs her willpower to resist.
if pig_state = CONST_INT['slave_exist']:
	barn_pig_food += (7 - pig['ego'])
	if pig['age'] = CONST_INT['loli_age']: barn_pig_food -= 1
	pig_rate['fat'] += (7 - pig['ego'])
! feeds her kitchen scraps, 0$ and maybe table scraps. if farmhand_state = CONST_INT['slave_exist']: barn_pig_food += 1
	if farmhand_state = CONST_INT['slave_exist']: pig_rate['fat'] += 3
	if slave['diet_leftovers'] = 0: pig_rate['fat'] += 3
	pig['calories'] = barn_pig_food
end

!pig fitness levelling - increases or decreases to 3
if pig_state = CONST_INT['slave_exist']:
	if farmhand_state ! CONST_INT['slave_exist'] and pig['stamina'] > 3: pig_rate['stamina'] -= 4
	if pig['stamina'] < 3: pig_rate['stamina'] += 4
end

!egglayer fitness levelling
if harpy_state = CONST_INT['slave_exist']:
	if farmhand_state ! CONST_INT['slave_exist'] and harpy['stamina'] > 3: harpy_rate['stamina'] -= 2
	if harpy['stamina'] < 3: harpy_rate['stamina'] += 2
end

!Align livestock characteristics (calculate attribute values from rates, enforce upper/lower bounds)
dynamic $do_for_each_slave_in_list, '$dyn_calc_slave_attrs', '$livestock_slave_characters'

!pig slaughter
if pig_state = CONST_INT['slave_exist'] and pig['fat'] = 5 and farmhand_state = CONST_INT['slave_exist'] and barn_farmhand_slaughters = 1:
	dynamic $replace($dyn_meat_evaluation, 'dyn'+'slave', 'pig') &! calculate mince_gain, meat_gain, virgin_meat_gain - meat_price is also set but not used
	! kill the pig
	dynamic $clear_slave_instance, 'pig'

	!refill the fridge
	consume_mince =   mince_gain
	consume_meat =    meat_gain
	consume_virgin =  virgin_meat_gain

	decade_mince  += mince_gain
	decade_meat   += meat_gain
	decade_virgin += virgin_meat_gain

	estate_fridge_capacity = estate_size[$estate]/10 &! estate_size ranges [50, 250] - capacity is how many of each type will be stocked

	if fridge_space >= estate_fridge_capacity:
		virgin_meat_gain += fridge['virgin_meat']
		fridge['virgin_meat'] = 0
		if virgin_meat_gain < estate_fridge_capacity:
			fridge['virgin_meat'] = virgin_meat_gain
			virgin_meat_gain = 0
		else
			fridge['virgin_meat'] = estate_fridge_capacity
			virgin_meat_gain -= estate_fridge_capacity
		end
	end
	if fridge_space >= estate_fridge_capacity:
		meat_gain += fridge['meat']
		fridge['meat'] = 0
		if meat_gain < estate_fridge_capacity:
			fridge['meat'] = meat_gain
			meat_gain = 0
		else
			fridge['meat'] = estate_fridge_capacity
			meat_gain -= estate_fridge_capacity
		end
	end
	if fridge_space >= estate_fridge_capacity:
		mince_gain += fridge['mince']
		fridge['mince'] = 0
		if mince_gain < estate_fridge_capacity:
			fridge['mince'] = mince_gain
			mince_gain = 0
		else
			fridge['mince'] = estate_fridge_capacity
			mince_gain -= estate_fridge_capacity
		end
	end

	killvar 'estate_fridge_capacity'

	barn_pig_profit += mince_gain + meat_gain*2 + virgin_meat_gain*4
	consume_mince  -= mince_gain
	consume_meat   -= meat_gain
	consume_virgin -= virgin_meat_gain
	if consume_mince < 0:  consume_mince = 0
	if consume_meat < 0:   consume_meat = 0
	if consume_virgin < 0: consume_virgin = 0
! consumed is calced at retail price as this is the savings of growing your own.
	decade_consume += consume_mince + consume_meat*4 + consume_virgin*8
end

if barn_farmhand_prison = 1 and barn = 1 and dungeon = 1 and pig_state = CONST_INT['no_slave'] and prisoner_state ! CONST_INT['no_slave']:
	dynamic $swap_slave_instances, 'prisoner', 'pig'
	prisoner['orig_fat'] = prisoner['fat']
	prisoner_fattening = 0
end

is_gangbang = rand(0,2)
if guild_gangbang_reveal = 0 and guild_reputation >= 3 and is_gangbang > 0:
	guild_gangbang_reveal = 1
	msg 'Dear <<$master_name>>, <br>  I will be holding a gang-bang training session for my slave today at the guild and would be honored if you would join us. It will be an all-afternoon affair so drop in any time.<br><br>Regards, <br>  Lord Durbat'
end

!Post Patrician Quest - Daily Quest Handling - crushboss
if active[$quest[12]] = 1: gs 'quest12'
end_of_the_day = 1
EndOfDayEventCnt = 0
if slave['rules_broken'] = 1: rules_broken_message = 1 &! flag for end of day screen

!Save current interior settings - crushboss
gs 'interior_storage'
gt 'end_day_screen'

--- next_day ---------------------------------