STAR TREK 2d20
  1. #1

    LUA help for FG Calendar

    Hey there! I've been using the Calendar Tutorial on the forums 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?


    Code:
    --
    -- 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
    Custom calendar.png
    Attached Files Attached Files

  2. #2
    Based on what you're trying to do, I think the calculations should be:

    Code:
    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...

  3. #3
    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?

  4. #4
    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.

  5. #5
    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.

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
DICE PACKS BUNDLE

Log in

Log in