[XP] Global Switches and Variables for RMX-OS

Started by Blizzard, January 07, 2010, 02:25:30 pm

Previous topic - Next topic

Blizzard

January 07, 2010, 02:25:30 pm Last Edit: March 23, 2019, 11:34:03 am by Blizzard
Global Switches and Variables for RMX-OS
Authors: Blizzard
Version: 1.3
Type: RMX-OS plugin
Key Term: RMX-OS Plugin



Introduction

This script will allow to have global switches and variables on your server.

This script is to be distributed under the same terms and conditions like the script it was created for: RMX-OS.


Features


  • easy to configure
  • controls a certain set of switches globallly
  • controls a certain set of variables globallly



Screenshots

N/A for this sort of script.


Demo

N/A


Script

Just make a new script above main and paste this code into it.
Spoiler: ShowHide
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Global Switches and Variables for RMX-OS by Blizzard
# Version: 1.3
# Type: RMX-OS Add-on
# Date: 7.1.2010
# Date v1.01: 22.2.2010
# Date v1.1: 11.2.2011
# Date v1.2: 22.1.2013
# Date v1.21: 30.3.2013
# Date v1.3: 12.6.2013
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
#  This script is to be distributed under the same terms and conditions like
#  the script it was created for: RMX-OS.
#
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
# Information:
#
#   This script must be placed below RMX-OS and requires RMX-OS to work
#   properly. This script will allow to have global switches and variables on
#   your server.
#   
#   
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

if !defined?(RMXOS) || RMXOS::VERSION < 2.0
  raise 'ERROR: The "Global Switches and Variables" requires RMX-OS 2.0 or higher.'
end

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

GLOBAL_SWITCHES = [1, 2] # make sure this matches the extension configuration
GLOBAL_VARIABLES = [1, 2] # make sure this matches the extension configuration

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

$rmxos_gswivars = 1.3

#==============================================================================
# Game_Temp
#==============================================================================

class Game_Temp
 
  attr_accessor :disable_global
 
end
 
#==============================================================================
# Game_Switches
#==============================================================================

class Game_Switches
 
  alias set_gswivar_later []=
  def []=(id, value)
    if !$game_temp.disable_global && GLOBAL_SWITCHES.include?(id)
      $network.send_switch(id, value)
    else
      set_gswivar_later(id, value)
    end
  end
 
end
 
#==============================================================================
# Game_Switches
#==============================================================================

class Game_Variables
 
  alias set_gswivar_later []=
  def []=(id, value)
    if !$game_temp.disable_global && GLOBAL_VARIABLES.include?(id)
      $network.send_variable(id, value)
    else
      set_gswivar_later(id, value)
    end
  end
 
end
 
#==============================================================================
# RMXOS::Network
#==============================================================================

class RMXOS::Network
 
  alias check_game_gswivar_later check_game
  def check_game(message)
    case message
    when /\AGSWI\t(.+)\t(.+)/
      $game_temp.disable_global = true
      $game_switches[$1.to_i] = ($2 != '0')
      $game_temp.disable_global = false
      $game_map.need_refresh = true
      return true
    when /\AGVAR\t(.+)\t(.+)/
      $game_temp.disable_global = true
      $game_variables[$1.to_i] = $2.to_i
      $game_temp.disable_global = false
      $game_map.need_refresh = true
      return true
    end
    return check_game_gswivar_later(message)
  end
 
  alias check_loading_gswivar_later check_loading
  def check_loading(message)
    result = check_loading_gswivar_later(message)
    if result
      if @messages.include?(RMXOS::LOADING_END)
        @messages.delete(RMXOS::LOADING_END)
        self.send('LGDAT')
      end
    else
      case message
      when /\ALGSWI\t(.+)/ # load global switches
        hash = eval($1)
        $game_temp.disable_global = true
        hash.each_key {|id| $game_switches[id] = hash[id]}
        $game_temp.disable_global = false
        return true
      when /\ALGVAR\t(.+)/ # load global variables
        hash = eval($1)
        $game_temp.disable_global = true
        hash.each_key {|id| $game_variables[id] = hash[id]}
        $game_temp.disable_global = false
        $game_map.need_refresh = true
        @messages.push(RMXOS::LOADING_END)
        return true
      end
    end
    return result
  end
 
  def send_switch(id, value)
    self.send('GSWI', id, value ? 1 : 0)
  end
 
  def send_variable(id, value)
    self.send('GVAR', id, value)
  end
 
