[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"
|
---|
| 11 | #include "nv/lua/lua_raw.hh"
|
---|
| 12 |
|
---|
| 13 | static const char* NLUA_MAP_AREA_METATABLE = "map_area";
|
---|
| 14 |
|
---|
| 15 | typedef nv::flags<512> cell_set;
|
---|
| 16 |
|
---|
[198] | 17 | static nv::uint32 nlua_to_cell_id( lua_State* L, int index, nv::map_area* map )
|
---|
[179] | 18 | {
|
---|
| 19 | if ( lua_type( L, index ) == LUA_TSTRING )
|
---|
| 20 | return map->string_to_id( lua_tostring( L, index ) );
|
---|
| 21 | else
|
---|
[198] | 22 | return lua_tounsigned( L, index );
|
---|
[179] | 23 | }
|
---|
| 24 |
|
---|
[198] | 25 | // static cell_set nlua_to_cell_set( lua_State* L, int index, nv::map_area* map )
|
---|
| 26 | // {
|
---|
| 27 | // cell_set result;
|
---|
| 28 | // switch ( lua_type( L, index ) )
|
---|
| 29 | // {
|
---|
| 30 | // case LUA_TTABLE :
|
---|
| 31 | // {
|
---|
| 32 | // lua_pushnil( L );
|
---|
| 33 | // while ( lua_next( L, index ) != 0 )
|
---|
| 34 | // {
|
---|
| 35 | // if ( lua_type( L, -1 ) == LUA_TSTRING )
|
---|
| 36 | // result.set( map->string_to_id( lua_tostring( L, -1 ) ), true );
|
---|
| 37 | // else
|
---|
| 38 | // result.set( lua_tounsigned( L, -1 ), true );
|
---|
| 39 | // lua_pop( L, 1 );
|
---|
| 40 | // }
|
---|
| 41 | // } break;
|
---|
| 42 | // case LUA_TSTRING : result.set( map->string_to_id( lua_tostring( L, index ) ), true ); break;
|
---|
| 43 | // case LUA_TNUMBER : result.set( lua_tounsigned( L, index ), true ); break;
|
---|
| 44 | // }
|
---|
| 45 | // return result;
|
---|
| 46 | // }
|
---|
[179] | 47 |
|
---|
| 48 | bool nlua_is_map_area( lua_State* L, int index )
|
---|
| 49 | {
|
---|
| 50 | return luaL_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | nv::map_area* nlua_to_map_area( lua_State* L, int index )
|
---|
| 54 | {
|
---|
| 55 | return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE );
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void nlua_push_map_area( lua_State* L, nv::map_area* c )
|
---|
| 59 | {
|
---|
| 60 | nv::map_area** pm = (nv::map_area**) (lua_newuserdata(L, sizeof(nv::map_area*)));
|
---|
| 61 | *pm = c;
|
---|
| 62 | luaL_getmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 63 | lua_setmetatable( L, -2 );
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | static int nlua_map_area_tostring( lua_State* L )
|
---|
| 67 | {
|
---|
| 68 | lua_pushstring( L, "map_area" );
|
---|
| 69 | return 1;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static int nlua_map_area_gc( lua_State* L )
|
---|
| 73 | {
|
---|
| 74 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 75 | if ( ma != nullptr )
|
---|
| 76 | {
|
---|
| 77 | delete ma;
|
---|
| 78 | }
|
---|
| 79 | return 0;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | static int nlua_map_area_get_area( lua_State* L )
|
---|
| 83 | {
|
---|
| 84 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 85 | nlua_push_area( L, ma->get_rectangle() );
|
---|
| 86 | return 1;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | static int nlua_map_area_get_shift( lua_State* L )
|
---|
| 90 | {
|
---|
| 91 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 92 | nlua_push_coord( L, ma->get_shift() );
|
---|
| 93 | return 1;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | static int nlua_map_area_get_size( lua_State* L )
|
---|
| 97 | {
|
---|
| 98 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 99 | nlua_push_coord( L, ma->get_size() );
|
---|
| 100 | return 1;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | static int nlua_map_area_get_cell( lua_State* L )
|
---|
| 104 | {
|
---|
| 105 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 106 | lua_pushstring( L, ma->id_to_string( ma->get_cell( nlua_to_coord( L, 2 ) ) ).c_str() );
|
---|
| 107 | return 1;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static int nlua_map_area_set_cell( lua_State* L )
|
---|
| 111 | {
|
---|
| 112 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 113 | ma->set_cell( nlua_to_coord( L, 2 ), nlua_to_cell_id( L, 3, ma ) );
|
---|
| 114 | return 0;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | static int nlua_map_area_raw_get_cell( lua_State* L )
|
---|
| 118 | {
|
---|
| 119 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[198] | 120 | lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
|
---|
[179] | 121 | return 1;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static int nlua_map_area_raw_set_cell( lua_State* L )
|
---|
| 125 | {
|
---|
| 126 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[198] | 127 | ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 128 | return 0;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static int nlua_map_area_index( lua_State* L )
|
---|
| 132 | {
|
---|
| 133 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 134 | if ( lua_type( L, 1 ) == LUA_TSTRING )
|
---|
| 135 | {
|
---|
| 136 | luaL_getmetafield( L, 1, "__functions" );
|
---|
| 137 | lua_pushvalue( L, 2 );
|
---|
| 138 | lua_rawget( L, -2 );
|
---|
| 139 | }
|
---|
| 140 | else
|
---|
| 141 | {
|
---|
[198] | 142 | lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
|
---|
[179] | 143 | }
|
---|
| 144 | return 1;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | static int nlua_map_area_newindex( lua_State* L )
|
---|
| 148 | {
|
---|
| 149 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
[198] | 150 | ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
|
---|
[179] | 151 | return 0;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static int nlua_map_area_new_sub_area( lua_State* L )
|
---|
| 155 | {
|
---|
| 156 | nv::map_area* ma = nlua_to_map_area( L, 1 );
|
---|
| 157 | nlua_push_map_area( L, ma->create_sub_area( nlua_to_area( L, 2 ) ) );
|
---|
| 158 | return 1;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | static const luaL_Reg nlua_map_area_f[] = {
|
---|
| 162 | { "get_area", nlua_map_area_get_area },
|
---|
| 163 | { "get_shift", nlua_map_area_get_shift },
|
---|
| 164 | { "get_size", nlua_map_area_get_size },
|
---|
| 165 | { "get_cell", nlua_map_area_get_cell },
|
---|
| 166 | { "set_cell", nlua_map_area_set_cell },
|
---|
| 167 | { "raw_get_cell", nlua_map_area_raw_get_cell },
|
---|
| 168 | { "raw_set_cell", nlua_map_area_raw_set_cell },
|
---|
| 169 | { "new_sub_area", nlua_map_area_new_sub_area },
|
---|
| 170 | {NULL, NULL}
|
---|
| 171 | };
|
---|
| 172 |
|
---|
| 173 | static const luaL_Reg nlua_map_area_mt[] = {
|
---|
| 174 | { "__newindex", nlua_map_area_newindex },
|
---|
| 175 | { "__index", nlua_map_area_index },
|
---|
| 176 | { "__tostring", nlua_map_area_tostring },
|
---|
| 177 | { "__gc", nlua_map_area_gc },
|
---|
| 178 | {NULL, NULL}
|
---|
| 179 | };
|
---|
| 180 |
|
---|
[198] | 181 | static int luaopen_map_area( lua_State * L )
|
---|
[179] | 182 | {
|
---|
| 183 | luaL_newmetatable( L, NLUA_MAP_AREA_METATABLE );
|
---|
| 184 | nlua_register( L, nlua_map_area_mt, -1 );
|
---|
| 185 | lua_createtable( L, 0, 0 );
|
---|
| 186 | nlua_register( L, nlua_map_area_f, -1 );
|
---|
| 187 | lua_setfield(L, -2, "__functions" );
|
---|
| 188 | lua_pop( L, 1 );
|
---|
| 189 | return 0;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | void nlua_register_map_area_interface( lua_State* L, int index )
|
---|
| 193 | {
|
---|
| 194 | nlua_register( L, nlua_map_area_f, index );
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | void nlua_register_map_area( lua_State* L )
|
---|
| 198 | {
|
---|
| 199 | luaopen_map_area(L);
|
---|
| 200 | }
|
---|