#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure look_at_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure destroy_p_proc;
procedure Victor01;
procedure Victor02;
procedure Victor03;
procedure Victor04;
procedure Victor05;
procedure Victor06;
procedure Victor07;
procedure VictorEnd;
procedure VictorCombat;
procedure VictorDies;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable hostile1;


procedure start
begin
	if (script_action == critter_proc) then begin
		call critter_p_proc;
	end
	else begin
		if (script_action == look_at_proc) then begin
			call look_at_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
					else begin
						if (script_action == timed_event_proc) then begin
							call timed_event_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 (global_var(GVAR_BUST_SKULZ) != 2) then begin
		call sleeping;
	end
end

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

procedure map_enter_p_proc
begin
	home_tile := 13907;
	sleep_tile := 13097;
	sleep_time := 2203;
	wake_time := 707;
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 14);
	if (global_var(GVAR_BUST_SKULZ) == 2) then begin
		destroy_object(self_obj);
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (local_var(5)) then begin
		float_msg(self_obj, message_str(SCRIPT_VICTOR, 116), FLOAT_MSG_NORMAL);
	end
	else begin
		start_gdialog(SCRIPT_VICTOR, self_obj, 4, -1, -1);
		gsay_start;
		if (local_var(4)) then begin
			call Victor06;
		end
		else begin
			call Victor01;
		end
		gsay_end;
		end_dialogue;
	end
end

procedure timed_event_p_proc
begin
	if (obj_can_hear_obj(dude_obj, self_obj)) then begin
		float_msg(self_obj, message_str(SCRIPT_VICTOR, 102), FLOAT_MSG_PURPLE);
	end
	critter_dmg(self_obj, 250, 0);
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_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

procedure Victor01
begin
	set_local_var(4, 1);
	gsay_reply(SCRIPT_VICTOR, 103);
	giq_option(4, SCRIPT_VICTOR, 104, Victor02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VICTOR, 105, Victor03, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_VICTOR, 106, Victor07, NEUTRAL_REACTION);
end

procedure Victor02
begin
	gsay_message(SCRIPT_VICTOR, 107, NEUTRAL_REACTION);
	call VictorCombat;
end

procedure Victor03
begin
	gsay_reply(SCRIPT_VICTOR, 108);
	giq_option(4, SCRIPT_VICTOR, 109, Victor04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VICTOR, 110, Victor02, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_VICTOR, 111, Victor05, NEUTRAL_REACTION);
end

procedure Victor04
begin
	gsay_message(SCRIPT_VICTOR, 112, NEUTRAL_REACTION);
	call VictorCombat;
end

procedure Victor05
begin
	gsay_message(SCRIPT_VICTOR, 113, NEUTRAL_REACTION);
end

procedure Victor06
begin
	gsay_reply(SCRIPT_VICTOR, 114);
	giq_option(4, SCRIPT_VICTOR, 117, Victor04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_VICTOR, 118, VictorEnd, NEUTRAL_REACTION);
end

procedure Victor07
begin
	gsay_message(SCRIPT_VICTOR, 115, NEUTRAL_REACTION);
	call VictorDies;
end

procedure VictorEnd
begin
end

procedure VictorCombat
begin
	hostile1 := 1;
end

procedure VictorDies
begin
	add_timer_event(self_obj, game_ticks(5), 1);
end

procedure sleeping
begin
	if (local_var(5) == 1) then begin
		if (not(night_person) and (game_time_hour >= wake_time) and (game_time_hour < sleep_time) or (night_person and ((game_time_hour >= wake_time) or (game_time_hour < sleep_time)))) then begin
			if (((game_time_hour - wake_time) < 10) and ((game_time_hour - wake_time) > 0)) then begin
				if (tile_num(self_obj) != home_tile) then begin
					animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
				end
				else begin
					set_local_var(5, 0);
				end
			end
			else begin
				move_to(self_obj, home_tile, elevation(self_obj));
				if (tile_num(self_obj) == home_tile) then begin
					set_local_var(5, 0);
				end
			end
		end
	end
	else begin
		if (night_person and (game_time_hour >= sleep_time) and (game_time_hour < wake_time) or (not(night_person) and ((game_time_hour >= sleep_time) or (game_time_hour < wake_time)))) then begin
			if (((game_time_hour - sleep_time) < 10) and ((game_time_hour - sleep_time) > 0)) then begin
				if (tile_num(self_obj) != sleep_tile) then begin
					animate_move_obj_to_tile(self_obj, self_obj, ANIMATE_WALK);
				end
				else begin
					set_local_var(5, 1);
				end
			end
			else begin
				if (tile_num(self_obj) != sleep_tile) then begin
					move_to(self_obj, sleep_tile, elevation(self_obj));
				end
				else begin
					set_local_var(5, 1);
				end
			end
		end
	end
end