#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure damage_p_proc;
procedure look_at_p_proc;
procedure timed_event_p_proc;
procedure SendLemmyAtDay;
procedure Lemmy01;
procedure Lemmy02;
procedure Lemmy03;
procedure Lemmy04;
procedure Lemmy05;
procedure Lemmy06;
procedure Lemmy08;
procedure Lemmy09;
procedure Lemmy10;
procedure Lemmy10a;
procedure Lemmy10b;
procedure Lemmy10c;
procedure Lemmy11;
procedure Lemmy12;
procedure Lemmy13;
procedure Lemmy14;
procedure Lemmy15;
procedure Lemmy16;
procedure Lemmy17;
procedure Lemmy18;
procedure Lemmy19;
procedure Lemmy20;
procedure Lemmy20a;
procedure Lemmy20b;
procedure Lemmy20c;
procedure Lemmy20d;
procedure Lemmy20e;
procedure Lemmy20f;
procedure Lemmy21;
procedure Lemmy22;
procedure Lemmy23;
procedure Lemmy24;
procedure Lemmy24a;
procedure Lemmy24b;
procedure Lemmy24c;
procedure Lemmy24d;
procedure Lemmy24e;
procedure Lemmy24f;
procedure Lemmy25;
procedure Lemmy26;
procedure Lemmy27;
procedure LemmyEnd;
procedure LemmyCombat;

variable hostile1;
variable Only_Once := 1;
variable lastPsst;
variable Destination;
variable LastMove;
variable SetDayNight;

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
	variable LVar0 := 0;
	if (Only_Once) then begin
		Only_Once := 0;
		if (local_var(7) == 0) then begin
			set_local_var(7, 1);
			LVar0 := item_caps_adjust(self_obj, 500);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 41);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_HUB_PEASANTS);
		LastMove := 21325;
	end
	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
			else 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 == timed_event_proc) then begin
							call timed_event_p_proc;
						end
					end
				end
			end
		end
	end
end

procedure combat
begin
	hostile1 := 1;
end

procedure critter_p_proc
begin
	variable LVar0 := 0;
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	if (SetDayNight == 0) then begin
		add_timer_event(self_obj, game_ticks(random(5, 20)), 1);
		SetDayNight := 1;
	end
	if (local_var(6) == 0) then begin
		if ((((game_time / 10) - lastPsst) >= 10) and (tile_distance_objs(self_obj, dude_obj) <= 6)) then begin
			lastPsst := game_time / 10;
			float_msg(self_obj, message_str(SCRIPT_LEMMY, 101), FLOAT_MSG_RED);
		end
	end
	LVar0 := tile_num(self_obj);
	if ((LVar0 == 21917) or (LVar0 == 20116) or (LVar0 == 25318)) then begin
		if (anim_busy(self_obj) == 0) then begin
			if (has_trait(1, self_obj, 10) != 3) then begin
				anim(self_obj, 1000, 3);
			end
		end
	end
	else begin
		if (LVar0 == 26327) then begin
			if (anim_busy(self_obj) == 0) then begin
				if (has_trait(1, self_obj, 10) != 2) then begin
					anim(self_obj, 1000, 2);
				end
			end
		end
	end
end

procedure pickup_p_proc
begin
	if (source_obj == dude_obj) then begin
		hostile1 := 1;
	end
end

procedure talk_p_proc
begin
	call Get_Reaction;
	set_local_var(6, 1);
	if (local_var(4) == 0) then begin
		set_local_var(4, 1);
		start_gdialog(SCRIPT_LEMMY, self_obj, 4, -1, -1);
		gsay_start;
		call Lemmy01;
		gsay_end;
		end_dialogue;
	end
	else begin
		if (local_var(5) == 1) then begin
			start_gdialog(SCRIPT_LEMMY, self_obj, 4, -1, -1);
			gsay_start;
			call Lemmy26;
			gsay_end;
			end_dialogue;
		end
		else begin
			start_gdialog(SCRIPT_LEMMY, self_obj, 4, -1, -1);
			gsay_start;
			call Lemmy27;
			gsay_end;
			end_dialogue;
		end
	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
end

procedure damage_p_proc
begin
	variable LVar0 := 0;
	LVar0 := obj_pid(source_obj);
	if (party_member_obj(LVar0) != 0) then begin
		set_global_var(GVAR_ENEMY_HUB, 1);
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_LEMMY, 100));
end

procedure timed_event_p_proc
begin
	call SendLemmyAtDay;
end

