From e69e6dd75b1d1882639fc503ceb59a0fe7aa12b4 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 24 Nov 2009 23:02:01 +0000
Subject: [PATCH] calendars

git-svn-id: http://svn.net-core.org/repos/t-engine4@36 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Calendar.lua                      | 80 +++++++++++++++++++
 game/engine/GameTurnBased.lua                 |  2 +
 game/engine/utils.lua                         | 13 +++
 game/modules/tome/class/Game.lua              |  9 ++-
 game/modules/tome/data/calendar_rivendell.lua | 11 +++
 game/modules/tome/init.lua                    | 14 +++-
 6 files changed, 127 insertions(+), 2 deletions(-)
 create mode 100644 game/engine/Calendar.lua
 create mode 100644 game/modules/tome/data/calendar_rivendell.lua

diff --git a/game/engine/Calendar.lua b/game/engine/Calendar.lua
new file mode 100644
index 0000000000..189ba28b36
--- /dev/null
+++ b/game/engine/Calendar.lua
@@ -0,0 +1,80 @@
+require "engine.class"
+
+--- Defines factions
+module(..., package.seeall, class.make)
+
+seconds_per_turn = 10
+
+MINUTE = 60 / seconds_per_turn
+HOUR = MINUTE * 60
+DAY = HOUR * 24
+YEAR = DAY * 365
+DAY_START = HOUR * 6
+
+--- Create a calendar
+-- @param definition the file to load that returns a table containing calendar months
+-- @param datestring a string to format the date when requested, in the format "%s %s %s %d %d", standing for, day, month, year, hour, minute
+function _M:init(definition, datestring, start_year)
+	local data = dofile(definition)
+	self.calendar = {}
+	local days = 0
+	for _, e in ipairs(data) do
+		if not e[3] then e[3] = 0 end
+		table.insert(self.calendar, { days=days, name=e[2], length=e[1], offset=e[3] })
+		days = days + e[1]
+	end
+	assert(days == 365, "Calendar incomplete, days ends at "..days.." instead of 365")
+
+	self.datestring = datestring
+	self.start_year = start_year
+end
+
+function _M:getTimeDate(turn)
+	local doy, year = self:getDayOfYear(turn)
+	local hour, min = self:getTimeOfDay(turn)
+	return self.datestring:format(tostring(self:getDayOfMonth(doy)):ordinal(), self:getMonthName(doy), tostring(year):ordinal(), hour, min)
+end
+
+function _M:getDayOfYear(turn)
+	local d, y
+	d = math.floor(turn / self.DAY)
+	y = math.floor(d / 365)
+	d = math.floor(d % 365)
+	print(turn, d, y, self.YEAR)
+	return d, self.start_year + y
+end
+
+function _M:getTimeOfDay(turn)
+	local hour, min
+	min = math.floor((turn % self.DAY) / self.MINUTE)
+	hour = math.floor(min / 60)
+	min = math.floor(min % 60)
+	print(turn, hour, min, self.HOUR)
+	return hour, min
+end
+
+function _M:getMonthNum(dayofyear)
+	local i = #self.calendar
+
+	-- Find the period name
+	while ((i > 1) and (dayofyear < self.calendar[i].days)) do
+		i = i - 1
+	end
+
+	return i
+end
+
+function _M:getMonthName(dayofyear)
+	local month = self:getMonthNum(dayofyear)
+	return self.calendar[month].name
+end
+
+function _M:getDayOfMonth(dayofyear)
+	local month = self:getMonthNum(dayofyear)
+	return dayofyear - self.calendar[month].days + 1 + self.calendar[month].offset
+end
+
+function _M:getMonthLength(dayofyear)
+	local month = self:getMonthNum(dayofyear)
+	return self.calendar[month].length
+end
diff --git a/game/engine/GameTurnBased.lua b/game/engine/GameTurnBased.lua
index fec6f503c2..74bc9368fd 100644
--- a/game/engine/GameTurnBased.lua
+++ b/game/engine/GameTurnBased.lua
@@ -10,6 +10,7 @@ module(..., package.seeall, class.inherit(engine.GameEnergyBased))
 
 --- See engine.GameEnergyBased
 function _M:init(keyhandler, energy_to_act, energy_per_tick)
+	self.turn = 0
 	self.paused = false
 	engine.GameEnergyBased.init(self, keyhandler, energy_to_act, energy_per_tick)
 end
