#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure map_enter_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Sinthia00;
procedure Sinthia00a;
procedure Sinthia00b;
procedure Sinthia01;
procedure Sinthia02;
procedure Sinthia03;
procedure Sinthia04;
procedure Sinthia05;
procedure Sinthia06;
procedure Sinthia07;
procedure Sinthia08;
procedure Sinthia09;
procedure Sinthia10;
procedure Sinthia11;
procedure Sinthia12;
procedure Sinthia12a;
procedure Sinthia13;
procedure Sinthia13a;
procedure Sinthia14;
procedure Sinthia15;
procedure Sinthia16;
procedure Sinthia16a;
procedure Sinthia17;
procedure Sinthia18;
procedure Sinthia19;
procedure Sinthia20;
procedure Sinthia20a;
procedure Sinthia21;
procedure Sinthia22;
procedure Sinthia23;
procedure Sinthia24;
procedure Sinthia25;
procedure Sinthia26;
procedure Sinthia27;
procedure Sinthia28;
procedure Sinthia29;
procedure Sin;
procedure SinOn;
procedure SinthiaQuestions;
procedure SinthiaEnd;

variable do_it;
variable leaving;
variable dest_tile1;
variable raider_fall_down;
variable remove_Raider;
variable sleeping_disabled;
variable waypoint := 7000;

import variable raider_dead;
import variable Sinthia_ptr;
import variable JTRaider_ptr;

variable line08flag;
variable line23flag;
variable line24flag;
variable line25flag;
variable line26flag;

procedure sleeping;

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

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 damage_p_proc;


procedure start
begin
	Sinthia_ptr := self_obj;
	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
		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 == 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

procedure critter_p_proc
begin
	if (map_var(1) == 2) then begin
		if ((local_var(6) == 0) and obj_can_see_obj(self_obj, dude_obj) and (tile_distance_objs(self_obj, dude_obj) < 12)) then begin
			dialogue_system_enter;
		end
	end
	else begin
		if (map_var(1) == 1) then begin
			if ((local_var(7) == 0) and obj_can_see_obj(self_obj, dude_obj) and (tile_distance_objs(self_obj, dude_obj) < 12) and (raider_fall_down == 0)) then begin
				rm_timer_event(self_obj);
				add_timer_event(self_obj, 15, 2);
				reg_anim_func(2, JTRaider_ptr);
				reg_anim_func(1, 1);
				reg_anim_animate(JTRaider_ptr, 21, -1);
				reg_anim_animate(JTRaider_ptr, 49, -1);
				reg_anim_func(3, 0);
				raider_fall_down := 1;
			end
		end
	end
	if (global_var(GVAR_SAVE_SINTHIA) == 2) then begin
		if (map_var(1) != raider_dead) then begin
			if (sleeping_disabled == 0) then begin
				call sleeping;
			end
		end
		else begin
			set_map_var(0, 1);
			sleeping_disabled := 1;
			if (waypoint == 0) then begin
				dest_tile1 := 22502;
				waypoint := 1;
			end
			if (tile_distance(tile_num(self_obj), dest_tile1) > 3) then begin
				animate_move_obj_to_tile(self_obj, self_obj, ANIMATE_WALK);
			end
			else begin
				if (waypoint == 1) then begin
					dest_tile1 := 31930;
					waypoint := 2;
				end
				else begin
					if (waypoint == 2) then begin
						destroy_object(self_obj);
					end
				end
			end
		end
	end
end

procedure destroy_p_proc
begin
	Sinthia_ptr := 0;
	set_global_var(GVAR_SAVE_SINTHIA, 2);
	if (map_var(0) != 1) then begin
		set_map_var(0, 2);
	end
	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 look_at_p_proc
begin
	script_overrides;
	if (local_var(5) == 1) then begin
		display_msg(message_str(SCRIPT_SINTHIA, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_SINTHIA, 101));
	end
end

procedure map_enter_p_proc
begin
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 16);
	Sinthia_ptr := self_obj;
	home_tile := 17485;
	sleep_tile := 16681;
	sleep_time := 2300;
	wake_time := 1000;
	if (local_var(6) == 1) then begin
		Sinthia_ptr := 0;
		destroy_object(self_obj);
	end
end

