#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 pickup_p_proc;
procedure talk_p_proc;
procedure Medic00;
procedure Medic01;
procedure Medic02;
procedure Medic03;
procedure Medic04;
procedure Medic04a;
procedure Medic05;
procedure Medic06;
procedure Medic07;
procedure Medic08;
procedure Medic08a;
procedure Medic09;
procedure Medic10;
procedure Medic11;
procedure Medic12;
procedure Medic13;
procedure Medic14;
procedure Medic15;
procedure Medic16;
procedure Medic17;
procedure Medic18;
procedure Medic19;
procedure Medic20;
procedure Medic21;
procedure Medic22;
procedure Medic23;
procedure Medic24;
procedure flee_dude;
procedure pushdrugs;
procedure medicend;

variable damage;
variable intensity;
variable removal;
variable rndx;
variable rads;
variable initialized1;
variable player_hits;
variable player_max_hits;
variable home_tile := 13881;

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 (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 1);
		initialized1 := 1;
	end
	else begin
		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 == pickup_proc) then begin
							call pickup_p_proc;
						end
						else begin
							if (script_action == talk_proc) then begin
								call talk_p_proc;
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (local_var(5)) then begin
		if (tile_distance_objs(self_obj, dude_obj) < 8) then begin
			call flee_dude;
		end
		else begin
			if (tile_distance(tile_num(dude_obj), home_tile) > 4) then begin
				animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
			end
		end
	end
	else begin
		if (tile_num(self_obj) != home_tile) then begin
			animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_local_var(4, 1);
		set_local_var(5, 1);
	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
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_WATER_CHIP_4, 1);
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_MEDIC, 136));
end

procedure pickup_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_MEDIC, 160), FLOAT_MSG_NORMAL);
	set_local_var(5, 1);
end

procedure talk_p_proc
begin
	if (not(local_var(4))) then begin
		call Get_Reaction;
		start_gdialog(SCRIPT_MEDIC, self_obj, 4, -1, -1);
		gsay_start;
		player_hits := get_critter_stat(dude_obj, 35);
		player_max_hits := get_critter_stat(dude_obj, 7);
		if ((local_var(5) == 1) or (global_var(GVAR_WATER_CHIP_4) == 1)) then begin
			call Medic00;
		end
		else begin
			damage := player_max_hits - player_hits;
			if (damage > 7) then begin
				call Medic03;
			end
			else begin
				if (global_var(GVAR_FIND_WATER_CHIP) == 2) then begin
					call Medic24;
				end
				else begin
					call Medic04;
				end
			end
		end
		gsay_end;
		end_dialogue;
	end
	else begin
		display_msg(message_str(SCRIPT_MEDIC, 159));
	end
end

procedure Medic00
begin
	gsay_reply(SCRIPT_MEDIC, 100);
	giq_option(4, SCRIPT_MEDIC, 101, Medic01, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 102, Medic02, NEUTRAL_REACTION);
end

procedure Medic01
begin
	set_local_var(4, 1);
	gsay_message(SCRIPT_MEDIC, 103, NEUTRAL_REACTION);
end

procedure Medic02
begin
	set_local_var(4, 1);
	gsay_message(SCRIPT_MEDIC, 104, NEUTRAL_REACTION);
end

procedure Medic03
begin
	gsay_message(SCRIPT_MEDIC, 105, NEUTRAL_REACTION);
	call Medic16;
end

procedure Medic04
begin
	gsay_reply(SCRIPT_MEDIC, message_str(SCRIPT_MEDIC, 106) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_MEDIC, 107));
	giq_option(4, SCRIPT_MEDIC, 108, Medic06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 109, Medic04a, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 110, Medic05, NEUTRAL_REACTION);
end

procedure Medic04a
begin
	if (damage > 3) then begin
		call Medic14;
	end
	else begin
		call Medic13;
	end
end

procedure Medic05
begin
	gsay_message(SCRIPT_MEDIC, 111, NEUTRAL_REACTION);
	call pushdrugs;
end

procedure Medic06
begin
	gsay_message(SCRIPT_MEDIC, 112, NEUTRAL_REACTION);
end

procedure Medic07
begin
	gsay_reply(SCRIPT_MEDIC, 113);
	giq_option(4, SCRIPT_MEDIC, 114, Medic04a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 115, Medic08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 116, Medic11, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 117, Medic12, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 118, Medic15, NEUTRAL_REACTION);
end

procedure Medic08
begin
	gsay_reply(SCRIPT_MEDIC, 119);
	giq_option(4, SCRIPT_MEDIC, 120, medicend, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_MEDIC, 121, Medic08a, NEUTRAL_REACTION);
end

