# Author: Craig Smith
#
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the TeensyMUD Public License
# See LICENSE file for additional information.
#
require 'gettext'
# The climate class keeps track of weather conditions as well as other
# Time based conditions such as day/night and months
class Climate
include GetText
bindtextdomain("engine")
attr_accessor :temp, :humidity, :day, :month, :year, :hour, :minute
attr_reader :raining, :snowing, :night
def initialize
@temp = 75
@humidity = 0
@day = 1
@month = 1
@year = 2007
@hour = 13
@minute = 00
@raining = false
@snowing = false
@night = false
@msgqueue = []
end
# Retrieves a message of the message queue
# [+return+] String if a message else nil
def pump
return nil if @msgqueue.size <= 0
@msgqueue.pop
end
def time
desc = String.new
case @hour
when 23,0,1
desc << _("around midnight")
when 2..5
desc << _("middle of the night")
when 6
desc << _("dawn")
when 7..10
desc << _("early morning")
when 11..14
desc << _("afternoon")
when 15..19
desc << _("evening")
when 20
desc << _("dusk")
when 21..22
desc << _("night")
end
desc
end
def weather
desc = String.new
case @temp
when 97..107
desc << _("scorching hot")
when 86..96
desc << _("very hot")
when 70..85
desc << _("nice")
when 45..69
desc << _("brisk")
when 32..44
desc << _("cold")
when 15..31
desc << _("below freezing")
when -20..14
desc << _("artic cold")
end
if @raining
case rand(3)
when 1
desc << _(" and a slight drizzle of rain is falling")
when 2
desc << _(" and rainy")
when 3
desc << _(" and it is pouring rain")
else
desc << _(" and it is misting")
end
elsif @snowing
case rand(3)
when 1
desc << _(" and scattered flakes fall from the sky")
when 2
desc << _(" and it is snowing")
when 3
desc << _(" and deep drifts of snow move in the wind")
else
desc << _(" and an occasional flurry passes by")
end
end
desc
end
def precipitationCheck
if rand(100) > @humidity
if @temp > 32
if not @raining
@raining = true
@msgqueue.push _("It begins to rain.")
end
else
if not @snowing
@snowing = true
@msgqueue.push _("It begins to snow.")
end
end
else
if @raining
@raining = false
@msgqueue.push _("The rain stops.")
end
if @snowing
@snowing = false
@msgqueue.push _("The snow stops.")
end
end
end
def raiseTemp(amt)
@temp += amt
@temp = 106 if @temp > 106
end
def lowerTemp(amt)
@temp -= amt
@temp = -10 if @temp < -10
end
def raiseHumidity(amt)
@humidity += amt
@humidity == 100 if @humidity > 100
end
def lowerHumidity(amt)
@humidity -= amt
@humidity == 0 if @humidity < 0
end
def nextYear
@year += 1
end
def nextMonth
@month += 1
if @month > 12
@month = 1
nextYear
end
case @month
when 11,12,1
lowerTemp(20 + rand(10))
when 2,3,4
raiseTemp(10 + rand(10))
when 5,6,7
raiseTemp(20 + rand(10))
when 8,9,10
lowerTemp(10 + rand(10))
end
end
def nextDay
@day += 1
case @month
when 1,3,5,7,8,10,12
if @day > 31
@day = 1
nextMonth
end
when 4,6,9,11
if @day > 30
@day = 1
nextMonth
end
else
if @day > 29
@day = 1
nextMonth
end
end
if rand(1)
raiseHumidity(rand(10))
else
lowerHumidity(rand(10))
end
end
def nextHour
@hour += 1
if @hour > 23
@hour = 0
nextDay
end
case @hour
when 20,22,2,4
@temp -= rand(10)
when 6,10,12,4
@temp += rand(10)
end
if @hour == 6
@msgqueue.push _("The sun rises in the east.")
@night = false
elsif @hour == 20
@msgqueue.push _("The sun sets in the west.")
@night = true
end
precipitationCheck
end
def nextMinute
@minute += 1
if @minute > 59
@minute = 0
nextHour
end
end
end