#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 damage_p_proc;
procedure gambler1;
procedure gambler2;
procedure gambler3;
procedure gambler4;
procedure gambler5;

variable hostile1;
variable weapon_check;

procedure pickup_p_proc;


procedure start
begin
	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 ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			if (tile_distance_objs(self_obj, dude_obj) < 12) then begin
				if (not(weapon_check)) then begin
					dialogue_system_enter;
					add_timer_event(self_obj, game_ticks(600), 1);
				end
			end
		end
	end
	if (hostile1) then begin
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
	if (cur_map_index == MAP_JUNKCSNO) then begin
		if ((global_var(GVAR_CAPTURE_GIZMO) == 2) or (global_var(GVAR_GIZMO_DEAD) == 1)) then begin
			destroy_object(self_obj);
		end
	end
end

procedure destroy_p_proc
begin
	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;
	display_msg(message_str(SCRIPT_GENGAMBL, 100));
end

procedure map_enter_p_proc
begin
	if (local_var(0) == 0) then begin
		set_local_var(0, tile_num(self_obj));
	end
	if (not(obj_is_carrying_obj_pid(self_obj, 41))) then begin
		item_caps_adjust(self_obj, random(0, 200));
	end
	if (cur_map_index == MAP_JUNKCSNO) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 24);
		if ((global_var(GVAR_CAPTURE_GIZMO) == 2) or (global_var(GVAR_GIZMO_DEAD) == 1)) then begin
			destroy_object(self_obj);
		end
	end
end

procedure talk_p_proc
begin
	script_overrides;
	if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
		call gambler1;
	end
	else begin
		if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
			call gambler3;
		end
		else begin
			if (global_var(GVAR_KILL_KILLIAN) == 2) then begin
				call gambler4;
			end
			else begin
				if (global_var(GVAR_PLAYER_REPUTATION) < -10) then begin
					call gambler2;
				end
				else begin
					call gambler5;
				end
			end
		end
	end
end

procedure timed_event_p_proc
begin
	weapon_check := 0;
end

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

procedure gambler1
begin
	variable LVar0 := 0;
	weapon_check := 1;
	LVar0 := random(1, 3);
	if (LVar0 == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 101), FLOAT_MSG_NORMAL);
	end
	else begin
		if (LVar0 == 2) then begin
			float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 102), FLOAT_MSG_NORMAL);
		end
		else begin
			if (LVar0 == 3) then begin
				if (dude_is_male) then begin
					float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 103), FLOAT_MSG_NORMAL);
				end
				else begin
					float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 104), FLOAT_MSG_NORMAL);
				end
			end
		end
	end
end

procedure gambler2
begin
	float_msg(self_obj, message_str(SCRIPT_GENGAMBL, random(105, 107)), FLOAT_MSG_NORMAL);
end

procedure gambler3
begin
	float_msg(self_obj, message_str(SCRIPT_GENGAMBL, random(108, 110)), FLOAT_MSG_NORMAL);
end

procedure gambler4
begin
	float_msg(self_obj, message_str(SCRIPT_GENGAMBL, random(111, 113)), FLOAT_MSG_NORMAL);
end

procedure gambler5
begin
	variable LVar0 := 0;
	LVar0 := random(1, 6);
	if (LVar0 == 1) then begin
		if ((game_time_hour >= 700) and (game_time_hour < 1800)) then begin
			float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 114), FLOAT_MSG_NORMAL);
		end
		else begin
			float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 115), FLOAT_MSG_NORMAL);
		end
	end
	else begin
		float_msg(self_obj, message_str(SCRIPT_GENGAMBL, 114 + LVar0), FLOAT_MSG_NORMAL);
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end