Chaos Project

RPG Maker => RPG Maker Scripts => RMXP Script Database => Topic started by: winkio on January 19, 2009, 03:14:48 pm

Title: [XP] Day Night Climate System (DNC)
Post by: winkio on January 19, 2009, 03:14:48 pm
Day Night Climate System (DNC)
Authors: winkio
Version: 1.00
Type: Time and Weather System
Key Term: Custom Environment System



Introduction

This script keeps track of the time of day and tints the screen accordingly.
The user can define the length of the day and night.
Also, the user can create climates which determine the chances of different
types of weather, which are automatically applied to any map through this
script.



Features




Screenshots

Don't really have any.  They would just be tinted screens with and without weather effects.  Not too hard to imagine


Script

Spoiler: ShowHide
#|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
# Day Night Climate System (DNC) by Winkio
# Version: 1.00
#|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
#
#
#|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
# This script keeps track of the time of day and tints the screen accordingly.
# The user can define the length of the day and night.
# Also, the user can create climates which determine the chances of different
# types of weather, which are automatically applied to any map through this
# script.
#
# Have three sound files, "Wind.ogg", "Rain.ogg", and "Storm.ogg" in your BGS
# folder to be used as sound effects for those weathers.
#
# Commands:
# $game_dnc.disable - disables the DNC
# $game_dnc.enable  - enables the DNC
# $game_dnc.setup_climate(id, phase) - sets up a climate.  If phase is true,
#    it sets up the climate for the map of the id.  If phase it false, it sets
#    up the climate with that id
# $game_dnc.pass_time(amount) - time passes for the amount of seconds
# $game_dnc.goto_day - sets the time to daybreak
# $game_dnc.goto_night - sets the time to nightfall
# $game_dnc.set_weather_rates(rate1, rate2, rate3, rate4, rate5) - sets the
#    chances of getting different weather.  rates are from 0-100
# $game_dnc.set_weather_states(state1, state2, state3, state4, state5) - sets
#    what weather is currently happening or not (true/false)
# $game_dnc.set_time(newtime) - sets the time to newtime
#
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|

#===============================================================================
# Climates
#
# Configure your climates here.
#===============================================================================

module Climates
 
 #=============================================================================
 # get(id)
 #
 # returns the climate number for map with ID id.
 # when (MAP_ID) then return (CLIMATE_ID)
 #=============================================================================
 def self.get(id)
   case id
   when 12 then return 1
   end
   return 0
 end
 #=============================================================================
 # cycle(id)
 #
 # returns the length of a day/night cycle for the climate with ID id.
 # (in seconds)
 # when (CLIMATE_ID) then return (CYCLE_LENGTH)
 #=============================================================================
 def self.cycle(id)
   case id
   when 1 then return 60
   when 2 then return 300
   end
   return 0
 end
 #=============================================================================
 # day_night(id)
 #
 # returns the day start, night start, day transition, and night transition
 # times for the climate with ID id.
 # (in seconds)
 # when (CLIMATE_ID) then return
 # [day_start, night_start, day_transition, night_transition]
 #=============================================================================
 def self.day_night(id)
   case id
   when 1 then return [0, 30, 5, 5]
   when 2 then return [0, 150, 10, 10]
   end
   return [0, 0, 0, 0]
 end
 #=============================================================================
 # weather(id)
 #
 # returns the percent chance of wind, clouds, rain, snow, and storm weather
 # for the climate with ID id.
 # (0-100)
 # when (CLIMATE_ID) then return
 # [wind_chance, clouds_chance, rain_chance, snow_chance, storm_chance]
 #=============================================================================
 def self.weather(id)
   case id
   when 1 then return [20, 20, 20, 20, 20]
   when 2 then return [10, 10, 10, 10, 0]
   end
   return [0, 0, 0, 0, 0]
 end
 #=============================================================================
 # volatility(id)
 #
 # returns how often the weather changes for the climate with ID id.
 # (in seconds) greater than 0 or weather never updates.
 # when (CLIMATE_ID) then return (VOLATILITY)
 #=============================================================================
 def self.volatility(id)
   case id
   when 1 then return 15
   when 2 then return 30
   end
   return 0
 end
end

#==============================================================================
# DNC
#==============================================================================

