#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Raider0;
procedure Raider1;
procedure Raider2;
procedure Raider3;
procedure Raider4;
procedure Raider5;
procedure Raider6;
procedure Raider7;
procedure Raider8;
procedure Raider9;
procedure Raider10;
procedure Raider10a;
procedure Raider11;
procedure Raider12;
procedure Raider13;
procedure Raider14;
procedure Raider14a;
procedure Raider15;
procedure Raider16;
procedure Raider17;
procedure Raider17a;
procedure Raider17b;
procedure Raider18;
procedure Raider19;
procedure Raider20;
procedure Raider21;
procedure Raider22;
procedure Raider22a;
procedure Raider23;
procedure Raider24;
procedure Raider24a;
procedure Raider25;
procedure Raider26;
procedure Raider27;
procedure Raider27a;
procedure Raider28;
procedure Raider29;
procedure Raider29a;
procedure Raider30;
procedure Raider30a;
procedure Raider31;
procedure Raider32;
procedure Raider33;
procedure Raider34;
procedure Raiderend;
procedure RaiderCombat;
procedure RaiderSnap;
procedure safe;

variable hostile1;
variable known;
variable pissed;
variable sfx_name;
variable Sinthia_is_safe;
variable shoot_Sinthia;
variable will_negotiate;
variable line184flag;

import variable JTRaider_ptr;
import variable Sinthia_ptr;
import variable award;

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
	JTRaider_ptr := self_obj;
	if (script_action == critter_proc) then begin
		call critter_p_proc;
	end
	else begin
		if (script_action == damage_proc) then begin
			call damage_p_proc;
		end
		else begin
			if (script_action == destroy_proc) then begin
				call destroy_p_proc;
			end
			else begin
				if (script_action == look_at_proc) then begin
					call look_at_p_proc;
				end
				else begin
					if (script_action == map_enter_proc) then begin
						call map_enter_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 == timed_event_proc) then begin
									call timed_event_p_proc;
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if ((tile_distance_objs(self_obj, dude_obj) < 8) and (line184flag == 0) and (has_skill(dude_obj, 8) >= 50)) then begin
		line184flag := 1;
		display_msg(message_str(SCRIPT_JTRAIDER, 184));
	end
	else begin
		if (hostile1) then begin
			hostile1 := 0;
			pissed := 1;
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
		else begin
			if (pissed and (tile_distance_objs(dude_obj, self_obj) < 4)) then begin
				hostile1 := 1;
			end
			else begin
				if (obj_can_see_obj(self_obj, dude_obj) and (known == 0) and (using_skill(dude_obj, 8) == 0)) then begin
					dialogue_system_enter;
				end
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		if (using_skill(dude_obj, 8)) then begin
			if (is_success(roll_vs_skill(dude_obj, 8, 0))) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_JTRAIDER, 183));
				critter_injure(self_obj, 2);
				critter_injure(self_obj, 1);
				set_map_var(1, 1);
				set_map_var(3, 3);
				terminate_combat;
			end
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_map_var(1, 2);
	end
	set_global_var(GVAR_SAVE_SINTHIA, 2);
	set_map_var(3, 3);
	if (map_var(0) != 2) then begin
		award := 400;
	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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
		if ((global_var(GVAR_BAD_MONSTER) % 6) == 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_JTRAIDER, 100));
end

procedure map_enter_p_proc
begin
	JTRaider_ptr := self_obj;
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 15);
end

procedure pickup_p_proc
begin
	hostile1 := 1;
	pissed := 1;
	will_negotiate := 0;
end

procedure talk_p_proc
begin
	call Get_Reaction;
	if (hostile1) then begin
		call Raider34;
	end
	else begin
		if (global_var(GVAR_SAVE_SINTHIA) == 1) then begin
			start_gdialog(SCRIPT_JTRAIDER, self_obj, 4, -1, -1);
			gsay_start;
			if (not(known)) then begin
				call Raider0;
			end
			else begin
				if (map_var(3) == 1) then begin
					if (local_var(1) < 2) then begin
						call Raider17;
					end
					else begin
						call Raider9;
					end
					set_map_var(3, 2);
				end
				else begin
					if (will_negotiate) then begin
						call Raider21;
					end
					else begin
						call Raider31;
					end
				end
			end
			gsay_end;
			end_dialogue;
		end
		else begin
			display_msg(message_str(SCRIPT_JTRAIDER, 178));
		end
	end
	if (shoot_Sinthia) then begin
		game_ui_disable;
		reg_anim_func(2, self_obj);
		reg_anim_func(1, 1);
		reg_anim_animate(self_obj, 43, -1);
		reg_anim_animate(self_obj, 45, -1);
		sfx_name := sfx_build_weapon_name(1, obj_carrying_pid_obj(self_obj, 8), 0, Sinthia_ptr);
		reg_anim_play_sfx(self_obj, sfx_name, 0);
		reg_anim_animate(self_obj, 44, -1);
		reg_anim_func(3, 0);
		shoot_Sinthia := 0;
		add_timer_event(self_obj, 5, 2);
	end
	if (Sinthia_is_safe == 1) then begin
		set_map_var(3, 3);
		set_global_var(GVAR_SAVE_SINTHIA, 2);
		gfade_out(600);
		gfade_in(600);
		destroy_object(self_obj);
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		game_ui_enable;
		hostile1 := 1;
	end
	else begin
		if (fixed_param == 2) then begin
			critter_dmg(Sinthia_ptr, 75, 0);
			add_timer_event(self_obj, game_ticks(5), 1);
		end
	end