@@ -17,5 +18,6 @@ end
 function _M:tick()
 	while not self.paused do
 		engine.GameEnergyBased.tick(self)
+		self.turn = self.turn + 1
 	end
 end
diff --git a/game/engine/utils.lua b/game/engine/utils.lua
index 61bfae0d7b..6ed5608137 100644
--- a/game/engine/utils.lua
+++ b/game/engine/utils.lua
@@ -1,3 +1,16 @@
+function string.ordinal(number)
+	local suffix = "th"
+	number = tonumber(number)
+	if number == 1 then
+		suffix = "st"
+	elseif number == 2 then
+		suffix = "nd"
+	elseif number == 3 then
+		suffix = "rd"
+	end
+	return number..suffix
+end
+
 function string.capitalize(str)
 	if #str > 1 then
 		return string.upper(str:sub(1, 1))..str:sub(2)
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index 19a19d49a0..88649a6550 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -3,6 +3,7 @@ require "engine.GameTurnBased"
 require "engine.KeyCommand"
 require "engine.LogDisplay"
 local Tooltip = require "engine.Tooltip"
+local Calendar = require "engine.Calendar"
 local Zone = require "engine.Zone"
 local Map = require "engine.Map"
 local Level = require "engine.Level"
@@ -24,6 +25,8 @@ function _M:run()
 	Map:setViewPort(self.w, math.floor(self.h * 0.80), 16, 16)
 	Map:setViewerFaction("players")
 
+	self.calendar = Calendar.new("/data/calendar_rivendell.lua", "Today is the %s %s of the %s year of the Fourth Age of Middle-earth.\nThe time is %02d:%02d.", 122)
+
 	self.zone = Zone.new("ancient_ruins")
 
 	self.tooltip = engine.Tooltip.new(nil, nil, {255,255,255}, {30,30,30})
@@ -154,7 +157,7 @@ function _M:setupCommands()
 		end,
 		_GREATER = {"alias", "_LESS"},
 		-- Toggle tactical displau
-		[{"_t","ctrl"}] = function()
+		[{"_t","alt"}] = function()
 			if Map.view_faction then
 				Map:setViewerFaction(nil)
 				self.log("Tactical dislpay disabled.")
@@ -165,6 +168,10 @@ function _M:setupCommands()
 				self.level.map.changed = true
 			end
 		end,
+		-- Toggle tactical displau
+		[{"_t","ctrl"}] = function()
+			self.log(self.calendar:getTimeDate(self.turn))
+		end,
 	}
 	self.key:setCurrent()
 end
diff --git a/game/modules/tome/data/calendar_rivendell.lua b/game/modules/tome/data/calendar_rivendell.lua
new file mode 100644
index 0000000000..a73bba396c
--- /dev/null
+++ b/game/modules/tome/data/calendar_rivendell.lua
@@ -0,0 +1,11 @@
+return {
+	{ 1, "Yestarë", },
+	{ 54, "Tuilë" },
+	{ 72, "Lairë" },
+	{ 54, "Yávië" },
+	{ 3, "Enderi" },
+	{ 54, "Quellë" },
+	{ 72, "Hrívë" },
+	{ 54, "Coirë" },
+	{ 1, "Mettarë" },
+}
diff --git a/game/modules/tome/init.lua b/game/modules/tome/init.lua
index 2cccdc31ac..a5045a364d 100644
--- a/game/modules/tome/init.lua
+++ b/game/modules/tome/init.lua
@@ -1,4 +1,16 @@
-name = "Tales of Middle Earth"
+name = "Tales of Middle Earth: 4th Age"
 short_name = "tome"
+author = { "DarkGod", "darkgod@t-o-m-e.net" }
+description = [[
+Morgoth is banned in the Void, Sauron has been vanquished, the One Ring destroyed.
+The nations of Men, Dwarves, Elves and Hobbits are freed from the tyranny of the Rings.
+This is a golden age, or at least it should be...
+Trouble is brewing in the east. Rumours in Lake-town near the Lonely Mountain have it
+that some power is growing in the far east, orc have started to multiply in sigthings.
+
+However you do not think this concerns you, you set out to discover the old, explore
+old places, venture into the unknown for wealth and glory.
+But beware, the eastern troubles could be yours all too soon...
+]]
 version = {4,0,0}
 starter = "mod.load"
-- 
GitLab