Author Topic: About writting mods.  (Read 1545 times)

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
About writting mods.
« on: October 24, 2017, 12:51:16 AM »
Hello,

I just want to ask about mod writting, from coding im mostly know C but my question is pure curiosity. I saw that mods are mostly written in json files, but i saw lua also and i heard that C also could work. And my question is, how program read those lua files, and how could write c, and overall how it reading json exacly. I just want to know that, if someone can explain me ill be much gratefull :)

Offline Kevin Granade

  • Administrator
  • Survivor
  • *****
  • Posts: 5588
  • I code dead people.
    • View Profile
Re: About writting mods.
« Reply #1 on: October 24, 2017, 01:29:48 AM »
Adding or editing JSON.
The vast majority of the data dda handles (item definitions, crafting recipes, monster definitions, map generation templates, martial arts styles, mutations) is actually defined as json files.  How this works is that there is some code written in c++ that loads the json entities and turns them into data and logic that can be used by the game engine.  There are only certain things you can adjust about a JSON entity, because the code that reads it has to be written in such a way that it can apply that data to an entity of that type.
Implementation-wise, there are two components to this.  First we have a JSON library that was written for dda that can parse and emit JSON.  Secondly there is code for each kind of entity that reads JSON entries and turns them into game logic.

Adding or editing LUA.
LUA is a programming language that is designed to be embedded in other programs.  There are various parts of dda that can accept LUA as an input, usually as part of some JSON entity.  In theory, any behavior can be specified by LUA, but in practice, each piece of functionality that a piece of LUA can exercise has to have supporting C++ code.  As a result, there are only a few places where LUA can be injected into the code, making it difficult to work with.
Its like a fun family cookout, except your family is burning in flames while trying to eat you. -secretfire
I'm more excited than a survivor on meth and toast'ems. -Nighthawk
The the giant wasp is slammed through the zombie brute!

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
Re: About writting mods.
« Reply #2 on: October 24, 2017, 02:01:48 AM »
How this works is that there is some code written in c++ that loads the json entities and turns them into data and logic that can be used by the game engine.

Two questions:
 1.In what place of c++ code exacly json is loaded into game ?
 2.Is in that same manner like json, lua is loaded ? Becouse i saw mods where in one catalog was and lua and json files, there cant be just one big json file ? And why that lua is not good for implement like json exacly ?

Offline Zhilkin

  • NPC
  • **
  • Posts: 218
    • View Profile
Re: About writting mods.
« Reply #3 on: October 24, 2017, 06:02:31 AM »
Lua can be called from several places:

* in-game lua console (called from debug menu, useful for debug and testing purposes);
* mapgen (`lua` mapgen method);
* mapgen finishing (`lua` finishing script, called after mapgen is done);
* on game loaded (called from `preload.lua` in mod folder);
* in various callbacks (called from `main.lua` in mod folder).

You can use `dofile('./folder/subfolder/filename.lua')` function for convenience in any of the above.

There are currently four callbacks functions:

* on_new_player_created;
* on_skill_increased;
* on_minute_passed;
* on_day_passed.

I want to add more callback functions - https://github.com/CleverRaven/Cataclysm-DDA/pull/21744/.

Some useful links on LUA in Cataclysm:

* on lua support - https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/LUA_SUPPORT.md
* on lua mapgen - https://github.com/CleverRaven/Cataclysm-DDA/blob/master/doc/MAPGEN.md
* functions exported from cpp to lua - https://github.com/CleverRaven/Cataclysm-DDA/blob/master/lua/class_definitions.lua
* sample mods bundled with the game- https://github.com/CleverRaven/Cataclysm-DDA/tree/master/doc/sample_mods
* StatsThroughSkills mod bundled with game - https://github.com/CleverRaven/Cataclysm-DDA/blob/master/data/mods/StatsThroughSkills/main.lua
* Cataclysm-DDA-LUA mod collection for CDDA - https://github.com/ZhilkinSerg/Cataclysm-DDA-LUA or http://smf.cataclysmdda.com/index.php?topic=14861.0
* DegradeBuildings mod for CDDA - http://smf.cataclysmdda.com/index.php?topic=14825.0
* ForestGrowth mod for CDDA - http://smf.cataclysmdda.com/index.php?topic=15051.msg307590#msg307590
* links to some lua code/mods for CDDA - https://www.reddit.com/r/cataclysmdda/comments/6rb4wk/lua_mods/

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3839
    • View Profile
Re: About writting mods.
« Reply #4 on: October 24, 2017, 01:30:45 PM »
In what place of c++ code exacly json is loaded into game ?

Each type has its own loader function. They are defined in relevant files (say, item_factory.cpp covers item loading), then assigned to json types in init.cpp. That is, which function is called depends on "type" field of the json.
Many jsons use a structure called generic_factory (in generic_factory.h) to store the loaded values.

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
Re: About writting mods.
« Reply #5 on: October 24, 2017, 04:23:42 PM »
Which exacly lines of .cpp code load json and lua mods in those files ?
« Last Edit: October 24, 2017, 04:36:34 PM by however »

Offline Coolthulhu

  • Contributor
  • Survivor
  • ****
  • Posts: 3839
    • View Profile
Re: About writting mods.
« Reply #6 on: October 24, 2017, 05:08:06 PM »
There are hundreds if not thousands of lines that do that. You'll need to be much more specific.

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
Re: About writting mods.
« Reply #7 on: October 24, 2017, 05:25:54 PM »
I just not get it where game ask for mods.

Offline Zhilkin

  • NPC
  • **
  • Posts: 218
    • View Profile
Re: About writting mods.
« Reply #8 on: October 24, 2017, 07:21:22 PM »
Following function loads mod data (see https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/game.cpp#L384):

Code: [Select]
void game::load_data_from_dir( const std::string &path, const std::string &src, loading_ui &ui )

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
Re: About writting mods.
« Reply #9 on: October 24, 2017, 07:44:46 PM »
what is "const std::string & path, const std::string &src" ?

Offline Zhilkin

  • NPC
  • **
  • Posts: 218
    • View Profile
Re: About writting mods.
« Reply #10 on: October 24, 2017, 07:48:24 PM »
what is "const std::string & path, const std::string &src" ?
You can look for function calls:

Code: [Select]
load_data_from_dir( mod.path, mod.ident, ui );

first parameter is mod folder, second is mod identifier

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
Re: About writting mods.
« Reply #11 on: October 24, 2017, 08:13:12 PM »
Mod options are reading from that mod folder all time or those things go to ram untill game is end ?
« Last Edit: October 24, 2017, 08:14:55 PM by however »

Offline Zhilkin

  • NPC
  • **
  • Posts: 218
    • View Profile
Re: About writting mods.
« Reply #12 on: October 24, 2017, 09:35:51 PM »
Data is loaded to memory on new game start/savegame load.

Offline however

  • Zombie Food
  • *
  • Posts: 13
    • View Profile
Re: About writting mods.
« Reply #13 on: October 25, 2017, 02:14:08 AM »
Now i get it, thanks ! :)