#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure map_enter_p_proc;
procedure map_update_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure ChildEnd;
procedure child_heal;
procedure child_pois;
procedure Child00;
procedure Child01;
procedure Child02;
procedure Child03;
procedure Child04;
procedure Child04a;
procedure Child05;
procedure Child05a;
procedure Child05b;
procedure Child06;
procedure Child07;
procedure Child08;
procedure Child09;
procedure Child09a;
procedure Child10;
procedure Child11;
procedure Child12;
procedure Child13;
procedure Child14;
procedure Child15;
procedure Child16;
procedure Child17;
procedure flee_dude;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable initialized1;
variable scared;
variable cost;
variable hp_injured;
variable healing_cost;
variable poison_cost;

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 == 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
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (scared and (tile_distance_objs(self_obj, dude_obj) < 8) and (local_var(4) == 0)) then begin
		call flee_dude;
	end
	else begin
		call sleeping;
	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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
		if ((global_var(GVAR_BAD_MONSTER) % 6) == 0) then begin
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
		end
	end
end

procedure map_enter_p_proc
begin
	if (global_var(GVAR_JUNKTOWN_INVADED) == 1) then begin
		destroy_object(self_obj);
	end
	wake_time := 700;
	sleep_time := 1900;
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 20);
	if (local_var(6) == 0) then begin
		set_local_var(6, tile_num(self_obj));
	end
	home_tile := local_var(6);
	sleep_tile := 15275;
	if ((game_time / (10 * 60 * 60 * 24)) < 80) then begin
		set_obj_visibility(self_obj, 1);
	end
	else begin
		set_obj_visibility(self_obj, 0);
	end
end

procedure map_update_p_proc
begin
	if ((game_time / (10 * 60 * 60 * 24)) < 80) then begin
		set_obj_visibility(self_obj, 1);
	end
	else begin
		set_obj_visibility(self_obj, 0);
	end
end

procedure pickup_p_proc
begin
	scared := 1;
end

procedure talk_p_proc
begin
	if (scared) then begin
		float_msg(self_obj, message_str(SCRIPT_JCHIDMEM, 145), FLOAT_MSG_RED);
	end
	else begin
		if (local_var(5) == 0) then begin
			start_gdialog(SCRIPT_JCHIDMEM, self_obj, 4, -1, -1);
			gsay_start;
			call Child00;
			gsay_end;
			end_dialogue;
		end
	end
end

procedure ChildEnd
begin
end

procedure child_heal
begin
	game_time_advance(game_ticks(600 * hp_injured));
	critter_heal(dude_obj, hp_injured);
	set_global_var(GVAR_VAULT_13_INVADED_DATE, global_var(GVAR_VAULT_13_INVADED_DATE) - 1);
end

procedure child_pois
begin
	if (get_poison(dude_obj) != 0) then begin
		game_time_advance(game_ticks(1800));
	end
	poison(dude_obj, -get_poison(dude_obj));
end

procedure Child00
begin
	gsay_reply(SCRIPT_JCHIDMEM, 100);
	giq_option(4, SCRIPT_JCHIDMEM, 101, Child04, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JCHIDMEM, 102, Child11, NEUTRAL_REACTION);
	if ((map_var(2) == 1) and (local_var(7) == 0)) then begin
		giq_option(5, SCRIPT_JCHIDMEM, 103, Child15, NEUTRAL_REACTION);
	end
	giq_option(-3, SCRIPT_JCHIDMEM, 104, Child01, NEUTRAL_REACTION);
end

procedure Child01
begin
	gsay_message(SCRIPT_JCHIDMEM, 105, NEUTRAL_REACTION);
	if (get_critter_stat(dude_obj, 35) < get_critter_stat(dude_obj, 7)) then begin
		call child_heal;
		call Child03;
	end
	else begin
		call Child02;
	end
end

procedure Child02
begin
	gsay_message(SCRIPT_JCHIDMEM, 106, NEUTRAL_REACTION);
end

procedure Child03
begin
	critter_heal(dude_obj, get_critter_stat(dude_obj, 7) - get_critter_stat(dude_obj, 35));
	poison(dude_obj, -get_poison(dude_obj));
	gsay_message(SCRIPT_JCHIDMEM, 107, NEUTRAL_REACTION);
end