procedure SendLemmyAtDay
begin
	variable LVar0 := 0;
	Destination := 0;
	LVar0 := random(25, 45);
	while(Destination == 0) do begin
		Destination := random(1, 9);
		if (Destination == 1) then begin
			Destination := 25931;
		end
		else begin
			if (Destination == 2) then begin
				Destination := 25551;
			end
			else begin
				if (Destination == 3) then begin
					Destination := 21325;
				end
				else begin
					if (Destination == 4) then begin
						Destination := 21917;
					end
					else begin
						if (Destination == 5) then begin
							Destination := 18925;
						end
						else begin
							if (Destination == 6) then begin
								Destination := 23322;
							end
							else begin
								if (Destination == 7) then begin
									Destination := 26327;
								end
								else begin
									if (Destination == 8) then begin
										Destination := 20116;
									end
									else begin
										if (Destination == 9) then begin
											Destination := 25318;
										end
									end
								end
							end
						end
					end
				end
			end
		end
		if (Destination == LastMove) then begin
			Destination := 0;
		end
	end
	LastMove := Destination;
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_tile(self_obj, Destination, -1);
	reg_anim_func(3, 0);
	rm_timer_event(self_obj);
	add_timer_event(self_obj, game_ticks(LVar0), 1);
end

procedure Lemmy01
begin
	gsay_reply(SCRIPT_LEMMY, 102);
	giq_option(4, SCRIPT_LEMMY, 103, Lemmy03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 104, Lemmy04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 105, Lemmy05, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_LEMMY, 106, Lemmy02, NEUTRAL_REACTION);
end

procedure Lemmy02
begin
	gsay_reply(SCRIPT_LEMMY, 107);
	giq_option(-3, SCRIPT_LEMMY, 108, LemmyEnd, NEUTRAL_REACTION);
end

procedure Lemmy03
begin
	set_local_var(5, 1);
	gsay_message(SCRIPT_LEMMY, 109, NEUTRAL_REACTION);
end

procedure Lemmy04
begin
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_LEMMY, 110);
	end
	else begin
		gsay_reply(SCRIPT_LEMMY, 178);
	end
	giq_option(4, SCRIPT_LEMMY, 111, Lemmy06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 112, LemmyCombat, BAD_REACTION);
	giq_option(5, SCRIPT_LEMMY, 113, Lemmy03, NEUTRAL_REACTION);
end

procedure Lemmy05
begin
	gsay_reply(SCRIPT_LEMMY, 114);
	giq_option(4, SCRIPT_LEMMY, 116, Lemmy23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 117, Lemmy09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 118, Lemmy03, NEUTRAL_REACTION);
end

