#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat_p_proc;
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_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 Curtis01;
procedure Curtis02;
procedure Curtis03;
procedure Curtis03a;
procedure Curtis04;
procedure Curtis05;
procedure Curtis06;
procedure Curtis07;
procedure Curtis08;
procedure CurtisEnd;
procedure flee_dude;
procedure movement;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable initialized1;
variable new_tile;
variable round_counter;
variable hostile1;


procedure start
begin
	if (script_action == combat_proc) then begin
		call combat_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 == look_at_proc) then begin
					call look_at_p_proc;
				end
				else begin
					if (script_action == map_enter_proc) then begin
						call map_enter_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
	end
end

procedure combat_p_proc
begin
	if (fixed_param == 4) then begin
		round_counter := round_counter + 1;
	end
	if (round_counter > 3) then begin
		if (global_var(GVAR_ENEMY_SHADY_SANDS) == 0) then begin
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 5);
			set_global_var(GVAR_ENEMY_SHADY_SANDS, 1);
		end
	end
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		if (global_var(GVAR_ENEMY_SHADY_SANDS) == 1) then begin
			hostile1 := 1;
		end
	end
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	if (local_var(5) == 1) then begin
		if (tile_distance_objs(self_obj, dude_obj) < 8) then begin
			call flee_dude;
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_local_var(5, 1);
	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_CURTIS, 100));
end

procedure map_enter_p_proc
begin
	home_tile := 22713;
	wake_time := 615;
	sleep_time := 2115;
	sleep_tile := 27702;
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 2);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_PEASANT);
	add_timer_event(self_obj, game_ticks(5), 1);
end

procedure pickup_p_proc
begin
	set_local_var(5, 1);
end

procedure talk_p_proc
begin
	if ((local_var(5) == 1) or (global_var(GVAR_ENEMY_SHADY_SANDS) == 1)) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, random(100, 105)), FLOAT_MSG_RED);
	end
	else begin
		start_gdialog(SCRIPT_CURTIS, self_obj, 4, -1, -1);
		gsay_start;
		if (local_var(3) == 1) then begin
			call Curtis02;
		end
		else begin
			call Curtis01;
		end
		gsay_end;
		end_dialogue;
	end
	if (local_var(2) == 1) then begin
		set_local_var(2, 2);
		give_exp_points(500);
		display_msg(message_str(SCRIPT_CURTIS, 200));
	end
end

procedure timed_event_p_proc
begin
	if (local_var(4) == 0) then begin
		if (random(0, 3) == 3) then begin
			anim(self_obj, 10, 0);
		end
		else begin
			call movement;
		end
	end
	add_timer_event(self_obj, game_ticks(random(3, 7)), 1);
end

procedure Curtis01
begin
	gsay_reply(SCRIPT_CURTIS, 101);
	giq_option(4, SCRIPT_CURTIS, 102, Curtis03, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_CURTIS, 103, CurtisEnd, NEUTRAL_REACTION);
end

procedure Curtis02
begin
	gsay_reply(SCRIPT_CURTIS, 104);
	giq_option(0, SCRIPT_MODREACT, 106, Curtis03, NEUTRAL_REACTION);
end

procedure Curtis03
begin
	gsay_reply(SCRIPT_CURTIS, 105);
	call Curtis03a;
end

procedure Curtis03a
begin
	if (local_var(3) == 0) then begin
		gsay_option(SCRIPT_CURTIS, 106, Curtis04, NEUTRAL_REACTION);
	end
	if (local_var(0) == 0) then begin
		giq_option(5, SCRIPT_CURTIS, 107, Curtis05, NEUTRAL_REACTION);
	end
	if (local_var(1) == 0) then begin
		giq_option(4, SCRIPT_CURTIS, 108, Curtis06, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_CURTIS, 109, CurtisEnd, NEUTRAL_REACTION);
end

procedure Curtis04
begin
	gsay_reply(SCRIPT_CURTIS, 110);
	set_local_var(3, 1);
	call Curtis03a;
end

procedure Curtis05
begin
	set_local_var(0, 1);
	gsay_reply(SCRIPT_CURTIS, 111);
	call Curtis03a;
end

procedure Curtis06
begin
	gsay_reply(SCRIPT_CURTIS, 112);
	giq_option(4, SCRIPT_CURTIS, 113, Curtis03, NEUTRAL_REACTION);
	if ((has_skill(dude_obj, 12) >= 40) and (local_var(1) == 0)) then begin
		giq_option(5, SCRIPT_CURTIS, 114, Curtis07, NEUTRAL_REACTION);
	end
end

procedure Curtis07
begin
	gsay_reply(SCRIPT_CURTIS, 115);
	giq_option(4, SCRIPT_CURTIS, 116, CurtisEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_CURTIS, 117, Curtis08, NEUTRAL_REACTION);
end

procedure Curtis08
begin
	gsay_message(SCRIPT_CURTIS, 118, NEUTRAL_REACTION);
	set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 2);
	set_local_var(1, 1);
	set_local_var(2, 1);
end

procedure CurtisEnd
begin
end

procedure flee_dude
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	while(LVar1 < 5) do begin
		if (tile_distance(tile_num(dude_obj), tile_num_in_direction(tile_num(self_obj), LVar1, 3)) > LVar2) then begin
			LVar0 := tile_num_in_direction(tile_num(self_obj), LVar1, 3);
			LVar2 := tile_distance(tile_num(dude_obj), LVar0);
		end
		LVar1 := LVar1 + 1;
	end
	animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_RUN);
end

procedure movement
begin
	variable LVar0 := 0;
	LVar0 := random(0, 5);
	new_tile := tile_num_in_direction(tile_num(self_obj), LVar0, 1);
	if (tile_distance(home_tile, new_tile) < 6) then begin
		animate_move_obj_to_tile(self_obj, new_tile, ANIMATE_WALK);
	end
end

procedure sleeping
begin
	if (local_var(4) == 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(4, 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(4, 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(4, 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(4, 1);
				end
			end
		end
	end
end