{"action":"created","issue":{"url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12451","labels_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12451/labels{/name}","comments_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12451/comments","events_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12451/events","html_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12451","id":79885166,"number":12451,"title":"Updating the C++ to Lua interface [CR]","user":{"login":"BevapDin","id":5095435,"avatar_url":"https://avatars.githubusercontent.com/u/5095435?v=3","gravatar_id":"","url":"https://api.github.com/users/BevapDin","html_url":"https://github.com/BevapDin","followers_url":"https://api.github.com/users/BevapDin/followers","following_url":"https://api.github.com/users/BevapDin/following{/other_user}","gists_url":"https://api.github.com/users/BevapDin/gists{/gist_id}","starred_url":"https://api.github.com/users/BevapDin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BevapDin/subscriptions","organizations_url":"https://api.github.com/users/BevapDin/orgs","repos_url":"https://api.github.com/users/BevapDin/repos","events_url":"https://api.github.com/users/BevapDin/events{/privacy}","received_events_url":"https://api.github.com/users/BevapDin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-05-23T16:08:16Z","updated_at":"2015-05-23T16:39:26Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/pulls/12451","html_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12451","diff_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12451.diff","patch_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12451.patch"},"body":"So the Lua part is pretty much dead, isn't it? That's a shame. Is there somebody interested in doing some Lua scripting?\r\n\r\nThis updates it to work with `tripoint`s and `point`s. Both get value semantics, their data is stored and managed by Lua. The other exported classes like `monster`, `itype` etc. are only exported as references. A Lua script can access them, but can't create new ones on their own. Lua can spawn monsters and get a reference to the newly spawned one, but it can't create `itype` instances. Through this is probably not a problem as everyone writing Lua can just as easy add the new object to the json data.\r\n\r\nA bunch of C++ function are now available by Lua, mostly member functions of the already exported types and wrapper to get/set their members. The whole thing is not const correct in the sightliest bit, one can freely write data to C++ members that should not really be accessible (however, there are no `const_cast`s involved, so it's *technically* fine).\r\n\r\nI toke the liberty to remove some function declaration that are actually nowhere defined. Some of the code that was generated by the Lua script is now stuffed into a class (accessed via static functions). It's a bit more type safe and way easier to read than C++ code in a Lua string.\r\n\r\nWhat you can do (enter the commands in the Lua \"console\" from the debug menu):\r\n- Give some item the `use_action` \"CUSTOM_IUSE_TEST\", spawn it and apply it. The Lua part should show a popup saying where the item is (tripoint), what item it is and whether it's active. This can of course be extend as you like. If you're lazy, you may call the use function directly: `custom_iuse_test(player:i_at(0), false, player:pos() )`\r\n- Everybody is my friend: `for x=0,game.num_zombies()-1 do game.zombie(x).friendly = -1; end`\r\n- You don't belong here: `game.zombie(0):setpos(tripoint(10, 10, 0), false)`\r\n- Enter the command `item_test()` while the character stands on top of some items. The Lua function will print a popup with the item name of each item on the characters position (tests the wrapper for the `item_stack` and its iterator).\r\n- Enter `necro()` while standing on a (revivable) corpse. The corpse will revive, the monster appears on a adjacent square (try in an open area, it needs space).\r\n- You don't like your character name? You don't have to live with! Enter `player.name = \"Urist McLazy\"` - may screw up your save!\r\n- You have no power? `player.power_level = 1000` What about `max_power_level`? That can be changed, too!\r\n- Get rid of all the items (under your feet)! `map:i_clear(player:pos())`\r\n- Get rid of all the items (by all, I mean all): `for x = 0,11*12-1 do for y = 0,11*12-1 do map:i_clear( tripoint(x,y,0) );end;end`\r\n- You're a cannibal, but you find never find enough human corpses? This is the solution: `map:add_corpse(player:pos())` - comes with free loot!\r\n- Or are you a Grazer? Get some shrubs: `map:draw_square_ter(\"t_shrub\", 60, 60, 80, 80)`\r\n- Zombies are stupid, right? They even kill themselves: `game.zombie(0):die(game.zombie(0) )` or you can make the dirty work: `game.zombie(0):die(player)` (watch the kill count)\r\n- Twice as interactive: `game.popup(game.string_input_popup(\"?\", 100, \"\") )` or `player.name = game.string_input_popup( \"Name?\", 100, player.name)` Stack as much as you like.\r\n\r\nThe exported function behave exactly as the C++ function (including all the bugs and preconditions), for mapgen relevant are probably the mapgen functions. This piece works:\r\n```JSON\r\n\"lua\" : [\r\n    \"game.popup('Lua is there to stay!')\",\r\n    \"game.popup(map:name(tripoint(0,0,0)))\",\r\n    \"game.dofile('<abs-path-to-some-Lua-script->')\"\r\n],\r\n```\r\nIt shows two popups and tries to load the listed file (must be an absolute path, for whatever reason). \r\n\r\n# Don't forget to compile with `LUA=1`\r\n\r\nAnd I gave the Lua debug input box a history, like the filter in the \"List items around\" has. And it can read files, I found this string to be very useful: `game.dofile(\"/tmp/x.lua\")` (it must be an absolute path). Once entered, you can get it back from the history with only a few keystrokes. The file is read each time the function is called, so you can edit the file with a proper editor.\r\n\r\n----\r\n\r\nThe bugs are probably strong in this one (-:\r\n\r\nTODO (for later, not here, not now, just for reference).\r\n- Function overloading must be done manually (grab the arguments and dispatch the to the proper function, alternatively give each function a unique name in Lua). Currently the class definitions contain several function of the same name, only the last definition of those is actually used.\r\n- Allow access to the game instance via `g` and maybe rename the global `map` to `m`\r\n- Implement a Lua wrapper that supports reference *and* value semantic (e.g. for items).\r\n- Allow loading Lua scripts files from mapgen definition with some relative path"},"comment":{"url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/comments/104923396","html_url":"https://github.com/CleverRaven/Cataclysm-DDA/pull/12451#issuecomment-104923396","issue_url":"https://api.github.com/repos/CleverRaven/Cataclysm-DDA/issues/12451","id":104923396,"user":{"login":"DavidKeaton","id":1715605,"avatar_url":"https://avatars.githubusercontent.com/u/1715605?v=3","gravatar_id":"","url":"https://api.github.com/users/DavidKeaton","html_url":"https://github.com/DavidKeaton","followers_url":"https://api.github.com/users/DavidKeaton/followers","following_url":"https://api.github.com/users/DavidKeaton/following{/other_user}","gists_url":"https://api.github.com/users/DavidKeaton/gists{/gist_id}","starred_url":"https://api.github.com/users/DavidKeaton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DavidKeaton/subscriptions","organizations_url":"https://api.github.com/users/DavidKeaton/orgs","repos_url":"https://api.github.com/users/DavidKeaton/repos","events_url":"https://api.github.com/users/DavidKeaton/events{/privacy}","received_events_url":"https://api.github.com/users/DavidKeaton/received_events","type":"User","site_admin":false},"created_at":"2015-05-23T16:39:26Z","updated_at":"2015-05-23T16:39:26Z","body":"it would require it, but luckily since it is an easy lang to embed, it may just be as easy as slipping in the DLL into the zips distributed, would it not? the typical load order of DLLs on win32 always checks the current directory first (which is where DLL hijacking is a byproduct from)."}}