[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[179] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/lua/lua_map_area.hh"
|
---|
[319] | 8 | #include "nv/core/flags.hh"
|
---|
[179] | 9 | #include "nv/lua/lua_area.hh"
|
---|
| 10 | #include "nv/lua/lua_glm.hh"
|
---|
[206] | 11 | #include "nv/lua/lua_values.hh"
|
---|
[179] | 12 | #include "nv/lua/lua_raw.hh"
|
---|
| 13 |
|
---|
| 14 | static const char* NLUA_MAP_AREA_METATABLE = "map_area";
|
---|
| 15 |
|
---|
| 16 | typedef nv::flags<512> cell_set;
|
---|
| 17 |
|
---|
[198] | 18 | static nv::uint32 nlua_to_cell_id( lua_State* L, int index, nv::map_area* map )
|
---|
[179] | 19 | {
|
---|
| 20 | if ( lua_type( L, index ) == LUA_TSTRING )
|
---|
| 21 | return map->string_to_id( lua_tostring( L, index ) );
|
---|
| 22 | else
|
---|
[198] | 23 | return lua_tounsigned( L, index );
|
---|
[179] | 24 | }
|
---|
| 25 |
|
---|
[198] | 26 | // static cell_set nlua_to_cell_set( lua_State* L, int index, nv::map_area* map )
|
---|
| 27 | // {
|
---|
| 28 | // cell_set result;
|
---|
| 29 | // switch ( lua_type( L, index ) )
|
---|
| 30 | // {
|
---|
| 31 | // case LUA_TTABLE :
|
---|
| 32 | // {
|
---|
| 33 | // lua_pushnil( L );
|
---|
| 34 | // while ( lua_next( L, index ) != 0 )
|
---|
| 35 | // {
|
---|
| 36 | // if ( lua_type( L, -1 ) == LUA_TSTRING )
|
---|
| 37 | // result.set( map->string_to_id( lua_tostring( L, -1 ) ), true );
|
---|
| 38 | // else
|
---|
| 39 | // result.set( lua_tounsigned( L, -1 ), true );
|
---|
| 40 | // lua_pop( L, 1 );
|
---|
| 41 | // }
|
---|
| 42 | // } break;
|
---|
| 43 | // case LUA_TSTRING : result.set( map->string_to_id( lua_tostring( L, index ) ), true ); break;
|
---|
| 44 | // case LUA_TNUMBER : result.set( lua_tounsigned( L, index ), true ); break;
|
---|
| 45 | // }
|
---|
| 46 | // return result;
|
---|
| 47 | // }
|
---|
[179] | 48 |
|
---|
[207] | 49 | bool nv::lua::detail::is_map_area( lua_State* L, int index )
|
---|
[179] | 50 | {
|
---|
| 51 | return luaL_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[207] | 54 | nv::map_area* nv::lua::detail::to_map_area( lua_State* L, int index )
|
---|
[179] | 55 | {
|
---|
[263] | 56 | nv::map_area* o = nullptr;
|
---|
| 57 | if ( lua_istable( L , index ) )
|
---|
[206] | 58 | {
|
---|
[263] | 59 | lua_pushstring( L, "__map_area_ptr" );
|
---|
| 60 | lua_rawget( L, index );
|
---|
| 61 | if ( lua_isuserdata( L, -1 ) )
|
---|
[206] | 62 | {
|
---|
[263] | 63 | o = static_cast<nv::map_area*>( lua_touserdata( L, -1 ) );
|
---|
| 64 | }
|
---|
| 65 | lua_pop( L, 1 );
|
---|
[206] | 66 | }
|
---|
| 67 | else
|
---|
| 68 | {
|
---|
| 69 | return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE );
|
---|
| 70 | }
|
---|
[263] | 71 | return o;
|
---|
[179] | 72 | }
|
---|
| 73 |
|
---|
[207] | 74 | using nv::lua::detail::is_map_area;
|
---|
| 75 | using nv::lua::detail::to_map_area;
|
---|
| 76 | using nv::lua::detail::push_map_area;
|
---|
| 77 |
|
---|
| 78 | using nv::lua::detail::is_coord;
|
---|
| 79 | using nv::lua::detail::to_coord;
|
---|
| 80 | using nv::lua::detail::to_pcoord;
|
---|
| 81 | using nv::lua::detail::push_coord;
|
---|
| 82 |
|
---|
| 83 | using nv::lua::detail::is_area;
|
---|
| 84 | using nv::lua::detail::to_area;
|
---|
| 85 | using nv::lua::detail::to_parea;
|
---|
| 86 | using nv::lua::detail::push_area;
|
---|
| 87 |
|
---|
| 88 | void nv::lua::detail::push_map_area( lua_State* L, nv::map_area* c )
|
---|
[179] | 89 | {
|
---|
| 90 | nv::map_area** pm = (nv::map_area**) (lua_newuserdata(L, sizeof(nv::map_area*)));
|
---|
| 91 | *pm = c;
|
---|
| 92 | luaL_getmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 93 | lua_setmetatable( L, -2 );
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | static int nlua_map_area_tostring( lua_State* L )
|
---|
| 97 | {
|
---|
| 98 | lua_pushstring( L, "map_area" );
|
---|
| 99 | return 1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | static int nlua_map_area_gc( lua_State* L )
|
---|
| 103 | {
|
---|
[207] | 104 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
[179] | 105 | if ( ma != nullptr )
|
---|
| 106 | {
|
---|
| 107 | delete ma;
|
---|
| 108 | }
|
---|
| 109 | return 0;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | static int nlua_map_area_get_area( lua_State* L )
|
---|
| 113 | {
|
---|
[207] | 114 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
[206] | 115 | nv::rectangle r = ma->get_rectangle();
|
---|
| 116 | r.lr.x -= 1;
|
---|
| 117 | r.lr.y -= 1;
|
---|
[207] | 118 | push_area( L, r );
|
---|
[179] | 119 | return 1;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | static int nlua_map_area_get_shift( lua_State* L )
|
---|
| 123 | {
|
---|
[207] | 124 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 125 | push_coord( L, ma->get_shift() );
|
---|
[179] | 126 | return 1;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | static int nlua_map_area_get_size( lua_State* L )
|
---|
| 130 | {
|
---|
[207] | 131 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 132 | push_coord( L, ma->get_size() );
|
---|
[179] | 133 | return 1;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static int nlua_map_area_get_cell( lua_State* L )
|
---|
| 137 | {
|
---|
[207] | 138 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 139 | lua_pushstring( L, ma->id_to_string( ma->get_cell( to_coord( L, 2 ) ) ).c_str() );
|
---|
[179] | 140 | return 1;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | static int nlua_map_area_set_cell( lua_State* L )
|
---|
| 144 | {
|
---|
[207] | 145 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 146 | ma->set_cell( to_coord( L, 2 ), nlua_to_cell_id( L, 3, ma ) );
|
---|
[179] | 147 | return 0;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static int nlua_map_area_raw_get_cell( lua_State* L )
|
---|
| 151 | {
|
---|
[207] | 152 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 153 | lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
|
---|
[179] | 154 | return 1;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | static int nlua_map_area_raw_set_cell( lua_State* L )
|
---|
| 158 | {
|
---|
[207] | 159 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 160 | ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 161 | return 0;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | static int nlua_map_area_index( lua_State* L )
|
---|
| 165 | {
|
---|
[207] | 166 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
[179] | 167 | if ( lua_type( L, 1 ) == LUA_TSTRING )
|
---|
| 168 | {
|
---|
| 169 | luaL_getmetafield( L, 1, "__functions" );
|
---|
| 170 | lua_pushvalue( L, 2 );
|
---|
| 171 | lua_rawget( L, -2 );
|
---|
| 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
[207] | 175 | lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
|
---|
[179] | 176 | }
|
---|
| 177 | return 1;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | static int nlua_map_area_newindex( lua_State* L )
|
---|
| 181 | {
|
---|
[207] | 182 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 183 | ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 184 | return 0;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | static int nlua_map_area_new_sub_area( lua_State* L )
|
---|
| 188 | {
|
---|
[207] | 189 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 190 | push_map_area( L, ma->create_sub_area( to_area( L, 2 ) ) );
|
---|
[179] | 191 | return 1;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | static const luaL_Reg nlua_map_area_f[] = {
|
---|
| 195 | { "get_area", nlua_map_area_get_area },
|
---|
| 196 | { "get_shift", nlua_map_area_get_shift },
|
---|
| 197 | { "get_size", nlua_map_area_get_size },
|
---|
| 198 | { "get_cell", nlua_map_area_get_cell },
|
---|
| 199 | { "set_cell", nlua_map_area_set_cell },
|
---|
| 200 | { "raw_get_cell", nlua_map_area_raw_get_cell },
|
---|
| 201 | { "raw_set_cell", nlua_map_area_raw_set_cell },
|
---|
| 202 | { "new_sub_area", nlua_map_area_new_sub_area },
|
---|
| 203 | {NULL, NULL}
|
---|
| 204 | };
|
---|
| 205 |
|
---|
| 206 | static const luaL_Reg nlua_map_area_mt[] = {
|
---|
| 207 | { "__newindex", nlua_map_area_newindex },
|
---|
| 208 | { "__index", nlua_map_area_index },
|
---|
| 209 | { "__tostring", nlua_map_area_tostring },
|
---|
| 210 | { "__gc", nlua_map_area_gc },
|
---|
| 211 | {NULL, NULL}
|
---|
| 212 | };
|
---|
| 213 |
|
---|
[198] | 214 | static int luaopen_map_area( lua_State * L )
|
---|
[179] | 215 | {
|
---|
| 216 | luaL_newmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 217 | nlua_register( L, nlua_map_area_mt, -1 );
|
---|
| 218 | lua_createtable( L, 0, 0 );
|
---|
| 219 | nlua_register( L, nlua_map_area_f, -1 );
|
---|
| 220 | lua_setfield(L, -2, "__functions" );
|
---|
| 221 | lua_pop( L, 1 );
|
---|
| 222 | return 0;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[207] | 225 | void nv::lua::register_map_area_interface( lua_State* L, int index )
|
---|
[179] | 226 | {
|
---|
| 227 | nlua_register( L, nlua_map_area_f, index );
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[207] | 230 | void nv::lua::register_map_area( lua_State* L )
|
---|
[179] | 231 | {
|
---|
| 232 | luaopen_map_area(L);
|
---|
| 233 | }
|
---|
[263] | 234 |
|
---|
[265] | 235 | void nv::lua::register_map_area_instance( lua_State* L, ref object_index, map_area* area )
|
---|
[263] | 236 | {
|
---|
[265] | 237 | lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[263] | 238 | lua_pushstring( L, "__map_area_ptr" );
|
---|
| 239 | lua_pushlightuserdata( L, (map_area*)area );
|
---|
| 240 | lua_rawset( L, -3 );
|
---|
| 241 | lua_pop( L, 1 );
|
---|
| 242 | }
|
---|