[179] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 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"
|
---|
| 8 | #include "nv/flags.hh"
|
---|
| 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 | {
|
---|
[206] | 56 | if ( lua_type( L, index ) == LUA_TTABLE )
|
---|
| 57 | {
|
---|
| 58 | nv::map_area* o = nullptr;
|
---|
| 59 | if ( lua_istable( L , index ) )
|
---|
| 60 | {
|
---|
| 61 | lua_pushstring( L, "__map_area_ptr" );
|
---|
| 62 | lua_rawget( L, index );
|
---|
| 63 | if ( lua_isuserdata( L, -1 ) )
|
---|
| 64 | {
|
---|
| 65 | o = static_cast<nv::map_area*>( lua_touserdata( L, -1 ) );
|
---|
| 66 | }
|
---|
| 67 | lua_pop( L, 1 );
|
---|
| 68 | }
|
---|
| 69 | return o;
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | {
|
---|
| 73 | return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE );
|
---|
| 74 | }
|
---|
[179] | 75 | }
|
---|
| 76 |
|
---|
[207] | 77 | using nv::lua::detail::is_map_area;
|
---|
| 78 | using nv::lua::detail::to_map_area;
|
---|
| 79 | using nv::lua::detail::push_map_area;
|
---|
| 80 |
|
---|
| 81 | using nv::lua::detail::is_coord;
|
---|
| 82 | using nv::lua::detail::to_coord;
|
---|
| 83 | using nv::lua::detail::to_pcoord;
|
---|
| 84 | using nv::lua::detail::push_coord;
|
---|
| 85 |
|
---|
| 86 | using nv::lua::detail::is_area;
|
---|
| 87 | using nv::lua::detail::to_area;
|
---|
| 88 | using nv::lua::detail::to_parea;
|
---|
| 89 | using nv::lua::detail::push_area;
|
---|
| 90 |
|
---|
| 91 | void nv::lua::detail::push_map_area( lua_State* L, nv::map_area* c )
|
---|
[179] | 92 | {
|
---|
| 93 | nv::map_area** pm = (nv::map_area**) (lua_newuserdata(L, sizeof(nv::map_area*)));
|
---|
| 94 | *pm = c;
|
---|
| 95 | luaL_getmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 96 | lua_setmetatable( L, -2 );
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | static int nlua_map_area_tostring( lua_State* L )
|
---|
| 100 | {
|
---|
| 101 | lua_pushstring( L, "map_area" );
|
---|
| 102 | return 1;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | static int nlua_map_area_gc( lua_State* L )
|
---|
| 106 | {
|
---|
[207] | 107 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
[179] | 108 | if ( ma != nullptr )
|
---|
| 109 | {
|
---|
| 110 | delete ma;
|
---|
| 111 | }
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | static int nlua_map_area_get_area( lua_State* L )
|
---|
| 116 | {
|
---|
[207] | 117 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
[206] | 118 | nv::rectangle r = ma->get_rectangle();
|
---|
| 119 | r.lr.x -= 1;
|
---|
| 120 | r.lr.y -= 1;
|
---|
[207] | 121 | push_area( L, r );
|
---|
[179] | 122 | return 1;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static int nlua_map_area_get_shift( lua_State* L )
|
---|
| 126 | {
|
---|
[207] | 127 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 128 | push_coord( L, ma->get_shift() );
|
---|
[179] | 129 | return 1;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | static int nlua_map_area_get_size( lua_State* L )
|
---|
| 133 | {
|
---|
[207] | 134 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 135 | push_coord( L, ma->get_size() );
|
---|
[179] | 136 | return 1;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | static int nlua_map_area_get_cell( lua_State* L )
|
---|
| 140 | {
|
---|
[207] | 141 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 142 | lua_pushstring( L, ma->id_to_string( ma->get_cell( to_coord( L, 2 ) ) ).c_str() );
|
---|
[179] | 143 | return 1;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static int nlua_map_area_set_cell( lua_State* L )
|
---|
| 147 | {
|
---|
[207] | 148 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 149 | ma->set_cell( to_coord( L, 2 ), nlua_to_cell_id( L, 3, ma ) );
|
---|
[179] | 150 | return 0;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static int nlua_map_area_raw_get_cell( lua_State* L )
|
---|
| 154 | {
|
---|
[207] | 155 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 156 | lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
|
---|
[179] | 157 | return 1;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | static int nlua_map_area_raw_set_cell( lua_State* L )
|
---|
| 161 | {
|
---|
[207] | 162 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 163 | ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 164 | return 0;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | static int nlua_map_area_index( lua_State* L )
|
---|
| 168 | {
|
---|
[207] | 169 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
[179] | 170 | if ( lua_type( L, 1 ) == LUA_TSTRING )
|
---|
| 171 | {
|
---|
| 172 | luaL_getmetafield( L, 1, "__functions" );
|
---|
| 173 | lua_pushvalue( L, 2 );
|
---|
| 174 | lua_rawget( L, -2 );
|
---|
| 175 | }
|
---|
| 176 | else
|
---|
| 177 | {
|
---|
[207] | 178 | lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
|
---|
[179] | 179 | }
|
---|
| 180 | return 1;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | static int nlua_map_area_newindex( lua_State* L )
|
---|
| 184 | {
|
---|
[207] | 185 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 186 | ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 187 | return 0;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | static int nlua_map_area_new_sub_area( lua_State* L )
|
---|
| 191 | {
|
---|
[207] | 192 | nv::map_area* ma = to_map_area( L, 1 );
|
---|
| 193 | push_map_area( L, ma->create_sub_area( to_area( L, 2 ) ) );
|
---|
[179] | 194 | return 1;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | static const luaL_Reg nlua_map_area_f[] = {
|
---|
| 198 | { "get_area", nlua_map_area_get_area },
|
---|
| 199 | { "get_shift", nlua_map_area_get_shift },
|
---|
| 200 | { "get_size", nlua_map_area_get_size },
|
---|
| 201 | { "get_cell", nlua_map_area_get_cell },
|
---|
| 202 | { "set_cell", nlua_map_area_set_cell },
|
---|
| 203 | { "raw_get_cell", nlua_map_area_raw_get_cell },
|
---|
| 204 | { "raw_set_cell", nlua_map_area_raw_set_cell },
|
---|
| 205 | { "new_sub_area", nlua_map_area_new_sub_area },
|
---|
| 206 | {NULL, NULL}
|
---|
| 207 | };
|
---|
| 208 |
|
---|
| 209 | static const luaL_Reg nlua_map_area_mt[] = {
|
---|
| 210 | { "__newindex", nlua_map_area_newindex },
|
---|
| 211 | { "__index", nlua_map_area_index },
|
---|
| 212 | { "__tostring", nlua_map_area_tostring },
|
---|
| 213 | { "__gc", nlua_map_area_gc },
|
---|
| 214 | {NULL, NULL}
|
---|
| 215 | };
|
---|
| 216 |
|
---|
[198] | 217 | static int luaopen_map_area( lua_State * L )
|
---|
[179] | 218 | {
|
---|
| 219 | luaL_newmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 220 | nlua_register( L, nlua_map_area_mt, -1 );
|
---|
| 221 | lua_createtable( L, 0, 0 );
|
---|
| 222 | nlua_register( L, nlua_map_area_f, -1 );
|
---|
| 223 | lua_setfield(L, -2, "__functions" );
|
---|
| 224 | lua_pop( L, 1 );
|
---|
| 225 | return 0;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[207] | 228 | void nv::lua::register_map_area_interface( lua_State* L, int index )
|
---|
[179] | 229 | {
|
---|
| 230 | nlua_register( L, nlua_map_area_f, index );
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[207] | 233 | void nv::lua::register_map_area( lua_State* L )
|
---|
[179] | 234 | {
|
---|
| 235 | luaopen_map_area(L);
|
---|
| 236 | }
|
---|