#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 boslori00;
procedure boslori01;
procedure boslori02;
procedure boslori03;
procedure boslori04;
procedure boslori05;
procedure boslori06;
procedure boslori07;
procedure boslori08;
procedure boslori09;
procedure boslori10;
procedure boslori11;
procedure boslori12;
procedure boslori13;
procedure boslori14;
procedure boslori15;
procedure boslori16;
procedure boslori17;
procedure boslori18;
procedure boslori19;
procedure boslori20;
procedure Dumb02;
procedure Dumb03;
procedure Dumb04;
procedure Dumb05;
procedure Dumb06;
procedure Dumb06a;
procedure raisiq;
procedure boslori22;
procedure bosloriend;
procedure sorry;
procedure do_dialog;

variable hostile1;
variable only_once := 1;
variable heal;
variable cost;
variable money;
variable temp;
variable rad_level;
variable Hurting;
variable armor;
variable bag_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, 44);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_SCRIBE);
	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
	hostile1 := 1;
end

procedure critter_p_proc
begin
	if (global_var(GVAR_ENEMY_BROTHERHOOD)) then begin
		hostile1 := 1;
	end
	if (global_var(GVAR_ENEMY_BROTHERHOOD)) then begin
		hostile1 := 1;
	end
	if (tile_distance_objs(self_obj, dude_obj) > 12) then begin
		hostile1 := 0;
	end
	if (hostile1) then begin
		set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
		hostile1 := 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
		hostile1 := 1;
	end
end

procedure talk_p_proc
begin
	call Get_Reaction;
	start_gdialog(SCRIPT_BOSLORI, self_obj, 4, -1, -1);
	gsay_start;
	call do_dialog;
	gsay_end;
	end_dialogue;
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
	rm_timer_event(self_obj);
	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_BOSLORI, 100));
end

procedure boslori00
begin
	heal := get_critter_stat(dude_obj, 7) - get_critter_stat(dude_obj, 35);
	gsay_reply(SCRIPT_BOSLORI, 102);
	if (heal > 0) then begin
		giq_option(4, SCRIPT_BOSLORI, 103, boslori04, NEUTRAL_REACTION);
	end
	giq_option(5, SCRIPT_BOSLORI, 104, boslori05, NEUTRAL_REACTION);
	if (local_var(5) != 1) then begin
		giq_option(4, SCRIPT_BOSLORI, 105, boslori07, NEUTRAL_REACTION);
	end
	else begin
		giq_option(4, SCRIPT_BOSLORI, 106, boslori09, NEUTRAL_REACTION);
	end
	if (local_var(11) == 1) then begin
		giq_option(-3, SCRIPT_BOSLORI, 107, boslori01, NEUTRAL_REACTION);
	end
	else begin
		giq_option(-3, SCRIPT_BOSLORI, 107, Dumb02, NEUTRAL_REACTION);
	end
end

procedure boslori01
begin
	gsay_message(SCRIPT_BOSLORI, 108, NEUTRAL_REACTION);
	if (heal > 0) then begin
		call boslori02;
	end
	else begin
		call boslori03;
	end
end

procedure boslori02
begin
	critter_heal(dude_obj, heal);
	gsay_message(SCRIPT_BOSLORI, 109, NEUTRAL_REACTION);
end

procedure boslori03
begin
	gsay_message(SCRIPT_BOSLORI, 110, NEUTRAL_REACTION);
end

procedure boslori04
begin
	critter_heal(dude_obj, heal);
	gsay_message(SCRIPT_BOSLORI, random(112, 116), NEUTRAL_REACTION);
end

procedure boslori05
begin
	gsay_message(SCRIPT_BOSLORI, 117, NEUTRAL_REACTION);
	gfade_out(600);
	rad_level := get_critter_stat(dude_obj, 37);
	radiation_dec(dude_obj, rad_level);
	game_time_advance(game_ticks(86400));
	gfade_in(600);
	gsay_message(SCRIPT_BOSLORI, 146, NEUTRAL_REACTION);
end

procedure boslori06
begin
	gsay_message(SCRIPT_BOSLORI, 118, NEUTRAL_REACTION);
end

procedure boslori07
begin
	gsay_reply(SCRIPT_BOSLORI, 119);
	giq_option(4, SCRIPT_BOSLORI, 120, boslori08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_BOSLORI, 121, bosloriend, NEUTRAL_REACTION);
end

procedure boslori08
begin
	set_local_var(5, 1);
	gsay_message(SCRIPT_BOSLORI, 122, NEUTRAL_REACTION);
	call boslori10;
end

