source: trunk/src/lua/lua_map_area.cc @ 435

Last change on this file since 435 was 431, checked in by epyon, 10 years ago
  • hash storage type
  • string hash storage type
  • several minor fixes
  • more cleanups needed
File size: 6.2 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/lua/lua_map_area.hh"
8#include "nv/stl/flags.hh"
9#include "nv/lua/lua_area.hh"
10#include "nv/lua/lua_glm.hh"
11#include "nv/lua/lua_values.hh"
12#include "nv/lua/lua_raw.hh"
13
14static const char* NLUA_MAP_AREA_METATABLE = "map_area";
15
16typedef nv::flags<512> cell_set;
17
18static nv::uint32 nlua_to_cell_id( lua_State* L, int index, nv::map_area* map )
19{
20        if ( lua_type( L, index ) == LUA_TSTRING )
21                return map->string_to_id( lua_tostring( L, index ) );
22        else
23                return lua_tounsigned( L, index );
24}
25
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// }
48
49bool nv::lua::detail::is_map_area( lua_State* L, int index )
50{
51        return luaL_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
52}
53
54nv::map_area* nv::lua::detail::to_map_area( lua_State* L, int index )
55{
56        nv::map_area* o = nullptr;
57        if ( lua_istable( L , index ) )
58        {
59                lua_pushliteral( L, "__map_area_ptr" );
60                lua_rawget( L, index );
61                if ( lua_isuserdata( L, -1 ) )
62                {
63                        o = static_cast<nv::map_area*>( lua_touserdata( L, -1 ) );
64                }
65                lua_pop( L, 1 );
66        }
67        else
68        {
69                return *reinterpret_cast<nv::map_area**>( luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE ) );
70        }
71        return o;
72}
73
74using nv::lua::detail::is_map_area;
75using nv::lua::detail::to_map_area;
76using nv::lua::detail::push_map_area;
77
78using nv::lua::detail::is_coord;
79using nv::lua::detail::to_coord;
80using nv::lua::detail::to_pcoord;
81using nv::lua::detail::push_coord;
82
83using nv::lua::detail::is_area;
84using nv::lua::detail::to_area;
85using nv::lua::detail::to_parea;
86using nv::lua::detail::push_area;
87
88void nv::lua::detail::push_map_area( lua_State* L, nv::map_area* c )
89{
90        nv::map_area** pm = reinterpret_cast<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
96static int nlua_map_area_tostring( lua_State* L )
97{
98        lua_pushliteral ( L, "map_area" );
99        return 1;
100}
101
102static int nlua_map_area_gc( lua_State* L )
103{
104        nv::map_area* ma = to_map_area( L, 1 );
105        if ( ma != nullptr )
106        {
107                delete ma;
108        }
109        return 0;
110}
111
112static int nlua_map_area_get_area( lua_State* L )
113{
114        nv::map_area* ma = to_map_area( L, 1 );
115        nv::rectangle r  = ma->get_rectangle();
116        r.lr.x -= 1;
117        r.lr.y -= 1;
118        push_area( L, r );
119        return 1;
120}
121
122static int nlua_map_area_get_shift( lua_State* L )
123{
124        nv::map_area* ma = to_map_area( L, 1 );
125        push_coord( L, ma->get_shift() );
126        return 1;
127}
128
129static int nlua_map_area_get_size( lua_State* L )
130{
131        nv::map_area* ma = to_map_area( L, 1 );
132        push_coord( L, ma->get_size() );
133        return 1;
134}
135
136static int nlua_map_area_get_cell( lua_State* L )
137{
138        nv::map_area* ma = to_map_area( L, 1 );
139        nv::string_view result( ma->id_to_string( ma->get_cell( to_coord( L, 2 ) ) ) );
140        lua_pushlstring( L, result.data(), result.size() );
141        return 1;
142}
143
144static int nlua_map_area_set_cell( lua_State* L )
145{
146        nv::map_area* ma = to_map_area( L, 1 );
147        ma->set_cell( to_coord( L, 2 ), nlua_to_cell_id( L, 3, ma ) );
148        return 0;
149}
150
151static int nlua_map_area_raw_get_cell( lua_State* L )
152{
153        nv::map_area* ma = to_map_area( L, 1 );
154        lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
155        return 1;
156}
157
158static int nlua_map_area_raw_set_cell( lua_State* L )
159{
160        nv::map_area* ma = to_map_area( L, 1 );
161        ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
162        return 0;
163}
164
165static int nlua_map_area_index( lua_State* L )
166{
167        nv::map_area* ma = to_map_area( L, 1 );
168        if ( lua_type( L, 1 ) == LUA_TSTRING )
169        {
170                luaL_getmetafield( L, 1, "__functions" );
171                lua_pushvalue( L, 2 );
172                lua_rawget( L, -2 );
173        }
174        else
175        {
176                lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
177        }
178        return 1;
179}
180
181static int nlua_map_area_newindex( lua_State* L )
182{
183        nv::map_area* ma = to_map_area( L, 1 );
184        ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
185        return 0;
186}
187
188static int nlua_map_area_new_sub_area( lua_State* L )
189{
190        nv::map_area* ma = to_map_area( L, 1 );
191        push_map_area( L, ma->create_sub_area( to_area( L, 2 ) ) );
192        return 1;
193}
194
195static const luaL_Reg nlua_map_area_f[] = {
196        { "get_area",     nlua_map_area_get_area },
197        { "get_shift",    nlua_map_area_get_shift },
198        { "get_size",     nlua_map_area_get_size },
199        { "get_cell",     nlua_map_area_get_cell },
200        { "set_cell",     nlua_map_area_set_cell },
201        { "raw_get_cell", nlua_map_area_raw_get_cell },
202        { "raw_set_cell", nlua_map_area_raw_set_cell },
203        { "new_sub_area", nlua_map_area_new_sub_area },
204        {NULL, NULL}
205};
206
207static const luaL_Reg nlua_map_area_mt[] = {
208        { "__newindex", nlua_map_area_newindex },
209        { "__index",    nlua_map_area_index },
210        { "__tostring", nlua_map_area_tostring },
211        { "__gc",       nlua_map_area_gc },
212        {NULL, NULL}
213};
214
215static int luaopen_map_area( lua_State * L )
216{
217        luaL_newmetatable( L, NLUA_MAP_AREA_METATABLE );
218        nlua_register( L, nlua_map_area_mt, -1 );
219        lua_createtable( L, 0, 0 );
220        nlua_register( L, nlua_map_area_f, -1 );
221        lua_setfield(L, -2, "__functions" );
222        lua_pop( L, 1 );
223        return 0;
224}
225
226void nv::lua::register_map_area_interface( lua_State* L, int index )
227{
228        nlua_register( L, nlua_map_area_f, index );
229}
230
231void nv::lua::register_map_area( lua_State* L )
232{
233        luaopen_map_area(L);
234}
235
236void nv::lua::register_map_area_instance( lua_State* L, ref object_index, map_area* area )
237{
238        lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() );
239        lua_pushliteral( L, "__map_area_ptr" );
240        lua_pushlightuserdata( L, area );
241        lua_rawset( L, -3 );
242        lua_pop( L, 1 );
243}
Note: See TracBrowser for help on using the repository browser.