#include <define.h>

//#define NAME                    SCRIPT_

#include <command.h>
//#include <ModReact.h>



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Vault00;
procedure Vault00a;
procedure Vault00b;
procedure Vault00c;
procedure Vault01;
procedure Vault02;
procedure Vault03;
procedure Vault04;
procedure Vault05;
procedure Vault06;
procedure Vault07;
procedure Vault08;
procedure Vault09;
procedure Vault10;
procedure Vault11;
procedure flee_dude;
procedure get_rations;
procedure set_ration_tile;
procedure set_sleep_tile;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable gword1;
variable gword2;
variable PICK;
variable hostile1;
variable sleeping_disabled;
variable ration_tile;

import variable recipient;
import variable WtrGrd_ptr;
import variable getting_ration;

procedure Get_Reaction;
procedure React_To_Level;
procedure Level_To_React;
procedure Up_React;
procedure Down_React;
procedure Bottom_React;
procedure Top_React;
procedure Big_Up_React;
procedure Big_Down_React;
procedure Up_React_Level;
procedure Down_React_Level;
procedure Goodbyes;

variable exit_line;


procedure start
begin
	if (script_action == critter_proc) then begin
		call critter_p_proc;
	end
	else begin
		if (script_action == damage_proc) then begin
			call damage_p_proc;
		end
		else begin
			if (script_action == destroy_proc) then begin
				call destroy_p_proc;
			end
			else begin
				if (script_action == map_enter_proc) then begin
					call map_enter_p_proc;
				end
				else begin
					if (script_action == pickup_proc) then begin
						call pickup_p_proc;
					end
					else begin
						if (script_action == talk_proc) then begin
							call talk_p_proc;
						end
						else begin
							if (script_action == timed_event_proc) then begin
								call timed_event_p_proc;
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (local_var(7) and (tile_distance_objs(self_obj, dude_obj) < 8)) then begin
			call flee_dude;
		end
		if (global_var(GVAR_FIND_WATER_CHIP) == 0) then begin
			if (local_var(5) == 0) then begin
				if ((elevation(self_obj) == elevation(WtrGrd_ptr)) and (elevation(self_obj) == elevation(dude_obj))) then begin
					if ((game_time_hour > 700) and (game_time_hour < 900)) then begin
						call get_rations;
					end
				end
			end
		end
		if (not((game_time_hour >= 700) and (game_time_hour < 1800))) then begin
			sleeping_disabled := 0;
			set_local_var(5, 0);
		end
		if (sleeping_disabled == 0) then begin
			call sleeping;
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_local_var(7, 1);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_GOOD_MONSTER) > (2 * global_var(GVAR_BAD_MONSTER))) or (global_var(GVAR_BERSERKER_REPUTATION) == 1))) then begin
			set_global_var(GVAR_BERSERKER_REPUTATION, 1);
			set_global_var(GVAR_CHAMPION_REPUTATION, 0);
		end
		if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_BAD_MONSTER) > (3 * global_var(GVAR_GOOD_MONSTER))) or (global_var(GVAR_CHAMPION_REPUTATION) == 1))) then begin
			set_global_var(GVAR_CHAMPION_REPUTATION, 1);
			set_global_var(GVAR_BERSERKER_REPUTATION, 0);
		end
		set_global_var(GVAR_GOOD_MONSTER, global_var(GVAR_GOOD_MONSTER) + 1);
		if ((global_var(GVAR_GOOD_MONSTER) % 2) == 0) then begin
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
		end
	end
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_WATER_CHIP_4, 1);
	end
end

