#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure pre_dialogue;
procedure childend;
procedure childcbt;
procedure child_heal;
procedure child_pois;
procedure child00;
procedure child01;
procedure child02;
procedure child03;
procedure child04;
procedure child04a;
procedure child05;
procedure child05a;
procedure child06;
procedure child07;
procedure child08;
procedure child09;
procedure child09a;
procedure child10;
procedure child11;
procedure child12;
procedure child13;
procedure child14;
procedure child15;
procedure child16;

variable init_teams;
variable hostile1;
variable max_hp;
variable current_hp;
variable cur_pois;
variable hp_injured;
variable cost;
variable pois_cost;
variable cheat;
variable RADCOUNT;

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(init_teams)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 20);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BRAINWASHED_CHILDREN);
		init_teams := 1;
	end
	max_hp := get_critter_stat(dude_obj, 7);
	current_hp := get_critter_stat(dude_obj, 35);
	RADCOUNT := 0;
	if (script_action == talk_proc) then begin
		if (local_var(5) == 0) then begin
			call pre_dialogue;
		end
		else begin
			call childend;
		end
	end
	else begin
		if (script_action == damage_proc) then begin
			set_local_var(4, 1);
		end
		else begin
			if (script_action == look_at_proc) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_CHILDMEM, 100));
			end
			else begin
				if (script_action == pickup_proc) then begin
					hostile1 := 1;
				end
				else begin
					if (script_action == critter_proc) then begin
						if (hostile1) then begin
							hostile1 := 0;
							attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
						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

procedure do_dialogue
begin
	call Get_Reaction;
	start_gdialog(SCRIPT_CHILDMEM, self_obj, 4, -1, -1);
	gsay_start;
	call child00;
	gsay_end;
	end_dialogue;
end

procedure pre_dialogue
begin
	if (local_var(4)) then begin
		call child16;
	end
	else begin
		if (global_var(GVAR_SUPER_MUTANTS_KILLED) > 0) then begin
			call child15;
		end
		else begin
			call do_dialogue;
		end
	end
end

procedure childend
begin
end

procedure childcbt
begin
	hostile1 := 1;
end

procedure child_heal
begin
	game_time_advance(game_ticks(600 * hp_injured));
	critter_heal(dude_obj, hp_injured);
	set_global_var(GVAR_VAULT_13_INVADED_DATE, global_var(GVAR_VAULT_13_INVADED_DATE) - 1);
end

procedure child_pois
begin
	game_time_advance(game_ticks(1800));
	cur_pois := get_poison(dude_obj);
	poison(dude_obj, -cur_pois);
	set_global_var(GVAR_VAULT_13_INVADED_DATE, global_var(GVAR_VAULT_13_INVADED_DATE) - 1);
end

procedure child00
begin
	gsay_reply(SCRIPT_CHILDMEM, 101);
	giq_option(4, SCRIPT_CHILDMEM, 102, child04, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_CHILDMEM, 103, child11, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_CHILDMEM, 104, child01, NEUTRAL_REACTION);
end

procedure child01
begin
	gsay_message(SCRIPT_CHILDMEM, 105, NEUTRAL_REACTION);
	if (current_hp < max_hp) then begin
		hp_injured := max_hp - current_hp;
		call child_heal;
		call child03;
	end
	else begin
		call child02;
	end
end

procedure child02
begin
	gsay_message(SCRIPT_CHILDMEM, 106, NEUTRAL_REACTION);
	call childend;
end

procedure child03
begin
	gsay_message(SCRIPT_CHILDMEM, 107, NEUTRAL_REACTION);
	call childend;
end

