#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 damage_p_proc;
procedure look_at_p_proc;
procedure combat_p_proc;
procedure timed_event_p_proc;
procedure Billy00;
procedure Billy01;
procedure Billy02;
procedure Billy03;
procedure Billy04;
procedure Billy05;
procedure Billy06;
procedure Billy07;
procedure Billy08;
procedure Billy09;
procedure Billy10;
procedure Billy11;
procedure Billy12;
procedure Billy13;
procedure Billy14;
procedure Billy14a;
procedure Billy15;
procedure Billy15a;
procedure Billy16;
procedure Billy17;
procedure Billy18;
procedure Billy19;
procedure Billy20;
procedure Billy21;
procedure Billy22;
procedure Billy23;
procedure BillyRunAway;
procedure SendToStart;
procedure SendToLeft;
procedure SendToRight;
procedure SendToLower;
procedure SendToSleep;
procedure BillyEnd;

variable hostile1;
variable Only_Once := 1;
variable Once_Which_One;
variable Runaway;
variable Sleeping;
variable SetDayNight;

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_HUB_PEASANTS);
		Billy_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 == combat_proc) then begin
							call combat_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

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 (Runaway == 0) then begin
		if (global_var(GVAR_DESTROY_VATS_8) == 1) then begin
			call BillyRunAway;
		end
		if ((game_time_hour >= 600) and (game_time_hour < 700)) then begin
			if (SetDayNight != 1) then begin
				add_timer_event(self_obj, game_ticks(10), 1);
				SetDayNight := 1;
			end
		end
	end
end

procedure pickup_p_proc
begin
	if (source_obj == dude_obj) then begin
		call Billy11;
	end
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (local_var(6) == 1) then begin
		call Billy10;
	end
	else begin
		if (obj_can_see_obj(self_obj, Dan_ptr) or obj_can_hear_obj(self_obj, Dan_ptr)) then begin
			if (map_var(0) != 2) then begin
				set_map_var(0, 1);
			end
		end
		if (local_var(4) == 1) then begin
			if (map_var(1) == 1) then begin
				call Billy21;
			end
			else begin
				if (map_var(0) == 2) then begin
					call Billy14;
				end
				else begin
					call Billy23;
				end
			end
		end
		else begin
			call Billy00;
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_CHILDKILLER_REPUTATION, global_var(GVAR_CHILDKILLER_REPUTATION) + 1);
		set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
	end
	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 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 look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_BILLY, 100));
end

procedure combat_p_proc
begin
	set_local_var(6, 1);
end

procedure timed_event_p_proc
begin
	if (Runaway == 0) then 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, 13889, -1);
				reg_anim_func(3, 0);
				add_timer_event(self_obj, game_ticks(60 * 5), 2);
				Sleeping := 0;
			end
			else begin
				if (fixed_param == 1) then begin
					call SendToStart;
				end
				if (fixed_param == 2) then begin
					call SendToRight;
				end
				if (fixed_param == 3) then begin
					call SendToLeft;
				end
				if (fixed_param == 4) then begin
					call SendToLower;
				end
			end
		end
		else begin
			call SendToSleep;
		end
	end
end

procedure Billy00
begin
	set_local_var(4, 1);
	start_gdialog(SCRIPT_BILLY, self_obj, 4, -1, -1);
	gsay_start;
	gsay_reply(SCRIPT_BILLY, 101);
	giq_option(4, SCRIPT_BILLY, 102, Billy04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 103, Billy01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 104, BillyEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_BILLY, 105, Billy06, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_BILLY, 106, Billy06, NEUTRAL_REACTION);
	gsay_end;
	end_dialogue;
end

procedure Billy01
begin
	set_local_var(11, 1);
	gsay_reply(SCRIPT_BILLY, 107);
	giq_option(4, SCRIPT_BILLY, 108, Billy02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 109, Billy03, NEUTRAL_REACTION);
end

procedure Billy02
begin
	set_global_var(GVAR_MISSING_CARAVAN, 1);
	gsay_message(SCRIPT_BILLY, 110, NEUTRAL_REACTION);