end

procedure Raider0
begin
	known := 1;
	gsay_reply(SCRIPT_JTRAIDER, 101);
	giq_option(-3, SCRIPT_JTRAIDER, 102, Raider1, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTRAIDER, 103, Raider2, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTRAIDER, 104, Raider8, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_JTRAIDER, 105, Raider4, NEUTRAL_REACTION);
end

procedure Raider1
begin
	gsay_message(SCRIPT_JTRAIDER, 106, NEUTRAL_REACTION);
	call RaiderSnap;
end

procedure Raider2
begin
	gsay_reply(SCRIPT_JTRAIDER, 107);
	giq_option(4, SCRIPT_JTRAIDER, 108, Raider3, NEUTRAL_REACTION);
end

procedure Raider3
begin
	call Down_React;
	gsay_message(SCRIPT_JTRAIDER, 109, BAD_REACTION);
	pissed := 1;
end

procedure Raider4
begin
	gsay_reply(SCRIPT_JTRAIDER, 110);
	giq_option(7, SCRIPT_JTRAIDER, 111, Raider5, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_JTRAIDER, 112, Raider7, NEUTRAL_REACTION);
end

procedure Raider5
begin
	gsay_reply(SCRIPT_JTRAIDER, 113);
	giq_option(7, SCRIPT_JTRAIDER, 114, Raider6, GOOD_REACTION);
end

procedure Raider6
begin
	call Up_React;
	pissed := 1;
	gsay_message(SCRIPT_JTRAIDER, 115, GOOD_REACTION);
end

procedure Raider7
begin
	gsay_message(SCRIPT_JTRAIDER, 116, BAD_REACTION);
	pissed := 1;
end

procedure Raider8
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_JTRAIDER, 119, BAD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_JTRAIDER, 120, BAD_REACTION);
	end
	call RaiderSnap;
end

procedure Raider9
begin
	gsay_reply(SCRIPT_JTRAIDER, 121);
	giq_option(4, SCRIPT_JTRAIDER, 122, Raider10, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_JTRAIDER, 123, Raider14, NEUTRAL_REACTION);
end

procedure Raider10
begin
	gsay_reply(SCRIPT_JTRAIDER, 124);
	giq_option(4, SCRIPT_JTRAIDER, 125, Raider11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTRAIDER, 126, Raider10a, NEUTRAL_REACTION);
end

procedure Raider10a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		will_negotiate := 1;
		call Raider12;
	end
	else begin
		call Raider13;
	end
end

procedure Raider11
begin
	gsay_reply(SCRIPT_JTRAIDER, 127);
	giq_option(4, SCRIPT_JTRAIDER, 128, RaiderCombat, BAD_REACTION);
end

procedure Raider12
begin
	gsay_message(SCRIPT_JTRAIDER, 129, NEUTRAL_REACTION);
end

procedure Raider13
begin
	gsay_message(SCRIPT_JTRAIDER, 130, NEUTRAL_REACTION);
end

procedure Raider14
begin
	gsay_reply(SCRIPT_JTRAIDER, 131);
	giq_option(7, SCRIPT_JTRAIDER, 132, Raider14a, NEUTRAL_REACTION);
end

procedure Raider14a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 20))) then begin
		will_negotiate := 1;
		call Raider16;
	end
	else begin
		call Raider15;
	end
end

procedure Raider15
begin
	gsay_message(SCRIPT_JTRAIDER, 133, NEUTRAL_REACTION);
end

procedure Raider16
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_JTRAIDER, 136, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_JTRAIDER, 137, NEUTRAL_REACTION);
	end
end

procedure Raider17
begin
	gsay_reply(SCRIPT_JTRAIDER, 138);
	giq_option(4, SCRIPT_JTRAIDER, 139, Raider17a, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_JTRAIDER, 140, Raider18, NEUTRAL_REACTION);
	giq_option(8, SCRIPT_JTRAIDER, 141, Raider17b, NEUTRAL_REACTION);
end

procedure Raider17a
begin
	will_negotiate := 0;
end

