#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure map_enter_p_proc;
procedure map_exit_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure set_sleep_tile;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable dest_tile1 := 7000;
variable hostile1;
variable sleeping_disabled;
variable waypoint;

import variable Neal_ptr;
import variable Skul_target;
import variable Trish_ptr;


procedure start
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 == map_exit_proc) then begin
						call map_exit_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 critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (cur_map_index == MAP_JUNKCSNO) then begin
			if ((map_var(2) == 2) and (waypoint == 0)) then begin
				waypoint := 1;
				dest_tile1 := 26297;
				sleeping_disabled := 1;
			end
			else begin
				if ((global_var(GVAR_DESTROY_VATS_10) == 1) and (waypoint == 0)) then begin
					waypoint := 1;
					sleeping_disabled := 1;
					add_timer_event(self_obj, game_ticks(8), 3);
				end
			end
			if (waypoint != 0) then begin
				if (tile_distance(tile_num(self_obj), dest_tile1) > 3) then begin
					animate_move_obj_to_tile(self_obj, dest_tile1, ANIMATE_WALK);
				end
				else begin
					if (waypoint == 1) then begin
						if (map_var(2) == 0) then begin
							dest_tile1 := 33519;
						end
						else begin
							dest_tile1 := 20076;
							waypoint := 2;
						end
					end
				end
			end
			if (sleeping_disabled == 0) then begin
				call sleeping;
			end
		end
		else begin
			if (cur_map_index == MAP_JUNKKILL) then begin
				if ((global_var(GVAR_BUST_SKULZ) != 2) and (sleeping_disabled == 0)) then begin
					call sleeping;
				end
			end
		end
	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_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
	if (home_tile == 20082) then begin
		Skul_target := 0;
	end
	if ((cur_map_index == MAP_JUNKCSNO) and (map_var(2) == 1)) then begin
		set_map_var(0, map_var(0) - 1);
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_GENSKULZ, 100));
end

procedure map_enter_p_proc
begin
	if (not(local_var(5))) then begin
		set_local_var(5, tile_num(self_obj));
	end
	home_tile := local_var(5);
	if (cur_map_index == MAP_JUNKENT) then begin
		if (global_var(GVAR_BUST_SKULZ) == 2) then begin
			set_obj_visibility(self_obj, 0);
		end
		else begin
			set_obj_visibility(self_obj, 1);
		end
	end
	if (cur_map_index != MAP_JUNKCSNO) then begin
		if (cur_map_index == MAP_JUNKENT) then begin
			if (global_var(GVAR_BUST_SKULZ) == 2) then begin
				add_timer_event(self_obj, game_ticks(random(30, 60)), 1);
			end
		end
		else begin
			if (cur_map_index == MAP_JUNKKILL) then begin
				add_timer_event(self_obj, game_ticks(random(30, 60)), 1);
			end
		end
	end
	else begin
		if ((global_var(GVAR_DESTROY_VATS_10) == 1) and (map_var(2) == 0)) then begin
			destroy_object(self_obj);
		end
	end
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 14);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_SKULZ);
	if (global_var(GVAR_BUST_SKULZ) == 2) then begin
		if (cur_map_index == MAP_JUNKENT) then begin
			set_obj_visibility(self_obj, 0);
		end
		else begin
			destroy_object(self_obj);
		end
	end
	call set_sleep_tile;
	if ((home_tile == 20082) and (cur_map_index == MAP_JUNKCSNO)) then begin
		Skul_target := self_obj;
	end
end

procedure map_exit_p_proc
begin
	if (cur_map_index == MAP_JUNKCSNO) then begin
		if ((global_var(GVAR_DESTROY_VATS_10) == 1) and (map_var(2) == 0)) then begin
			destroy_object(self_obj);
		end
	end
end

procedure pickup_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_GENSKULZ, 101), FLOAT_MSG_RED);
	hostile1 := 1;
end

procedure talk_p_proc
begin
	variable LVar0 := 0;
	if (cur_map_index == MAP_JUNKENT) then begin
		set_local_var(6, 104);
	end
	else begin
		if ((cur_map_index == MAP_JUNKCSNO) and (map_var(2) == 2)) then begin
			set_local_var(6, 112);
		end
		else begin
			if (local_var(6) == 0) then begin
				set_local_var(6, random(102, 105));
			end
		end
	end
	LVar0 := message_str(SCRIPT_GENSKULZ, local_var(6));
	if (local_var(4) == 1) then begin
		LVar0 := message_str(SCRIPT_GENSKULZ, 106);
	end
	float_msg(self_obj, LVar0, FLOAT_MSG_NORMAL);
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		if ((local_var(4) == 0) and (combat_is_initialized == 0)) then begin
			if (random(0, 1)) then begin
				float_msg(self_obj, message_str(SCRIPT_GENSKULZ, random(107, 112)), FLOAT_MSG_NORMAL);
			end
			else begin
				animate_move_obj_to_tile(self_obj, tile_num_in_direction(local_var(5), random(0, 3), random(0, 5)), ANIMATE_WALK);
			end
		end
		add_timer_event(self_obj, game_ticks(random(30, 60)), 1);
	end
	else begin
		if ((fixed_param == 2) and (Trish_ptr != 0) and (Neal_ptr != 0)) then begin
			float_msg(self_obj, message_str(SCRIPT_SHARK, 102), FLOAT_MSG_RED);
			reg_anim_func(2, self_obj);
			reg_anim_func(1, 1);
			reg_anim_obj_move_to_obj(self_obj, Trish_ptr, -1);
			reg_anim_animate(self_obj, 16, -1);
			reg_anim_animate(Trish_ptr, 20, 4);
			reg_anim_animate(Trish_ptr, 48, -1);
			reg_anim_animate(Trish_ptr, 37, -1);
			reg_anim_obj_run_to_tile(Trish_ptr, 19889, -1);
			reg_anim_func(3, 0);
			add_timer_event(Neal_ptr, game_ticks(3), 1);
		end
		else begin
			if (fixed_param == 3) then begin
				dest_tile1 := 26297;
				sleeping_disabled := 1;
			end
		end
	end
end

procedure set_sleep_tile
begin
	if (cur_map_index == MAP_JUNKKILL) then begin
		if (home_tile == 13502) then begin
			sleep_tile := 13093;
		end
		else begin
			if (home_tile == 13516) then begin
				sleep_tile := 13719;
			end
			else begin
				if (home_tile == 13717) then begin
					sleep_tile := 13721;
				end
			end
		end
		sleep_time := random(2200, 2215);
		wake_time := random(800, 815);
	end
	else begin
		if (cur_map_index == MAP_JUNKCSNO) then begin
			sleep_tile := 7000;
			sleep_time := 300;
			wake_time := 1600;
			night_person := 1;
		end
		else begin
			if (cur_map_index == MAP_JUNKENT) then begin
				sleep_tile := home_tile;
				sleep_time := 2200;
				wake_time := 1000;
			end
		end
	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