#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure damage_p_proc;
procedure destroy_p_proc;
procedure map_enter_p_proc;
procedure map_update_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure PeasantC00;
procedure PeasantC00a;
procedure PeasantC01;
procedure PeasantC02;
procedure PeasantC03;
procedure PeasantC03a;
procedure PeasantC04;
procedure PeasantC05;
procedure PeasantC06;
procedure PeasantC07;
procedure PeasantCEnd;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;

import variable removal_ptr;


procedure start
begin
	if (script_action == destroy_proc) then begin
		call destroy_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_update_proc) then begin
				call map_update_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
				end
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
		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
end

procedure map_enter_p_proc
begin
	if (map_var(2) == 1) then begin
		set_obj_visibility(self_obj, 1);
		removal_ptr := self_obj;
	end
	else begin
		sleep_time := random(2215, 2230);
		wake_time := random(715, 730);
		if ((game_time / (10 * 60 * 60 * 24)) < 80) then begin
			sleep_tile := 15275;
			home_tile := 14267;
		end
		else begin
			sleep_tile := 23524;
			home_tile := 23923;
		end
	end
end

procedure map_update_p_proc
begin
	call sleeping;
end

procedure pickup_p_proc
begin
	set_local_var(1, 1);
	float_msg(self_obj, message_str(SCRIPT_PEASANTC, 124), FLOAT_MSG_RED);
end

procedure talk_p_proc
begin
	if (local_var(2) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 166), FLOAT_MSG_NORMAL);
	end
	else begin
		if (local_var(1) == 1) then begin
			float_msg(self_obj, message_str(SCRIPT_PEASANTC, 124), FLOAT_MSG_RED);
		end
		else begin
			if ((game_time / (10 * 60 * 60 * 24)) < 80) then begin
				set_local_var(0, 1);
				if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
					float_msg(self_obj, message_str(SCRIPT_PEASANTC, 100), FLOAT_MSG_RED);
				end
				else begin
					if (global_var(GVAR_PLAYER_REPUTATION) < -10) then begin
						float_msg(self_obj, message_str(SCRIPT_PEASANTC, 101), FLOAT_MSG_LIGHT_RED);
					end
					else begin
						if (dude_is_male) then begin
							float_msg(self_obj, message_str(SCRIPT_PEASANTC, random(102, 103)), FLOAT_MSG_GREEN);
						end
						else begin
							float_msg(self_obj, message_str(SCRIPT_PEASANTC, random(102, 104)), FLOAT_MSG_GREEN);
						end
					end
				end
			end
			else begin
				set_map_var(2, 1);
				start_gdialog(SCRIPT_PEASANTC, self_obj, 4, -1, -1);
				gsay_start;
				call PeasantC00;
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure PeasantC00
begin
	gsay_reply(SCRIPT_PEASANTC, 105);
	giq_option(4, SCRIPT_PEASANTC, 106, PeasantCEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_PEASANTC, 107, PeasantC00a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_PEASANTC, 108, PeasantC01, NEUTRAL_REACTION);
end

procedure PeasantC00a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call PeasantC03;
	end
	else begin
		call PeasantC02;
	end
end

procedure PeasantC01
begin
	gsay_message(SCRIPT_PEASANTC, 109, NEUTRAL_REACTION);
end

procedure PeasantC02
begin
	gsay_message(SCRIPT_PEASANTC, 110, NEUTRAL_REACTION);
end

procedure PeasantC03
begin
	gsay_reply(SCRIPT_PEASANTC, 111);
	giq_option(5, SCRIPT_PEASANTC, 112, PeasantC04, BAD_REACTION);
	giq_option(6, SCRIPT_PEASANTC, 113, PeasantC03a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_PEASANTC, 106, PeasantCEnd, NEUTRAL_REACTION);
end

procedure PeasantC03a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -10))) then begin
		call PeasantC06;
	end
	else begin
		call PeasantC05;
	end
end

procedure PeasantC04
begin
	set_local_var(1, 1);
	gsay_message(SCRIPT_PEASANTC, 114, BAD_REACTION);
end

procedure PeasantC05
begin
	gsay_message(SCRIPT_PEASANTC, 115, NEUTRAL_REACTION);
end

procedure PeasantC06
begin
	variable LVar0 := 0;
	LVar0 := message_str(SCRIPT_PEASANTC, 116);
	if (global_var(GVAR_KILLIAN_DEAD) == 1) then begin
		if (global_var(GVAR_GIZMO_DEAD) == 1) then begin
			LVar0 := LVar0 + message_str(SCRIPT_PEASANTC, 120);
		end
		else begin
			LVar0 := LVar0 + message_str(SCRIPT_PEASANTC, 119);
		end
	end
	else begin
		if (global_var(GVAR_GIZMO_DEAD) == 1) then begin
			LVar0 := LVar0 + message_str(SCRIPT_PEASANTC, 118);
		end
		else begin
			LVar0 := LVar0 + message_str(SCRIPT_PEASANTC, 117);
		end
	end
	gsay_reply(SCRIPT_PEASANTC, LVar0);
	giq_option(5, SCRIPT_PEASANTC, 121, PeasantC07, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_PEASANTC, 122, PeasantCEnd, NEUTRAL_REACTION);
end

procedure PeasantC07
begin
	gsay_message(SCRIPT_PEASANTC, 123, NEUTRAL_REACTION);
end

procedure PeasantCEnd
begin
end

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