procedure Raider17b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		will_negotiate := 1;
		call Raider20;
	end
	else begin
		call Raider19;
	end
end

procedure Raider18
begin
	gsay_message(SCRIPT_JTRAIDER, 142, BAD_REACTION);
	call RaiderSnap;
end

procedure Raider19
begin
	gsay_message(SCRIPT_JTRAIDER, 143, BAD_REACTION);
	will_negotiate := 0;
end

procedure Raider20
begin
	call Up_React;
	gsay_message(SCRIPT_JTRAIDER, 144, GOOD_REACTION);
end

procedure Raider21
begin
	gsay_reply(SCRIPT_JTRAIDER, 145);
	giq_option(4, SCRIPT_JTRAIDER, 146, Raider22, NEUTRAL_REACTION);
end

procedure Raider22
begin
	gsay_reply(SCRIPT_JTRAIDER, 147);
	giq_option(4, SCRIPT_JTRAIDER, 148, Raider23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTRAIDER, 149, Raider24, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JTRAIDER, 150, Raider22a, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_JTRAIDER, 151, Raider27, NEUTRAL_REACTION);
end

procedure Raider22a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Raider26;
	end
	else begin
		call Raider23;
	end
end

procedure Raider23
begin
	gsay_message(SCRIPT_JTRAIDER, 152, BAD_REACTION);
	call RaiderSnap;
end

procedure Raider24
begin
	gsay_reply(SCRIPT_JTRAIDER, 153);
	giq_option(4, SCRIPT_JTRAIDER, 154, Raider24a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JTRAIDER, 155, Raider25, NEUTRAL_REACTION);
end

procedure Raider24a
begin
	if (item_caps_total(dude_obj) >= 100) then begin
		item_caps_adjust(dude_obj, -100);
		call safe;
	end
	else begin
		call Raider23;
	end
end

procedure Raider25
begin
	gsay_message(SCRIPT_JTRAIDER, 156, BAD_REACTION);
	call RaiderSnap;
end

procedure Raider26
begin
	gsay_message(SCRIPT_JTRAIDER, 157, GOOD_REACTION);
	call safe;
end

procedure Raider27
begin
	gsay_reply(SCRIPT_JTRAIDER, 158);
	giq_option(5, SCRIPT_JTRAIDER, 159, Raider28, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JTRAIDER, 160, Raider27a, NEUTRAL_REACTION);
end

procedure Raider27a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Raider29;
	end
	else begin
		call Raider28;
	end
end

procedure Raider28
begin
	gsay_message(SCRIPT_JTRAIDER, 161, BAD_REACTION);
	call RaiderSnap;
end

procedure Raider29
begin
	gsay_reply(SCRIPT_JTRAIDER, 162);
	giq_option(5, SCRIPT_JTRAIDER, 163, Raider29a, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JTRAIDER, 164, Raider30, NEUTRAL_REACTION);
end

procedure Raider29a
begin
	if (item_caps_total(dude_obj) >= 100) then begin
		item_caps_adjust(dude_obj, -100);
		call safe;
	end
	else begin
		call Raider23;
	end
end

procedure Raider30
begin
	gsay_reply(SCRIPT_JTRAIDER, 165);
	giq_option(5, SCRIPT_JTRAIDER, 166, Raider23, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_JTRAIDER, 167, Raider30a, NEUTRAL_REACTION);
end

procedure Raider30a
begin
	if (item_caps_total(dude_obj) >= 200) then begin
		item_caps_adjust(dude_obj, -200);
		call safe;
	end
	else begin
		call Raider23;
	end
end

procedure Raider31
begin
	gsay_reply(SCRIPT_JTRAIDER, 168);
	giq_option(4, SCRIPT_JTRAIDER, 169, Raider32, NEUTRAL_REACTION);
	if (dude_is_male) then begin
		giq_option(6, SCRIPT_JTRAIDER, 172, Raider33, BAD_REACTION);
	end
	else begin
		giq_option(6, SCRIPT_JTRAIDER, 173, Raider33, BAD_REACTION);
	end
end

procedure Raider32
begin
	gsay_message(SCRIPT_JTRAIDER, 174, BAD_REACTION);
	call RaiderSnap;
end

procedure Raider33
begin
	gsay_message(SCRIPT_JTRAIDER, 175, BAD_REACTION);
	call RaiderCombat;
end

procedure Raider34
begin
	float_msg(self_obj, message_str(SCRIPT_JTRAIDER, 176), FLOAT_MSG_RED);
	call RaiderSnap;
end

procedure Raiderend
begin
end

procedure RaiderCombat
begin
	hostile1 := 1;
end

procedure RaiderSnap
begin
	shoot_Sinthia := 1;
end

procedure safe
begin
	award := 1000;
	pissed := 0;
	Sinthia_is_safe := 1;
	set_obj_visibility(self_obj, 1);
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