Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enemy Detection System
#1
Enemy Detection System
by GubiD
Aug 12 2007

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given.


This system allows events that are named something including "enemy" to set approach move type to start battles. The will be told to approach Game_Player, until Game_Player has gotten a specified distance away. At that time the event will return to its original position if desired.

Please see the demo for a visual demonstration.

If you use this script, please credit myself and Near Fantasia for the path finding.

EDIT:
I have made a couple of modifcations to this to fix the enemy from walking through walls, tree's and whatever other objesticals they might have between them and their original position. These changes are also updated in the demo as well.

EDIT:
I have made some additional improvements to the script and reposted it below. I will try to get the demo updated before the day is out.

EDIT:
Added ability to use a easy variable so that distance is easy to set... also changed it to be <= which means it is that distance and below, not one less than the typed number. Also did a quick bug fix I noticed while testing something else.

EDIT: 10/2/07
Added ability to set and resume speed for the chasing event, if desired. Updated Demo!

EDIT: 8/18/08
Created the SNEAK system using common events. Must use NEW download link...
Download

Please disregard this download link.. unless you dont want the SNEAK system.

.zip   EnemyDetect.zip (Size: 206.2 KB / Downloads: 2)

Code:
#--------------------------------------------------------------------------
# Enemy Detection System
# by Gubid - 8/12/07
# v 1.2.2 Updated 10/2/07
#--------------------------------------------------------------------------
#Description:
#This system allows events that are named something including "ENEMY" then
#that event will be told to approach Game_Player, until Game_Player has gotten
#a specified distance away. At that time event will return to its original
#position if desired. If you happen to get close enough to the returning event
#it will begin to follow you again.
#
#Thanks to Near Fantasia for supplying the slightly modified path finding script.
#--------------------------------------------------------------------------
#if there are any questions as to the use of this script or
#modifications/additions/etc, please post it to the thread on CreationAyslum.net
#--------------------------------------------------------------------------

#--------------------------------------------------------------------------
#Update_Detect - How to Stop Detection if desired
#--------------------------------------------------------------------------
#1. Within a event, add a SCRIPT (3rd page)
#2. type "update_detect(EVENT_ID, TRUE/FALSE)" -Note: it defaults to false

#--------------------------------------------------------------------------
#RETURN TO POSITION
#--------------------------------------------------------------------------
#Use this to determine if you would like the event to return to its original
#position or not.
#--------------------------------------------------------------------------
RETURN_TO_POSITION = true

#--------------------------------------------------------------------------
#CHASE_SPEED and ENABLE_SPEED_CHANGE
#--------------------------------------------------------------------------
#Use this to set the speed in which enemies will approach after spotting you
#and whether or not their speed will change.
#--------------------------------------------------------------------------
CHASE_SPEED = 3
ENABLE_SPEED_CHANGE = true

#--------------------------------------------------------------------------
#VIEW_RANGE
#--------------------------------------------------------------------------
#Use this to set the distance in which you must be in order to start the detection
#process, or escape to stop.
#--------------------------------------------------------------------------
VIEW_RANGE = 6

#--------------------------------------------------------------------------
#ESCAPE_RANGE
#--------------------------------------------------------------------------
#Use this to set the distance in which you must escape to stop the detection process
#--------------------------------------------------------------------------
ESCAPE_RANGE = 9

#--------------------------------------------------------------------------
#Other Notes
#--------------------------------------------------------------------------
#If the event has a custom move route, they will resume that route when finished
#chasing you, but if you dont set RETURN_TO_POSITION, then they will resume
#their custom route at the location they stopped chasing you.

class Game_Map
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
# Refresh map if necessary
if $game_map.need_refresh
refresh
end
# If scrolling
if @scroll_rest > 0
# Change from scroll speed to distance in map coordinates
distance = 2 ** @scroll_speed
# Execute scrolling
case @scroll_direction
when 2 # Down
scroll_down(distance)
when 4 # Left
scroll_left(distance)
when 6 # Right
scroll_right(distance)
when 8 # Up
scroll_up(distance)
end
# Subtract distance scrolled
@scroll_rest -= distance
end
# Update map event
for event in @events.values
event.update
###HERE IS WHAT I ADDED
if !event.name.include?("enemy")
next
else
if event.character_name != ""
if event.stop_detect?
next
end
if check_distance(event.id) == true
if event.exclamation == false
event.old_type = event.move_type
event.move_type = 2
if ENABLE_SPEED_CHANGE
event.old_speed = event.move_speed
event.move_speed = CHASE_SPEED
end
event.move_speed = CHASE_SPEED
event.exclamation = true
event.animation_id = 98 #98 happens to be the exclamation animation
event.f_route.clear
end
else
if event.exclamation == true
event.exclamation = false
if RETURN_TO_POSITION == true
if event.x != @map.events[event.id].x or event.y != @map.events[event.id].y
route = get_route(event, @map.events[event.id].x, @map.events[event.id].y)
event.run_route(route)
end
else
event.restore_route
end
end
end
end
end
###END WHAT I ADDED
end
# Update common event
for common_event in @common_events.values
common_event.update
end
# Manage fog scrolling
@fog_ox -= @fog_sx / 8.0
@fog_oy -= @fog_sy / 8.0
# Manage change in fog color tone
if @fog_tone_duration >= 1
d = @fog_tone_duration
target = @fog_tone_target
@fog_tone.red = (@fog_tone.red * (d - 1) + target.red) / d
@fog_tone.green = (@fog_tone.green * (d - 1) + target.green) / d
@fog_tone.blue = (@fog_tone.blue * (d - 1) + target.blue) / d
@fog_tone.gray = (@fog_tone.gray * (d - 1) + target.gray) / d
@fog_tone_duration -= 1
end
# Manage change in fog opacity level
if @fog_opacity_duration >= 1
d = @fog_opacity_duration
@fog_opacity = (@fog_opacity * (d - 1) + @fog_opacity_target) / d
@fog_opacity_duration -= 1
end
end

