#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure timeforwhat;
procedure do_dialogue;
procedure prisonrcbt;
procedure prisonrend;
procedure prisonr00;
procedure prisonr01;
procedure prisonr02;
procedure prisonr03;
procedure prisonr04;
procedure prisonr05;
procedure prisonr06;
procedure prisonr07;
procedure prisonr08;
procedure leave_map;

variable Hostile;
variable init_teams;
variable rndx;
variable rndy;
variable rndz;
variable my_hex;


procedure start
begin
	if (not(init_teams)) then begin
		init_teams := 1;
		if (local_var(3) > 9) then begin
			set_obj_visibility(self_obj, 1);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 30);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GHOUL_COWARD);
	end
	if (local_var(3) < 10) then begin
		if (script_action == timed_event_proc) then begin
			call timeforwhat;
		end
		else begin
			if (script_action == talk_proc) then begin
				if (local_var(1) == 0) then begin
					call do_dialogue;
				end
				else begin
					float_msg(self_obj, message_str(SCRIPT_PRISONR, 126), FLOAT_MSG_RED);
				end
			end
			else begin
				if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
					script_overrides;
					display_msg(message_str(SCRIPT_PRISONR, 100));
				end
				else begin
					if (script_action == pickup_proc) then begin
						Hostile := 1;
					end
					else begin
						if (script_action == critter_proc) then begin
							if (map_var(6) != 0) then begin
								if (local_var(1) == 1) then begin
									call leave_map;
								end
								else begin
									if (obj_can_see_obj(self_obj, dude_obj) and (tile_distance_objs(self_obj, dude_obj) < 5)) then begin
										dialogue_system_enter;
									end
								end
							end
							else begin
								if (Hostile) then begin
									Hostile := 0;
									attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
								end
							end
						end
						else begin
							if (script_action == destroy_proc) then 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
						end
					end
				end
			end
		end
	end
end

procedure timeforwhat
begin
	if (fixed_param == 1) then begin
		Hostile := 1;
	end
end

procedure do_dialogue
begin
	start_gdialog(SCRIPT_PRISONR, self_obj, 4, -1, -1);
	gsay_start;
	if (map_var(6) != 0) then begin
		call prisonr06;
	end
	else begin
		if (local_var(0)) then begin
			call prisonr05;
		end
		else begin
			set_local_var(0, 1);
			call prisonr00;
		end
	end
	gsay_end;
	end_dialogue;
end

procedure prisonrcbt
begin
	add_timer_event(self_obj, game_ticks(2), 1);
end

procedure prisonrend
begin
end

procedure prisonr00
begin
	gsay_reply(SCRIPT_PRISONR, 101);
	giq_option(4, SCRIPT_PRISONR, 102, prisonrend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_PRISONR, 103, prisonr01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_PRISONR, 104, prisonr02, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_PRISONR, 105, prisonr03, NEUTRAL_REACTION);
end

procedure prisonr01
begin
	gsay_message(SCRIPT_PRISONR, 106, NEUTRAL_REACTION);
end

procedure prisonr02
begin
	gsay_message(SCRIPT_PRISONR, 107, NEUTRAL_REACTION);
end

procedure prisonr03
begin
	gsay_reply(SCRIPT_PRISONR, 108);
	giq_option(6, SCRIPT_PRISONR, 109, prisonr04, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_PRISONR, 110, prisonr02, NEUTRAL_REACTION);
end

procedure prisonr04
begin
	gsay_reply(SCRIPT_PRISONR, 111);
	giq_option(6, SCRIPT_PRISONR, 112, prisonr02, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_PRISONR, 113, prisonrend, NEUTRAL_REACTION);
end

procedure prisonr05
begin
	gsay_message(SCRIPT_PRISONR, 114, NEUTRAL_REACTION);
end

procedure prisonr06
begin
	set_local_var(2, 15863);
	set_local_var(3, 0);
	set_local_var(1, 1);
	give_exp_points(500);
	display_msg(message_str(SCRIPT_PRISONR, 124) + 500 + message_str(SCRIPT_PRISONR, 125));
	gsay_reply(SCRIPT_PRISONR, 115);
	giq_option(4, SCRIPT_PRISONR, 116, prisonrend, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_PRISONR, 117, prisonr07, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_PRISONR, 118, prisonrend, NEUTRAL_REACTION);
end

procedure prisonr07
begin
	gsay_reply(SCRIPT_PRISONR, 119);
	giq_option(5, SCRIPT_PRISONR, 120, prisonr08, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_PRISONR, 121, prisonrend, NEUTRAL_REACTION);
end

procedure prisonr08
begin
	gsay_reply(SCRIPT_PRISONR, 122);
	giq_option(5, SCRIPT_PRISONR, 123, prisonrend, NEUTRAL_REACTION);
end

procedure leave_map
begin
	my_hex := tile_num(self_obj);
	if (my_hex == local_var(2)) then begin
		set_local_var(3, local_var(3) + 1);
		if (local_var(3) > 7) then begin
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
			set_local_var(3, 10);
			set_obj_visibility(self_obj, 1);
			move_to(self_obj, 0, 0);
		end
		else begin
			if (local_var(3) == 1) then begin
				set_local_var(2, 15863);
			end
			else begin
				if (local_var(3) == 2) then begin
					set_local_var(2, 13065);
				end
				else begin
					if (local_var(3) == 3) then begin
						set_local_var(2, 13081);
					end
					else begin
						if (local_var(3) == 4) then begin
							set_local_var(2, 14496);
						end
						else begin
							if (local_var(3) == 5) then begin
								set_local_var(2, 11900);
							end
							else begin
								if (local_var(3) == 6) then begin
									set_local_var(2, 8700);
								end
								else begin
									if (local_var(3) == 7) then begin
										set_local_var(2, 5900);
									end
								end
							end
						end
					end
				end
			end
			animate_move_obj_to_tile(self_obj, local_var(2), ANIMATE_WALK);
		end
	end
	animate_move_obj_to_tile(self_obj, local_var(2), ANIMATE_WALK);
end