class DNC
 
 attr_accessor :cid
 attr_accessor :time
 attr_accessor :day
 attr_accessor :weather
 attr_accessor :cycle
 attr_accessor :day_start
 attr_accessor :night_start
 attr_accessor :day_tran
 attr_accessor :night_tran
 attr_accessor :wind
 attr_accessor :clouds
 attr_accessor :rain
 attr_accessor :snow
 attr_accessor :storm
 attr_accessor :current_weather
 attr_accessor :volatility
 
 def initialize
   @time = 0
   @day = true
   @weather = [false, false, false, false, false]
   setup_climate(0)
 end
 
 def setup_climate(id, phase=true)
   if phase
     @cid = Climates.get(id)
   else
     @cid = id
   end
   @cycle = Climates.cycle(cid)
   @day_start = Climates.day_night(cid)[0]
   @night_start = Climates.day_night(cid)[1]
   @day_tran = Climates.day_night(cid)[2]
   @night_tran = Climates.day_night(cid)[3]
   @wind = Climates.weather(cid)[0]
   @clouds = Climates.weather(cid)[1]
   @rain = Climates.weather(cid)[2]
   @snow = Climates.weather(cid)[3]
   @storm = Climates.weather(cid)[4]
   @volatility = Climates.volatility(cid)
   if @cycle != 0
     @time %= @cycle
   end
 end
 
 def self.disable
   setup_climate(0)
 end
 
 def self.enable
   setup_climate($game_map.map_id)
 end
 
 def pass_time(amount=1)
   if @cycle != 0
     @time = (@time + amount)%@cycle
   end
 end
 
 def goto_day
   @time = @day_start
   apply_day_night
 end
 
 def goto_night
   @time = @night_start
   apply_day_night
 end
 
 def set_weather_rates(rate1, rate2, rate3, rate4, rate5)
   @wind = rate1
   @clouds = rate2
   @rain = rate3
   @snow = rate4
   @storm = rate5
 end
 
 def set_weather_states(state1, state2, state3, state4, state5)
   @weather = [state1, state2, state3, state4, state5]
 end
 
 def set_time(newtime)
   @time = newtime % @cycle
   apply_day_night
 end
 
 def apply_day_night
   # determine if it is day or night
   if @time < @night_start and @time > @day_start
     newday = true
   else
     newday = false
   end
   # transition if not the same as current state of day
   if @cid != 0 and newday != @day
     if newday
       $game_screen.start_tone_change(Tone.new(0, 0, 0, 0), 40*@day_tran)
     else
       $game_screen.start_tone_change(Tone.new(-40, -40, 0, 0), 40*@night_tran)
     end
     @day = newday
   end
 end
 
 def apply_climate
   # determine weather
   if @cid != 0
     chance = rand(100)
     if chance < @wind
       @weather[0] = true
     else
       @weather[0] = false
     end
     chance = rand(100)
     if chance < @clouds
       @weather[1] = true
     else
       @weather[1] = false
     end
     chance = rand(100)
     if chance < @rain
       @weather[2] = true
     else
       @weather[2] = false
     end
     chance = rand(100)
     if chance < @snow
       @weather[3] = true
     else
       @weather[3] = false
     end
     chance = rand(100)
     if chance < @storm
       @weather[4] = true
     else
       @weather[4] = false
     end
   end
   # apply weather
   if @weather[3]
     # apply snow
     $game_system.bgs_fade(2)
     $game_screen.weather(3, 5, 200)
     if @day
       $game_screen.start_tone_change(Tone.new(0, 0, 0, 64), 160)
     else
       $game_screen.start_tone_change(Tone.new(-40, -40, 0, 128), 160)
     end
   elsif @weather[4]
     #apply storm
     Audio.bgs_play("Audio/BGS/Storm.ogg", 80, 100)
     $game_screen.weather(2, 5, 200)
     if @day
     $game_screen.start_tone_change(Tone.new(-28, -28, -28, 28), 160)
     else
       $game_screen.start_tone_change(Tone.new(-58, -58, -58, 54), 160)
     end
   elsif @weather[2]
     #apply rain
     Audio.bgs_play("Audio/BGS/Rain.ogg", 80, 100)
     $game_screen.weather(1, 5, 200)
     if @day
       $game_screen.start_tone_change(Tone.new(-15, -15, -15, 64), 160)
     else
       $game_screen.start_tone_change(Tone.new(-50, -50, -50, 128), 160)
     end
   elsif @weather[1]
     #apply clouds
     $game_screen.weather(0, 5, 200)
     $game_system.bgs_fade(2)
     if @day
       $game_screen.start_tone_change(Tone.new(-10, -10, -10, 32), 160)
     else
       $game_screen.start_tone_change(Tone.new(-45, -45, -45, 64), 160)
     end
   else
     if @day
       $game_screen.start_tone_change(Tone.new(0, 0, 0, 0), 160)
     else
       $game_screen.start_tone_change(Tone.new(-40, -40, 0, 0), 160)
     end
   end
   if @weather[0]
     #apply wind
     $game_screen.weather(0, 5, 200)
     Audio.bgs_play("Audio/BGS/Wind.ogg", 80, 100)
   end
   #if no weather
   if @weather == [false, false, false, false, false, false]
     $game_screen.weather(0, 5, 200)
     $game_system.bgs_fade(2)
   end
 end
 
