#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 damage_p_proc;
procedure Dan00;
procedure Dan01;
procedure Dan02;
procedure Dan03;
procedure Dan04;
procedure Dan05;
procedure Dan06;
procedure Dan07;
procedure Dan08;
procedure Dan09;
procedure Dan10;
procedure Dan11;
procedure Dan12;
procedure Dan13;
procedure Dan14;
procedure Dan15;
procedure Dan16;
procedure Dan17;
procedure Dan18;
procedure Dan19;
procedure Dan20;
procedure Dan21;
procedure Dan22;
procedure Dan23;
procedure Dan24;
procedure Dan25;
procedure Dan26;
procedure Dan27;
procedure Dan28;
procedure DanAtNight;
procedure SendToStart;
procedure SendToStreet;
procedure SendToCattle1;
procedure SendToCattle2;
procedure SendToSleep;
procedure DanEnd;

variable hostile1;
variable Only_Once := 1;
variable SetDayNight;
variable NightCount;
variable Talky;
variable WalkToTalker;
variable Sleeping;

import variable Billy_ptr;
import variable Dan_ptr;

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, 62);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_MERCHANT);
		Dan_ptr := self_obj;
	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
					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)) then begin
		if (SetDayNight == 0) then begin
			add_timer_event(self_obj, game_ticks(30), 3);
			SetDayNight := 1;
		end
	end
	if (map_var(0) == 1) then begin
		if (WalkToTalker == 0) then begin
			reg_anim_func(2, self_obj);
			reg_anim_func(1, 1);
			reg_anim_obj_move_to_tile(self_obj, 13891, -1);
			reg_anim_func(3, 0);
			WalkToTalker := 1;
		end
	end
	if (tile_num(self_obj) == 13891) then begin
		if (map_var(0) == 1) then begin
			if (Talky == 0) then begin
				call Dan27;
				Talky := 1;
				add_timer_event(self_obj, game_ticks(60 * 2), 1);
			end
		end
	end
end

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

procedure talk_p_proc
begin
	call Get_Reaction;
	if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
		call DanAtNight;
	end
	else begin
		if (local_var(4) == 1) then begin
			set_local_var(4, 2);
			if ((map_var(0) == 2) and (local_var(8) == 0)) then begin
				if (local_var(1) > 1) then begin
					start_gdialog(SCRIPT_DAN, self_obj, 4, -1, -1);
					gsay_start;
					call Dan15;
					gsay_end;
					end_dialogue;
				end
				else begin
					call Dan28;
				end
			end
			else begin
				call Dan28;
			end
		end
		else begin
			if (local_var(4) == 2) then begin
				if ((map_var(0) == 2) and (local_var(8) == 0)) then begin
					if (local_var(1) == 1) then begin
						start_gdialog(SCRIPT_DAN, self_obj, 4, -1, -1);
						gsay_start;
						call Dan16;
						gsay_end;
						end_dialogue;
					end
					else begin
						call Dan28;
					end
				end
				else begin
					call Dan28;
				end
			end
			else begin
				start_gdialog(SCRIPT_DAN, self_obj, 4, -1, -1);
				gsay_start;
				call Dan00;
				gsay_end;
				end_dialogue;
				set_local_var(4, 1);
			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_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
	set_global_var(GVAR_DESTROY_VATS_8, 1);
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_DAN, 100));
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
		if (Sleeping == 1) then begin
			reg_anim_func(2, self_obj);
			reg_anim_func(1, 1);
			reg_anim_obj_move_to_tile(self_obj, 12871, -1);
			reg_anim_func(3, 0);
			add_timer_event(self_obj, game_ticks(60 * 25), 2);
			Sleeping := 0;
		end
		else begin
			if (fixed_param == 1) then begin
				call SendToStart;
			end
			else begin
				if (fixed_param == 2) then begin
					call SendToStreet;
				end
				else begin
					if (fixed_param == 3) then begin
						call SendToCattle1;
					end
					else begin
						if (fixed_param == 4) then begin
							call SendToCattle2;
						end
					end
				end
			end
		end
	end
	else begin
		call SendToSleep;
		if (fixed_param == 4) then begin
			if (tile_distance_objs(self_obj, dude_obj) <= 10) then begin
				float_msg(self_obj, message_str(SCRIPT_DAN, 215), FLOAT_MSG_RED);
			end
		end
		else begin
			if (fixed_param == 5) then begin
				NightCount := 0;
				if (tile_distance_objs(self_obj, dude_obj) <= 10) then begin
					float_msg(self_obj, message_str(SCRIPT_DAN, 216), FLOAT_MSG_RED);
					hostile1 := 1;
				end
			end
		end
	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 Dan00