end


Make a new file with an .rb extension in the Extensions folder of RMX-OS and copy-paste this script into it.
Spoiler: ShowHide
module RMXOS
 
  def self.load_current_extension
    return GlobalSwitchesVariables
  end
 
  module Data
    GlobalSwitchChange = 'Client \'CLIENT\' changed switch \'ID\' to \'STATE\'.'
    GlobalVariableChange = 'Client \'CLIENT\' changed variable \'ID\' to \'STATE\'.'
  end
 
end

#======================================================================
# module GlobalSwitchesVariables
#======================================================================

module GlobalSwitchesVariables
 
  VERSION = 1.3
  RMXOS_VERSION = 2.0
  SERVER_THREAD = false
  IDENTIFIER = 'Global Switches and Variables'
 
  # START Configuration
  GLOBAL_SWITCHES = [1, 2] # make sure this matches the configuration in the client
  GLOBAL_VARIABLES = [1, 2] # make sure this matches the configuration in the client
  SWITCHES_FILENAME = './gswitches.dat' # filename for global switch saving
  VARIABLES_FILENAME = './gvariables.dat' # filename for global variable saving
  SERVER_DISPLAY = true # show log in command prompt screen
  LOG_FILENAME = './logs/gswivar.log' # leave empty if no log file should be created
  DELETE_LOG_ON_START = false
  # END Configuration
 
  def self.initialize
    @mutex = Mutex.new
    @switches = {}
    GLOBAL_SWITCHES.each {|id| @switches[id] = false}
    if FileTest.exist?(SWITCHES_FILENAME)
      file = File.open(SWITCHES_FILENAME, 'r')
      ids = Marshal.load(file)
      file.close
      (ids & GLOBAL_SWITCHES).each {|id| @switches[id] = true}
    end
    @variables = {}
    GLOBAL_VARIABLES.each {|id| @variables[id] = 0}
    if FileTest.exist?(VARIABLES_FILENAME)
      file = File.open(VARIABLES_FILENAME, 'r')
      hash = Marshal.load(file)
      file.close
      (hash.keys & GLOBAL_VARIABLES).each {|id| @variables[id] = hash[id]}
    end
    if LOG_FILENAME != ''
      File.delete(LOG_FILENAME) if DELETE_LOG_ON_START && FileTest.exist?(LOG_FILENAME)
      RMXOS::Logs[IDENTIFIER] = LOG_FILENAME
    end
  end
 
  def self.mutex
    return @mutex
  end
 
  def self.main
    while RMXOS.server.running
      @mutex.synchronize {
        self.server_update
      }
      sleep(0.1)
    end
  end
 
  def self.server_update
  end
 
  def self.client_update(client)
    case client.message
    when /\ALGDAT\Z/ # load global data
      client.send('LGSWI', @switches.inspect.sub(' ', ''))
      client.send('LGVAR', @variables.inspect.sub(' ', ''))
      return true
    when /\AGSWI\t(.+)\t(.+)/ # global switch change
      id = $1.to_i
      value = ($2 != '0')
      @switches[id] = value
      self.save_switches
      client.sender.send_to_all(client.message, true)
      message = RMXOS::Data.args(RMXOS::Data::GlobalSwitchChange, {'CLIENT' => client.player.username, 'ID' => id.to_s, 'STATE' => value.to_s})
      self.log(message)
      return true
    when /\AGVAR\t(.+)\t(.+)/ # global variable change
      id = $1.to_i
      value = $2.to_i
      @variables[id] = value
      self.save_variables
      client.sender.send_to_all(client.message, true)
      message = RMXOS::Data.args(RMXOS::Data::GlobalVariableChange, {'CLIENT' => client.player.username, 'ID' => id.to_s, 'STATE' => value.to_s})
      self.log(message)
      return true
    end
    return false
  end
 
  def self.save_switches
    ids = @switches.keys.find_all {|id| @switches[id]}
    file = File.open(SWITCHES_FILENAME, 'w')
    Marshal.dump(ids, file)
    file.close
  end
 
  def self.save_variables
    hash = {}
    @variables.each_key {|id| hash[id] = @variables[id] if @variables[id] != 0}
    file = File.open(VARIABLES_FILENAME, 'w')
    Marshal.dump(hash, file)
    file.close
  end
 
  def self.log(message)
    puts message if SERVER_DISPLAY
    RMXOS.log('Log', IDENTIFIER, message)
  end
 
