#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure timeforwhat;
procedure genericend;
procedure genericcbt;
procedure generic00;
procedure generic01;
procedure generic02;
procedure generic03;

variable prev_tile;
variable new_tile;
variable loopcount;
variable noevent;
variable rndx;
variable rndy;
variable rndz;
variable dist;
variable curtime;
variable prevtime;
variable hostile1;
variable SET;
variable MUTANTS;
variable MAXLEASH := 9;
variable DAY;
variable initialized1;


procedure start
begin
	if (not(initialized1)) then begin
		if (local_var(0) == 0) then begin
			set_local_var(0, 1);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 32);
		initialized1 := 1;
	end
	curtime := DAY;
	if (prevtime != curtime) then begin
		noevent := curtime;
		prevtime := curtime;
		animate_move_obj_to_tile(self_obj, local_var(0), ANIMATE_WALK);
	end
	if (script_action == timed_event_proc) then begin
		call timeforwhat;
	end
	else begin
		if (script_action == talk_proc) then begin
			call do_dialogue;
		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
					else begin
						loopcount := loopcount + 1;
						if (loopcount > 40) then begin
							loopcount := 0;
							if (noevent == 0) then begin
								noevent := 1;
								rndx := 5 + random(1, 10);
								add_timer_event(self_obj, game_ticks(rndx), 0);
							end
						end
					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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
							if ((global_var(GVAR_BAD_MONSTER) % 6) == 0) then begin
								set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
							end
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	start_gdialog(SCRIPT_MEANDER, self_obj, 4, -1, -1);
	gsay_start;
	if (hostile1) then begin
		call generic01;
	end
	else begin
		call generic00;
	end
	gsay_end;
	end_dialogue;
end

procedure timeforwhat
begin
	noevent := curtime;
	rndy := random(1, 6);
	if (rndy > 1) then begin
		dist := 2;
	end
	if (rndy > 5) then begin
		dist := 3;
	end
	rndz := random(0, 5);
	prev_tile := tile_num(self_obj);
	new_tile := tile_num_in_direction(prev_tile, rndz, dist);
	if (tile_distance(local_var(0), new_tile) < MAXLEASH) then begin
		animate_move_obj_to_tile(self_obj, new_tile, ANIMATE_WALK);
	end
end

procedure genericend
begin
end

procedure genericcbt
begin
end

procedure generic00
begin
	rndx := random(1, 7);
	if (rndx == 1) then begin
		gsay_message(SCRIPT_MEANDER, 100, NEUTRAL_REACTION);
	end
	if (rndx == 2) then begin
		gsay_message(SCRIPT_MEANDER, 101, NEUTRAL_REACTION);
	end
	if (rndx == 3) then begin
		gsay_message(SCRIPT_MEANDER, 102, NEUTRAL_REACTION);
	end
	if (rndx == 4) then begin
		gsay_message(SCRIPT_MEANDER, 103, NEUTRAL_REACTION);
	end
	if (rndx == 5) then begin
		gsay_message(SCRIPT_MEANDER, 104, NEUTRAL_REACTION);
	end
	if (rndx == 6) then begin
		call generic02;
	end
	if (rndx == 7) then begin
		call generic03;
	end
end

procedure generic01
begin
	rndx := random(1, 4);
	if (rndx == 1) then begin
		gsay_message(SCRIPT_MEANDER, 105, NEUTRAL_REACTION);
	end
	if (rndx == 2) then begin
		gsay_message(SCRIPT_MEANDER, 106, NEUTRAL_REACTION);
	end
	if (rndx == 3) then begin
		gsay_message(SCRIPT_MEANDER, 107, NEUTRAL_REACTION);
	end
	if (rndx == 4) then begin
		gsay_message(SCRIPT_MEANDER, 108, NEUTRAL_REACTION);
	end
end

procedure generic02
begin
	if (SET == 0) then begin
		rndx := random(1, 2);
		if (rndx == 1) then begin
			gsay_message(SCRIPT_MEANDER, 109, NEUTRAL_REACTION);
		end
		if (rndx == 2) then begin
			gsay_message(SCRIPT_MEANDER, 110, NEUTRAL_REACTION);
		end
	end
	else begin
		gsay_message(SCRIPT_MEANDER, 111, NEUTRAL_REACTION);
	end
end

procedure generic03
begin
	if (MUTANTS == 0) then begin
		rndx := random(1, 2);
		if (rndx == 1) then begin
			gsay_message(SCRIPT_MEANDER, 112, NEUTRAL_REACTION);
		end
		if (rndx == 2) then begin
			gsay_message(SCRIPT_MEANDER, 113, NEUTRAL_REACTION);
		end
	end
	else begin
		gsay_message(SCRIPT_MEANDER, 114, NEUTRAL_REACTION);
	end
end