procedure child04
begin
	variable LVar0 := 0;
	if (local_var(1) >= 3) then begin
		LVar0 := 5;
		pois_cost := 12;
	end
	else begin
		if (local_var(1) < 2) then begin
			LVar0 := 20;
			pois_cost := 50;
		end
		else begin
			LVar0 := 10;
			pois_cost := 25;
		end
	end
	hp_injured := max_hp - current_hp;
	cost := (hp_injured * LVar0) + pois_cost;
	cheat := 3 * cost / 4;
	if (hp_injured > 0) then begin
		gsay_message(SCRIPT_CHILDMEM, 108, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_CHILDMEM, 109, NEUTRAL_REACTION);
	end
	if (get_poison(dude_obj)) then begin
		gsay_message(SCRIPT_CHILDMEM, 110, NEUTRAL_REACTION);
	end
	if (RADCOUNT > 50) then begin
		gsay_message(SCRIPT_CHILDMEM, 111, NEUTRAL_REACTION);
	end
	if ((hp_injured > 0) or get_poison(dude_obj)) then begin
		gsay_reply(SCRIPT_CHILDMEM, message_str(SCRIPT_CHILDMEM, 113) + cost + message_str(SCRIPT_CHILDMEM, 114));
		giq_option(3, SCRIPT_CHILDMEM, 115, child04a, NEUTRAL_REACTION);
		giq_option(3, SCRIPT_CHILDMEM, 116, childend, NEUTRAL_REACTION);
		giq_option(3, SCRIPT_CHILDMEM, 117, child09, NEUTRAL_REACTION);
		giq_option(3, SCRIPT_CHILDMEM, message_str(SCRIPT_CHILDMEM, 118) + cheat + message_str(SCRIPT_CHILDMEM, 119), child05, NEUTRAL_REACTION);
	end
end

procedure child04a
begin
	if (cost > item_caps_total(dude_obj)) then begin
		call child08;
	end
	else begin
		call child06;
	end
end

procedure child05
begin
	gsay_reply(SCRIPT_CHILDMEM, 120);
	giq_option(3, SCRIPT_CHILDMEM, 121, child05a, NEUTRAL_REACTION);
	giq_option(3, SCRIPT_CHILDMEM, 122, childend, NEUTRAL_REACTION);
end

procedure child05a
begin
	set_local_var(5, 1);
	call childend;
end

procedure child06
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_CHILDMEM, 123, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_CHILDMEM, 124, NEUTRAL_REACTION);
	end
	item_caps_adjust(dude_obj, -cost);
	gfade_out(400);
	gfade_in(400);
	if (hp_injured > 0) then begin
		call child_heal;
	end
	if (get_poison(dude_obj)) then begin
		call child_pois;
	end
	call child07;
end

procedure child07
begin
	gsay_message(SCRIPT_CHILDMEM, 125, NEUTRAL_REACTION);
	call childend;
end

procedure child08
begin
	if (cost > item_caps_total(dude_obj)) then begin
		cost := item_caps_total(dude_obj);
	end
	gsay_reply(SCRIPT_CHILDMEM, 126);
	giq_option(3, SCRIPT_CHILDMEM, 127, child06, NEUTRAL_REACTION);
	giq_option(3, SCRIPT_CHILDMEM, 128, childend, NEUTRAL_REACTION);
end

procedure child09
begin
	gsay_reply(SCRIPT_CHILDMEM, 129);
	giq_option(3, SCRIPT_CHILDMEM, 130, child09a, NEUTRAL_REACTION);
	giq_option(3, SCRIPT_CHILDMEM, 131, childend, NEUTRAL_REACTION);
end

procedure child09a
begin
	if (item_caps_total(dude_obj) >= cost) then begin
		call child10;
	end
	else begin
		cost := item_caps_total(dude_obj);
		call child06;
	end
end

procedure child10
begin
	gsay_message(SCRIPT_CHILDMEM, 132, NEUTRAL_REACTION);
	call child06;
end

procedure child11
begin
	gsay_reply(SCRIPT_CHILDMEM, 133);
	giq_option(3, SCRIPT_CHILDMEM, 134, childend, NEUTRAL_REACTION);
	giq_option(3, SCRIPT_CHILDMEM, 135, child12, NEUTRAL_REACTION);
end

procedure child12
begin
	gsay_reply(SCRIPT_CHILDMEM, 136);
	giq_option(3, SCRIPT_CHILDMEM, 137, child14, NEUTRAL_REACTION);
	giq_option(3, SCRIPT_CHILDMEM, 138, child13, NEUTRAL_REACTION);
end

procedure child13
begin
	gsay_message(SCRIPT_CHILDMEM, 139, NEUTRAL_REACTION);
	call childend;
end

procedure child14
begin
	gsay_reply(SCRIPT_CHILDMEM, 140);
	giq_option(3, SCRIPT_CHILDMEM, 141, childend, NEUTRAL_REACTION);
end

procedure child15
begin
	float_msg(self_obj, message_str(SCRIPT_CHILDMEM, 142), FLOAT_MSG_LIGHT_RED);
	call childend;
end

procedure child16
begin
	float_msg(self_obj, message_str(SCRIPT_CHILDMEM, 143), FLOAT_MSG_LIGHT_RED);
	call childcbt;
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