#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure timed_event_p_proc;
procedure map_enter_p_proc;
procedure damage_p_proc;
procedure SendToWork;
procedure SendToSleep;
procedure WakeUpCall;
procedure LukeDialog;
procedure LukeEnd;
procedure LukeCombat;
procedure Luke1;
procedure Luke2;
procedure Luke3;
procedure Luke4;
procedure Luke5;
procedure Luke6;
procedure Luke7;
procedure Luke8;

variable hostile1;
variable Only_Once := 1;
variable SetDayNight;
variable Sleeping;
variable LastMove;
variable CaravanAgain := 1;

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 (Only_Once) then begin
		Only_Once := 0;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 64);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_MERCHANT);
	end
	if (script_action == look_at_proc) then begin
		call look_at_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 == 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 == timed_event_proc) then begin
							call timed_event_p_proc;
						end
						else begin
							if (script_action == map_enter_proc) then begin
								call map_enter_p_proc;
							end
						end
					end
				end
			end
		end
	end
end

procedure combat
begin
	hostile1 := 1;
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 ((game_time_hour >= 600) and (game_time_hour < 700) or ((game_time_hour >= 700) and (game_time_hour < 1800))) then begin
		call WakeUpCall;
	end
	else begin
		call SendToSleep;
	end
	if (Sleeping == 1) then begin
		if (tile_num(self_obj) != 11099) then begin
			animate_move_obj_to_tile(self_obj, 11099, ANIMATE_WALK);
		end
		else begin
			set_obj_visibility(self_obj, 1);
			Sleeping := 2;
			LastMove := 11099;
		end
	end
end

procedure pickup_p_proc
begin
	if (source_obj == dude_obj) then begin
		hostile1 := 1;
	end
end

procedure talk_p_proc
begin
	if ((game_time_hour >= 600) and (game_time_hour < 700) or ((game_time_hour >= 700) and (game_time_hour < 1800))) then begin
		call Get_Reaction;
		call LukeDialog;
	end
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_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

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_HGENCVAN, 300));
end

procedure timed_event_p_proc
begin
	if ((game_time_hour >= 600) and (game_time_hour < 700) or ((game_time_hour >= 700) and (game_time_hour < 1800))) then begin
		call SendToWork;
	end
end

procedure map_enter_p_proc
begin
	if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
		move_to(self_obj, 11099, 0);
		set_obj_visibility(self_obj, 1);
		Sleeping := 2;
		LastMove := 11099;
	end
end

procedure damage_p_proc
begin
	variable LVar0 := 0;
	LVar0 := obj_pid(source_obj);
	if (party_member_obj(LVar0) != 0) then begin
		set_global_var(GVAR_ENEMY_HUB, 1);
	end
end

procedure SendToWork
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := 0;
	LVar1 := random(15, 45);
	while(LVar0 == 0) do begin
		LVar0 := random(1, 12);
		if (LVar0 == 1) then begin
			LVar0 := 11504;
		end
		else begin
			if (LVar0 == 2) then begin
				LVar0 := 11904;
			end
			else begin
				if (LVar0 == 3) then begin
					LVar0 := 12102;
				end
				else begin
					if (LVar0 == 4) then begin
						LVar0 := 12299;
					end
					else begin
						if (LVar0 == 5) then begin
							LVar0 := 11098;
						end
						else begin
							if (LVar0 == 6) then begin
								LVar0 := 11497;
							end
							else begin
								if (LVar0 == 7) then begin
									LVar0 := 10698;
								end
								else begin
									if (LVar0 == 8) then begin
										LVar0 := 13503;
									end
									else begin
										if (LVar0 == 9) then begin
											LVar0 := 13503;
										end
										else begin
											if (LVar0 == 10) then begin
												LVar0 := 12700;
											end
											else begin
												if (LVar0 == 11) then begin
													LVar0 := 12704;
												end
												else begin
													if (LVar0 == 12) then begin
														LVar0 := 12305;
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
		if (LVar0 == LastMove) then begin
			LVar0 := 0;
		end
	end
	LastMove := LVar0;
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_tile(self_obj, LVar0, -1);
	reg_anim_func(3, 0);
	add_timer_event(self_obj, game_ticks(LVar1), 1);
end

procedure SendToSleep
begin
	if (Sleeping == 0) then begin
		Sleeping := 1;
		SetDayNight := 0;
		if (random(0, 100) >= 80) then begin
			float_msg(self_obj, message_str(SCRIPT_HGENCVAN, 124), FLOAT_MSG_BLUE);
		end
	end
end

procedure WakeUpCall
begin
	if (SetDayNight == 0) then begin
		Sleeping := 0;
		SetDayNight := 1;
		set_obj_visibility(self_obj, 0);
		add_timer_event(self_obj, game_ticks(1), 5);
	end
end

procedure LukeDialog
begin
	start_gdialog(SCRIPT_HGENCVAN, self_obj, 4, -1, -1);
	gsay_start;
	call Luke1;
	gsay_end;
	end_dialogue;
end

procedure LukeEnd
begin
end

procedure LukeCombat
begin
	hostile1 := 1;
end

procedure Luke1
begin
	gsay_reply(SCRIPT_HGENCVAN, 203);
	giq_option(-3, SCRIPT_HGENCVAN, 204, Luke2, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 205, Luke3, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 206, Luke4, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 207, Luke5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 208, LukeEnd, NEUTRAL_REACTION);
	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
		giq_option(4, SCRIPT_HGENCVAN, 209, LukeCombat, BAD_REACTION);
	end
end

procedure Luke2
begin
	gsay_message(SCRIPT_HGENCVAN, 210, NEUTRAL_REACTION);
end

procedure Luke3
begin
	gsay_reply(SCRIPT_HGENCVAN, 211);
	giq_option(4, SCRIPT_HGENCVAN, 212, Luke4, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 213, Luke5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 214, Luke6, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 215, Luke7, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 216, LukeEnd, NEUTRAL_REACTION);
end

procedure Luke4
begin
	gsay_reply(SCRIPT_HGENCVAN, 217);
	giq_option(4, SCRIPT_HGENCVAN, 218, Luke5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 219, LukeEnd, NEUTRAL_REACTION);
end

procedure Luke5
begin
	gsay_reply(SCRIPT_HGENCVAN, 301);
	if (CaravanAgain == 1) then begin
		giq_option(4, SCRIPT_HGENCVAN, 221, Luke8, NEUTRAL_REACTION);
		CaravanAgain := 0;
	end
	giq_option(4, SCRIPT_HGENCVAN, 222, Luke6, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 223, Luke3, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 224, Luke7, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 225, LukeEnd, NEUTRAL_REACTION);
end

procedure Luke6
begin
	gsay_reply(SCRIPT_HGENCVAN, 226);
	giq_option(4, SCRIPT_HGENCVAN, 227, Luke4, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 228, Luke7, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 229, LukeEnd, NEUTRAL_REACTION);
end

procedure Luke7
begin
	gsay_reply(SCRIPT_HGENCVAN, 230);
	giq_option(4, SCRIPT_HGENCVAN, 231, Luke3, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 232, Luke4, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 233, Luke5, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 234, LukeEnd, NEUTRAL_REACTION);
end

procedure Luke8
begin
	gsay_reply(SCRIPT_HGENCVAN, 235);
	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
		giq_option(4, SCRIPT_HGENCVAN, 236, LukeCombat, BAD_REACTION);
	end
	giq_option(4, SCRIPT_HGENCVAN, 237, Luke7, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HGENCVAN, 238, LukeEnd, NEUTRAL_REACTION);
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