#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 FishrMan00;
procedure FishrMan01;
procedure FishrMan02;
procedure FishrMan03;
procedure FishrMan03a;
procedure FishrMan04;
procedure FishrMan04a;
procedure FishrMan04b;
procedure FishrMan05;
procedure FishrMan05a;
procedure FishrMan06;
procedure FishrMan07;
procedure FishrMan07a;
procedure FishrMan08;
procedure FishrMan08a;
procedure FishrMan09;
procedure FishrMan09a;
procedure FishrMan10;
procedure FishrMan11;
procedure FishrMan12;
procedure FishrMan12a;
procedure FishrMan13;
procedure FishrMan14;
procedure FishrMan15;
procedure FishrMan15a;
procedure FishrMan16;
procedure FishrMan17;
procedure FishrMan18;
procedure FishrMan19;
procedure FishrMan20;
procedure FishrMan21;
procedure FishrMan22;
procedure FishrMan23;
procedure FishrMan24;
procedure FishrMan25;
procedure FishrManBarter;
procedure FishrManCombat;
procedure FishrManEnd;
procedure FishrManEnd1;
procedure FishrManEnd2;

variable hostile1;
variable initialized1;
variable known;

export 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_PEASANT);
		hostile1 := 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 == talk_proc) then begin
							call talk_p_proc;
						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
	if (dude_enemy) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			hostile1 := 1;
			set_global_var(GVAR_SAVE_TANDI_5, 1);
		end
	end
end

procedure damage_p_proc
begin
	dude_enemy := 1;
	set_global_var(GVAR_SAVE_TANDI_5, 1);
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_FISHRMAN, 100));
end

procedure pickup_p_proc
begin
	dude_enemy := 1;
	hostile1 := 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, 101), FLOAT_MSG_NORMAL);
	end
	else begin
		if ((local_var(1) < 2) or (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) or (global_var(GVAR_PLAYER_REPUTATION) < -30)) then begin
			call FishrMan00;
		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 FishrMan25;
			end
			else begin
				start_gdialog(SCRIPT_FISHRMAN, self_obj, 4, -1, -1);
				gsay_start;
				if (known) then begin
					call FishrMan03;
				end
				else begin
					if (global_var(GVAR_PLAYER_REPUTATION) >= 30) then begin
						call FishrMan02;
					end
					else begin
						call FishrMan01;
					end
				end
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure FishrMan00
begin
	float_msg(self_obj, message_str(SCRIPT_FISHRMAN, 101 + (dude_is_female)), FLOAT_MSG_NORMAL);
end

procedure FishrMan01
begin
	known := 1;
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_FISHRMAN, 103);
	end
	else begin
		gsay_reply(SCRIPT_FISHRMAN, 104);
	end
	if ((global_var(GVAR_FIND_WATER_CHIP) == 1) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_FISHRMAN, 105, FishrMan04, NEUTRAL_REACTION);
	end
	giq_option(7, SCRIPT_FISHRMAN, 106, FishrMan05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 107, FishrMan06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 108, FishrMan07, NEUTRAL_REACTION);
	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
		giq_option(4, SCRIPT_FISHRMAN, 109, FishrMan08, BAD_REACTION);
	end
	giq_option(-3, SCRIPT_FISHRMAN, 110, FishrMan09, NEUTRAL_REACTION);
end

procedure FishrMan02
begin
	known := 1;
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_FISHRMAN, 111);
	end
	else begin
		gsay_reply(SCRIPT_FISHRMAN, 112);
	end
	if ((global_var(GVAR_FIND_WATER_CHIP) == 1) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_FISHRMAN, 105, FishrMan04, NEUTRAL_REACTION);
	end
	giq_option(7, SCRIPT_FISHRMAN, 106, FishrMan05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 108, FishrMan07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 113, FishrMan13, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FISHRMAN, 110, FishrMan14, NEUTRAL_REACTION);
end

procedure FishrMan03
begin
	gsay_reply(SCRIPT_FISHRMAN, 114);
	giq_option(4, SCRIPT_FISHRMAN, 115, FishrMan15, BAD_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 116, FishrMan16, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 117, FishrMan03a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FISHRMAN, 110, FishrMan19, NEUTRAL_REACTION);
end

procedure FishrMan03a
begin
	if (get_critter_stat(dude_obj, 3) >= 6) then begin
		call FishrMan17;
	end
	else begin
		call FishrMan18;
	end
end

procedure FishrMan04
begin
	gsay_reply(SCRIPT_FISHRMAN, 118);
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan04a, NEUTRAL_REACTION);
end

procedure FishrMan04a
begin
	gsay_reply(SCRIPT_FISHRMAN, 119);
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan04b, NEUTRAL_REACTION);
end

procedure FishrMan04b
begin
	gsay_reply(SCRIPT_FISHRMAN, 120);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd1, NEUTRAL_REACTION);
end

procedure FishrMan05
begin
	gsay_reply(SCRIPT_FISHRMAN, 122);
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan05a, NEUTRAL_REACTION);
end

procedure FishrMan05a
begin
	gsay_reply(SCRIPT_FISHRMAN, 123);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd1, NEUTRAL_REACTION);
end

procedure FishrMan06
begin
	gsay_reply(SCRIPT_FISHRMAN, 124);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd1, NEUTRAL_REACTION);
end

