#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat_p_proc;
procedure critter_p_proc;
procedure destroy_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure GenPalB00;
procedure GenPalB01;
procedure GenPalB02;
procedure GenPalB03;
procedure GenPalB04;
procedure GenPalB05;
procedure GenPalB06;
procedure GenPalB07;
procedure GenPalB08;
procedure GenPalB09;
procedure GenPalB10;
procedure GenPalB10a;
procedure GenPalB11;
procedure GenPalB12;
procedure GenPalB13;
procedure GenPalB14;
procedure GenPalB15;
procedure GenPalB16;
procedure GenPalB17;
procedure GenPalB18;
procedure GenPalB19;
procedure GenPalB20;
procedure GenPalB20a;
procedure GenPalB21;
procedure GenPalB22;
procedure GenPalB23;
procedure GenPalB24;
procedure GenPalB25;
procedure flee_dude;

variable annoyed;
variable hostile1;
variable initialized1;
variable known;
variable round_counter;
variable scared;

import variable random_seed_1;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 44);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_PALADIN);
		hostile1 := global_var(GVAR_SAVE_TANDI_5);
		initialized1 := 1;
	end
	else begin
		if (script_action == combat_proc) then begin
			call combat_p_proc;
		end
		else 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 == 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

procedure combat_p_proc
begin
	if (fixed_param == 4) then begin
		round_counter := round_counter + 1;
		if (get_critter_stat(self_obj, 35) < 10) then begin
			scared := 1;
		end
	end
end

procedure critter_p_proc
begin
	if (scared) then begin
		if (tile_distance_objs(self_obj, dude_obj) < 8) then begin
			call flee_dude;
		end
	end
	else begin
		if (hostile1) then begin
			hostile1 := 0;
			random_seed_1 := 1;
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		random_seed_1 := 1;
		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
		set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
	end
end

procedure pickup_p_proc
begin
	if (not(scared)) then begin
		hostile1 := 1;
		set_global_var(GVAR_SAVE_TANDI_5, 1);
	end
end

procedure talk_p_proc
begin
	if (known) then begin
		if (annoyed) then begin
			call GenPalB23;
		end
		else begin
			start_gdialog(SCRIPT_GENPALB, self_obj, 4, -1, -1);
			gsay_start;
			call GenPalB24;
			gsay_end;
			end_dialogue;
		end
	end
	else begin
		if (scared) then begin
			call GenPalB01;
		end
		else begin
			if ((random_seed_1 == 1) or (global_var(GVAR_ENEMY_BROTHERHOOD) == 1)) then begin
				call GenPalB00;
			end
			else begin
				start_gdialog(SCRIPT_GENPALB, self_obj, 4, -1, -1);
				gsay_start;
				if (global_var(GVAR_BROTHERHOOD_SEED) == 2) then begin
					call GenPalB02;
				end
				else begin
					if (global_var(GVAR_BROTHERHOOD_OF_STEEL) >= 1) then begin
						if (global_var(GVAR_PLAYER_REPUTATION) >= 20) then begin
							call GenPalB03;
						end
						else begin
							call GenPalB04;
						end
					end
					else begin
						if ((global_var(GVAR_PLAYER_REPUTATION) < 20) or (((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))) or (global_var(GVAR_CHILDKILLER_REPUTATION) > 2)) then begin
							call GenPalB06;
						end
						else begin
							call GenPalB05;
						end
					end
				end
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure GenPalB00
begin
	float_msg(self_obj, message_str(SCRIPT_GENPALB, 100), FLOAT_MSG_RED);
	hostile1 := 1;
	set_global_var(GVAR_SAVE_TANDI_5, 1);
end

procedure GenPalB01
begin
	float_msg(self_obj, message_str(SCRIPT_GENPALB, 101), FLOAT_MSG_RED);
end

procedure GenPalB02
begin
	known := 1;
	gsay_reply(SCRIPT_GENPALB, 102);
	giq_option(7, SCRIPT_GENPALB, 103, GenPalB07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 104, GenPalB08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 105, GenPalB09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 106, GenPalB10, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GENPALB, 107, GenPalB11, NEUTRAL_REACTION);
end

procedure GenPalB03
begin
	known := 1;
	gsay_reply(SCRIPT_GENPALB, 108);
	giq_option(7, SCRIPT_GENPALB, 109, GenPalB12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 110, GenPalB13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 111, GenPalB14, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 112, GenPalB15, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 113, GenPalB16, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GENPALB, 114, GenPalB11, NEUTRAL_REACTION);
end

procedure GenPalB04
begin
	known := 1;
	annoyed := 1;
	gsay_message(SCRIPT_GENPALB, 115, BAD_REACTION);
end