procedure boslori09
begin
	gsay_reply(SCRIPT_BOSLORI, 126);
	call boslori10;
end

procedure boslori10
begin
	if (local_var(6) != 1) then begin
		giq_option(4, SCRIPT_BOSLORI, 127, boslori11, NEUTRAL_REACTION);
	end
	if (local_var(7) != 1) then begin
		giq_option(4, SCRIPT_BOSLORI, 128, boslori12, NEUTRAL_REACTION);
	end
	if (local_var(8) != 1) then begin
		giq_option(4, SCRIPT_BOSLORI, 129, boslori13, NEUTRAL_REACTION);
	end
	if (local_var(9) != 1) then begin
		giq_option(4, SCRIPT_BOSLORI, 130, boslori14, NEUTRAL_REACTION);
	end
	if (local_var(10) != 1) then begin
		giq_option(4, SCRIPT_BOSLORI, 131, boslori15, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_BOSLORI, 132, bosloriend, NEUTRAL_REACTION);
end

procedure boslori11
begin
	temp := get_critter_stat(dude_obj, 0);
	if (temp > 9) then begin
		call sorry;
	end
	else begin
		gsay_reply(SCRIPT_BOSLORI, 133);
		giq_option(4, SCRIPT_BOSLORI, 135, boslori18, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BOSLORI, 136, boslori17, NEUTRAL_REACTION);
	end
end

procedure boslori12
begin
	temp := get_critter_stat(dude_obj, 1);
	if (temp > 9) then begin
		call sorry;
	end
	else begin
		gsay_reply(SCRIPT_BOSLORI, 137);
		giq_option(4, SCRIPT_BOSLORI, 135, boslori19, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BOSLORI, 136, boslori17, NEUTRAL_REACTION);
	end
end

procedure boslori13
begin
	temp := get_critter_stat(dude_obj, 2);
	if (temp > 9) then begin
		call sorry;
	end
	else begin
		gsay_reply(SCRIPT_BOSLORI, 139);
		giq_option(4, SCRIPT_BOSLORI, 135, boslori20, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BOSLORI, 136, boslori17, NEUTRAL_REACTION);
	end
end

procedure boslori14
begin
	temp := get_critter_stat(dude_obj, 4);
	if (temp > 9) then begin
		call sorry;
	end
	else begin
		gsay_reply(SCRIPT_BOSLORI, 141);
		giq_option(4, SCRIPT_BOSLORI, 135, raisiq, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BOSLORI, 136, boslori17, NEUTRAL_REACTION);
	end
end

procedure boslori15
begin
	temp := get_critter_stat(dude_obj, 5);
	if (temp > 9) then begin
		call sorry;
	end
	else begin
		gsay_reply(SCRIPT_BOSLORI, 143);
		giq_option(4, SCRIPT_BOSLORI, 135, boslori22, NEUTRAL_REACTION);
		giq_option(4, SCRIPT_BOSLORI, 136, boslori17, NEUTRAL_REACTION);
	end
end

procedure boslori16
begin
	gsay_message(SCRIPT_BOSLORI, random(147, 149), NEUTRAL_REACTION);
end

procedure boslori17
begin
	gsay_message(SCRIPT_BOSLORI, 153, NEUTRAL_REACTION);
	call boslori10;
end

procedure boslori18
begin
	cost := 2000;
	if (item_caps_total(dude_obj) >= cost) then begin
		bag_ptr := create_object_sid(16777262, 0, 0, 860);
		move_to(bag_ptr, 0, 0);
		armor := critter_inven_obj(dude_obj, 0);
		if (armor != 0) then begin
			move_obj_inven_to_obj(dude_obj, bag_ptr);
			move_obj_inven_to_obj(bag_ptr, dude_obj);
			destroy_object(bag_ptr);
		end
		set_local_var(6, 1);
		item_caps_adjust(dude_obj, -cost);
		gsay_message(SCRIPT_BOSLORI, 154, NEUTRAL_REACTION);
		gfade_out(600);
		game_time_advance(game_ticks(3 * 604800));
		gfade_in(600);
		temp := get_critter_stat(dude_obj, 0);
		set_critter_stat(dude_obj, 0, 1);
		gsay_message(SCRIPT_BOSLORI, 156, NEUTRAL_REACTION);
	end
	else begin
		call boslori16;
	end
end

procedure boslori19
begin
	cost := 4000;
	if (item_caps_total(dude_obj) >= cost) then begin
		set_local_var(7, 1);
		item_caps_adjust(dude_obj, -cost);
		gsay_message(SCRIPT_BOSLORI, 159, NEUTRAL_REACTION);
		gfade_out(600);
		game_time_advance(game_ticks(604800));
		gfade_in(600);
		temp := get_critter_stat(dude_obj, 1);
		set_critter_stat(dude_obj, 1, 1);
		gsay_message(SCRIPT_BOSLORI, 161, NEUTRAL_REACTION);
	end
	else begin
		call boslori16;
	end
