#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 Zack00;
procedure Zack01;
procedure Zack02;
procedure Zack03;
procedure Zack04;
procedure Zack05;
procedure ZackYes;
procedure ZackNo;
procedure ZackBarter;
procedure ZackEnd;

import variable Locker_Ptr;

variable Initialize := 1;
variable BarterMod := 4294967286; /* (-10) */
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
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 48);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GUNRUNNERS);
		Initialize := 0;
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	if (local_var(4) == 1) then begin
		display_msg(message_str(SCRIPT_ZACK, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_ZACK, 101));
	end
end

procedure description_p_proc
begin
	script_overrides;
	if (local_var(4) == 1) then begin
		display_msg(message_str(SCRIPT_ZACK, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_ZACK, 101));
	end
end

procedure talk_p_proc
begin
	if (global_var(GVAR_ENEMY_BLADE) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, random(100, 105)), FLOAT_MSG_RED);
	end
	else begin
		BarterMod := -10;
		move_obj_inven_to_obj(Locker_Ptr, self_obj);
		item_caps_adjust(self_obj, 3000 + random(1, 1000));
		call Get_Reaction;
		start_gdialog(SCRIPT_ZACK, self_obj, 4, -1, -1);
		gsay_start;
		if (local_var(5) == 2) then begin
			BarterMod := 15;
		end
		else begin
			if (local_var(5) == 1) then begin
				BarterMod := 5;
			end
			else begin
				BarterMod := -10;
			end
		end
		gdialog_set_barter_mod(BarterMod);
		if (local_var(4) == 0) then begin
			call Zack00;
		end
		else begin
			call Zack01;
		end
		gsay_end;
		end_dialogue;
		item_caps_adjust(self_obj, -item_caps_total(self_obj));
		move_obj_inven_to_obj(self_obj, Locker_Ptr);
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_BLADE, 1);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_BLADE, 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
	end
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		if (global_var(GVAR_ENEMY_BLADE) == 1) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
end

procedure pickup_p_proc
begin
	set_global_var(GVAR_ENEMY_BLADE, 1);
end

procedure Zack00
begin
	gsay_reply(SCRIPT_ZACK, 102);
	gsay_option(SCRIPT_ZACK, 104, ZackBarter, NEUTRAL_REACTION);
	gsay_option(SCRIPT_ZACK, 103, Zack02, NEUTRAL_REACTION);
	if (local_var(5) == 0) then begin
		DisplayMessage := 117;
		gsay_option(SCRIPT_ZACK, 105, Zack04, NEUTRAL_REACTION);
	end
	else begin
		if (local_var(5) == 1) then begin
			DisplayMessage := 123;
			gsay_option(SCRIPT_ZACK, 122, Zack05, NEUTRAL_REACTION);
		end
	end
	gsay_option(SCRIPT_ZACK, 107, ZackEnd, NEUTRAL_REACTION);
end

procedure Zack01
begin
	gsay_reply(SCRIPT_ZACK, 106);
	gsay_option(SCRIPT_ZACK, 104, ZackBarter, NEUTRAL_REACTION);
	gsay_option(SCRIPT_ZACK, 103, Zack02, NEUTRAL_REACTION);
	if (local_var(5) == 0) then begin
		DisplayMessage := 117;
		gsay_option(SCRIPT_ZACK, 105, Zack04, NEUTRAL_REACTION);
	end
	else begin
		if (local_var(5) == 1) then begin
			DisplayMessage := 123;
			gsay_option(SCRIPT_ZACK, 122, Zack05, NEUTRAL_REACTION);
		end
	end
	gsay_option(SCRIPT_ZACK, 107, ZackEnd, NEUTRAL_REACTION);
end

procedure Zack02
begin
	gsay_reply(SCRIPT_ZACK, 114);
	gsay_option(SCRIPT_ZACK, 115, Zack03, NEUTRAL_REACTION);
end

procedure Zack03
begin
	gsay_reply(SCRIPT_ZACK, 111);
	gsay_option(SCRIPT_ZACK, 113, ZackBarter, NEUTRAL_REACTION);
end

procedure Zack04
begin
	if (global_var(GVAR_WATER_CHIP_8) == 9250) then begin
		BarterMod := 15;
		set_local_var(5, 2);
		call ZackYes;
	end
	else begin
		if (global_var(GVAR_GUN_RUNNER) == 9201) then begin
			BarterMod := 5;
			set_local_var(5, 1);
			call ZackYes;
		end
		else begin
			BarterMod := -10;
			call ZackNo;
		end
	end
end

procedure Zack05
begin
	if (global_var(GVAR_WATER_CHIP_8) == 9250) then begin
		BarterMod := 0;
		set_local_var(5, 2);
		call ZackYes;
	end
	else begin
		call ZackNo;
	end
end

procedure ZackYes
begin
	gsay_reply(SCRIPT_ZACK, 116);
	gsay_option(SCRIPT_ZACK, 120, ZackBarter, NEUTRAL_REACTION);
	gsay_option(SCRIPT_ZACK, 121, ZackEnd, NEUTRAL_REACTION);
end

procedure ZackNo
begin
	gsay_reply(SCRIPT_ZACK, DisplayMessage);
	gsay_option(SCRIPT_ZACK, 119, ZackBarter, NEUTRAL_REACTION);
	gsay_option(SCRIPT_ZACK, 107, ZackEnd, NEUTRAL_REACTION);
end

procedure ZackBarter
begin
	gdialog_mod_barter(BarterMod);
	gsay_message(SCRIPT_ZACK, 108, NEUTRAL_REACTION);
end

procedure ZackEnd
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