procedure GenPalB05
begin
	known := 1;
	gsay_reply(SCRIPT_GENPALB, 116);
	if ((global_var(GVAR_FIND_WATER_CHIP) == 0) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_GENPALB, message_str(SCRIPT_GENPALB, 117) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_GENPALB, 118), GenPalB17, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_GENPALB, message_str(SCRIPT_GENPALB, 117) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_GENPALB, 119), GenPalB18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, message_str(SCRIPT_GENPALB, 117) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_GENPALB, 120), GenPalB19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, message_str(SCRIPT_GENPALB, 117) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_GENPALB, 121), GenPalB21, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 122, GenPalB21, BAD_REACTION);
	giq_option(-3, SCRIPT_GENPALB, 123, GenPalB22, NEUTRAL_REACTION);
end

procedure GenPalB06
begin
	known := 1;
	annoyed := 1;
	gsay_message(SCRIPT_GENPALB, 124, BAD_REACTION);
end

procedure GenPalB07
begin
	gsay_message(SCRIPT_GENPALB, 125, NEUTRAL_REACTION);
end

procedure GenPalB08
begin
	gsay_message(SCRIPT_GENPALB, 126, NEUTRAL_REACTION);
end

procedure GenPalB09
begin
	gsay_message(SCRIPT_GENPALB, 127, NEUTRAL_REACTION);
end

procedure GenPalB10
begin
	gsay_reply(SCRIPT_GENPALB, 128);
	giq_option(0, SCRIPT_MODREACT, 106, GenPalB10a, NEUTRAL_REACTION);
end

procedure GenPalB10a
begin
	gsay_message(SCRIPT_GENPALB, 129, NEUTRAL_REACTION);
end

procedure GenPalB11
begin
	gsay_message(SCRIPT_GENPALB, 130, NEUTRAL_REACTION);
end

procedure GenPalB12
begin
	gsay_reply(SCRIPT_GENPALB, 131);
	if ((global_var(GVAR_FIND_WATER_CHIP) == 0) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_GENPALB, 132, GenPalB17, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_GENPALB, 133, GenPalB18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 134, GenPalB19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 135, GenPalB20, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 122, GenPalB21, NEUTRAL_REACTION);
end

procedure GenPalB13
begin
	known := 1;
	annoyed := 1;
	gsay_message(SCRIPT_GENPALB, 136, BAD_REACTION);
end

procedure GenPalB14
begin
	gsay_message(SCRIPT_GENPALB, 137, NEUTRAL_REACTION);
end

procedure GenPalB15
begin
	gsay_message(SCRIPT_GENPALB, 138, NEUTRAL_REACTION);
end

procedure GenPalB16
begin
	gsay_message(SCRIPT_GENPALB, 139, NEUTRAL_REACTION);
	gfade_out(600);
	game_time_advance(game_ticks(random(86400, 172800)));
	load_map(13, 0);
	gfade_in(600);
end

procedure GenPalB17
begin
	gsay_message(SCRIPT_GENPALB, 140, NEUTRAL_REACTION);
end

procedure GenPalB18
begin
	gsay_message(SCRIPT_GENPALB, 141, NEUTRAL_REACTION);
end

procedure GenPalB19
begin
	annoyed := 1;
	gsay_message(SCRIPT_GENPALB, 142, BAD_REACTION);
end

procedure GenPalB20
begin
	gsay_reply(SCRIPT_GENPALB, 143);
	giq_option(0, SCRIPT_MODREACT, 106, GenPalB20a, NEUTRAL_REACTION);
end

procedure GenPalB20a
begin
	gsay_message(SCRIPT_GENPALB, 144, NEUTRAL_REACTION);
end

procedure GenPalB21
begin
	hostile1 := 1;
	set_global_var(GVAR_SAVE_TANDI_5, 1);
	if (dude_is_male) then begin
		gsay_message(SCRIPT_GENPALB, 145, BAD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_GENPALB, 146, BAD_REACTION);
	end
end

procedure GenPalB22
begin
	gsay_message(SCRIPT_GENPALB, 147, NEUTRAL_REACTION);
end

procedure GenPalB23
begin
	float_msg(self_obj, message_str(SCRIPT_GENPALB, 148), FLOAT_MSG_RED);
end

procedure GenPalB24
begin
	gsay_reply(SCRIPT_GENPALB, 149);
	if ((global_var(GVAR_FIND_WATER_CHIP) == 0) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_GENPALB, 132, GenPalB17, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_GENPALB, 133, GenPalB18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 150, GenPalB25, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 135, GenPalB20, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENPALB, 122, GenPalB21, BAD_REACTION);
end

procedure GenPalB25
begin
	annoyed := 1;
	gsay_message(SCRIPT_GENPALB, 151, 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