RMX-OS Map (Instances)

Started by nathmatt, August 21, 2012, 03:41:05 pm

Previous topic - Next topic

nathmatt

So i have been messing around with an idea of how map instances could work.

This is my server extension so far my idea is to create a class for every map instance that stores the id, map_id, and
list of clients on that instance.

Spoiler: ShowHide
module RMXOS

#------------------------------------------------------------------
# Passes the extension's main module to RMX-OS on the top
# level so it can handle this extension.
# Returns: Module of this extension for update.
#------------------------------------------------------------------
def self.load_current_extension
return Map_Instances
end

end

#======================================================================
# module ExtensionSkeleton
#======================================================================

module Map_Instances

# extension version
VERSION = 1.0
# required RMX-OS version
RMXOS_VERSION = 1.2
# whether the server should update this extension in an idividual thread or not
SERVER_THREAD = true
# the extension's name/identifier
IDENTIFIER = 'Extension Skeleton'

# :::: START Configuration
  NonInstMaps = [] # Array of non instance maps
MaxPlayers = nil  # nill if not using style 2
  # 0 only party  members can see each other requires Blizz ABS.
  # 1 only guild  members can see each other.
  # 2 once there is MaxPlayers player will be sent to a new_instance
  Instance_Style = 0
# :::: END Configuration

#------------------------------------------------------------------
# Initializes the extension (i.e. instantiation of classes).
#------------------------------------------------------------------
def self.initialize
    @instances = {}
    @style = {}
    @index = {}
end
  #------------------------------------------------------------------
# Sets instances.
#------------------------------------------------------------------
  def self.set_instances(client,style = Instance_Style)
    case style
    when 0
      id = client.player.user_id
      id = client.sender.get_client_by_name(client.player.partyleader).user_id if client.player.partyleader !=  ''
      map_id = client.player.map_id
      key = [id,map_id]
      if @instances[key] != nil
        @instances[key].add_client(client)
      else
        @instances[key] = Map_Instance.new(id,[client],map_id)
      end
      return @instances[key]
    when 1
      id = client.player.user_id
      id = client.sender.get_client_by_name(client.player.guildleader).id if client.player.guildleader != ''
      map_id = client.player.map_id
      key = [id,map_id]
      if @instances[key] !=nil
        @instances[key].add_client(client)
      else
        @instances[key] = Map_Instance.new(id,[client],map_id)
      end
      return @instances[key]
    when 2
      map_id = client.player.map_id
      @index[map_id] = 0 if  @index[map_id] == nil
      key = [@index,map_id]
      if @instances[key] != nil
        @instances[key].add_client(client)
      else
        @instances[key] = Map_Instance.new(id,[client],map_id)
      end
      @index[map_id] += 1 if @instances[key].clients.size >= (MaxPlayers - 10) # allow room for instance swap
      return @instances[key]
    end
  end
  #------------------------------------------------------------------
  # Exit map
# -----------------------------------------------------------------
  def self.exit_map(client,style = Instance_Style)
    style = @style[client] if @style[client] != nil
    @style[client] = nil
    case style
    when 0
      id = client.sender.get_client_by_name(client.player.partyleader).id
      map_id = client.player.map_id
      key = [id,map_id]
      @instances[key].remove_client(client)
      @instances.delete(key) if @instances[key].clients.size = 0
    when 1
      id = client.sender.get_client_by_name(client.player.guildleader).id
      map_id = client.player.map_id
      key = [id,map_id]
      @instances[key].remove_client(client)
      @instances.delete(key) if @instances[key].clients.size = 0
    end
  end
#------------------------------------------------------------------
# Calls constant updating on the server.
#------------------------------------------------------------------
def self.main
# while server is running
while RMXOS.server.running
self.server_update
sleep(0.1) # 0.1 seconds pause, decreases server load
end
end
#------------------------------------------------------------------
# Handles the server update.
#------------------------------------------------------------------
def self.server_update
# - YOUR SERVER CODE HERE
end
#------------------------------------------------------------------
# Handles updating from a client.
# client - Client instance (from Client.rb)
# Returns: Whether to stop check the message or not.
#------------------------------------------------------------------
def self.client_update(client)
# - YOUR CLIENT MESSAGE CODE HERE
    case client.message
    when /\AMENIM(.+)/
      @style[client] = $1.to_i
      client.sender.get_map_clients(false,$1.to_i)
      return true
    end
return false
end

end

#======================================================================
# Sender
#----------------------------------------------------------------------
# Provides methods for sending messages.
#======================================================================

class Map_Instance
 
  def initialize(id,clients,map_id)
    @id = id
    @clients = clients
    @map_id =clients[0].player.map_id
  end
 
  def add_client(client)
    @clients.push(client) if !@clients.include?(client)
  end
 
  def remove_client(client)
    @clients.delete(client)
  end
 
  def clients
    return @clients
  end
 
end

class Client
 
  alias instance_check_game check_game
  def check_game
    case @message
    when /\AMEX\Z/ # map exit
      Map_Instances.exit_map
    end
    return instance_check_game
  end
 
end

class Sender
 
  #----------------------------------------------------------------------
# Gets all clients on the same map including or excluding self.
#  including - whether to include or exclude this client
# Returns: Clients on the same map.
#----------------------------------------------------------------------
def get_map_clients(including = false,style=nil)
# find all clients on this map
clients = $clients.find_all {|client| client.player.map_id == @client.player.map_id}
# exclude self if necessary
clients.delete(@client) if !including
return Map_Instances.set_instances(@client).clients if !Map_Instances::NonInstMaps.include?(@client.player.map_id)
    return clients
end
 
end


I am editing the get_map_clients method for desired effect as you can see.

I am still trying to come up with a effective way for max map player limits.

What i want to know is if this if an effective way to make them or should i try another method.
Join Dead Frontier
Sorry, I will no longer be scripting for RMXP. I may or may not give support for my scripts. I don't have the will to script in RGSS anymore.
My script