#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0;
variable GVar1 := 1;
variable GVar2;
variable GVar3;

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 Richie00;
procedure Richie01;
procedure Richie01a;
procedure Richie01b;
procedure Richie02;
procedure Richie03;
procedure Richie04;
procedure Richie05;
procedure Richie06;
procedure Richie07;
procedure Richie08;
procedure Richie09;
procedure Richie10;
procedure Richie11;
procedure Richie12;
procedure Richie13;
procedure RichieLeave;
procedure RichieEnd;

import variable Richie_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;


procedure start
begin
	if (GVar1) then begin
		GVar1 := 0;
		Richie_Ptr := self_obj;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 73);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BERZERKER);
	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
				end
			end
		end
	end
end

procedure combat
begin
	GVar0 := 1;
end

procedure critter_p_proc
begin
	if (GVar0) then begin
		GVar0 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
end

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

procedure talk_p_proc
begin
	call Get_Reaction;
	if ((map_var(23) == 1) or (map_var(15) > 0) or (map_var(18) == 2)) then begin
		call Richie00;
	end
	else begin
		if (map_var(20) == 0) then begin
			set_map_var(20, 1);
			start_gdialog(SCRIPT_RICHIE, self_obj, 4, -1, -1);
			gsay_start;
			call Richie01;
			gsay_end;
			end_dialogue;
		end
		else begin
			call Richie02;
		end
	end
	if (GVar2) then begin
		animate_move_obj_to_tile(self_obj, 27450, ANIMATE_WALK);
	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_RICHIE, 100));
end

procedure Richie00
begin
	float_msg(self_obj, message_str(SCRIPT_RICHIE, 101), FLOAT_MSG_RED);
	call combat;
end

procedure Richie01
begin
	gsay_reply(SCRIPT_RICHIE, 102);
	giq_option(7, SCRIPT_RICHIE, 103, Richie01a, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_RICHIE, 104, Richie01b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_RICHIE, 105, Richie06, BAD_REACTION);
	giq_option(4, SCRIPT_RICHIE, 106, Richie07, BAD_REACTION);
	giq_option(4, SCRIPT_RICHIE, 107, Richie08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_RICHIE, 108, Richie09, BAD_REACTION);
	giq_option(-3, SCRIPT_RICHIE, 109, Richie09, NEUTRAL_REACTION);
end

procedure Richie01a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Richie03;
	end
	else begin
		call Richie04;
	end
end

procedure Richie01b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Richie05;
	end
	else begin
		call Richie04;
	end
end

procedure Richie02
begin
	float_msg(self_obj, message_str(SCRIPT_RICHIE, 110), FLOAT_MSG_RED);
end

procedure Richie03
begin
	gsay_message(SCRIPT_RICHIE, 111, GOOD_REACTION);
	GVar2 := 1;
end

procedure Richie04
begin
	gsay_message(SCRIPT_RICHIE, 112, BAD_REACTION);
end

procedure Richie05
begin
	gsay_message(SCRIPT_RICHIE, 113, GOOD_REACTION);
	GVar2 := 1;
end

procedure Richie06
begin
	gsay_message(SCRIPT_RICHIE, 114, BAD_REACTION);
end

procedure Richie07
begin
	gsay_message(SCRIPT_RICHIE, 115, BAD_REACTION);
	call combat;
end

procedure Richie08
begin
	gsay_message(SCRIPT_RICHIE, 116, NEUTRAL_REACTION);
end

procedure Richie09
begin
	gsay_reply(SCRIPT_RICHIE, 117);
	giq_option(-3, SCRIPT_RICHIE, 118, Richie10, BAD_REACTION);
	giq_option(-3, SCRIPT_RICHIE, 119, Richie11, GOOD_REACTION);
	giq_option(-3, SCRIPT_RICHIE, 120, Richie12, NEUTRAL_REACTION);
end

procedure Richie10
begin
	gsay_message(SCRIPT_RICHIE, 121, NEUTRAL_REACTION);
end

procedure Richie11
begin
	gsay_message(SCRIPT_RICHIE, 123, GOOD_REACTION);
end

procedure Richie12
begin
	gsay_reply(SCRIPT_RICHIE, 123);
	giq_option(-3, SCRIPT_RICHIE, 124, Richie10, BAD_REACTION);
	giq_option(-3, SCRIPT_RICHIE, 125, Richie11, GOOD_REACTION);
	giq_option(-3, SCRIPT_RICHIE, 126, Richie13, NEUTRAL_REACTION);
end

procedure Richie13
begin
	gsay_message(SCRIPT_RICHIE, 127, NEUTRAL_REACTION);
end

procedure RichieLeave
begin
end

procedure RichieEnd
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
	GVar3 := message_str(SCRIPT_MODREACT, random(100, 105));
end