[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 |
|
---|
| 49 | bool nlua_is_map_area( lua_State* L, int index )
|
---|
| 50 | {
|
---|
| 51 | return luaL_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | nv::map_area* nlua_to_map_area( lua_State* L, int index )
|
---|
| 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 |
|
---|
| 77 | void nlua_push_map_area( lua_State* L, nv::map_area* c )
|
---|
| 78 | {
|
---|
| 79 | nv::map_area** pm = (nv::map_area**) (lua_newuserdata(L, sizeof(nv::map_area*)));
|
---|
| 80 | *pm = c;
|
---|
| 81 | luaL_getmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 82 | lua_setmetatable( L, -2 );
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | static int nlua_map_area_tostring( lua_State* L )
|
---|
| 86 | {
|
---|
| 87 | lua_pushstring( L, "map_area" );
|
---|
| 88 | return 1;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | static int nlua_map_area_gc( lua_State* L )
|
---|
| 92 | {
|
---|
| 93 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 94 | if ( ma != nullptr )
|
---|
| 95 | {
|
---|
| 96 | delete ma;
|
---|
| 97 | }
|
---|
| 98 | return 0;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static int nlua_map_area_get_area( lua_State* L )
|
---|
| 102 | {
|
---|
| 103 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[206] | 104 | nv::rectangle r = ma->get_rectangle();
|
---|
| 105 | r.lr.x -= 1;
|
---|
| 106 | r.lr.y -= 1;
|
---|
| 107 | nlua_push_area( L, r );
|
---|
[179] | 108 | return 1;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | static int nlua_map_area_get_shift( lua_State* L )
|
---|
| 112 | {
|
---|
| 113 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 114 | nlua_push_coord( L, ma->get_shift() );
|
---|
| 115 | return 1;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | static int nlua_map_area_get_size( lua_State* L )
|
---|
| 119 | {
|
---|
| 120 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 121 | nlua_push_coord( L, ma->get_size() );
|
---|
| 122 | return 1;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static int nlua_map_area_get_cell( lua_State* L )
|
---|
| 126 | {
|
---|
| 127 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 128 | lua_pushstring( L, ma->id_to_string( ma->get_cell( nlua_to_coord( L, 2 ) ) ).c_str() );
|
---|
| 129 | return 1;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | static int nlua_map_area_set_cell( lua_State* L )
|
---|
| 133 | {
|
---|
| 134 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 135 | ma->set_cell( nlua_to_coord( L, 2 ), nlua_to_cell_id( L, 3, ma ) );
|
---|
| 136 | return 0;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | static int nlua_map_area_raw_get_cell( lua_State* L )
|
---|
| 140 | {
|
---|
| 141 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[198] | 142 | lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
|
---|
[179] | 143 | return 1;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static int nlua_map_area_raw_set_cell( lua_State* L )
|
---|
| 147 | {
|
---|
| 148 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[198] | 149 | ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 150 | return 0;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static int nlua_map_area_index( lua_State* L )
|
---|
| 154 | {
|
---|
| 155 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 156 | if ( lua_type( L, 1 ) == LUA_TSTRING )
|
---|
| 157 | {
|
---|
| 158 | luaL_getmetafield( L, 1, "__functions" );
|
---|
| 159 | lua_pushvalue( L, 2 );
|
---|
| 160 | lua_rawget( L, -2 );
|
---|
| 161 | }
|
---|
| 162 | else
|
---|
| 163 | {
|
---|
[198] | 164 | lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
|
---|
[179] | 165 | }
|
---|
| 166 | return 1;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | static int nlua_map_area_newindex( lua_State* L )
|
---|
| 170 | {
|
---|
| 171 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[198] | 172 | ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 173 | return 0;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | static int nlua_map_area_new_sub_area( lua_State* L )
|
---|
| 177 | {
|
---|
| 178 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 179 | nlua_push_map_area( L, ma->create_sub_area( nlua_to_area( L, 2 ) ) );
|
---|
| 180 | return 1;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | static const luaL_Reg nlua_map_area_f[] = {
|
---|
| 184 | { "get_area", nlua_map_area_get_area },
|
---|
| 185 | { "get_shift", nlua_map_area_get_shift },
|
---|
| 186 | { "get_size", nlua_map_area_get_size },
|
---|
| 187 | { "get_cell", nlua_map_area_get_cell },
|
---|
| 188 | { "set_cell", nlua_map_area_set_cell },
|
---|
| 189 | { "raw_get_cell", nlua_map_area_raw_get_cell },
|
---|
| 190 | { "raw_set_cell", nlua_map_area_raw_set_cell },
|
---|
| 191 | { "new_sub_area", nlua_map_area_new_sub_area },
|
---|
| 192 | {NULL, NULL}
|
---|
| 193 | };
|
---|
| 194 |
|
---|
| 195 | static const luaL_Reg nlua_map_area_mt[] = {
|
---|
| 196 | { "__newindex", nlua_map_area_newindex },
|
---|
| 197 | { "__index", nlua_map_area_index },
|
---|
| 198 | { "__tostring", nlua_map_area_tostring },
|
---|
| 199 | { "__gc", nlua_map_area_gc },
|
---|
| 200 | {NULL, NULL}
|
---|
| 201 | };
|
---|
| 202 |
|
---|
[198] | 203 | static int luaopen_map_area( lua_State * L )
|
---|
[179] | 204 | {
|
---|
| 205 | luaL_newmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 206 | nlua_register( L, nlua_map_area_mt, -1 );
|
---|
| 207 | lua_createtable( L, 0, 0 );
|
---|
| 208 | nlua_register( L, nlua_map_area_f, -1 );
|
---|
| 209 | lua_setfield(L, -2, "__functions" );
|
---|
| 210 | lua_pop( L, 1 );
|
---|
| 211 | return 0;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | void nlua_register_map_area_interface( lua_State* L, int index )
|
---|
| 215 | {
|
---|
| 216 | nlua_register( L, nlua_map_area_f, index );
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void nlua_register_map_area( lua_State* L )
|
---|
| 220 | {
|
---|
| 221 | luaopen_map_area(L);
|
---|
| 222 | }
|
---|