PDA

View Full Version : LUA help for FG Calendar



jentastic
April 2nd, 2021, 16:45
Hey there! I've been using the Calendar Tutorial on the forums (https://www.fantasygrounds.com/forums/showthread.php?47885-Fantasy-Grounds-Custom-Calendar-Tutorial) to create my own custom calendar for my campaign. My calendar is ultra simple — 12 months, 30 days each, 7 days per week. I hadn't ever taken the time to learn XML, but basic programming knowledge means I actually did okay.

The problem is that every month starts on "Sunday". I see that the original creator used an `.ext` to calculate the proper start day of the month, but the LUA calculations are set for a 366 day year with alternating month lengths. I've attached the calendar `.xml` and added the LUA from that tutorial and wondered if someone could walk me through how to make the changes for my calendar?



--
-- This function sets up our custom calendar calling functions.
--

function onInit()
-- Register the function to calculate the day of week for the Example 2 calendar. In the calender mod db.xml, you tell FG which function to use with the <lunardaycalc type="string">MKCustom</lunardaycalc> tag.
CalendarManager.registerLunarDayHandler("Example3", calcExample3LunarDay);
end

--
-- This function calculates the day of the week for the MKCustom calendar. the MKCustom calendar is a simple calendar where odd months have 30 days and even months have 31 days
--
function calcExample3LunarDay(nYear, nMonth, nDay)
local rYear = nYear * 366;
local rMonth = nMonth-1;
local rMonthDays = (rMonth*30)+math.floor(rMonth/2);
local rDay = (rYear + rMonthDays + nDay)%7;
if rDay == 0 then
return 7;
end
return rDay;
end

45370

darrenan
April 2nd, 2021, 21:26
Based on what you're trying to do, I think the calculations should be:


local rYear = nYear * 360;
local rMonth = nMonth-1;
local rMonthDays = rMonth * 30;
local rDay = (rYear + rMonthDays + nDay) % 7;
if rDay == 0 then rDay = 7 end


With constant month lengths, the length of a year is just 12*30, so 360.
The rMonth/2 bit does the alternating month lengths, which you're not doing, so you don't need that.

Not sure why all your months are starting on the same day though, still thinking that through...

darrenan
April 2nd, 2021, 21:33
Looks like the calendar name in the .xml is Example1 but in your code snippet above you're registering Example3, could that be part of the problem?

jentastic
April 2nd, 2021, 21:39
Thank you for the LUA I needed! I was going to double check Example 3 against Example 1 to make sure my calendar was working on both fronts, but I needed to fix the LUA first.

darrenan
April 2nd, 2021, 21:40
You might also want to play around with these two ways of getting rDay and see what difference they make, the default implementation of getLunarDay (which is used if there is no registered lunar day handler) in CalendarManager does the latter.


local rDay = (rYear + rMonthDays + nDay) % 7;
if rDay == 0 then rDay = 7 end


local rDay = ((rYear + rMonthDays + nDay) % 7) + 1;