#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 look_at_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure FishrSon00;
procedure FishrSon01;
procedure FishrSon02;
procedure FishrSon02a;
procedure FishrSon03;
procedure FishrSon04;
procedure FishrSon05;
procedure FishrSon05a;
procedure FishrSon06;
procedure FishrSon07;
procedure FishrSon08;
procedure FishrSon09;
procedure FishrSon10;
procedure FishrSon11;
procedure FishrSon11a;
procedure FishrSon12;
procedure FishrSon13;
procedure FishrSon14;
procedure FishrSon15;
procedure FishrSon16;
procedure FishrSon17;
procedure FishrSon18;
procedure FishrSon19;
procedure FishrSon20;
procedure FishrSon21;
procedure flee_dude;

variable initialized1;
variable known;

import variable dude_enemy;

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 (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 2);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_LITTLE_KIDS);
		dude_enemy := global_var(GVAR_SAVE_TANDI_5);
		initialized1 := 1;
	end
	else 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 == look_at_proc) then begin
						call look_at_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
	end
end

procedure critter_p_proc
begin
	if (dude_enemy and (tile_distance_objs(dude_obj, self_obj) < 8)) then begin
		call flee_dude;
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		dude_enemy := 1;
		set_global_var(GVAR_SAVE_TANDI_5, 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
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_FISHRSON, 100));
end

procedure pickup_p_proc
begin
	dude_enemy := 1;
	set_global_var(GVAR_SAVE_TANDI_5, 1);
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (dude_enemy) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, 103), FLOAT_MSG_NORMAL);
	end
	else begin
		if ((global_var(GVAR_CHILDKILLER_REPUTATION) > 2) or (local_var(1) < 2) or (global_var(GVAR_PLAYER_REPUTATION) < -30)) then begin
			call FishrSon00;
		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 FishrSon01;
			end
			else begin
				start_gdialog(SCRIPT_FISHRSON, self_obj, 4, -1, -1);
				gsay_start;
				if (known or (global_var(GVAR_PLAYER_REPUTATION) < 30)) then begin
					call FishrSon04;
				end
				else begin
					call FishrSon02;
				end
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure FishrSon00
begin
	float_msg(self_obj, message_str(SCRIPT_FISHRSON, 101), FLOAT_MSG_NORMAL);
end

procedure FishrSon01
begin
	float_msg(self_obj, message_str(SCRIPT_FISHRSON, 102), FLOAT_MSG_NORMAL);
end

procedure FishrSon02
begin
	known := 1;
	gsay_reply(SCRIPT_FISHRSON, 103);
	call FishrSon02a;
end

procedure FishrSon02a
begin
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 106, FishrSon08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 107, FishrSon10, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FISHRSON, 108, FishrSon09, NEUTRAL_REACTION);
end

procedure FishrSon03
begin
	known := 1;
	gsay_reply(SCRIPT_FISHRSON, 109);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	call FishrSon02a;
end

procedure FishrSon04
begin
	gsay_reply(SCRIPT_FISHRSON, 111);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	call FishrSon02a;
end

procedure FishrSon05
begin
	gsay_reply(SCRIPT_FISHRSON, 112);
	giq_option(0, SCRIPT_MODREACT, 106, FishrSon05a, NEUTRAL_REACTION);
end

procedure FishrSon05a
begin
	gsay_reply(SCRIPT_FISHRSON, 113);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 106, FishrSon08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 114, FishrSon12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 115, FishrSon13, NEUTRAL_REACTION);
end

procedure FishrSon06
begin
	gsay_reply(SCRIPT_FISHRSON, 116);
	giq_option(4, SCRIPT_FISHRSON, 117, FishrSon14, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 106, FishrSon08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 114, FishrSon12, NEUTRAL_REACTION);
end

procedure FishrSon07
begin
	gsay_message(SCRIPT_FISHRSON, 118, NEUTRAL_REACTION);
end

procedure FishrSon08
begin
	gsay_reply(SCRIPT_FISHRSON, 119);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 106, FishrSon08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 120, FishrSon15, NEUTRAL_REACTION);
end

procedure FishrSon09
begin
	gsay_message(SCRIPT_FISHRSON, 121, NEUTRAL_REACTION);
end

procedure FishrSon10
begin
	gsay_reply(SCRIPT_FISHRSON, 122);
	giq_option(4, SCRIPT_FISHRSON, 123, FishrSon16, GOOD_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 124, FishrSon16, GOOD_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 125, FishrSon16, GOOD_REACTION);
end

procedure FishrSon11
begin
	gsay_reply(SCRIPT_FISHRSON, 126);
	giq_option(0, SCRIPT_MODREACT, 106, FishrSon11a, GOOD_REACTION);
end

procedure FishrSon11a
begin
	gsay_message(SCRIPT_FISHRSON, 127, GOOD_REACTION);
end

procedure FishrSon12
begin
	gsay_reply(SCRIPT_FISHRSON, 128);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 106, FishrSon08, NEUTRAL_REACTION);
end

procedure FishrSon13
begin
	gsay_reply(SCRIPT_FISHRSON, 129);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 106, FishrSon08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 130, FishrSon17, NEUTRAL_REACTION);
end

procedure FishrSon14
begin
	gsay_message(SCRIPT_FISHRSON, 131, NEUTRAL_REACTION);
end

procedure FishrSon15
begin
	gsay_reply(SCRIPT_FISHRSON, 132);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 133, FishrSon18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 134, FishrSon19, NEUTRAL_REACTION);
end

procedure FishrSon16
begin
	gsay_message(SCRIPT_FISHRSON, 135, GOOD_REACTION);
end

procedure FishrSon17
begin
	gsay_reply(SCRIPT_FISHRSON, 136);
	giq_option(4, SCRIPT_FISHRSON, 137, FishrSon20, NEUTRAL_REACTION);
	if (global_var(GVAR_BROTHERHOOD_OF_STEEL) > 0) then begin
		giq_option(4, SCRIPT_FISHRSON, 138, FishrSon21, NEUTRAL_REACTION);
	end
end

procedure FishrSon18
begin
	gsay_reply(SCRIPT_FISHRSON, 139);
	giq_option(4, SCRIPT_FISHRSON, 110, FishrSon05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 104, FishrSon06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 105, FishrSon07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRSON, 134, FishrSon18, NEUTRAL_REACTION);
end

procedure FishrSon19
begin
	gsay_message(SCRIPT_FISHRSON, 140, NEUTRAL_REACTION);
end

procedure FishrSon20
begin
	gsay_message(SCRIPT_FISHRSON, 141, GOOD_REACTION);
end

procedure FishrSon21
begin
	gsay_message(SCRIPT_FISHRSON, 142, NEUTRAL_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 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