New Event Location System
#1
NELS
New Event Location System
By Ánemus
Aug 29 2008

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


Description

It was, indeed, the idea of making a C Action system (that tells you the actions that can be taken with C) that made me do this script. There is no way I could check for each event in the map to see if they are close or below the character to check for the action that can be taken, not uless I patch the default system and put it on top, which would have been smarter but I didn't think of it then. So I made a new system. The system didn't have anything to do whith NELS until I found out how the event checking worked (I already knew how it worked, didn't meditate about it though). So what it did was actually checking every single event in the map each time you pressed a direction key (an actually move), or press C. So it was exactly my first gess to make the C Action system, and the guess I didn't follow. So, one day, while talking to Ito, he asked me to make a script that you can give it a location on the map, and then it will return the id of the event (if any) that is there. The idea is to store it in a variable, but I wanted to make it a bit more universal.

So I thought of the C Action System, and give it a try. After solving a few bugs, reorganizing the systems and check over my ideas, I got this script which I was going to cal NELSON but I didnt found the O and the N. So is NELS alone.

What it does
  1. First of all, it still uses the id event system (original) so there are not supposed to be any compatibility issues with other scripts. What it actually does, is to have a reference system that has the positions as key and the id as value (you can read the script, there is no point in keeping the secret.
  2. This is actually supposed to be the first thing it does. Reduces Lag a bit in heavily charged maps. The try out whas with two games one with the NELS and the other without it, in a poor computer and not telling the tester which one had the NELS. I asked him to run it, and said the NELS one run better. By the way, the game contained a single map 40x30 with 300 events.
  3. Seondly, it allows you to get the id of an event located on the postion you want, which you can use in scripts, or in events. I will explain the syntaxis of both.
  4. Also, and this is a rather stupid point cuz you could do it before, but no one said how, is that you can get the position of an event that you say the ID for scripts (which is useless, there is another way, but mine is shorter i guess) or for events (which will be helpful for those that don't know that way).
  5. Finally, I should say, It is the perfect base for a C Action script. Not going to say how to do it, because then everyone would have one, but this system makes it rather simple smooth and fast.

Code

Code:
class Game_Map
  attr_accessor :relation
  def setup(map_id)
    @map_id = map_id
    @map = load_data(sprintf("Data/Map%03d.rxdata", @map_id))
    tileset = $data_tilesets[@map.tileset_id]
    @tileset_name = tileset.tileset_name
    @autotile_names = tileset.autotile_names
    @panorama_name = tileset.panorama_name
    @panorama_hue = tileset.panorama_hue
    @fog_name = tileset.fog_name
    @fog_hue = tileset.fog_hue
    @fog_opacity = tileset.fog_opacity
    @fog_blend_type = tileset.fog_blend_type
    @fog_zoom = tileset.fog_zoom
    @fog_sx = tileset.fog_sx
    @fog_sy = tileset.fog_sy
    @battleback_name = tileset.battleback_name
    @passages = tileset.passages
    @priorities = tileset.priorities
    @terrain_tags = tileset.terrain_tags
    @display_x = 0
    @display_y = 0
    @need_refresh = false
    @events = {}
    @relation = {}
    for i in @map.events.keys
      relationkey = "#{@map.events[i].x}x#{@map.events[i].y}"
      @relation[relationkey] = i
      key = i
      @events[key] = Game_Event.new(@map_id, @map.events[i])
    end
    @common_events = {}
    for i in 1...$data_common_events.size
      @common_events[i] = Game_CommonEvent.new(i)
    end
    @fog_ox = 0
    @fog_oy = 0
    @fog_tone = Tone.new(0, 0, 0, 0)
    @fog_tone_target = Tone.new(0, 0, 0, 0)
    @fog_tone_duration = 0
    @fog_opacity_duration = 0
    @fog_opacity_target = 0
    @scroll_direction = 2
    @scroll_rest = 0
    @scroll_speed = 4
  end
  def get_id (x, y)
    r = @relation["#{x}x#{y}"]
    return r[r.size-1] if r != nil and r.is_a?(Array)
    return r if r != nil
    return -1
  end
  def get_x (id)
    return @events[id].x if @events[id] != nil
    return -1
  end
  def get_y (id)
    return @events[id].y if @events[id] != nil
    return -1
  end
end
#===============================================================================

#
#===============================================================================

class Game_Character
  def passable?(x, y, d)
    r = $game_map.relation
    new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
    new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
    unless $game_map.valid?(new_x, new_y)
      return false
    end
    if @through
      return true
    end
    unless $game_map.passable?(x, y, d, self)
      return false
    end
    unless $game_map.passable?(new_x, new_y, 10 - d)
      return false
    end
    if $game_map.events[r["#{new_x}x#{new_y}"]] != nil and  not $game_map.events[r["#{new_x}x#{new_y}"]].through
      if self != $game_player
        return false
      end
      if $game_map.events[r["#{new_x}x#{new_y}"]].character_name != ""
        return false
      end
    end
    if $game_player.x == new_x and $game_player.y == new_y
      unless $game_player.through
        if @character_name != ""
          return false
        end
      end
    end
    return true
  end
end
#===============================================================================

#
#===============================================================================

class Game_Player
  def check_event_trigger_here(triggers)
    r = $game_map.relation
    result = false
    if $game_system.map_interpreter.running?
      return result
    end
    if not r["#{@x}x#{@y}"].is_a?(Array)
      if $game_map.events[r["#{@x}x#{@y}"]] != nil and triggers.include?($game_map.events[r["#{@x}x#{@y}"]].trigger)
        if not $game_map.events[r["#{@x}x#{@y}"]].jumping? and $game_map.events[r["#{@x}x#{@y}"]].over_trigger?
          $game_map.events[r["#{@x}x#{@y}"]].start
          result = true
        end
      end
    else
      for i in r["#{@x}x#{@y}"]
        if $game_map.events[i] != nil and triggers.include?($game_map.events[i].trigger)
          if not $game_map.events[i].jumping? and $game_map.events[i].over_trigger?
            $game_map.events[i].start
            result = true
          end
        end
      end
    end
    return result
  end
#===============================================================================

  def check_event_trigger_there(triggers)
    r = $game_map.relation
    result = false
    if $game_system.map_interpreter.running?
      return result
    end
    new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
    new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
    if not r["#{new_x}x#{new_y}"].is_a?(Array)
      if $game_map.events[r["#{new_x}x#{new_y}"]] != nil and triggers.include?($game_map.events[r["#{new_x}x#{new_y}"]].trigger)
        if not $game_map.events[r["#{new_x}x#{new_y}"]].jumping? and not $game_map.events[r["#{new_x}x#{new_y}"]].over_trigger?
          $game_map.events[r["#{new_x}x#{new_y}"]].start
          result = true
        end
      end
    else
      for i in r["#{new_x}x#{new_y}"]
        if $game_map.events[i] != nil and triggers.include?($game_map.events[i].trigger)
          if not $game_map.events[i].jumping? and not $game_map.events[i].over_trigger?
            $game_map.events[i].start
            result = true
          end
        end
      end
    end
    if result == false
      if $game_map.counter?(new_x, new_y)
        new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
        new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
        if not r["#{new_x}x#{new_y}"].is_a?(Array)
          if $game_map.events[r["#{new_x}x#{new_y}"]] != nil and triggers.include?($game_map.events["#{new_x}x#{new_y}"].trigger)
            if not $game_map.events[r["#{new_x}x#{new_y}"]].jumping? and not $game_map.events["#{new_x}x#{new_y}"].over_trigger?
              $game_map.events[r["#{new_x}x#{new_y}"]].start
              result = true
            end
          end
        else
          for i in r["#{new_x}x#{new_y}"]
            if $game_map.events[i] != nil and triggers.include?($game_map.events[i].trigger)
              if not $game_map.events[i].jumping? and not $game_map.events[i].over_trigger?
                $game_map.events[i].start
                result = true
              end
            end
          end
        end
      end
    end
    return result
  end
#===============================================================================

  def check_event_trigger_touch(x, y)
    r = $game_map.relation
    result = false
    if $game_system.map_interpreter.running?
      return result
    end
    if not r["#{x}x#{y}"].is_a?(Array)
      if $game_map.events[r["#{x}x#{y}"]] != nil and [1,2].include?($game_map.events[r["#{x}x#{y}"]].trigger)
        if not $game_map.events[r["#{x}x#{y}"]].jumping? and $game_map.events[r["#{x}x#{y}"]].over_trigger?
          $game_map.events[r["#{x}x#{y}"]].start
          result = true
        end
      end
    else
      for i in r["#{x}x#{y}"]
        if $game_map.events[i] != nil and [1,2].include?($game_map.events[i].trigger)
          if not $game_map.events[i].jumping? and $game_map.events[i].over_trigger?
            $game_map.events[i].start
            result = true
          end
        end
      end
    end
    return result
  end
end
#===============================================================================

#
#===============================================================================

class Game_Event < Game_Character
  def erase
    @erased = true
    old = $game_map.relation["#{@x}x#{@y}"]
    if not old.is_a?(Array)
      old = nil
    elsif old.size == 2
      pos = 0
      for hum in old
        mov = hum if hum == self.id
        break if hum == self.id
        pos += 1
      end
      old = old[(pos == 1 ? 0 : 1)]
    else
      pos = 0
      for hum in old
        mov = hum if hum == self.id
        break if hum == self.id
        pos += 1
      end
      old.delete(mov)
    end
    $game_map.relation["#{@x}x#{@y}"] = old
    refresh
  end
#-------------------------------------------------------------------------------
  def move_down(turn_enabled = true)
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_left(turn_enabled = true)
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_right(turn_enabled = true)
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_up(turn_enabled = true)
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_lower_left
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_lower_right
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_upper_left
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------
  def move_upper_right
    old_x = self.x
    old_y = self.y
    super
    relation (old_x, old_y)
  end
#-------------------------------------------------------------------------------  
  def relation (x, y)
    old = $game_map.relation["#{x}x#{y}"]
    new = $game_map.relation["#{@x}x#{@y}"]
    if not old.is_a?(Array)
      if new == nil
        new = old
      elsif new.is_a?(Array)
        new.push (old)
      else
        new = [new, old]
      end  
      old = nil
    elsif old.size == 2
      pos = 0
      for hum in old
        mov = hum if hum == self.id
        break if hum == self.id
        pos += 1
      end
      if new == nil
        new = mov
      elsif new.is_a?(Array)
        new.push (mov)
      else
        new = [new, mov]
      end
      old = old[(pos == 1 ? 0 : 1)]
    else
      pos = 0
      for hum in old
        mov = hum if hum == self.id
        break if hum == self.id
        pos += 1
      end
      if new == nil
        new = mov
      elsif new.is_a?(Array)
        new.push (mov)
      else
        new = [new, mov]
      end
      old.delete(mov)
    end
    $game_map.relation["#{x}x#{y}"] = old
    $game_map.relation["#{@x}x#{@y}"] = new
  end
end

I would ask, even if this sistem doesn't create any visual diference with the game, to add me in the credits for this.




Instructions
  • How to get the id of an event in a location
    The code is simple:

    Code:
    $game_map.get_id(x,y) #returns a number
  • Where x and y are the coordinates in standard form. If no event is found returns -1. If there are two events in the same spot (yes it can happen), it returns the last one to be there, wich usually corresponds to the one that is moving or is interesting.

    To set a variable with this value use this code:

    Code:
    $game_variables[id] = $game_map.get_id(x,y)
  • Where id is the id of the variable that interests us.
  • How to get the coordinates of an event with a certain ID
    The code is simple:

    Code:
    $game_map.get_x(id) #returns a number (X coordinate)
    $game_map.get_y(id) #returns a number (Y coordinate)
  • Where id is the id of the event. There can't be two events with the same ID, so no problems there.

    To set a variable with these values use this codes:

    Code:
    $game_variables[v_id] = $game_map.get_x(e_id) #returns a number (X coordinate)
    $game_variables[v_id] = $game_map.get_y(e_id) #returns a number (Y coordinate)
  • Where v_id is the id of the variable that interests us. Don't use the same variable for both coordinates, it will be pointless, you would get only the last one. And e_id is the id of the event.
  • How to make the C Action System
    Haha, find out.
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Set Event Location Randomly xackery 0 2,609 05-08-2009, 01:00 PM
Last Post: xackery
  Addon or Update Script System BETA samamanjaro 0 2,490 11-15-2006, 01:00 PM
Last Post: samamanjaro
  A Sprite That Follows The Player Or An Event Near Fantastica 0 2,349 02-04-2005, 01:00 PM
Last Post: Near Fantastica
  Memorize Location Script - Release 2 Dubealex 0 2,262 11-10-2004, 01:00 PM
Last Post: Dubealex



Users browsing this thread: 1 Guest(s)