end

$game_dnc = DNC.new

#==============================================================================
# Scene_Map
#==============================================================================

class Scene_Map
 
 attr_accessor :updatetimer
 attr_accessor :weathertimer
 
 alias main_dnc_later main
 def main
   @weathertimer = 0
   @updatetimer = Graphics.frame_count / Graphics.frame_rate * 2
   $game_dnc.setup_climate($game_map.map_id)
   main_dnc_later
 end
 
 alias update_dnc_later update
 def update
   if @updatetimer != Graphics.frame_count / Graphics.frame_rate * 2
     update_dnc
     @updatetimer = Graphics.frame_count / Graphics.frame_rate * 2
   end
   update_dnc_later
 end
 
 def update_dnc
   if $game_dnc.volatility != 0
     $game_dnc.pass_time
     $game_dnc.apply_day_night
     if @weathertimer >= $game_dnc.volatility
       $game_dnc.apply_climate
       @weathertimer %= $game_dnc.volatility
     end
     @weathertimer += 1
   end
 end
 
 alias transfer_player_dnc_later transfer_player
 def transfer_player
   if $game_map.map_id != $game_temp.player_new_map_id
     $game_dnc.setup_climate($game_temp.player_new_map_id)
   end
   transfer_player_dnc_later
 end
 
end




Instructions

Make sure that you have a sound effect for wind, rain, and storm in your Audio BGS folder named Wind.ogg, Rain.ogg, and Storm.ogg respectively.
Configure climates in the script.
Place below defaults and above main.  Shouldn't conflict with any other scripts...


Compatibility

No problems so far!
Works independently of fog on map!


Credits and Thanks




Author's Notes

If anybody uses this and wants me to improve/redo the existing effects just let me know.  I'd be happy to.
Planned future updates:


This script was my first ALMOST EPIC moment - I coded the whole thing in about an hour without testing, and only had one small error at the end.  I shall keep trying until I get that epic moment.  When I do, you will know it.
Title: Re: Day Night Climate System (DNC)
Post by: Blizzard on January 19, 2009, 03:42:46 pm
That would be ATES with settings that can be changed during gameplay + random weather generator. This was actually supposed to be ATES v1.0. ._.
Title: Re: Day Night Climate System (DNC)
Post by: winkio on January 19, 2009, 03:57:51 pm
...ok.  I think it has a few more features than you are envisioning, but you'll see when I release it.  Which will be soon, btw.

EDIT: Soon is now.  And as you are reading this, that first now was about 1.6 seconds ago. :O.o:
Title: Re: [XP]Day Night Climate System (DNC)
Post by: fugibo on January 21, 2009, 07:36:48 pm
Yeah, sorry about not coming through, Blizz. Kinda out of my scope now.

But, good work on this, winkio. And for YOUR information, every time I read that post two up, now is not 1.6 seconds ago; it is more like 1 minute+ at the time of writing.
Title: Re: [XP]Day Night Climate System (DNC)
Post by: winkio on January 21, 2009, 08:05:43 pm
Actually, the time that you read the first now was about 1.6 seconds ago.  But I don't really care, ambiguosity FTW! :&

EDIT:  If anybody uses this and wants me to improve/redo the existing effects just let me know.  I'd be happy to.
Title: Re: [XP]Day Night Climate System (DNC)
Post by: Blizzard on January 22, 2009, 06:19:13 am
What do you think about a merged system with ATES? :)
Title: Re: [XP]Day Night Climate System (DNC)
Post by: winkio on January 22, 2009, 09:16:35 am
Hmm.  What all would that entail?
Title: Re: [XP]Day Night Climate System (DNC)
Post by: Blizzard on January 22, 2009, 10:12:02 am
All that I originally planned for ATES: Calender, Time, DNS, Temperature, Weather (including fog and maybe wind). Also using ccoa's Weather System it could be extended. ;)
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 23, 2009, 05:30:54 pm
That sounds fairly simple.  Calendar would take all of 5 minutes to make.  And temperature wouldn't take too long either.  Do you like the climate setup I have or will I be changing it?  (if we change it, then I will maintain this script separately, otherwise, I'll combine them.)
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Blizzard on January 23, 2009, 06:02:25 pm
Believe me, calender will take more than 5 minutes. :P
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 23, 2009, 06:05:11 pm
er, array of months, and days per week is all I need for that, right?
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Exiled_by_choise on January 23, 2009, 10:42:10 pm
Hey guys this merge sounds great can you let me know when it's complete.

