[XP] CCTS Weather Plugin for RMX-OS

Started by [Luke], September 12, 2010, 05:47:49 am

Previous topic - Next topic

[Luke]

September 12, 2010, 05:47:49 am Last Edit: March 12, 2011, 05:24:38 am by Blizzard
Global CCTS Weather for RMX-OS
Authors: Luke
Version: 0.9
Type: RMX-OS Plugin
Key Term: RMX-OS Plugin



Introduction

This script is the first version of the CCTS plugin for RMX-OS. By now it preforms only global weather processing. I've posted it here to ask you for your comments before I complete the Global Time part of the plugin, which won't be neccessary if you use the real time for CCTS.

Important notice: It's my first script. Refrain from treating me like a lazy and lame scripter. Noob one - a little bit better.

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


Features


  • global weather for every climate
  • easy to configure
  • works with Zer0 Advanced Weather
  • random weather update time
  • server messages informing about weather update



Screenshots

Not really, no.


Demo

Likewise.


Script

Note: This example script uses 10 climates instead of the default CCTS 6 to show the sense of parent climates. Configure it as you wish.

RMX-OS plugin, put in the client below CCTS and RMX-OS:
Spoiler: ShowHide

#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# CCTS Global Weather Plugin for RMX-OS by Luke
# Version: 0.90
# Type: RMX-OS Add-on
# Date: 09.09.2010
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
#  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 v1.1 or
#   higher to work properly.
#   This script proceeds the weather update for every climate.
#   The client sends the climate ID to the server and receives current weather.
#  
# Note:
#  
#   That's my first script. And it's for RMX-OS. And it's lame.
#   Therefore, it may not work. Report bugs, please.
#   It requires Zer0 Advanced Weather and CCTS.
#   Probably the first, definitely the second.
#  
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

if !defined?(RMXOS) || RMXOS::VERSION < 1.13
 raise 'ERROR: The "Global Weather" requires RMX-OS 1.01or higher.'
elsif !$ccts || $ccts < 1.0
 raise 'ERROR: The "Global Weather" requires CCTS 1.0 or higher.'
end

$rmxos_gccts = 0.90

#==============================================================================
# RMXOS::Network
#==============================================================================

class RMXOS::Network
 # This method checks for the two new server messages.
 alias check_game_weather_later check_game
 def check_game(message)
   # The message of weather type, power and transition.
   if message =~ /\AWEA\t(.+)\s(.+)\s(.+)\s(.+)/
       $global_weather_type = $1.to_i
       $global_weather_power = $2.to_i
       $global_weather_transition = $3.to_i
       $global_weather_variation = $4.to_i
     return true
   end
   # This message is sent when the server updates the weather.
   if message =~ /\AWEC\Z/
   # It's like "Hey, new forecast! Wanna see it?"
       $network.send("WER#{$game_map.climate.id}")
       # And that's like "Yeah dude, show me mine"
     return true
   end
   return check_game_weather_later(message)
 end
 
 # This method sends weather request on map change.
 alias enter_map_later enter_map
 def enter_map
   enter_map_later
   # "I've set all the map variables including the new climate"
   $network.send("WER#{$game_map.climate.id}")
   # "Now gimme its forecast"
   $game_screen.weather_control
 end
end

class Game_Screen
 def weather_delay
   # This method overwrites the random-based update time.
   # The value weather_delay was used when the client defined the weather.
   # Removing the need of this value would require much more code.
   # And I'm not sure if creating several new aliases would speed up anything.
   # So the weather updates itself to the global value after each 666 seconds,
   # notwithstanding that when it changes, server informs about it anyway.
   return 666
 end
 
 def weather_control
   # This method overwrites the random-based weather setting.
   # Made by copypasting and changing. Probably some of those freaky ifelses
   # were important and I shouldn't have deleted them. But for now, it works.
   # Let me know if it doesn't.
   return if @weather_count < @weather_wait &&
     ($game_map.climate.id == $game_map.old_climate.id ||
     $game_map.climate.weather && @last_weather != nil)
   @weather_count = 0
   @weather_wait = weather_delay
   if ($global_weather_type != nil && $global_weather_power != nil &&
      $global_weather_transition != nil && $global_weather_variation != nil)
     if $global_weather_variation == 0
       weather($global_weather_type,
               $global_weather_power,
               $global_weather_transition)
     else
       weather($global_weather_type,
               $global_weather_power,
               $global_weather_transition,
               $global_weather_variation)
     end
   else
     weather(0,0,0)
   end
 end
 
end


Server extension:
Spoiler: ShowHide