procedure talk_p_proc
begin
	if (local_var(6) == 1) then begin
		display_msg(message_str(SCRIPT_SINTHIA, 171));
	end
	else begin
		if ((map_var(3) > 0) and (map_var(3) < 3)) then begin
			call Sinthia07;
		end
		else begin
			call Get_Reaction;
			start_gdialog(SCRIPT_SINTHIA, self_obj, 4, -1, -1);
			gsay_start;
			set_local_var(5, 1);
			if (map_var(1) == 2) then begin
				call Sinthia16;
			end
			else begin
				if ((map_var(1) == 1) and (local_var(7) == 0)) then begin
					call Sinthia17;
				end
				else begin
					if (not(line08flag) and (global_var(GVAR_SAVE_SINTHIA) == 2)) then begin
						call Sinthia08;
					end
					else begin
						if (map_var(3) < 3) then begin
							call Sinthia00;
						end
						else begin
							if (local_var(1) < 2) then begin
								call Sinthia29;
							end
							else begin
								call Sinthia20;
							end
						end
					end
				end
			end
			gsay_end;
			end_dialogue;
			if (do_it) then begin
				call Sin;
			end
			if (remove_Raider) then begin
				if (JTRaider_ptr != 0) then begin
					display_msg(message_str(SCRIPT_SINTHIA, 177));
					destroy_object(JTRaider_ptr);
					JTRaider_ptr := 0;
					remove_Raider := 0;
				end
			end
		end
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 2) then begin
		dialogue_system_enter;
	end
	else begin
		anim(self_obj, 1000, 3);
	end
end

procedure Sinthia00
begin
	gsay_reply(SCRIPT_SINTHIA, 102);
	giq_option(4, SCRIPT_SINTHIA, 103, SinthiaEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_SINTHIA, 104, Sinthia00a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SINTHIA, 105, Sinthia00b, NEUTRAL_REACTION);
end

procedure Sinthia00a
begin
	if (dude_is_male) then begin
		call Sinthia04;
	end
	else begin
		call Sinthia03;
	end
end

procedure Sinthia00b
begin
	if (dude_is_male) then begin
		call Sinthia01;
	end
	else begin
		call Sinthia02;
	end
end

procedure Sinthia01
begin
	gsay_message(SCRIPT_SINTHIA, 106, NEUTRAL_REACTION);
end

procedure Sinthia02
begin
	gsay_message(SCRIPT_SINTHIA, 107, NEUTRAL_REACTION);
end

procedure Sinthia03
begin
	gsay_message(SCRIPT_SINTHIA, 108, NEUTRAL_REACTION);
end

procedure Sinthia04
begin
	gsay_reply(SCRIPT_SINTHIA, 109);
	giq_option(6, SCRIPT_SINTHIA, 110, Sinthia05, NEUTRAL_REACTION);
	if (global_var(GVAR_DOG_EMPATHY)) then begin
		giq_option(6, SCRIPT_SINTHIA, 111, Sinthia06, NEUTRAL_REACTION);
	end
end

procedure Sinthia05
begin
	gsay_message(SCRIPT_SINTHIA, 112, NEUTRAL_REACTION);
end

procedure Sinthia06
begin
	call Down_React;
	gsay_message(SCRIPT_SINTHIA, 113, NEUTRAL_REACTION);
end

procedure Sinthia07
begin
	float_msg(self_obj, message_str(SCRIPT_SINTHIA, random(114, 117)), FLOAT_MSG_LIGHT_RED);
end

procedure Sinthia08
begin
	line08flag := 1;
	call Big_Up_React;
	gsay_reply(SCRIPT_SINTHIA, 118);
	giq_option(4, SCRIPT_SINTHIA, 119, Sinthia10, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_SINTHIA, 120, Sinthia11, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SINTHIA, 121, Sinthia09, NEUTRAL_REACTION);
end

procedure Sinthia09
begin
	gsay_message(SCRIPT_SINTHIA, 122, NEUTRAL_REACTION);
end

procedure Sinthia10
begin
	call Down_React;
	gsay_message(SCRIPT_SINTHIA, 123, NEUTRAL_REACTION);
end

procedure Sinthia11
begin
	gsay_reply(SCRIPT_SINTHIA, 124);
	giq_option(5, SCRIPT_SINTHIA, 125, Sinthia12, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_SINTHIA, 126, Sinthia15, NEUTRAL_REACTION);
end

procedure Sinthia12
begin
	gsay_reply(SCRIPT_SINTHIA, 127);
	giq_option(0, SCRIPT_MODREACT, 106, Sinthia12a, GOOD_REACTION);
end

procedure Sinthia12a
begin
	gsay_reply(SCRIPT_SINTHIA, 128);
	giq_option(5, SCRIPT_SINTHIA, 129, Sinthia14, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_SINTHIA, 130, Sinthia13, NEUTRAL_REACTION);
end

procedure Sinthia13
begin
	gsay_reply(SCRIPT_SINTHIA, 131);
	giq_option(0, SCRIPT_MODREACT, 106, Sinthia13a, NEUTRAL_REACTION);
end

procedure Sinthia13a
begin
	if ((dude_is_male) or is_success(do_check(dude_obj, 3, 0))) then begin
		gsay_reply(SCRIPT_SINTHIA, 132);
		giq_option(0, SCRIPT_MODREACT, 106, SinOn, GOOD_REACTION);
	end
	else begin
		gsay_message(SCRIPT_SINTHIA, 133, GOOD_REACTION);
	end
end