begin
	gsay_reply(SCRIPT_DAN, 102);
	giq_option(5, SCRIPT_DAN, 106, Dan11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 107, Dan01, NEUTRAL_REACTION);
	if (map_var(0) == 2) then begin
		giq_option(4, SCRIPT_DAN, 108, Dan14, NEUTRAL_REACTION);
		set_local_var(8, 1);
	end
	giq_option(-3, SCRIPT_DAN, 109, Dan02, NEUTRAL_REACTION);
end

procedure Dan01
begin
	gsay_reply(SCRIPT_DAN, 110);
	giq_option(4, SCRIPT_DAN, 111, Dan03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 112, Dan05, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_DAN, 113, Dan11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 114, DanEnd, NEUTRAL_REACTION);
end

procedure Dan02
begin
	gsay_message(SCRIPT_DAN, 115, BAD_REACTION);
end

procedure Dan03
begin
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_DAN, 118);
	end
	else begin
		gsay_reply(SCRIPT_DAN, 119);
	end
	giq_option(4, SCRIPT_DAN, 120, Dan04, BAD_REACTION);
	giq_option(5, SCRIPT_DAN, 121, Dan11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 122, DanEnd, NEUTRAL_REACTION);
end

procedure Dan04
begin
	gsay_reply(SCRIPT_DAN, 123);
	giq_option(4, SCRIPT_DAN, 124, Dan06, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 125, DanEnd, NEUTRAL_REACTION);
end

procedure Dan05
begin
	gsay_reply(SCRIPT_DAN, 126);
	giq_option(4, SCRIPT_DAN, 127, Dan06, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 128, DanEnd, NEUTRAL_REACTION);
end

procedure Dan06
begin
	set_local_var(5, 1);
	gsay_reply(SCRIPT_DAN, 129);
	giq_option(4, SCRIPT_DAN, 130, Dan07, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 131, Dan09, NEUTRAL_REACTION);
end

procedure Dan07
begin
	gsay_reply(SCRIPT_DAN, 132);
	giq_option(4, SCRIPT_DAN, 133, Dan10, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 134, Dan08, NEUTRAL_REACTION);
end

procedure Dan08
begin
	gsay_reply(SCRIPT_DAN, 135);
	giq_option(4, SCRIPT_DAN, 136, Dan09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 137, Dan11, NEUTRAL_REACTION);
end

procedure Dan09
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_DAN, 138, BAD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_DAN, 139, BAD_REACTION);
	end
end

procedure Dan10
begin
	gsay_message(SCRIPT_DAN, 141, BAD_REACTION);
	call combat;
end

procedure Dan11
begin
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_DAN, 142);
	end
	else begin
		gsay_reply(SCRIPT_DAN, message_str(SCRIPT_DAN, 142) + " " + message_str(SCRIPT_DAN, 143));
	end
	giq_option(7, SCRIPT_DAN, 144, Dan12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 145, Dan13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 146, DanEnd, NEUTRAL_REACTION);
end

procedure Dan12
begin
	gsay_reply(SCRIPT_DAN, 147);
	giq_option(4, SCRIPT_DAN, 148, DanEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 149, DanEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_DAN, 150, Dan13, NEUTRAL_REACTION);
end

procedure Dan13
begin
	gsay_message(SCRIPT_DAN, 151, NEUTRAL_REACTION);
end

procedure Dan14
begin
	gsay_reply(SCRIPT_DAN, 152);
	giq_option(4, SCRIPT_DAN, 153, Dan17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 154, DanEnd, NEUTRAL_REACTION);
end

procedure Dan15
begin
	gsay_reply(SCRIPT_DAN, 155);
	giq_option(5, SCRIPT_DAN, 156, Dan11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 157, Dan01, NEUTRAL_REACTION);
	if (map_var(0) == 2) then begin
		giq_option(4, SCRIPT_DAN, 158, Dan14, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_DAN, 159, DanEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_DAN, 160, Dan02, NEUTRAL_REACTION);
end

procedure Dan16
begin
	gsay_reply(SCRIPT_DAN, 161);
	if (map_var(0) == 2) then begin
		giq_option(4, SCRIPT_DAN, 162, Dan14, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_DAN, 163, DanEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_DAN, 164, Dan02, NEUTRAL_REACTION);