Thanx
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Calintz on January 23, 2009, 11:25:35 pm
Winkio, post some screenshots please...
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 23, 2009, 11:27:34 pm
QuoteScreenshots

Don't really have any.  They would just be tinted screens with and without weather effects.  Not too hard to imagine


It doesn't really look like much by itself.  It must be experienced in game.
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Blizzard on January 24, 2009, 07:26:11 am
Quote from: winkio on January 23, 2009, 06:05:11 pm
er, array of months, and days per week is all I need for that, right?


Data structure is simple, sure, but what about implementation? Correct calculation of seconds, minutes, hours, days, weeks, months (which are independant of weeks) and years? Trust me, it's not as easy as a 5 minute job.
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 24, 2009, 10:14:28 am
wait, I have to get seconds and minutes?  The lowest amount of time I can keep track of is hours...  Would it really be practical to keep track of minutes and seconds?  Because they would be changing so fast...
Title: Re: [XP] Day Night Climate System (DNC)
Post by: fugibo on January 24, 2009, 10:57:35 am
Hey, don't for get shadows and reflections that vary by time of day, script checks so certain events only occur at certain times of day, and different enemies depending on month, weather, time, yada yada.

And a changing color scheme would be cool too, though that would take a lot of effort.
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 24, 2009, 01:53:35 pm
shadows and reflections just sound like extra lag not needed on an RMXP game.  Everything else I agree with.
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Blizzard on January 25, 2009, 06:44:14 am
Quote from: winkio on January 24, 2009, 10:14:28 am
wait, I have to get seconds and minutes?  The lowest amount of time I can keep track of is hours...  Would it really be practical to keep track of minutes and seconds?  Because they would be changing so fast...


You need at least minutes. ATES does that without problems.

Quote from: WcW on January 24, 2009, 10:57:35 am
Hey, don't for get shadows and reflections that vary by time of day


Could be optional using Rataime's script.

Quote from: WcW on January 24, 2009, 10:57:35 am
script checks so certain events only occur at certain times of day, and different enemies depending on month, weather, time, yada yada.


Not necessary. The day/night switches allow the user to simply make enemies and put conditions into their attacks. The only thing would be day/night enemy troop encounters.

Quote from: WcW on January 24, 2009, 10:57:35 am
And a changing color scheme would be cool too, though that would take a lot of effort.


It would complicate things but not really add that much to the system. Better not. xD
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 25, 2009, 09:18:27 am
I don't think you understand Blizz: the system keeps track of real-time seconds, but depending on how long the days/nights are, those could be either minutes or hours in game.  Perhaps I should add a conversion factor?
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Sally on January 25, 2009, 09:36:01 am
Quote from: winkio on January 23, 2009, 05:30:54 pm
That sounds fairly simple.  Calendar would take all of 5 minutes to make.  And temperature wouldn't take too long either.  Do you like the climate setup I have or will I be changing it?  (if we change it, then I will maintain this script separately, otherwise, I'll combine them.)


you should also make it so players hit lower when its really hot and really cold, to give it a realistic feel.
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 25, 2009, 09:50:42 am
Ok, this script just controls the environment (and will continue to do so).  The developer can decide to implement his own ssystems off of this to create different events depending on time/weather, have different battles, etc.  But the script alone is just environment. 

And Blizz, sorry for being a pain in the ass.  I added a conversion factor so you can control how fast time passes (which can be changed real time) as well as an AM/PM option (which is for the time).  I'm keeping track of seconds, minutes, hours, AM/PM (optional), day, week, month, and year right now.  That should be everything I need to set up a calendar when I get there.

Next in line is temperatures...
Title: Re: [XP] Day Night Climate System (DNC)
Post by: Blizzard on January 25, 2009, 11:01:00 am
Now you've confused me. Does DNC count real seconds or are they converted into in-game time (i.e. 1 second = 1 minute in-game)?
Title: Re: [XP] Day Night Climate System (DNC)
Post by: winkio on January 25, 2009, 11:58:40 am
It counts real time seconds.  Then, I added a conversion factor to game time so you can configure how long one real-time second is in-game (IE 1 second real time = 1 minute game time, or whatever).  Also, using this option, you can slow-down or speed up how quickly time passes in-game with events.