module CCTS
 Climates = 10 # The quantity of climates. Count them correctly or expect errors.
 def self.child_of(climate_id)
   # Define here which climates are linked to the parent ones.
   # For example, climate "Snow Indoor" have the same weather as "Snow Outdoor".
   # This is to make the BGS change when the player travels from indoor of one climate to another.
   # Pattern: when CHILD_CLIMATE then return PARENT_CLIMATE
   # The index of CHILD_CLIMATE must be HIGHER than PARENT_CLIMATE.
   case climate_id
     when 1 then return 0 # Normal Indoor (1) is a child of Normal Outdoor (0)
     when 7 then return 2 # Snow Indoor (7) is a child of Snow Outdoor (2)
     when 8 then return 3 # Desert Indoor (8) is a child of Desert Outdoor (3)
     when 9 then return 0 # No-Tint Indoor (9) is a child of Normal Outdoor
   end
   return -1 # When the climate has no parent.
 end
 def self.weather_prob(climate_id, month)
   # This is the module from the CCTS config. Works same as there. You can delete this part of Config
   # in your client scripts since the script won't be using it anymore.
   # If a climate has a parent one (see above), you don't have to include its number here.
   case climate_id
   when 0 # Normal Outdoor
     case month
     # Winter
     when 1, 2, 12
       return [[3, 17], [16, 12]]
     # Spring
     when 3, 4, 5
       return [[1, 30], [2, 25], [4, 5], [5, 25], [9, 10]]  
     # Summer  
     when 6, 7, 8
       return [[1, 15], [2, 7], [5, 11], [9, 10]]
     # Autumn
     when 9, 10, 11
       return [[1, 15], [5, 10], [9, 10]]
     end
   when 2 # Snow/Arctic (year-round, no month branching)    
     return [[3, 80], [16, 50]]
   when 3 # Desert (year-round, no month branching)
     return [[1, 5]]
   when 6 # Forest (when there are trees, you can see the "leaf" weather)
     case month
     when 1, 2, 12
       return [[3, 13], [16, 9]]
     when 3, 4, 5
       return [[1, 30], [2, 25], [4, 5], [5, 25], [9, 10]]  
     when 6, 7, 8
       return [[1, 15], [2, 7], [5, 11], [6, 15], [7, 15], [8, 15], [9, 10]]
     when 9, 10, 11
       return [[1, 15], [5, 10], [6, 25, rand(2)+1], [7, 25, rand(2)+1], [8, 25, rand(2)+1]]
     end
   end
 end
end


module RMXOS
 
 def self.load_current_extension
   return GlobalWeather
 end  
 
end

#======================================================================
# module GlobalWeather
#======================================================================

module GlobalWeather
 
 VERSION = 0.90
 RMXOS_VERSION = 1.13
 SERVER_THREAD = true
 
 # START Configuration
 WEATHER_MIN = 120 # the minimum time after which the weather is updated
 WEATHER_MAX = 360 # the maximum time.
 SERVER_MESSAGES = 3
   # 0 - no messages
   # 1 - informing about weather update,
   # 2 - informing about the update and sending to clients
   # 3 - informing about the update, the updated weather value and sending to clients
 # END Configuration
 
 def self.initialize
   @client_times = {}
   @climate_update = 0
   @climate_weather = []
   @weather_interval = 240 # starting value.
 end
 
 def self.main
   while RMXOS.server.running
     self.server_update
     sleep(0.1)
   end
 end
 
 def self.server_update
   time = Time.now
   if (time - @climate_update).to_i > @weather_interval
     print("\nWeather update initialized...\n") if SERVER_MESSAGES > 0
     for i in 0...CCTS::Climates
       self.update_weather(i)
     end
     @weather_interval = WEATHER_MIN + rand(WEATHER_MAX - WEATHER_MIN)
     @climate_update = Time.now
     client.sender.send_to_all("WEC")
   end
 end
 
 def self.client_update(client)
   case client.message
   when /\AWER(.+)/ # WEather Request. Not a rocket science.
     self.send_weather(client,$1.to_i)
   end
   return false
 end
 
 def self.send_weather(client, climate)
   begin
     weather = @climate_weather[climate]
     client.send("WEA\t#{weather[0]}\t#{weather[1]}\t#{weather[2]}\t#{weather[3]}")
     print ("Sent weather #{weather} to client #{client.player.username}\n") if SERVER_MESSAGES > 1
     @client_times[client] = time
   rescue
     @client_times.delete(client)
   end
 end
 
 def self.update_weather(climate_id)
   parent = CCTS.child_of(climate_id)
   if parent != (-1)
     @climate_weather[climate_id] = @climate_weather[parent]
     return
   end
   time = Time.now
   chance = rand(100) + 1
   month = time.month
   climate_weather = CCTS.weather_prob(climate_id, month)
   type, power, transition, variation = 0, 0, 0, 0
   return if climate_weather == nil
   climate_weather.each_index {|i|
     climate_weather[i] = nil unless chance <= climate_weather[i][1]}
   climate_weather.compact!
   unless climate_weather.empty?
     r = rand(climate_weather.size)
     type = climate_weather[r][0]
     if climate_weather[r][2] != nil
       variation = climate_weather[r][2]
     end
   end
   power = rand(50) + 1
   transition = rand(120) + 160
   @climate_weather[climate_id] = [type, power, transition, variation]
   print("Updated climate #{climate_id} weather to #{@climate_weather[climate_id]}.\n") if SERVER_MESSAGES > 2
 end
end



Instructions

Take the server extension, put it in the /RMX-OS Server/Extension/ folder and set the constants. Then add the RMX-OS plugin below RMX-OS script. Observe the errors and report them here or make your own version of this script. ;)


Compatibility

Compatibile with Zer0 Advanced Weather. Requires CCTS.


Credits and Thanks


  • Blizzard, for the mighty RMX-OS
  • Zer0, for the CCTS and Advanced Weather
  • and me.



Author's Notes

Well... it's not completed yet as it doesn't process the global time. Important note: it takes the month date from the actual month (I'm currently working on the Global Time, what will solve the problem), so the weather in game matches the one outside your window. Unless you live in Australia.
I'll let you know as soon as I finish the Global CCTS Time part of this plugin. I released it, as I said, to ask you for advices. Also, for people who will use the real time for CCTS, since that's all that they need.
And again: first script, don't bite.

[Luke]

Ummm, topic to delete. There was a small bug that I wanted to fix and suddenly it caused a LOT of trouble.
Any chance that somebody will write the CCTS plugin properly?

stripe103

You could lock it for now and then when you have fixed it, unlock it again.