end

procedure Dan17
begin
	gsay_reply(SCRIPT_DAN, 168);
	giq_option(4, SCRIPT_DAN, 169, Dan19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 170, Dan18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 171, DanEnd, NEUTRAL_REACTION);
end

procedure Dan18
begin
	gsay_reply(SCRIPT_DAN, 172);
	giq_option(5, SCRIPT_DAN, 173, Dan19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 174, DanEnd, NEUTRAL_REACTION);
end

procedure Dan19
begin
	gsay_reply(SCRIPT_DAN, 175);
	giq_option(4, SCRIPT_DAN, 176, Dan20, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 177, DanEnd, 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_DAN, 178, Dan20, BAD_REACTION);
	end
	giq_option(4, SCRIPT_DAN, 179, Dan22, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 180, Dan23, NEUTRAL_REACTION);
end

procedure Dan20
begin
	gsay_reply(SCRIPT_DAN, 181);
	giq_option(4, SCRIPT_DAN, 182, Dan21, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 183, Dan21, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 184, Dan24, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 185, Dan25, NEUTRAL_REACTION);
end

procedure Dan21
begin
	gsay_reply(SCRIPT_DAN, 186);
	giq_option(4, SCRIPT_DAN, 187, DanEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 188, DanEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_DAN, 189, DanEnd, NEUTRAL_REACTION);
end

procedure Dan22
begin
	set_local_var(7, 1);
	set_map_var(1, 1);
	gsay_message(SCRIPT_DAN, 190, NEUTRAL_REACTION);
end

procedure Dan23
begin
	gsay_message(SCRIPT_DAN, 191, NEUTRAL_REACTION);
end

procedure Dan24
begin
	gsay_reply(SCRIPT_DAN, 192);
	giq_option(4, SCRIPT_DAN, 193, DanEnd, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 194, DanEnd, BAD_REACTION);
	giq_option(4, SCRIPT_DAN, 195, DanEnd, BAD_REACTION);
end

procedure Dan25
begin
	gsay_message(SCRIPT_DAN, 196, NEUTRAL_REACTION);
end

procedure Dan26
begin
	if (tile_num(Billy_ptr) != 9858) then begin
		float_msg(self_obj, message_str(SCRIPT_DAN, random(197, 199)), FLOAT_MSG_RED);
	end
end

procedure Dan27
begin
	float_msg(self_obj, message_str(SCRIPT_DAN, random(200, 203)), FLOAT_MSG_RED);
	set_map_var(0, 2);
end

procedure Dan28
begin
	float_msg(self_obj, message_str(SCRIPT_DAN, random(208, 213)), FLOAT_MSG_RED);
end

procedure DanAtNight
begin
	if (NightCount == 0) then begin
		float_msg(self_obj, message_str(SCRIPT_DAN, 214), FLOAT_MSG_RED);
		NightCount := 1;
		add_timer_event(self_obj, game_ticks(5), 4);
		add_timer_event(self_obj, game_ticks(10), 5);
	end
end

procedure SendToStart
begin
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_tile(self_obj, 15522, -1);
	reg_anim_func(3, 0);
	add_timer_event(self_obj, game_ticks(60 * 12), 2);
end

procedure SendToStreet
begin
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_tile(self_obj, 15295, -1);
	reg_anim_func(3, 0);
	add_timer_event(self_obj, game_ticks(60 * 2), 3);
end

procedure SendToCattle1
begin
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_tile(self_obj, 12871, -1);
	reg_anim_func(3, 0);
	if (random(1, 10) >= 6) then begin
		add_timer_event(self_obj, game_ticks(60 * 3), 4);
	end
	else begin
		add_timer_event(self_obj, game_ticks(60 * 4), 1);
	end
end

procedure SendToCattle2
begin
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_tile(self_obj, 12871, -1);
	reg_anim_func(3, 0);
	add_timer_event(self_obj, game_ticks(60 * 4), 1);
end

procedure SendToSleep
begin
	if (Sleeping == 0) then begin
		reg_anim_func(2, self_obj);
		reg_anim_func(1, 1);
		reg_anim_obj_move_to_tile(self_obj, 14932, -1);
		reg_anim_func(3, 0);
		SetDayNight := 0;
		Sleeping := 1;
	end
end

procedure DanEnd
begin
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