procedure Medic08a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Medic10;
	end
	else begin
		call Medic09;
	end
end

procedure Medic09
begin
	gsay_message(SCRIPT_MEDIC, 122, NEUTRAL_REACTION);
end

procedure Medic10
begin
	gsay_reply(SCRIPT_MEDIC, 123);
	giq_option(4, SCRIPT_MEDIC, 124, pushdrugs, NEUTRAL_REACTION);
end

procedure Medic11
begin
	gsay_message(SCRIPT_MEDIC, 125, NEUTRAL_REACTION);
end

procedure Medic12
begin
	gsay_reply(SCRIPT_MEDIC, 126);
	call Medic04a;
end

procedure Medic13
begin
	gsay_message(SCRIPT_MEDIC, 127, NEUTRAL_REACTION);
end

procedure Medic14
begin
	gsay_message(SCRIPT_MEDIC, 128, NEUTRAL_REACTION);
	call Medic16;
end

procedure Medic15
begin
	gsay_message(SCRIPT_MEDIC, 129, NEUTRAL_REACTION);
end

procedure Medic16
begin
	gsay_reply(SCRIPT_MEDIC, 130);
	call Medic17;
end

procedure Medic17
begin
	gfade_out(600);
	game_time_advance(game_ticks(damage * 10));
	critter_heal(dude_obj, damage);
	gfade_in(600);
	gsay_message(SCRIPT_MEDIC, 131, NEUTRAL_REACTION);
	if (get_critter_stat(dude_obj, 37) > 30) then begin
		call Medic18;
	end
end

procedure Medic18
begin
	removal := 70 + random(1, 6) + random(1, 6) + random(1, 6);
	rads := get_critter_stat(dude_obj, 37);
	rndx := rads * 20;
	if (rads > 400) then begin
		intensity := message_str(SCRIPT_MEDIC, 132);
	end
	else begin
		if (rads > 200) then begin
			intensity := message_str(SCRIPT_MEDIC, 133);
		end
		else begin
			if (rads > 100) then begin
				intensity := message_str(SCRIPT_MEDIC, 134);
			end
			else begin
				if (rads > 30) then begin
					intensity := message_str(SCRIPT_MEDIC, 135);
				end
			end
		end
	end
	gsay_reply(SCRIPT_MEDIC, intensity);
	giq_option(4, SCRIPT_MEDIC, 138, Medic19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 139, Medic22, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 140, Medic20, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_MEDIC, 141, Medic21, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 142, Medic19, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 143, Medic22, NEUTRAL_REACTION);
end

procedure Medic19
begin
	gsay_message(SCRIPT_MEDIC, 144, NEUTRAL_REACTION);
end

procedure Medic20
begin
	gsay_reply(SCRIPT_MEDIC, message_str(SCRIPT_MEDIC, 145) + rndx + message_str(SCRIPT_MEDIC, 146));
	giq_option(4, SCRIPT_MEDIC, 147, Medic19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 148, Medic22, NEUTRAL_REACTION);
end

procedure Medic21
begin
	gsay_reply(SCRIPT_MEDIC, message_str(SCRIPT_MEDIC, 149) + rndx + message_str(SCRIPT_MEDIC, 150) + removal + message_str(SCRIPT_MEDIC, 151));
	giq_option(4, SCRIPT_MEDIC, 152, Medic19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MEDIC, 153, Medic22, NEUTRAL_REACTION);
end

procedure Medic22
begin
	gsay_message(SCRIPT_MEDIC, random(154, 155), NEUTRAL_REACTION);
	call Medic23;
	radiation_inc(dude_obj, -removal);
end

procedure Medic23
begin
	gfade_out(600);
	game_time_advance(game_ticks(rndx));
	gfade_in(600);
	gsay_message(SCRIPT_MEDIC, 156, NEUTRAL_REACTION);
end

procedure Medic24
begin
	gsay_reply(SCRIPT_MEDIC, 157);
	giq_option(4, SCRIPT_MEDIC, 109, Medic04a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MODREACT, 103, medicend, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MEDIC, 110, Medic05, NEUTRAL_REACTION);
end

procedure flee_dude
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	while(LVar1 < 5) do begin
		if (tile_distance(tile_num(dude_obj), tile_num_in_direction(tile_num(self_obj), LVar1, 3)) > LVar2) then begin
			LVar0 := tile_num_in_direction(tile_num(self_obj), LVar1, 3);
			LVar2 := tile_distance(tile_num(dude_obj), LVar0);
		end
		LVar1 := LVar1 + 1;
	end
	animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_RUN);
end

procedure pushdrugs
begin
	variable LVar0 := 0;
	LVar0 := create_object_sid(40, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar0);
end

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