procedure Sinthia14
begin
	gsay_message(SCRIPT_SINTHIA, 134, NEUTRAL_REACTION);
	if (not(global_var(GVAR_KILLIAN_DEAD)) and not(global_var(GVAR_GIZMO_DEAD))) then begin
		gsay_message(SCRIPT_SINTHIA, 136, NEUTRAL_REACTION);
	end
end

procedure Sinthia15
begin
	gsay_reply(SCRIPT_SINTHIA, 137);
	giq_option(5, SCRIPT_SINTHIA, 138, Sinthia14, NEUTRAL_REACTION);
end

procedure Sinthia16
begin
	variable LVar0 := 0;
	set_local_var(6, 1);
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_SINTHIA, 141);
	end
	else begin
		gsay_reply(SCRIPT_SINTHIA, 142);
	end
	giq_option(0, SCRIPT_MODREACT, 106, Sinthia16a, NEUTRAL_REACTION);
end

procedure Sinthia16a
begin
	call Bottom_React;
	dest_tile1 := 7521;
	gsay_message(SCRIPT_SINTHIA, 143, NEUTRAL_REACTION);
end

procedure Sinthia17
begin
	set_local_var(7, 1);
	gsay_reply(SCRIPT_SINTHIA, 144);
	giq_option(4, SCRIPT_SINTHIA, 145, Sinthia19, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SINTHIA, 146, Sinthia18, NEUTRAL_REACTION);
end

procedure Sinthia18
begin
	call Bottom_React;
	gsay_message(SCRIPT_SINTHIA, 147, NEUTRAL_REACTION);
end

procedure Sinthia19
begin
	gsay_reply(SCRIPT_SINTHIA, 148);
	giq_option(4, SCRIPT_SINTHIA, 149, Sinthia08, NEUTRAL_REACTION);
	remove_Raider := 1;
end

procedure Sinthia20
begin
	gsay_reply(SCRIPT_SINTHIA, 150);
	giq_option(4, SCRIPT_SINTHIA, 151, Sinthia22, NEUTRAL_REACTION);
	if (dude_is_male) then begin
		giq_option(4, SCRIPT_SINTHIA, 152, Sinthia20a, NEUTRAL_REACTION);
	end
	if (not(line23flag)) then begin
		giq_option(6, SCRIPT_SINTHIA, 153, Sinthia23, NEUTRAL_REACTION);
	end
end

procedure Sinthia20a
begin
	if (item_caps_total(dude_obj) >= 40) then begin
		item_caps_adjust(dude_obj, -40);
		call Sinthia27;
	end
	else begin
		call Sinthia28;
	end
end

procedure Sinthia21
begin
	gsay_message(SCRIPT_SINTHIA, 154, NEUTRAL_REACTION);
end

procedure Sinthia22
begin
	gsay_message(SCRIPT_SINTHIA, 155, NEUTRAL_REACTION);
end

procedure Sinthia23
begin
	line23flag := 1;
	gsay_reply(SCRIPT_SINTHIA, 156);
	call SinthiaQuestions;
end

procedure Sinthia24
begin
	line24flag := 1;
	gsay_reply(SCRIPT_SINTHIA, 161);
	call SinthiaQuestions;
end

procedure Sinthia25
begin
	line25flag := 1;
	gsay_reply(SCRIPT_SINTHIA, 162);
	call SinthiaQuestions;
end

procedure Sinthia26
begin
	line26flag := 1;
	gsay_reply(SCRIPT_SINTHIA, 163);
	call SinthiaQuestions;
end

procedure Sinthia27
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := random(1, 3);
	LVar1 := message_str(SCRIPT_SINTHIA, 163 + LVar0);
	float_msg(self_obj, LVar1, FLOAT_MSG_LIGHT_RED);
	call SinOn;
end

procedure Sinthia28
begin
	call Down_React;
	gsay_message(SCRIPT_SINTHIA, 167, NEUTRAL_REACTION);
end

procedure Sinthia29
begin
	gsay_message(SCRIPT_SINTHIA, 168, NEUTRAL_REACTION);
end

procedure Sin
begin
	gfade_out(600);
	game_time_advance(game_ticks(600));
	move_to(dude_obj, 18291, 0);
	if (not(is_success(do_check(dude_obj, 4, 0)))) then begin
		set_map_var(7, 1);
	end
	gfade_in(600);
	do_it := 0;
end

procedure SinOn
begin
	do_it := 1;
end

procedure SinthiaQuestions
begin
	if (not(line24flag)) then begin
		giq_option(6, SCRIPT_SINTHIA, 157, Sinthia24, NEUTRAL_REACTION);
	end
	if (not(line25flag)) then begin
		giq_option(6, SCRIPT_SINTHIA, 158, Sinthia25, NEUTRAL_REACTION);
	end
	if (not(line26flag)) then begin
		giq_option(6, SCRIPT_SINTHIA, 159, Sinthia26, NEUTRAL_REACTION);
	end
	giq_option(6, SCRIPT_SINTHIA, 160, SinthiaEnd, NEUTRAL_REACTION);
end

procedure SinthiaEnd
begin
end

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

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