def check_distance(id = nil)
unless id == nil
event = $game_map.events[id]
range = ($game_player.x - event.x).abs + ($game_player.y - event.y).abs
if range <= VIEW_RANGE and range > 0
return true
elsif range <= ESCAPE_RANGE and range > 0 and event.exclamation
return true
end
end
end

def get_route(event, x=0, y=0)
target_x = x
target_y = y
max = (target_x - event.x).abs + (target_y - event.y).abs + 5
position = [[event.x, event.y]]
route = [[]]
more_step = [0]
for i in more_step
x = position[i][0]
y = position[i][1]
if !position.include?([x, y + 1]) and event.passable?(x, y, 2)
position.push([x, y + 1])
route.push(route[i] + [2])
if route[i].size + 1 < max
more_step.push(route.index(route[i] + [2]))
end
end

if !position.include?([x - 1, y]) and event.passable?(x, y, 4)
position.push([x - 1, y])
route.push(route[i] + [4])
if route[i].size + 1 < max
more_step.push(route.index(route[i] + [4]))
end
end

if !position.include?([x + 1, y]) and event.passable?(x, y, 6)
position.push([x + 1, y])
route.push(route[i] + [6])
if route[i].size + 1 < max
more_step.push(route.index(route[i] + [6]))
end
end

if !position.include?([x, y - 1]) and event.passable?(x, y, 8)
position.push([x, y - 1])
route.push(route[i] + [8])
if route[i].size + 1 < max
more_step.push(route.index(route[i] + [8]))
end
end
end
if position.index([target_x, target_y]) == false
return [2]
else
return route[position.index([target_x, target_y])]
end
end
end


class Game_Event
attr_accessor :move_type
attr_accessor :move_speed
attr_accessor :old_speed
attr_accessor :exclamation
attr_accessor :old_type
attr_accessor :stop_trigger
attr_reader :f_route

def initialize(map_id, event)
super()
@map_id = map_id
@event = event
@id = @event.id
@erased = false
@starting = false
@through = true
@name = @event.name
@exclamation = false
@f_route = []
@step = 0
@old_type = @move_type
@old_move_route = @move_route
@old_speed = @move_speed
@stop_trigger = false
# Move to starting position
moveto(@event.x, @event.y)
refresh

end

def stop_detect?
return @stop_trigger
end

def restore_route
@move_type = @old_type
@move_route_index = @original_move_route_index
@move_speed = @old_speed
refresh
return
end

def name
return @name
end

alias en_det_update update
def update
if @f_route.size > 0
run_path unless self.moving?
if @f_route.size == 0
restore_route
end
end
en_det_update
end

def run_route(route)
@f_route = route
@step = 0
end

def run_path
if @f_route.size > 0
if @f_route[@step] == nil
@f_route = []
end
action = @f_route[@step]
@step += 1
case action
when 2
self.move_down
when 4
self.move_left
when 6
self.move_right
when 8
self.move_up
end
end
end
end

class Interpreter
def update_detect(id, set = false)
$game_map.events[id].stop_trigger = set
end

def spotted?
is_true = 0
for event in $game_map.events.values
if event.exclamation
is_true += 1
end
end
if is_true > 0
return true
else
return false
end
end

def change_switch(id = 0, value = false, type = 0, switch = "A")
if id > 0
if type == 0 #for use of main switches
#id is switch_id and value should set to true or false
$game_switches[id] = value
elsif type == 1 #For use of self switches
#id is event.id and switch is the desired selfswitch. It must be text!
#value is true or false
key = [$game_map.map_id, id, switch]
# Updates self switch
$game_self_switches[key] = value
end
# Refresh map
$game_map.need_refresh = true
return true
end
end
end
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  A little Modification for Screenshot system Maumau 0 2,445 12-04-2008, 01:00 PM
Last Post: Maumau
  Summon/Custom Skills System! Reno-s--Joker 0 2,300 10-07-2008, 01:00 PM
Last Post: Reno-s--Joker
  Rotimikid's Enemy level Script ShockWave 0 2,357 08-24-2007, 01:00 PM
Last Post: ShockWave
  Enemy HP Bars El Conductor 0 2,163 07-29-2007, 01:00 PM
Last Post: El Conductor
  CNS (Custom Name System) Samo the thief 0 2,052 02-12-2007, 01:00 PM
Last Post: Samo the thief
  CSS (Custom Shop System) Samo the thief 0 2,390 01-29-2007, 01:00 PM
Last Post: Samo the thief
  My Saving system crystal 0 1,986 01-16-2007, 01:00 PM
Last Post: crystal
  Star Ocean Shop System Hawk-McKain 0 2,113 10-20-2006, 01:00 PM
Last Post: Hawk-McKain
  Actor Enemy HP Bars Raziel 0 2,755 07-14-2006, 01:00 PM
Last Post: Raziel
  Slipknot Advance Debug System Sheol 0 1,771 12-23-2005, 01:00 PM
Last Post: Sheol



Users browsing this thread: