#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure moveme;
procedure goto00;
procedure goto00a;
procedure goto01;
procedure goto02;
procedure goto03;
procedure goto03a;
procedure goto04;
procedure goto05;
procedure goto06;
procedure goto07;
procedure goto07a;
procedure goto08;
procedure gotoend;
procedure gotocbt;
procedure gotoret;

variable HOSTILE;
variable only_once := 1;
variable DISGUISED;
variable ARMED;
variable moving := 1;
variable my_hex;
variable home_tile;
variable smoke_tile;
variable indlog;


procedure start
begin
	if (local_var(4) == 0) then begin
		set_local_var(4, 1);
		set_local_var(5, 1);
		set_local_var(6, 0);
		set_local_var(7, 22091);
	end
	if (only_once) then begin
		only_once := 0;
		home_tile := 23488;
		smoke_tile := 21299;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 34);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_SUPER_MUTANT_SGT);
		if (local_var(0) == 1) then begin
			set_obj_visibility(self_obj, 1);
			moving := 0;
		end
	end
	else begin
		if (local_var(0) != 1) then begin
			if (script_action == damage_proc) then begin
				if (global_var(GVAR_ENEMY_VAULT_13) == 0) then begin
					set_global_var(GVAR_ENEMY_VAULT_13, 1);
				end
			end
			else begin
				if (script_action == talk_proc) then begin
					call do_dialogue;
				end
				else begin
					if (script_action == timed_event_proc) then begin
						if (fixed_param == 1) then begin
							moving := 1;
						end
					end
					else begin
						if (script_action == pickup_proc) then begin
							HOSTILE := 1;
						end
					end
				end
			end
			if (script_action == critter_proc) then begin
				if (HOSTILE) then begin
					HOSTILE := 0;
					attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
				end
				else begin
					if (moving) then begin
						call moveme;
					end
				end
			end
			else begin
				if (script_action == look_at_proc) then begin
					script_overrides;
					if (local_var(3)) then begin
						display_msg(message_str(SCRIPT_MLOPS, 302));
					end
					else begin
						display_msg(message_str(SCRIPT_MLOPS, 200));
					end
				end
				else begin
					if (script_action == destroy_proc) then begin
						set_local_var(0, 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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
							if ((global_var(GVAR_BAD_MONSTER) % 6) == 0) then begin
								set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
							end
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	start_gdialog(SCRIPT_MLOPS, self_obj, 4, -1, -1);
	gsay_start;
	indlog := 1;
	ARMED := 0;
	DISGUISED := 0;
	if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
		ARMED := 1;
	end
	if (obj_pid(critter_inven_obj(dude_obj, 0)) == 113) then begin
		if (metarule(16, 0) > 1) then begin
			DISGUISED := 0;
		end
		else begin
			DISGUISED := 1;
		end
	end
	if ((DISGUISED == 0) or (ARMED == 1)) then begin
		call goto01;
	end
	else begin
		if (local_var(2) == 1) then begin
			call goto08;
		end
		else begin
			set_local_var(2, 1);
			call goto00;
		end
	end
	indlog := 0;
	gsay_end;
	end_dialogue;
end

procedure moveme
begin
	my_hex := tile_num(self_obj);
	if (my_hex == local_var(7)) then begin
		set_local_var(3, 0);
		if (local_var(5)) then begin
			set_local_var(6, local_var(6) + 1);
		end
		else begin
			set_local_var(6, local_var(6) - 1);
		end
		if (local_var(6) > 2) then begin
			set_local_var(3, 1);
			moving := 0;
			set_local_var(6, 1);
			set_local_var(5, 0);
			add_timer_event(self_obj, game_ticks(180), 1);
		end
		else begin
			if (local_var(6) < 0) then begin
				moving := 0;
				set_local_var(6, 1);
				set_local_var(5, 1);
				add_timer_event(self_obj, game_ticks(600), 1);
			end
		end
		if (local_var(6) == 0) then begin
			set_local_var(7, home_tile);
		end
		else begin
			if (local_var(6) == 1) then begin
				set_local_var(7, 22091);
			end
			else begin
				if (local_var(6) == 2) then begin
					set_local_var(7, smoke_tile);
				end
			end
		end
	end
	else begin
		animate_move_obj_to_tile(self_obj, local_var(7), ANIMATE_WALK);
	end
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		ARMED := 0;
		DISGUISED := 0;
		if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
			ARMED := 1;
		end
		if (obj_pid(critter_inven_obj(dude_obj, 0)) == 113) then begin
			if (metarule(16, 0) > 1) then begin
				DISGUISED := 0;
			end
			else begin
				DISGUISED := 1;
			end
		end
		if ((DISGUISED == 0) or (ARMED == 1)) then begin
			if (tile_distance_objs(self_obj, dude_obj) < 6) then begin
				if (local_var(1) < 1) then begin
					set_local_var(1, 1);
					add_timer_event(self_obj, game_ticks(15), 2);
					dialogue_system_enter;
				end
			end
		end
	end
end

procedure goto00
begin
	gsay_reply(SCRIPT_MLOPS, 202);
	giq_option(-3, SCRIPT_MLOPS, 203, goto01, BAD_REACTION);
	giq_option(4, SCRIPT_MLOPS, 204, goto02, BAD_REACTION);
	giq_option(7, SCRIPT_MLOPS, 205, goto00a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MLOPS, 206, gotocbt, BAD_REACTION);
end

procedure goto00a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call goto04;
	end
	else begin
		call goto03;
	end
end

procedure goto01
begin
	if (indlog) then begin
		gsay_message(SCRIPT_MLOPS, 207, BAD_REACTION);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_MLOPS, 207), FLOAT_MSG_RED);
	end
	call gotocbt;
end

procedure goto02
begin
	gsay_message(SCRIPT_MLOPS, 208, BAD_REACTION);
	call gotocbt;
end

procedure goto03
begin
	gsay_reply(SCRIPT_MLOPS, 209);
	giq_option(4, SCRIPT_MLOPS, 210, gotocbt, BAD_REACTION);
	giq_option(7, SCRIPT_MLOPS, 211, goto03a, NEUTRAL_REACTION);
end

procedure goto03a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call goto04;
	end
	else begin
		call goto02;
	end
end

procedure goto04
begin
	gsay_reply(SCRIPT_MLOPS, 212);
	giq_option(7, SCRIPT_MLOPS, 213, gotoend, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_MLOPS, 214, goto05, NEUTRAL_REACTION);
end

procedure goto05
begin
	gsay_reply(SCRIPT_MLOPS, 215);
	giq_option(7, SCRIPT_MLOPS, 216, goto06, NEUTRAL_REACTION);
	giq_option(8, SCRIPT_MLOPS, 217, goto07, BAD_REACTION);
end

procedure goto06
begin
	gsay_message(SCRIPT_MLOPS, 218, NEUTRAL_REACTION);
end

procedure goto07
begin
	gsay_reply(SCRIPT_MLOPS, 219);
	giq_option(4, SCRIPT_MLOPS, 220, goto07a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MLOPS, 221, gotocbt, BAD_REACTION);
end

procedure goto07a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call goto06;
	end
	else begin
		call goto01;
	end
end

procedure goto08
begin
	if (indlog) then begin
		gsay_message(SCRIPT_MLOPS, 222, NEUTRAL_REACTION);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_MLOPS, 222), FLOAT_MSG_RED);
	end
end

procedure gotoend
begin
end

procedure gotocbt
begin
	HOSTILE := 1;
end

procedure gotoret
begin
end