#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure look_at_p_proc;
procedure description_p_proc;
procedure talk_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure Zim00;
procedure Zim01;
procedure Zim02;
procedure Zim03;
procedure Zim03a;
procedure Zim04;
procedure Zim05;
procedure Zim06;
procedure Zim07;
procedure Zim08;
procedure Zim08a;
procedure Zim09;
procedure Zim10;
procedure Zim11;
procedure Zim12;
procedure Zim13;
procedure Zim14;
procedure Zim15;
procedure Zim16;
procedure Zim17;
procedure Zim18;
procedure Zim19;
procedure Zim20;
procedure Zim21;
procedure Zim22;
procedure Zim23;
procedure Zim24;
procedure Zim25;
procedure Zim26;
procedure Zim27;
procedure ZimEnd;

import variable JonPtr;

variable Initialize := 1;
variable DisplayMessage;

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 (Initialize) then begin
		if (obj_is_carrying_obj_pid(self_obj, 41) == 0) then begin
			item_caps_adjust(self_obj, random(50, 150));
		end
		if (global_var(GVAR_BLADES) == 9103) then begin
			if (global_var(GVAR_ZIMMERMAN) != 9003) then begin
				critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 89);
			end
			else begin
				critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
			end
		end
		else begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_PEASANT);
		JonPtr := self_obj;
		Initialize := 0;
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	if (local_var(4) == 1) then begin
		display_msg(message_str(SCRIPT_JON, 176));
	end
	else begin
		display_msg(message_str(SCRIPT_JON, 175));
	end
end

procedure description_p_proc
begin
	script_overrides;
	if (local_var(4) == 1) then begin
		display_msg(message_str(SCRIPT_JON, 176));
	end
	else begin
		display_msg(message_str(SCRIPT_JON, 175));
	end
end

procedure talk_p_proc
begin
	if (global_var(GVAR_ENEMY_ADYTUM) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, random(100, 105)), FLOAT_MSG_RED);
	end
	else begin
		if ((local_var(4) == 1) and (get_critter_stat(dude_obj, 4) < 4)) then begin
			float_msg(self_obj, message_str(SCRIPT_JON, 139), FLOAT_MSG_NORMAL);
		end
		else begin
			if (global_var(GVAR_ZIMMERMAN) == 2) then begin
				float_msg(self_obj, message_str(SCRIPT_JON, random(183, 187)), FLOAT_MSG_NORMAL);
			end
			else begin
				call Get_Reaction;
				start_gdialog(SCRIPT_JON, self_obj, 4, -1, -1);
				gsay_start;
				DisplayMessage := 100;
				if (local_var(4) == 0) then begin
					call Zim00;
				end
				else begin
					if (global_var(GVAR_ZIMMERMAN) == 0) then begin
						call Zim01;
					end
					else begin
						if (obj_is_carrying_obj_pid(dude_obj, 238)) then begin
							if (global_var(GVAR_ZIMMERMAN) == 9001) then begin
								call Zim24;
							end
							else begin
								call Zim27;
							end
						end
						else begin
							if (global_var(GVAR_ZIMMERMAN) == 1) then begin
								call Zim17;
							end
							else begin
								if (global_var(GVAR_ZIMMERMAN) == 9001) then begin
									call Zim19;
								end
								else begin
									if (global_var(GVAR_ZIMMERMAN) == 9002) then begin
										call Zim21;
									end
								end
							end
						end
					end
				end
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		if (global_var(GVAR_ENEMY_REGULATOR) != 1) then begin
			set_global_var(GVAR_ENEMY_ADYTUM, 1);
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		if (global_var(GVAR_ENEMY_REGULATOR) != 1) then begin
			set_global_var(GVAR_ENEMY_ADYTUM, 1);
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
		end
		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
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		if ((global_var(GVAR_BLADES) == 9103) and (global_var(GVAR_ZIMMERMAN) != 9003)) then begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 89);
			float_msg(self_obj, message_str(SCRIPT_JON, 174), FLOAT_MSG_RED);
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
		else begin
			if (global_var(GVAR_ENEMY_ADYTUM) == 1) then begin
				attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
			end
		end
	end
	if (global_var(GVAR_ENEMY_REGULATOR) == 1) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 89);
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
end

procedure pickup_p_proc
begin
	set_global_var(GVAR_ENEMY_ADYTUM, 1);
end

procedure Zim00
begin
	DisplayMessage := 182;
	gsay_reply(SCRIPT_JON, 100);
	giq_option(4, SCRIPT_JON, 181, Zim01, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JON, 104, Zim13, NEUTRAL_REACTION);
	set_local_var(4, 1);
end

procedure Zim01
begin
	gsay_reply(SCRIPT_JON, DisplayMessage);
	giq_option(4, SCRIPT_JON, 101, Zim02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JON, 102, Zim14, NEUTRAL_REACTION);
	if (obj_is_carrying_obj_pid(dude_obj, 238)) then begin
		giq_option(4, SCRIPT_JON, 178, Zim26, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_JON, 103, ZimEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_JON, 104, Zim13, NEUTRAL_REACTION);
end

procedure Zim02
begin
	gsay_reply(SCRIPT_JON, 105);
	gsay_option(SCRIPT_JON, 106, Zim03, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 107, Zim03, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 108, Zim04, NEUTRAL_REACTION);
end

procedure Zim03
begin
	gsay_reply(SCRIPT_JON, 109);
	gsay_option(SCRIPT_JON, 110, Zim04, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 111, Zim13, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 112, Zim03a, NEUTRAL_REACTION);
end

procedure Zim03a
begin
	set_global_var(GVAR_ZIMMERMAN, 1);
end

