Allow damage for attached objects, add attach/detach callbacks (#6786)
[oweals/minetest.git] / src / script / lua_api / l_modchannels.cpp
1 /*
2 Minetest
3 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <cassert>
21 #include <log.h>
22 #include "lua_api/l_modchannels.h"
23 #include "l_internal.h"
24 #include "modchannels.h"
25
26 int ModApiChannels::l_mod_channel_join(lua_State *L)
27 {
28         if (!lua_isstring(L, 1))
29                 return 0;
30
31         std::string channel = luaL_checkstring(L, 1);
32         if (channel.empty())
33                 return 0;
34
35         getGameDef(L)->joinModChannel(channel);
36         ModChannel *channelObj = getGameDef(L)->getModChannel(channel);
37         assert(channelObj);
38         ModChannelRef::create(L, channelObj);
39
40         int object = lua_gettop(L);
41         lua_pushvalue(L, object);
42         return 1;
43 }
44
45 void ModApiChannels::Initialize(lua_State *L, int top)
46 {
47         API_FCT(mod_channel_join);
48 }
49
50 /*
51  * ModChannelRef
52  */
53
54 ModChannelRef::ModChannelRef(ModChannel *modchannel) : m_modchannel(modchannel)
55 {
56 }
57
58 int ModChannelRef::l_leave(lua_State *L)
59 {
60         ModChannelRef *ref = checkobject(L, 1);
61         ModChannel *channel = getobject(ref);
62         if (!channel)
63                 return 0;
64
65         getGameDef(L)->leaveModChannel(channel->getName());
66         // Channel left, invalidate the channel object ptr
67         // This permits to invalidate every object action from Lua because core removed
68         // channel consuming link
69         ref->m_modchannel = nullptr;
70         return 0;
71 }
72
73 int ModChannelRef::l_send_all(lua_State *L)
74 {
75         ModChannelRef *ref = checkobject(L, 1);
76         ModChannel *channel = getobject(ref);
77         if (!channel || !channel->canWrite())
78                 return 0;
79
80         // @TODO serialize message
81         std::string message = luaL_checkstring(L, 2);
82
83         getGameDef(L)->sendModChannelMessage(channel->getName(), message);
84         return 0;
85 }
86
87 int ModChannelRef::l_is_writeable(lua_State *L)
88 {
89         ModChannelRef *ref = checkobject(L, 1);
90         ModChannel *channel = getobject(ref);
91         if (!channel)
92                 return 0;
93
94         lua_pushboolean(L, channel->canWrite());
95         return 1;
96 }
97 void ModChannelRef::Register(lua_State *L)
98 {
99         lua_newtable(L);
100         int methodtable = lua_gettop(L);
101         luaL_newmetatable(L, className);
102         int metatable = lua_gettop(L);
103
104         lua_pushliteral(L, "__metatable");
105         lua_pushvalue(L, methodtable);
106         lua_settable(L, metatable); // hide metatable from lua getmetatable()
107
108         lua_pushliteral(L, "__index");
109         lua_pushvalue(L, methodtable);
110         lua_settable(L, metatable);
111
112         lua_pushliteral(L, "__gc");
113         lua_pushcfunction(L, gc_object);
114         lua_settable(L, metatable);
115
116         lua_pop(L, 1); // Drop metatable
117
118         luaL_openlib(L, 0, methods, 0); // fill methodtable
119         lua_pop(L, 1);                  // Drop methodtable
120 }
121
122 void ModChannelRef::create(lua_State *L, ModChannel *channel)
123 {
124         ModChannelRef *o = new ModChannelRef(channel);
125         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
126         luaL_getmetatable(L, className);
127         lua_setmetatable(L, -2);
128 }
129
130 int ModChannelRef::gc_object(lua_State *L)
131 {
132         ModChannelRef *o = *(ModChannelRef **)(lua_touserdata(L, 1));
133         delete o;
134         return 0;
135 }
136
137 ModChannelRef *ModChannelRef::checkobject(lua_State *L, int narg)
138 {
139         luaL_checktype(L, narg, LUA_TUSERDATA);
140
141         void *ud = luaL_checkudata(L, narg, className);
142         if (!ud)
143                 luaL_typerror(L, narg, className);
144
145         return *(ModChannelRef **)ud; // unbox pointer
146 }
147
148 ModChannel *ModChannelRef::getobject(ModChannelRef *ref)
149 {
150         return ref->m_modchannel;
151 }
152
153 // clang-format off
154 const char ModChannelRef::className[] = "ModChannelRef";
155 const luaL_Reg ModChannelRef::methods[] = {
156         luamethod(ModChannelRef, leave),
157         luamethod(ModChannelRef, is_writeable),
158         luamethod(ModChannelRef, send_all),
159         {0, 0},
160 };
161 // clang-format on