end




Instructions

In the script in the first comment.


Compatibility

Requires RMX-OS to work.


Credits and Thanks


  • Boris "Blizzard" Mikić



Author's Notes

If you find any bugs, please report them here:
http://forum.chaos-project.com

That's it! N-Joy! =D
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Okay so pretty much this can be used like this.

Theres one chest in the Forest of Killer Bunnies. It has 5000 gold.
When someone opens it it then turns on a global switch and no one else can get that gold right?

If thats what it is. Awesome script.

Ryex

I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

January 08, 2010, 03:50:33 am #3 Last Edit: January 08, 2010, 03:52:25 am by Blizzard
Lol! Well, I have that and some other scripts (RMX-OS + Blizz-ABS stuff) that I need to finish by the end of this month so I if you want me not to beat you to it, you better get working. xD

@G_G: Yup. Stuff is still saved in the database, but as soon as you enter the server, your switches and variables are overwritten by the with the global values. Also, this script does not ensure consistency completely. The game designers has to take care of that themselves.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Hellfire Dragon


edwardthefma

January 10, 2010, 05:05:16 pm #5 Last Edit: January 11, 2010, 12:22:51 pm by edwardthefma
this script works  it is badd ass this is going to be the key to having a monstor killed and having the pepol
in the map see it happen
tho i have a question
if i hit a swich and stay thare
and some 1 else enters the map dose it swich that same swich for them
i am the lead dev for the shellium mmorpg project
http://wiki.shellium.org/w/Mmorpg
shellium.org :) free linux shells pm me and i will gladly
help you get 1

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

Quote from: the script
raise 'ERROR: The "Global DNS" requires RMX-OS 1.09 or higher.'

"Global DNS" ?

lol
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

February 26, 2010, 02:28:28 am #8 Last Edit: February 26, 2010, 02:30:33 am by Blizzard
Lulz

*fixes*
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Sith-Man

Could this be used for having a character show up on a different map after having been talked to?

Like if I wanted to have an area where a player could select party members, and have the selectable characters be walking around the area after having been unlocked/recruited.

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Wizered67

is it possible to have a global variable equal to a string?

G_G

use this in a script call.
$game_variables[id] = "string here"

Wizered67

I have, and it works fine when i dont do a global variable, but when i do, it always is 0.

stripe103

I have a problem with this script. When I make a global variable or switch and try to change them, they just change back to 0 or false. I can't even see that it changes on the game but on the server it says that they are changed.
These are the scripts I use(in the current order):
RMX-OS
Global Vars and Switches
Blizz-ABS 1, 2 and 3
Blizz-ABS Controller for RMX-OS
Questbook by Zeriab
Self Variables by DrakoShade
The Chaos Project Debug Menu by Blizzard
Location Window by Hunter X
Multiple Timers by Glitchfinder
ForeverZer0's Advanced Weather(the one based on Ccoa's script)
and at last the RMX-OS Main

By Axerax

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

stripe103

I have changed the order of the scripts and they are still not working. But what did I miss in the configuration?
I did configure the what switches and variables should be global, both on the client and the server.

By Axerax

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

stripe103

June 29, 2010, 02:31:23 pm #18 Last Edit: June 29, 2010, 02:33:16 pm by stripe103
Yes I did. That is the strange thing. I have made everything I was supposed to. Setup the script in the client. Adding the extension on the server and setting that up and adding the extension file name to the config.ini file. But I think that the server and client communicates enough since the server knows when a global variable/switch is changed. The thing is that it change it back to 0/false.

EDIT: I just have to ask. Can it be the server computer itself that is to old? The game runs and all but the computer is about 7 years old. It is my parents old computer. I'm just using it as a developement server. I will hopefully change it later.

By Axerax

Blizzard

Well, the RMX-OS server system is relatively lightweight so a weak server machine shouldn't become a bottleneck if there are only a few players online.

Try finding the file to which the server saves the global switches and variables and see if they are properly saved. Also, make sure you are using the newest versions. I changed something recently and I had to update a few RMX-OS pkugins so they work with the newest version of RMX-OS.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.