procedure Child04
begin
	if (local_var(1) == 3) then begin
		healing_cost := 5;
		poison_cost := 12;
	end
	else begin
		if (local_var(1) == 1) then begin
			healing_cost := 20;
			poison_cost := 50;
		end
		else begin
			healing_cost := 10;
			poison_cost := 25;
		end
	end
	hp_injured := get_critter_stat(dude_obj, 7) - get_critter_stat(dude_obj, 35);
	cost := hp_injured * healing_cost;
	if (get_poison(dude_obj) != 0) then begin
		cost := cost + poison_cost;
	end
	if (hp_injured > 0) then begin
		gsay_message(SCRIPT_JCHIDMEM, 108, NEUTRAL_REACTION);
	end
	if (get_poison(dude_obj) != 0) then begin
		gsay_message(SCRIPT_JCHIDMEM, 109, NEUTRAL_REACTION);
	end
	if (get_critter_stat(dude_obj, 37) > 50) then begin
		gsay_message(SCRIPT_JCHIDMEM, 110, NEUTRAL_REACTION);
	end
	if (cost > 0) then begin
		gsay_reply(SCRIPT_JCHIDMEM, message_str(SCRIPT_JCHIDMEM, 111) + cost + message_str(SCRIPT_JCHIDMEM, 112));
		giq_option(4, SCRIPT_JCHIDMEM, 113, Child04a, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_JCHIDMEM, 114, ChildEnd, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_JCHIDMEM, 115, Child09, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_JCHIDMEM, message_str(SCRIPT_JCHIDMEM, 116) + (cost * 0.75000) + message_str(SCRIPT_JCHIDMEM, 117), Child05, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_JCHIDMEM, 146, NEUTRAL_REACTION);
	end
end

procedure Child04a
begin
	if (cost > item_caps_total(dude_obj)) then begin
		call Child08;
	end
	else begin
		call Child06;
	end
end

procedure Child05
begin
	gsay_reply(SCRIPT_JCHIDMEM, 118);
	giq_option(4, SCRIPT_JCHIDMEM, 119, Child05a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JCHIDMEM, 120, Child05b, BAD_REACTION);
end

procedure Child05a
begin
	if (cost > item_caps_total(dude_obj)) then begin
		call Child08;
	end
	else begin
		call Child06;
	end
end

procedure Child05b
begin
	set_local_var(5, 1);
	call ChildEnd;
end

procedure Child06
begin
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_JCHIDMEM, 121);
	end
	else begin
		gsay_reply(SCRIPT_JCHIDMEM, 122);
	end
	giq_option(0, SCRIPT_MODREACT, 106, Child07, NEUTRAL_REACTION);
end

procedure Child07
begin
	gfade_out(600);
	item_caps_adjust(dude_obj, -cost);
	call child_heal;
	call child_pois;
	gfade_in(600);
	gsay_message(SCRIPT_JCHIDMEM, 123, NEUTRAL_REACTION);
end

procedure Child08
begin
	gsay_reply(SCRIPT_JCHIDMEM, 124);
	cost := item_caps_total(dude_obj);
	giq_option(4, SCRIPT_JCHIDMEM, 125, Child06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JCHIDMEM, 129, ChildEnd, NEUTRAL_REACTION);
end

procedure Child09
begin
	gsay_reply(SCRIPT_JCHIDMEM, 127);
	giq_option(4, SCRIPT_JCHIDMEM, 128, Child09a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JCHIDMEM, 129, ChildEnd, NEUTRAL_REACTION);
end

procedure Child09a
begin
	if (cost > item_caps_total(dude_obj)) then begin
		call Child06;
	end
	else begin
		call Child10;
	end
end

procedure Child10
begin
	gsay_reply(SCRIPT_JCHIDMEM, 130);
	giq_option(0, SCRIPT_MODREACT, 106, Child06, NEUTRAL_REACTION);
end

procedure Child11
begin
	gsay_reply(SCRIPT_JCHIDMEM, 131);
	giq_option(4, SCRIPT_JCHIDMEM, 132, ChildEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JCHIDMEM, 133, Child12, NEUTRAL_REACTION);
end

procedure Child12
begin
	gsay_reply(SCRIPT_JCHIDMEM, 134);
	giq_option(4, SCRIPT_JCHIDMEM, 135, Child14, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JCHIDMEM, 136, Child13, NEUTRAL_REACTION);
end

procedure Child13
begin
	gsay_message(SCRIPT_JCHIDMEM, 137, BAD_REACTION);
end

procedure Child14
begin
	gsay_reply(SCRIPT_JCHIDMEM, 138);
	giq_option(4, SCRIPT_JCHIDMEM, 139, ChildEnd, NEUTRAL_REACTION);
end

procedure Child15
begin
	set_local_var(7, 1);
	gsay_reply(SCRIPT_JCHIDMEM, 140);
	giq_option(5, SCRIPT_JCHIDMEM, 141, Child16, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JCHIDMEM, 142, ChildEnd, NEUTRAL_REACTION);
end

procedure Child16
begin
	gsay_reply(SCRIPT_JCHIDMEM, 143);
	giq_option(5, SCRIPT_JCHIDMEM, 144, ChildEnd, NEUTRAL_REACTION);
end

procedure Child17
begin
	scared := 1;
	gsay_message(SCRIPT_JCHIDMEM, 145, BAD_REACTION);
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 sleeping
begin
	if (local_var(4) == 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(4, 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(4, 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(4, 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(4, 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