procedure FishrMan07
begin
	gsay_reply(SCRIPT_FISHRMAN, 125);
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan07a, NEUTRAL_REACTION);
end

procedure FishrMan07a
begin
	gsay_reply(SCRIPT_FISHRMAN, 126);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd1, NEUTRAL_REACTION);
end

procedure FishrMan08
begin
	call Down_React_Level;
	call Down_React_Level;
	gsay_reply(SCRIPT_FISHRMAN, 127);
	giq_option(4, SCRIPT_FISHRMAN, 128, FishrMan08a, BAD_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 129, FishrManCombat, BAD_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 130, FishrMan22, NEUTRAL_REACTION);
end

procedure FishrMan08a
begin
	if ((get_critter_stat(dude_obj, 0) >= 8) or obj_is_carrying_obj_pid(dude_obj, 11) or obj_is_carrying_obj_pid(dude_obj, 12) or obj_is_carrying_obj_pid(dude_obj, 13) or obj_is_carrying_obj_pid(dude_obj, 118) or obj_is_carrying_obj_pid(dude_obj, 15)) then begin
		call FishrMan20;
	end
	else begin
		call FishrMan21;
	end
end

procedure FishrMan09
begin
	gsay_reply(SCRIPT_FISHRMAN, 131 + (dude_is_female));
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan09a, NEUTRAL_REACTION);
end

procedure FishrMan09a
begin
	variable LVar0 := 0;
	if (obj_is_carrying_obj_pid(self_obj, 109)) then begin
		LVar0 := obj_carrying_pid_obj(self_obj, 109);
		rm_obj_from_inven(self_obj, LVar0);
		add_obj_to_inven(dude_obj, LVar0);
		gsay_message(SCRIPT_FISHRMAN, 133, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_FISHRMAN, 134, NEUTRAL_REACTION);
	end
end

procedure FishrMan10
begin
	gsay_reply(SCRIPT_FISHRMAN, 135);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd2, NEUTRAL_REACTION);
end

procedure FishrMan11
begin
	gsay_reply(SCRIPT_FISHRMAN, 137);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd2, NEUTRAL_REACTION);
end

procedure FishrMan12
begin
	gsay_reply(SCRIPT_FISHRMAN, 138);
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan12a, NEUTRAL_REACTION);
end

procedure FishrMan12a
begin
	gsay_reply(SCRIPT_FISHRMAN, 139);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd2, NEUTRAL_REACTION);
end

procedure FishrMan13
begin
	gsay_reply(SCRIPT_FISHRMAN, 140);
	call FishrManBarter;
end

procedure FishrMan14
begin
	gsay_reply(SCRIPT_FISHRMAN, 141);
	giq_option(0, SCRIPT_MODREACT, 106, FishrManEnd2, NEUTRAL_REACTION);
end

procedure FishrMan15
begin
	gsay_reply(SCRIPT_FISHRMAN, 142);
	giq_option(0, SCRIPT_MODREACT, 106, FishrMan15a, NEUTRAL_REACTION);
end

procedure FishrMan15a
begin
	gsay_message(SCRIPT_FISHRMAN, 143, NEUTRAL_REACTION);
end

procedure FishrMan16
begin
	gsay_message(SCRIPT_FISHRMAN, 144, NEUTRAL_REACTION);
end

procedure FishrMan17
begin
	gsay_reply(SCRIPT_FISHRMAN, 145);
	if ((global_var(GVAR_FIND_WATER_CHIP) == 1) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_FISHRMAN, 146, FishrMan04, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_FISHRMAN, 147, FishrMan11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 148, FishrMan23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 149, FishrMan24, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FISHRMAN, 108, FishrMan07, NEUTRAL_REACTION);
end

procedure FishrMan18
begin
	gsay_message(SCRIPT_FISHRMAN, 150, NEUTRAL_REACTION);
end

procedure FishrMan19
begin
	gsay_message(SCRIPT_FISHRMAN, 151, NEUTRAL_REACTION);
end

procedure FishrMan20
begin
	gsay_reply(SCRIPT_FISHRMAN, 152);
	call FishrManBarter;
end

procedure FishrMan21
begin
	gsay_message(SCRIPT_FISHRMAN, 153, BAD_REACTION);
	call FishrManCombat;
end

procedure FishrMan22
begin
	gsay_message(SCRIPT_FISHRMAN, 154, BAD_REACTION);
end

procedure FishrMan23
begin
	gsay_message(SCRIPT_FISHRMAN, 155, NEUTRAL_REACTION);
end

procedure FishrMan24
begin
	gsay_message(SCRIPT_FISHRMAN, 156, NEUTRAL_REACTION);
end

procedure FishrMan25
begin
	float_msg(self_obj, message_str(SCRIPT_FISHRMAN, 157), FLOAT_MSG_NORMAL);
end

procedure FishrManBarter
begin
	gdialog_mod_barter(0);
	giq_option(0, SCRIPT_MODREACT, 100, FishrManEnd, NEUTRAL_REACTION);
end

procedure FishrManCombat
begin
	hostile1 := 1;
	set_global_var(GVAR_SAVE_TANDI_5, 1);
end

procedure FishrManEnd
begin
end

procedure FishrManEnd1
begin
	gsay_message(SCRIPT_FISHRMAN, 121, NEUTRAL_REACTION);
end

procedure FishrManEnd2
begin
	gsay_message(SCRIPT_FISHRMAN, 136, NEUTRAL_REACTION);
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