end

procedure boslori20
begin
	cost := 3000;
	if (item_caps_total(dude_obj) >= cost) then begin
		set_local_var(8, 1);
		item_caps_adjust(dude_obj, -cost);
		gsay_message(SCRIPT_BOSLORI, 164, NEUTRAL_REACTION);
		gfade_out(600);
		game_time_advance(game_ticks(604800));
		gfade_in(600);
		temp := get_critter_stat(dude_obj, 2);
		set_critter_stat(dude_obj, 2, 1);
		gsay_message(SCRIPT_BOSLORI, 166, NEUTRAL_REACTION);
	end
	else begin
		call boslori16;
	end
end

procedure Dumb02
begin
	gsay_reply(SCRIPT_BOSLORI, 400);
	giq_option(-3, SCRIPT_BOSLORI, 401, Dumb04, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_BOSLORI, 402, Dumb03, NEUTRAL_REACTION);
end

procedure Dumb03
begin
	gsay_reply(SCRIPT_BOSLORI, 403);
	giq_option(-3, SCRIPT_BOSLORI, 404, Dumb06a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_BOSLORI, 405, boslori05, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_BOSLORI, 406, bosloriend, NEUTRAL_REACTION);
end

procedure Dumb04
begin
	variable LVar0 := 0;
	LVar0 := 3000;
	if (item_caps_total(dude_obj) >= LVar0) then begin
		set_local_var(9, 1);
		set_local_var(11, 1);
		item_caps_adjust(dude_obj, -LVar0);
		gsay_message(SCRIPT_BOSLORI, 164, NEUTRAL_REACTION);
		gfade_out(600);
		game_time_advance(game_ticks(604800));
		gfade_in(600);
		temp := get_critter_stat(dude_obj, 4);
		set_critter_stat(dude_obj, 4, 1);
		gsay_reply(SCRIPT_BOSLORI, 407);
		giq_option(4, SCRIPT_BOSLORI, 409, Dumb06, NEUTRAL_REACTION);
		giq_option(-3, SCRIPT_BOSLORI, 408, Dumb05, NEUTRAL_REACTION);
	end
	else begin
		call boslori16;
	end
end

procedure Dumb05
begin
	gsay_message(SCRIPT_BOSLORI, 410, NEUTRAL_REACTION);
end

procedure Dumb06
begin
	gsay_reply(SCRIPT_BOSLORI, 411);
	giq_option(4, SCRIPT_BOSLORI, 412, bosloriend, NEUTRAL_REACTION);
end

procedure Dumb06a
begin
	Hurting := get_critter_stat(dude_obj, 7) - get_critter_stat(dude_obj, 35);
	critter_heal(dude_obj, Hurting);
	gsay_message(SCRIPT_BOSLORI, 413, NEUTRAL_REACTION);
end

procedure raisiq
begin
	cost := 6000;
	if (item_caps_total(dude_obj) >= cost) then begin
		set_local_var(9, 1);
		item_caps_adjust(dude_obj, -cost);
		gsay_message(SCRIPT_BOSLORI, 168, NEUTRAL_REACTION);
		gfade_out(600);
		game_time_advance(game_ticks(3 * 604800));
		gfade_in(600);
		temp := get_critter_stat(dude_obj, 4);
		set_critter_stat(dude_obj, 4, 1);
		gsay_message(SCRIPT_BOSLORI, 170, NEUTRAL_REACTION);
	end
	else begin
		call boslori16;
	end
end

procedure boslori22
begin
	cost := 5000;
	if (item_caps_total(dude_obj) >= cost) then begin
		set_local_var(10, 1);
		item_caps_adjust(dude_obj, -cost);
		gsay_message(SCRIPT_BOSLORI, 174, NEUTRAL_REACTION);
		gfade_out(600);
		game_time_advance(game_ticks(3 * 604800));
		gfade_in(600);
		temp := get_critter_stat(dude_obj, 5);
		set_critter_stat(dude_obj, 5, 1);
		gsay_message(SCRIPT_BOSLORI, 176, NEUTRAL_REACTION);
	end
	else begin
		call boslori16;
	end
end

procedure bosloriend
begin
end

procedure sorry
begin
	gsay_message(SCRIPT_BOSLORI, 300, NEUTRAL_REACTION);
end

procedure do_dialog
begin
	call boslori00;
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