procedure Lemmy06
begin
	gsay_reply(SCRIPT_LEMMY, 119);
	giq_option(4, SCRIPT_LEMMY, 120, Lemmy05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 121, Lemmy09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 123, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy08
begin
	gsay_message(SCRIPT_LEMMY, 128, NEUTRAL_REACTION);
end

procedure Lemmy09
begin
	gsay_reply(SCRIPT_LEMMY, 129);
	call Lemmy18;
end

procedure Lemmy10
begin
	gsay_reply(SCRIPT_LEMMY, 130);
	giq_option(4, SCRIPT_LEMMY, 131, Lemmy10a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 132, Lemmy10b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 133, Lemmy10c, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 134, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy10a
begin
	variable LVar0 := 0;
	if (item_caps_total(dude_obj) >= 500) then begin
		LVar0 := item_caps_adjust(dude_obj, -500);
		call Lemmy13;
	end
	else begin
		call Lemmy11;
	end
end

procedure Lemmy10b
begin
	variable LVar0 := 0;
	if (is_success(do_check(dude_obj, 3, -3)) or is_success(roll_vs_skill(dude_obj, 15, -30))) then begin
		if (item_caps_total(dude_obj) >= 400) then begin
			LVar0 := item_caps_adjust(dude_obj, -400);
			call Lemmy14;
		end
		else begin
			call Lemmy11;
		end
	end
	else begin
		call Lemmy11;
	end
end

procedure Lemmy10c
begin
	if (is_success(do_check(dude_obj, 0, -3))) then begin
		call Lemmy15;
	end
	else begin
		call Lemmy12;
	end
end

procedure Lemmy11
begin
	gsay_message(SCRIPT_LEMMY, 135, NEUTRAL_REACTION);
end

procedure Lemmy12
begin
	set_local_var(5, 1);
	gsay_message(SCRIPT_LEMMY, 136, BAD_REACTION);
end

procedure Lemmy13
begin
	gsay_message(SCRIPT_LEMMY, 137, NEUTRAL_REACTION);
	call Lemmy16;
end

procedure Lemmy14
begin
	gsay_message(SCRIPT_LEMMY, 138, NEUTRAL_REACTION);
	call Lemmy16;
end

procedure Lemmy15
begin
	gsay_message(SCRIPT_LEMMY, 139, NEUTRAL_REACTION);
	call Lemmy16;
end

procedure Lemmy16
begin
	gsay_reply(SCRIPT_LEMMY, 140);
	giq_option(4, SCRIPT_LEMMY, 141, Lemmy17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 142, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy17
begin
	gsay_message(SCRIPT_LEMMY, 143, NEUTRAL_REACTION);
	call Lemmy18;
end

procedure Lemmy18
begin
	if (global_var(GVAR_FIND_WATER_CHIP) != 2) then begin
		giq_option(4, SCRIPT_LEMMY, 144, Lemmy10, NEUTRAL_REACTION);
	end
	if (global_var(GVAR_DECKER_KNOWN) == 1) then begin
		giq_option(4, SCRIPT_LEMMY, 145, Lemmy19, NEUTRAL_REACTION);
	end
	if (map_var(41) == 1) then begin
		giq_option(4, SCRIPT_LEMMY, 146, Lemmy20, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_LEMMY, 147, Lemmy23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 148, Lemmy08, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_LEMMY, 106, Lemmy02, NEUTRAL_REACTION);
end

procedure Lemmy19
begin
	gsay_reply(SCRIPT_LEMMY, 149);
	giq_option(4, SCRIPT_LEMMY, 150, Lemmy17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 151, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy20
begin
	gsay_reply(SCRIPT_LEMMY, 152);
	giq_option(4, SCRIPT_LEMMY, 153, Lemmy20a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 154, Lemmy20b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 155, Lemmy20c, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 156, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy20a
begin
	variable LVar0 := 0;
	if (item_caps_total(dude_obj) >= 300) then begin
		LVar0 := item_caps_adjust(dude_obj, -300);
		call Lemmy20d;
	end
	else begin
		call Lemmy11;
	end
end

procedure Lemmy20b
begin
	variable LVar0 := 0;
	if (is_success(do_check(dude_obj, 3, -3)) or is_success(roll_vs_skill(dude_obj, 15, -30))) then begin
		if (item_caps_total(dude_obj) >= 240) then begin
			LVar0 := item_caps_adjust(dude_obj, -240);
			call Lemmy20e;
		end
		else begin
			call Lemmy11;
		end
	end
	else begin
		call Lemmy11;
	end
end

procedure Lemmy20c
begin
	if (is_success(do_check(dude_obj, 0, -3))) then begin
		call Lemmy20f;
	end
	else begin
		call Lemmy12;
	end
end

procedure Lemmy20d
begin
	gsay_message(SCRIPT_LEMMY, 137, NEUTRAL_REACTION);
	call Lemmy21;
end

procedure Lemmy20e
begin
	gsay_message(SCRIPT_LEMMY, 138, NEUTRAL_REACTION);
	call Lemmy21;
end

procedure Lemmy20f
begin
	gsay_message(SCRIPT_LEMMY, 139, NEUTRAL_REACTION);
	call Lemmy21;
end

procedure Lemmy21
begin
	gsay_reply(SCRIPT_LEMMY, 157);
	giq_option(4, SCRIPT_LEMMY, 158, Lemmy22, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 159, Lemmy17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 160, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy22
begin
	gsay_reply(SCRIPT_LEMMY, 161);
	giq_option(4, SCRIPT_LEMMY, 162, Lemmy17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 163, LemmyCombat, BAD_REACTION);
	giq_option(4, SCRIPT_LEMMY, 164, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy23
begin
	gsay_reply(SCRIPT_LEMMY, 165);
	giq_option(4, SCRIPT_LEMMY, 166, Lemmy24, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 167, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy24
begin
	gsay_reply(SCRIPT_LEMMY, 168);
	giq_option(4, SCRIPT_LEMMY, 169, Lemmy24a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 170, Lemmy24b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 171, Lemmy24c, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 172, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy24a
begin
	variable LVar0 := 0;
	if (item_caps_total(dude_obj) >= 1000) then begin
		LVar0 := item_caps_adjust(dude_obj, -1000);
		call Lemmy24d;
	end
	else begin
		call Lemmy11;
	end
end

procedure Lemmy24b
begin
	variable LVar0 := 0;
	if (is_success(do_check(dude_obj, 3, -3)) or is_success(roll_vs_skill(dude_obj, 15, -30))) then begin
		if (item_caps_total(dude_obj) >= 800) then begin
			LVar0 := item_caps_adjust(dude_obj, -800);
			call Lemmy24e;
		end
		else begin
			call Lemmy11;
		end
	end
	else begin
		call Lemmy11;
	end
end

procedure Lemmy24c
begin
	if (is_success(do_check(dude_obj, 0, -3))) then begin
		call Lemmy24f;
	end
	else begin
		call Lemmy12;
	end
end

procedure Lemmy24d
begin
	gsay_message(SCRIPT_LEMMY, 137, NEUTRAL_REACTION);
	call Lemmy25;
end

procedure Lemmy24e
begin
	gsay_message(SCRIPT_LEMMY, 138, NEUTRAL_REACTION);
	call Lemmy25;
end

procedure Lemmy24f
begin
	gsay_message(SCRIPT_LEMMY, 139, NEUTRAL_REACTION);
	call Lemmy25;
end

procedure Lemmy25
begin
	set_global_var(GVAR_MASTER_FILLER_9, 1);
	gsay_reply(SCRIPT_LEMMY, 173);
	giq_option(4, SCRIPT_LEMMY, 174, Lemmy17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEMMY, 175, Lemmy08, NEUTRAL_REACTION);
end

procedure Lemmy26
begin
	gsay_message(SCRIPT_LEMMY, 176, NEUTRAL_REACTION);
end

procedure Lemmy27
begin
	gsay_reply(SCRIPT_LEMMY, 177);
	call Lemmy18;
end

procedure LemmyEnd
begin
end

procedure LemmyCombat
begin
	set_local_var(5, 1);
	call combat;
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