end

procedure Billy03
begin
	gsay_message(SCRIPT_BILLY, 111, NEUTRAL_REACTION);
end

procedure Billy04
begin
	set_local_var(10, 1);
	gsay_reply(SCRIPT_BILLY, 112);
	giq_option(4, SCRIPT_BILLY, 113, Billy12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 114, Billy05, NEUTRAL_REACTION);
end

procedure Billy05
begin
	gsay_reply(SCRIPT_BILLY, 115);
	giq_option(4, SCRIPT_BILLY, 116, Billy08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 117, Billy07, GOOD_REACTION);
	giq_option(4, SCRIPT_BILLY, 118, BillyEnd, NEUTRAL_REACTION);
end

procedure Billy06
begin
	gsay_message(SCRIPT_BILLY, 119, NEUTRAL_REACTION);
end

procedure Billy07
begin
	gsay_message(SCRIPT_BILLY, 120, GOOD_REACTION);
end

procedure Billy08
begin
	gsay_reply(SCRIPT_BILLY, 121);
	giq_option(4, SCRIPT_BILLY, 122, BillyEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 123, Billy09, GOOD_REACTION);
end

procedure Billy09
begin
	gsay_message(SCRIPT_BILLY, 124, GOOD_REACTION);
end

procedure Billy10
begin
	if (Once_Which_One == 0) then begin
		Once_Which_One := 1;
		float_msg(self_obj, message_str(SCRIPT_BILLY, 125), FLOAT_MSG_YELLOW);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_BILLY, random(126, 129)), FLOAT_MSG_YELLOW);
	end
	animate_move_obj_to_tile(self_obj, 15890, ANIMATE_RUN);
end

procedure Billy11
begin
	if (local_var(5) == 0) then begin
		set_local_var(5, 1);
		float_msg(self_obj, message_str(SCRIPT_BILLY, 130), FLOAT_MSG_YELLOW);
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_BILLY, 131), FLOAT_MSG_YELLOW);
	end
end

procedure Billy12
begin
	gsay_reply(SCRIPT_BILLY, 132);
	giq_option(4, SCRIPT_BILLY, 133, Billy13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 134, BillyEnd, NEUTRAL_REACTION);
end

procedure Billy13
begin
	gsay_message(SCRIPT_BILLY, 135, NEUTRAL_REACTION);
end

procedure Billy14
begin
	if (local_var(7) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_BILLY, 142), FLOAT_MSG_YELLOW);
	end
	else begin
		set_local_var(7, 1);
		start_gdialog(SCRIPT_BILLY, self_obj, 4, -1, -1);
		gsay_start;
		gsay_reply(SCRIPT_BILLY, 136);
		giq_option(4, SCRIPT_BILLY, 137, Billy15, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BILLY, 138, Billy14a, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BILLY, 139, BillyEnd, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_BILLY, 140, Billy06, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_BILLY, 141, Billy06, NEUTRAL_REACTION);
		gsay_end;
		end_dialogue;
	end
end

procedure Billy14a
begin
	if (item_caps_total(dude_obj) > 50) then begin
		item_caps_adjust(dude_obj, -50);
		item_caps_adjust(self_obj, 50);
		call Billy19;
	end
	else begin
		call Billy20;
	end
end

procedure Billy15
begin
	gsay_reply(SCRIPT_BILLY, 143);
	giq_option(4, SCRIPT_BILLY, 144, Billy15a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BILLY, 145, Billy18, BAD_REACTION);
	giq_option(4, SCRIPT_BILLY, 146, BillyEnd, NEUTRAL_REACTION);
end

procedure Billy15a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Billy16;
	end
	else begin
		call Billy17;
	end
end

procedure Billy16
begin
	gsay_message(SCRIPT_BILLY, 147, NEUTRAL_REACTION);
end

procedure Billy17
begin
	gsay_message(SCRIPT_BILLY, 148, BAD_REACTION);
end

procedure Billy18
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_BILLY, 149, BAD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_BILLY, 150, BAD_REACTION);
	end