procedure map_enter_p_proc
begin
	if (local_var(4) == 0) then begin
		set_local_var(4, tile_num(self_obj));
	end
	home_tile := local_var(4);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 1);
	call set_ration_tile;
	call set_sleep_tile;
	sleep_time := random(1900, 1930);
	wake_time := sleep_time - 1300;
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (local_var(6)) then begin
		if (random(0, 1)) then begin
			float_msg(self_obj, message_str(SCRIPT_GENVAULT, 166), FLOAT_MSG_NORMAL);
		end
		else begin
			display_msg(message_str(SCRIPT_GENVAULT, 167));
		end
	end
	else begin
		if (local_var(7) or global_var(GVAR_WATER_CHIP_4)) then begin
			call Vault00;
		end
		else begin
			if ((global_var(GVAR_FIND_WATER_CHIP) != 0) and (global_var(GVAR_FIND_WATER_CHIP) != 1)) then begin
				call Vault01;
			end
			else begin
				if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
					call Vault02;
				end
				else begin
					if (global_var(GVAR_VAULT_WATER) < 80) then begin
						call Vault00b;
					end
					else begin
						if (global_var(GVAR_VAULT_WATER) < 40) then begin
							call Vault00c;
						end
						else begin
							call Vault00a;
						end
					end
				end
			end
		end
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(WtrGrd_ptr), 2, 1), ANIMATE_WALK);
		add_timer_event(self_obj, game_ticks(3), 2);
	end
	else begin
		if (fixed_param == 2) then begin
			getting_ration := 1;
			if (random(0, 1)) then begin
				float_msg(self_obj, message_str(SCRIPT_GENVAULT, 165), FLOAT_MSG_NORMAL);
			end
			add_timer_event(self_obj, game_ticks(3), 3);
		end
		else begin
			if (fixed_param == 3) then begin
				recipient := 0;
				animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
			end
		end
	end
end

procedure Vault00
begin
	float_msg(self_obj, message_str(SCRIPT_GENVAULT, random(101, 104)), FLOAT_MSG_NORMAL);
end

procedure Vault00a
begin
	if (local_var(1) == 3) then begin
		call Vault03;
	end
	else begin
		if (local_var(1) == 2) then begin
			call Vault06;
		end
		else begin
			call Vault09;
		end
	end
end

procedure Vault00b
begin
	if (local_var(1) == 3) then begin
		call Vault04;
	end
	else begin
		if (local_var(1) == 2) then begin
			call Vault07;
		end
		else begin
			call Vault10;
		end
	end
end

procedure Vault00c
begin
	if (local_var(1) == 3) then begin
		call Vault05;
	end
	else begin
		if (local_var(1) == 2) then begin
			call Vault08;
		end
		else begin
			call Vault11;
		end
	end
end

procedure Vault01
begin
	PICK := random(1, 5);
	if (PICK == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 109), FLOAT_MSG_NORMAL);
	end
	else begin
		if (PICK == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENVAULT, 110), FLOAT_MSG_NORMAL);
		end
		else begin
			if (PICK == 3) then begin
				float_msg(self_obj, message_str(SCRIPT_GENVAULT, 111), FLOAT_MSG_NORMAL);
			end
			else begin
				if (PICK == 4) then begin
					if (dude_is_male) then begin
						float_msg(self_obj, message_str(SCRIPT_GENVAULT, 112), FLOAT_MSG_NORMAL);
					end
					else begin
						float_msg(self_obj, message_str(SCRIPT_GENVAULT, 113), FLOAT_MSG_NORMAL);
					end
				end
				else begin
					if (PICK == 5) then begin
						if (dude_is_male) then begin
							float_msg(self_obj, message_str(SCRIPT_GENVAULT, 114), FLOAT_MSG_NORMAL);
						end
						else begin
							float_msg(self_obj, message_str(SCRIPT_GENVAULT, 115), FLOAT_MSG_NORMAL);
						end
					end
				end
			end
		end
	end
end

procedure Vault02
begin
	call Down_React;
	float_msg(self_obj, message_str(SCRIPT_GENVAULT, random(116, 119)), FLOAT_MSG_NORMAL);
end

procedure Vault03
begin
	PICK := random(1, 4);
	if (PICK == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 120), FLOAT_MSG_NORMAL);
	end
	else begin
		if (PICK == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENVAULT, 121), FLOAT_MSG_NORMAL);
		end
		else begin
			if (PICK == 3) then begin
				float_msg(self_obj, message_str(SCRIPT_GENVAULT, 122), FLOAT_MSG_NORMAL);
			end
			else begin
				if (PICK == 4) then begin
					float_msg(self_obj, message_str(SCRIPT_GENVAULT, 123) + proto_data(obj_pid(dude_obj), 1) + message_str(0, 124), FLOAT_MSG_NORMAL);
				end
			end
		end
	end
end

procedure Vault04
begin
	float_msg(self_obj, message_str(SCRIPT_GENVAULT, random(125, 128)), FLOAT_MSG_NORMAL);
end

procedure Vault05
begin
	float_msg(self_obj, message_str(SCRIPT_GENVAULT, random(129, 133)), FLOAT_MSG_NORMAL);
end