procedure Zim04
begin
	gsay_reply(SCRIPT_JON, 113);
	gsay_option(SCRIPT_JON, 114, Zim07, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 115, Zim05, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 116, Zim05, NEUTRAL_REACTION);
end

procedure Zim05
begin
	gsay_reply(SCRIPT_JON, 117);
	gsay_option(SCRIPT_JON, 118, Zim06, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 119, Zim07, NEUTRAL_REACTION);
end

procedure Zim06
begin
	gsay_reply(SCRIPT_JON, 120);
	gsay_option(SCRIPT_JON, 121, Zim07, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 122, Zim11, NEUTRAL_REACTION);
end

procedure Zim07
begin
	gsay_reply(SCRIPT_JON, 123);
	gsay_option(SCRIPT_JON, 124, Zim09, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 125, Zim08, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 126, Zim10, NEUTRAL_REACTION);
end

procedure Zim08
begin
	gsay_reply(SCRIPT_JON, 127);
	gsay_option(SCRIPT_JON, 128, Zim09, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 129, Zim10, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 130, Zim08a, NEUTRAL_REACTION);
end

procedure Zim08a
begin
	call Bottom_React;
	set_global_var(GVAR_ZIMMERMAN, 1);
end

procedure Zim09
begin
	gsay_reply(SCRIPT_JON, 131);
	gsay_option(SCRIPT_JON, 132, Zim10, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 133, Zim08a, NEUTRAL_REACTION);
	set_global_var(GVAR_ZIMMERMAN, 1);
end

procedure Zim10
begin
	gsay_message(SCRIPT_JON, 134, NEUTRAL_REACTION);
	set_global_var(GVAR_ZIMMERMAN, 9001);
end

procedure Zim11
begin
	gsay_reply(SCRIPT_JON, 135);
	gsay_option(SCRIPT_JON, 136, Zim16, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 137, Zim08a, NEUTRAL_REACTION);
end

procedure Zim12
begin
	gsay_message(SCRIPT_JON, 138, NEUTRAL_REACTION);
	call Bottom_React;
	set_global_var(GVAR_ENEMY_REGULATOR, 1);
	gfade_out(600);
	move_to(dude_obj, 9097, 0);
	gfade_in(600);
end

procedure Zim13
begin
	gsay_message(SCRIPT_JON, 139, NEUTRAL_REACTION);
end

procedure Zim14
begin
	gsay_reply(SCRIPT_JON, 140);
	gsay_option(SCRIPT_JON, 141, Zim15, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 142, Zim12, NEUTRAL_REACTION);
end

procedure Zim15
begin
	gsay_reply(SCRIPT_JON, 143);
	gsay_option(SCRIPT_JON, 144, Zim03, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 145, Zim03, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 146, Zim04, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 147, Zim12, NEUTRAL_REACTION);
end

procedure Zim16
begin
	gsay_reply(SCRIPT_JON, 148);
	gsay_option(SCRIPT_JON, 149, Zim07, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 150, Zim07, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 151, Zim08a, NEUTRAL_REACTION);
end

procedure Zim17
begin
	gsay_reply(SCRIPT_JON, 152);
	gsay_option(SCRIPT_JON, 153, Zim18, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 154, Zim14, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 155, ZimEnd, NEUTRAL_REACTION);
end

procedure Zim18
begin
	gsay_message(SCRIPT_JON, 156, NEUTRAL_REACTION);
	set_global_var(GVAR_ZIMMERMAN, 9001);
end

procedure Zim19
begin
	gsay_reply(SCRIPT_JON, 157);
	gsay_option(SCRIPT_JON, 158, Zim12, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 159, Zim20, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 160, Zim13, NEUTRAL_REACTION);
end

procedure Zim20
begin
	gsay_message(SCRIPT_JON, 161, NEUTRAL_REACTION);
end

procedure Zim21
begin
	gsay_reply(SCRIPT_JON, 162);
	gsay_option(SCRIPT_JON, 163, Zim22, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 164, Zim23, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 165, Zim22, NEUTRAL_REACTION);
	set_global_var(GVAR_ZIMMERMAN, 2);
end

procedure Zim22
begin
	gsay_message(SCRIPT_JON, 166, NEUTRAL_REACTION);
	call Top_React;
	item_caps_adjust(dude_obj, 2000);
end

procedure Zim23
begin
	gsay_message(SCRIPT_JON, 167, NEUTRAL_REACTION);
	call Top_React;
	item_caps_adjust(dude_obj, 2500);
end

procedure Zim24
begin
	gsay_reply(SCRIPT_JON, 168);
	gsay_option(SCRIPT_JON, 169, Zim25, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 170, Zim26, NEUTRAL_REACTION);
end

procedure Zim25
begin
	gsay_reply(SCRIPT_JON, 171);
	gsay_option(SCRIPT_JON, 172, Zim26, NEUTRAL_REACTION);
end

procedure Zim26
begin
	variable LVar0 := 0;
	LVar0 := obj_carrying_pid_obj(dude_obj, 238);
	rm_obj_from_inven(dude_obj, LVar0);
	gsay_message(SCRIPT_JON, 173, NEUTRAL_REACTION);
	set_global_var(GVAR_ZIMMERMAN, 9003);
	if (global_var(GVAR_BLADES) == 9102) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
		set_global_var(GVAR_BLADES, 9103);
	end
	else begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
	end
end

procedure Zim27
begin
	gsay_reply(SCRIPT_JON, 177);
	gsay_option(SCRIPT_JON, 178, Zim26, NEUTRAL_REACTION);
	gsay_option(SCRIPT_JON, 179, ZimEnd, NEUTRAL_REACTION);
end

procedure ZimEnd
begin
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