end

procedure Billy19
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_BILLY, 151, GOOD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_BILLY, 152, GOOD_REACTION);
	end
	call BillyRunAway;
end

procedure Billy20
begin
	gsay_message(SCRIPT_BILLY, 153, GOOD_REACTION);
end

procedure Billy21
begin
	if (local_var(8) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_BILLY, random(158, 164)), FLOAT_MSG_YELLOW);
	end
	else begin
		set_local_var(8, 1);
		start_gdialog(SCRIPT_BILLY, self_obj, 4, -1, -1);
		gsay_start;
		gsay_reply(SCRIPT_BILLY, 154);
		giq_option(4, SCRIPT_BILLY, 155, Billy22, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BILLY, 156, BillyEnd, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_BILLY, 157, Billy06, NEUTRAL_REACTION);
		gsay_end;
		end_dialogue;
	end
end

procedure Billy22
begin
	gsay_message(SCRIPT_BILLY, 165, NEUTRAL_REACTION);
end

procedure Billy23
begin
	if (local_var(9) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_BILLY, random(172, 178)), FLOAT_MSG_YELLOW);
	end
	else begin
		set_local_var(9, 1);
		start_gdialog(SCRIPT_BILLY, self_obj, 4, -1, -1);
		gsay_start;
		gsay_reply(SCRIPT_BILLY, 166);
		if (local_var(10) == 0) then begin
			giq_option(4, SCRIPT_BILLY, 167, Billy04, NEUTRAL_REACTION);
		end
		if (local_var(11) == 4) then begin
			giq_option(4, SCRIPT_BILLY, 168, Billy01, NEUTRAL_REACTION);
		end
		giq_option(4, SCRIPT_BILLY, 169, BillyEnd, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_BILLY, 170, Billy06, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_BILLY, 171, Billy06, NEUTRAL_REACTION);
		gsay_end;
		end_dialogue;
	end
end

procedure BillyRunAway
begin
	Runaway := 1;
	animate_move_obj_to_tile(self_obj, 9858, ANIMATE_RUN);
end

procedure SendToStart
begin
	if (random(1, 10) >= 7) then begin
		reg_anim_func(2, self_obj);
		reg_anim_func(1, 1);
		reg_anim_obj_move_to_tile(self_obj, 13889, -1);
		reg_anim_func(3, 0);
	end
	else begin
		animate_move_obj_to_tile(self_obj, 13889, ANIMATE_RUN);
	end
	add_timer_event(self_obj, game_ticks(120), 2);
end

procedure SendToLeft
begin
	if (random(1, 10) >= 7) then begin
		reg_anim_func(2, self_obj);
		reg_anim_func(1, 1);
		reg_anim_obj_move_to_tile(self_obj, 13889, -1);
		reg_anim_func(3, 0);
	end
	else begin
		animate_move_obj_to_tile(self_obj, 13889, ANIMATE_RUN);
	end
	add_timer_event(self_obj, game_ticks(60), 4);
end

procedure SendToRight
begin
	if (random(1, 10) >= 7) then begin
		reg_anim_func(2, self_obj);
		reg_anim_func(1, 1);
		reg_anim_obj_move_to_tile(self_obj, 12072, -1);
		reg_anim_func(3, 0);
	end
	else begin
		animate_move_obj_to_tile(self_obj, 12072, ANIMATE_RUN);
	end
	add_timer_event(self_obj, game_ticks(60), 3);
end

procedure SendToLower
begin
	if (random(1, 10) >= 7) then begin
		reg_anim_func(2, self_obj);
		reg_anim_func(1, 1);
		reg_anim_obj_move_to_tile(self_obj, 13872, -1);
		reg_anim_func(3, 0);
	end
	else begin
		animate_move_obj_to_tile(self_obj, 13872, ANIMATE_RUN);
	end
	add_timer_event(self_obj, game_ticks(90), 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, 16285, -1);
		reg_anim_func(3, 0);
		Sleeping := 1;
		SetDayNight := 0;
	end
end

procedure BillyEnd
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