procedure Vault06
begin
	PICK := random(1, 5);
	if (PICK == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 134), FLOAT_MSG_NORMAL);
	end
	else begin
		if (PICK == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENVAULT, 135) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_GENVAULT, 136), FLOAT_MSG_NORMAL);
		end
		else begin
			if (PICK == 3) then begin
				float_msg(self_obj, message_str(SCRIPT_GENVAULT, 137), FLOAT_MSG_NORMAL);
			end
			else begin
				if (PICK == 4) then begin
					float_msg(self_obj, message_str(SCRIPT_GENVAULT, 138), FLOAT_MSG_NORMAL);
				end
				else begin
					if (PICK == 5) then begin
						float_msg(self_obj, message_str(SCRIPT_GENVAULT, 139), FLOAT_MSG_NORMAL);
					end
				end
			end
		end
	end
end

procedure Vault07
begin
	float_msg(self_obj, message_str(SCRIPT_GENVAULT, random(140, 144)), FLOAT_MSG_NORMAL);
end

procedure Vault08
begin
	float_msg(self_obj, message_str(SCRIPT_GENVAULT, random(145, 149)), FLOAT_MSG_NORMAL);
end

procedure Vault09
begin
	PICK := random(1, 5);
	if (PICK == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 150) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_GENVAULT, 151), FLOAT_MSG_NORMAL);
	end
	else begin
		if (PICK >= 4) then begin
			display_msg(message_str(SCRIPT_GENVAULT, 150 + PICK));
		end
		else begin
			float_msg(self_obj, message_str(SCRIPT_GENVAULT, 150 + PICK), FLOAT_MSG_NORMAL);
		end
	end
end

procedure Vault10
begin
	PICK := random(1, 5);
	if (PICK == 5) then begin
		display_msg(message_str(SCRIPT_GENVAULT, 160));
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 155 + PICK), FLOAT_MSG_NORMAL);
	end
end

procedure Vault11
begin
	PICK := random(1, 5);
	if (PICK == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 161), FLOAT_MSG_NORMAL);
	end
	else begin
		if (PICK == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENVAULT, 162), FLOAT_MSG_NORMAL);
		end
		else begin
			if (PICK == 3) then begin
				display_msg(message_str(SCRIPT_GENVAULT, 163));
			end
			else begin
				if (PICK == 4) then begin
					display_msg(message_str(SCRIPT_GENVAULT, 164));
				end
			end
		end
	end
end

procedure flee_dude
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	while(LVar1 < 5) do begin
		if (tile_distance(tile_num(dude_obj), tile_num_in_direction(tile_num(self_obj), LVar1, 3)) > LVar2) then begin
			LVar0 := tile_num_in_direction(tile_num(self_obj), LVar1, 3);
			LVar2 := tile_distance(tile_num(dude_obj), LVar0);
		end
		LVar1 := LVar1 + 1;
	end
	animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_RUN);
end

procedure get_rations
begin
	sleeping_disabled := 1;
	if (tile_num(self_obj) != ration_tile) then begin
		if (random(0, 1)) then begin
			animate_move_obj_to_tile(self_obj, ration_tile, ANIMATE_WALK);
		end
		else begin
			animate_move_obj_to_tile(self_obj, ration_tile, ANIMATE_RUN);
		end
	end
	else begin
		if (not(recipient)) then begin
			recipient := self_obj;
			add_timer_event(self_obj, game_ticks(3), 1);
			set_local_var(5, 1);
			sleeping_disabled := 0;
		end
	end
end

procedure set_ration_tile
begin
	if (elevation(self_obj) == 0) then begin
		ration_tile := tile_num_in_direction(14704, random(0, 5), random(1, 2));
	end
	else begin
		if (elevation(self_obj) == 1) then begin
			ration_tile := tile_num_in_direction(21895, random(0, 5), random(1, 2));
		end
		else begin
			if (elevation(self_obj) == 2) then begin
				ration_tile := tile_num_in_direction(22513, random(0, 5), random(1, 2));
			end
		end
	end
end

procedure set_sleep_tile
begin
	if (elevation(self_obj) == 0) then begin
		sleep_tile := 7000;
	end
	else begin
		if (elevation(self_obj) == 1) then begin
			sleep_tile := home_tile;
		end
		else begin
			if (elevation(self_obj) == 2) then begin
				sleep_tile := 7000;
			end
		end
	end
end

