# Lua on\_day\_passed from statsthruskills

**URL:** <https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184>\
**Category:** The Toolbox\
**Created:** [January 2, 2016, 2:55pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184 "2016-01-02T14:55:07Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 2, 2016, 2:55pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/1 "2016-01-02T14:55:07Z")

</div>

How does it know? Where is on\_day\_passed defined? I want to see if there are more events I can trigger on but I can’t even figure out how this one gets triggered. Can someone please help?

---

<div class="post-metadata">

**Author:** ![pisskop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/pisskop/32/1327_2.png) [@pisskop](https://discourse.cataclysmdda.org/u/pisskop)\
**Post date:** [January 2, 2016, 3:04pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/2 "2016-01-02T15:04:05Z")

</div>

what is it even involved with? what depends on it?

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 2, 2016, 3:32pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/3 "2016-01-02T15:32:59Z")

</div>

statsthruskills uses it to trigger its script every day at midnight. When it triggers it calculates your stats based on what your skill levels among other things, then displays the resulting changes.

What I can’t figure out is how the lua knows that MOD.on\_day\_passed = run this script at midnight. It’s seems to be creating it on the spot, it’s really confusing and I hate lua now, because I’m sure there’s a simple answer to this staring me in the face. I’ve used git to search the source for anything related to on\_day\_passed and it literally only occurs in statsthruskills and the sample elfmod. Somebody help me please.

_stomps out of the room, flouncily_

---

<div class="post-metadata">

**Author:** ![pisskop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/pisskop/32/1327_2.png) [@pisskop](https://discourse.cataclysmdda.org/u/pisskop)\
**Post date:** [January 2, 2016, 3:38pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/4 "2016-01-02T15:38:49Z")

</div>

So what youre saying is this s that mode where you reconjugure your base attribuites based on your current skill levels.

I don’t know lua but it seems to reason that yes it does generate its business at midnight and then uses that printout to run the mods gibbly bits.

So 2359 comes or w.e. and the mod scrit pulls out your skills and then puts them through their machinery?

I really do wish I had taken the time to learn code but then Id be a software engineer instead of a semi-employed student. :v  
It would seem to me that the first step is to figure out why it can get away with generating the script from magicponytime and what makes that possible. i.e. what it extracts.

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 2, 2016, 3:48pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/5 "2016-01-02T15:48:07Z")

</div>

Yes, that exactly I think? …magicponytime…Anyway, I’ve dug through catalua.cpp, it’s incomprehensible to me. I’ve compiled the game on my pc so I could view catabindings.cpp hoping it was being generated at build time, but it’s not there either as far as I can tell. I don’t know enough about either c++ or lua to code properly, I’ll freely admit that, but I can usually at least figure out what’s going on in existing code.

Where do you come from on\_passed\_day? I want to know how to add an event to the c++ side that I can call upon in lua. It’s for a discussion I was having about spawning static npc’s in building as the world is generated. Something in the source is give lua the ability to detect the passage of days, but I’ll be damned if I can find it ☹

---

<div class="post-metadata">

**Author:** ![pisskop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/pisskop/32/1327_2.png) [@pisskop](https://discourse.cataclysmdda.org/u/pisskop)\
**Post date:** [January 2, 2016, 3:55pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/6 "2016-01-02T15:55:38Z")

</div>

Doesnt CDDA detect passage of days on its own? Day 1 or day 2 or day 363 or w.e.

I dont know jack about lua, but I’d try to find the name for the variable [Days passed] and then find out what references that.

The hud displays how many days, so . . .

---

<div class="post-metadata">

**Author:** ![pisskop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/pisskop/32/1327_2.png) [@pisskop](https://discourse.cataclysmdda.org/u/pisskop)\
**Post date:** [January 2, 2016, 5:46pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/7 "2016-01-02T17:46:10Z")

</div>

In game.cpp, line 1030 - 1055

[spoiler]

[code]  
std::string sTemp;  
std::stringstream ssTemp;

```
    int days_survived = int(calendar::turn.get_turn() / DAYS(1));
    int days_adventured = int((calendar::turn.get_turn() - calendar::start.get_turn()) / DAYS(1));

    for (int lifespan = 0; lifespan < 2; ++lifespan) {
        // Show the second, "Adventured", lifespan
        // only if it's different from the first.
        if (lifespan && days_adventured == days_survived) {
            continue;
        }

        sTemp = lifespan ? _("Adventured:") : _("Survived:");
        mvwprintz(w_rip, iInfoLine++, (FULL_SCREEN_WIDTH / 2) - 5, c_ltgray, (sTemp + " ").c_str());

        int iDays = lifespan ? days_adventured : days_survived;
        ssTemp << iDays;
        wprintz(w_rip, c_magenta, ssTemp.str().c_str());
        ssTemp.str("");

        sTemp = (iDays == 1) ? _("day") : _("days");
        wprintz(w_rip, c_white, (" " + sTemp).c_str());
    }

    int iTotalKills = 0;

```

[/code][spoiler]

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 2, 2016, 6:00pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/8 "2016-01-02T18:00:05Z")

</div>

That is part of the memorial generation function. It only gets called when you die or suicide. That particular bit will figure out how many days your survivor made it before he croaked. The function starts on line 885 with “bool game::cleanup\_at\_end()” and ends at line 1147.

---

<div class="post-metadata">

**Author:** ![pisskop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/pisskop/32/1327_2.png) [@pisskop](https://discourse.cataclysmdda.org/u/pisskop)\
**Post date:** [January 2, 2016, 6:02pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/9 "2016-01-02T18:02:21Z")

</div>

It calls on how many days youve survived in addition to w.e. else it does.

Im not sure what you are looking for. Are you looking for how the games transitions to a new day?

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 2, 2016, 6:41pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/10 "2016-01-02T18:41:03Z")

</div>

That would be a start. Calender.h and calender.cpp handle turns, and I thought for sure I had the answer when I found the once\_every function in there, but none of the places it’s called seem related to midnight. Then I found seconds\_past\_midnight and minutes\_past\_midnight and thought, surely this is it? But no luck. Somehow the game knows when midnight strikes, and is giving lua access to that knowledge. Frustration.

---

<div class="post-metadata">

**Author:** ![pisskop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/pisskop/32/1327_2.png) [@pisskop](https://discourse.cataclysmdda.org/u/pisskop)\
**Post date:** [January 2, 2016, 6:44pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/11 "2016-01-02T18:44:43Z")

</div>

When the [days survived] increments, a script is extracting w.e. infos and manipulating it?  
the mods load when you load the game, so its not like it cant be happening any more than autosaves happen.

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 2, 2016, 6:50pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/12 "2016-01-02T18:50:34Z")

</div>

I love finding things like this, it’s really encouraging.

/\* Main Loop for cataclysm

- Linux only I guess
- But maybe not
- Who knows  
\*/

Ok, thats another thing to try to find, but how does lua know? Where does .on\_day\_passed () come from? You can’t just call a function without defining what it does somewhere. That is the place I want to find.

edit: I’m reading every single line of class\_definitions.lua. Again. Maybe I missed something. Not maybe…probably. YEARGH!

---

<div class="post-metadata">

**Author:** ![Coolthulhu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/coolthulhu/32/2446_2.png) [@Coolthulhu](https://discourse.cataclysmdda.org/u/Coolthulhu)\
**Post date:** [January 2, 2016, 11:12pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/13 "2016-01-02T23:12:05Z")

</div>

lua\_callback(“on\_day\_passed”) is called in src/game.cpp  
I don’t think lua callbacks need a specific definition, adding more callbacks, like “on\_skill\_change” or “on\_craft\_finish” would probably be as simple as just adding lua\_callback(callback\_name\_here) to relevant sections, then wrapping it up with a “#ifdef LUA” and “#endif” to make sure it doesn’t break builds.

My preferred way of seaching stuff in DDA sources is just parsing the entire src (or data) folder with a string search program. Linux (and I assume MacOSX) has `grep "string here" -r src`, but I’m pretty sure there are Windows equivalents.

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 3, 2016, 12:24am UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/14 "2016-01-03T00:24:40Z")

</div>

Holy cow, I knew it was staring me in the face. Githubs search engine lied to me…Thank you very much. I will definitely be tracking down whatever equivalent to grep exists in windows. This is so cool, the source is starting to make sense 🙂

#ifdef LUA and #endif are instructions for the compiler? I see a lot of #ifdef TILES throughout game.cpp, but no #ifdef LUA. How does that work with the lua\_callback instruction?

edit: Correction, I see a lot of #ifndef TILES, which is something entirely different maybe?

---

<div class="post-metadata">

**Author:** ![Malkeus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/malkeus/32/12037_2.png) [@Malkeus](https://discourse.cataclysmdda.org/u/Malkeus)\
**Post date:** [January 3, 2016, 1:11am UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/15 "2016-01-03T01:11:42Z")

</div>

Ok I get it now. If lua is not enabled, catalua.cpp will replace lua\_callback () with a blank function that does nothing. Does this mean I don’t need to use #ifdef #endif, since lua\_callback is an empty function when lua is not enabled? That is how I’m interpreting it right now, can you confirm/deny?

---

<div class="post-metadata">

**Author:** ![Coolthulhu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/coolthulhu/32/2446_2.png) [@Coolthulhu](https://discourse.cataclysmdda.org/u/Coolthulhu)\
**Post date:** [January 3, 2016, 4:08am UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/16 "2016-01-03T04:08:15Z")

</div>

> [@Malkeus](#):
>
> Ok I get it now. If lua is not enabled, catalua.cpp will replace lua\_callback () with a blank function that does nothing.

Sounds correct.

> [@Malkeus](#):
>
> I see a lot of #ifndef TILES, which is something entirely different maybe?

In this case the ‘n’ means ‘not’.

---

<div class="post-metadata">

**Author:** ![Catasteroid](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@Catasteroid](https://discourse.cataclysmdda.org/u/Catasteroid)\
**Post date:** [February 1, 2016, 4:53pm UTC](https://discourse.cataclysmdda.org/t/lua-on-day-passed-from-statsthruskills/11184/17 "2016-02-01T16:53:03Z")

</div>

Ah! Thank you for locating it, I’ve been looking for it myself in order to (hopefully) augment NPC’s restock\_shops.  
It is called on line [1343 of game.cpp](https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/game.cpp#L1343) in game:do\_turn():

[code]// MAIN GAME LOOP  
// Returns true if game is over (death, saved, quit, etc)  
1326: bool game::do\_turn()  
1327: {

…

1343: lua\_callback(“on\_day\_passed”);

…

1347: // Move hordes every 5 min[/code]  
I’ve been trying to look for other callbacks to see where I can get the game and LUA to (again hopefully) communicate without having to compile.