procedure sleeping
begin
	if (local_var(6) == 1) then begin
		if (not(night_person) and (game_time_hour >= wake_time) and (game_time_hour < sleep_time) or (night_person and ((game_time_hour >= wake_time) or (game_time_hour < sleep_time)))) then begin
			if (((game_time_hour - wake_time) < 10) and ((game_time_hour - wake_time) > 0)) then begin
				if (tile_num(self_obj) != home_tile) then begin
					animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
				end
				else begin
					set_local_var(6, 0);
				end
			end
			else begin
				move_to(self_obj, home_tile, elevation(self_obj));
				if (tile_num(self_obj) == home_tile) then begin
					set_local_var(6, 0);
				end
			end
		end
	end
	else begin
		if (night_person and (game_time_hour >= sleep_time) and (game_time_hour < wake_time) or (not(night_person) and ((game_time_hour >= sleep_time) or (game_time_hour < wake_time)))) then begin
			if (((game_time_hour - sleep_time) < 10) and ((game_time_hour - sleep_time) > 0)) then begin
				if (tile_num(self_obj) != sleep_tile) then begin
					animate_move_obj_to_tile(self_obj, self_obj, ANIMATE_WALK);
				end
				else begin
					set_local_var(6, 1);
				end
			end
			else begin
				if (tile_num(self_obj) != sleep_tile) then begin
					move_to(self_obj, sleep_tile, elevation(self_obj));
				end
				else begin
					set_local_var(6, 1);
				end
			end
		end
	end
end

procedure Get_Reaction
begin
	if (local_var(2) == 0) then begin
		set_local_var(0, 50);
		set_local_var(1, 2);
		set_local_var(2, 1);
		set_local_var(0, local_var(0) + (5 * get_critter_stat(dude_obj, 3)) - 25);
		set_local_var(0, local_var(0) + (10 * has_trait(0, dude_obj, 10)));
		if (has_trait(0, dude_obj, 39)) then begin
			if (global_var(GVAR_PLAYER_REPUTATION) > 0) then begin
				set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
			end
			else begin
				set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
			end
		end
		else begin
			if (local_var(3) == 1) then begin
				set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
			end
			else begin
				set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
			end
		end
		if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
			set_local_var(0, local_var(0) - 30);
		end
		if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_BAD_MONSTER) > (3 * global_var(GVAR_GOOD_MONSTER))) or (global_var(GVAR_CHAMPION_REPUTATION) == 1))) then begin
			set_local_var(0, local_var(0) + 20);
		end
		if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_GOOD_MONSTER) > (2 * global_var(GVAR_BAD_MONSTER))) or (global_var(GVAR_BERSERKER_REPUTATION) == 1))) then begin
			set_local_var(0, local_var(0) - 20);
		end
		call React_To_Level;
	end
end

procedure React_To_Level
begin
	if (local_var(0) <= 25) then begin
		set_local_var(1, 1);
	end
	else begin
		if (local_var(0) <= 75) then begin
			set_local_var(1, 2);
		end
		else begin
			set_local_var(1, 3);
		end
	end
end

procedure Level_To_React
begin
	if (local_var(1) == 1) then begin
		set_local_var(0, random(1, 25));
	end
	else begin
		if (local_var(1) == 2) then begin
			set_local_var(0, random(26, 75));
		end
		else begin
			set_local_var(0, random(76, 100));
		end
	end
end

procedure Up_React
begin
	set_local_var(0, local_var(0) + 10);
	call React_To_Level;
end

procedure Down_React
begin
	set_local_var(0, local_var(0) - 10);
	call React_To_Level;
end

procedure Bottom_React
begin
	set_local_var(1, 1);
	set_local_var(0, 1);
end

procedure Top_React
begin
	set_local_var(0, 100);
	set_local_var(1, 3);
end

procedure Big_Up_React
begin
	set_local_var(0, local_var(0) + 25);
	call React_To_Level;
end

procedure Big_Down_React
begin
	set_local_var(0, local_var(0) - 25);
	call React_To_Level;
end

procedure Up_React_Level
begin
	set_local_var(1, local_var(1) + 1);
	if (local_var(1) > 3) then begin
		set_local_var(1, 3);
	end
	call Level_To_React;
end

procedure Down_React_Level
begin
	set_local_var(1, local_var(1) - 1);
	if (local_var(1) < 1) then begin
		set_local_var(1, 1);
	end
	call Level_To_React;
end

procedure Goodbyes
begin
	exit_line := message_str(SCRIPT_MODREACT, random(100, 105));
end