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

Last change on this file since 220 was 207, checked in by epyon, 12 years ago
  • lua - concept change - all functionality in nv::lua except raw
  • lua - all modules made header-independent from raw (except obviously raw)
File size: 5.9 KB
Line 
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_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        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        }
75}
76
77using nv::lua::detail::is_map_area;
78using nv::lua::detail::to_map_area;
79using nv::lua::detail::push_map_area;
80
81using nv::lua::detail::is_coord;
82using nv::lua::detail::to_coord;
83using nv::lua::detail::to_pcoord;
84using nv::lua::detail::push_coord;
85
86using nv::lua::detail::is_area;
87using nv::lua::detail::to_area;
88using nv::lua::detail::to_parea;
89using nv::lua::detail::push_area;
90
91void nv::lua::detail::push_map_area( lua_State* L, nv::map_area* c )
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
99static int nlua_map_area_tostring( lua_State* L )
100{
101        lua_pushstring( L, "map_area" );
102        return 1;
103}
104
105static int nlua_map_area_gc( lua_State* L )
106{
107        nv::map_area* ma = to_map_area( L, 1 );
108        if ( ma != nullptr )
109        {
110                delete ma;
111        }
112        return 0;
113}
114
115static int nlua_map_area_get_area( lua_State* L )
116{
117        nv::map_area* ma = to_map_area( L, 1 );
118        nv::rectangle r  = ma->get_rectangle();
119        r.lr.x -= 1;
120        r.lr.y -= 1;
121        push_area( L, r );
122        return 1;
123}
124
125static int nlua_map_area_get_shift( lua_State* L )
126{
127        nv::map_area* ma = to_map_area( L, 1 );
128        push_coord( L, ma->get_shift() );
129        return 1;
130}
131
132static int nlua_map_area_get_size( lua_State* L )
133{
134        nv::map_area* ma = to_map_area( L, 1 );
135        push_coord( L, ma->get_size() );
136        return 1;
137}
138
139static int nlua_map_area_get_cell( lua_State* L )
140{
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() );
143        return 1;
144}
145
146static int nlua_map_area_set_cell( lua_State* L )
147{
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 ) );
150        return 0;
151}
152
153static int nlua_map_area_raw_get_cell( lua_State* L )
154{
155        nv::map_area* ma = to_map_area( L, 1 );
156        lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
157        return 1;
158}
159
160static int nlua_map_area_raw_set_cell( lua_State* L )
161{
162        nv::map_area* ma = to_map_area( L, 1 );
163        ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
164        return 0;
165}
166
167static int nlua_map_area_index( lua_State* L )
168{
169        nv::map_area* ma = to_map_area( L, 1 );
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        {
178                lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
179        }
180        return 1;
181}
182
183static int nlua_map_area_newindex( lua_State* L )
184{
185        nv::map_area* ma = to_map_area( L, 1 );
186        ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
187        return 0;
188}
189
190static int nlua_map_area_new_sub_area( lua_State* L )
191{
192        nv::map_area* ma = to_map_area( L, 1 );
193        push_map_area( L, ma->create_sub_area( to_area( L, 2 ) ) );
194        return 1;
195}
196
197static 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
209static 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
217static int luaopen_map_area( lua_State * L )
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
228void nv::lua::register_map_area_interface( lua_State* L, int index )
229{
230        nlua_register( L, nlua_map_area_f, index );
231}
232
233void nv::lua::register_map_area( lua_State* L )
234{
235        luaopen_map_area(L);
236}
Note: See TracBrowser for help on using the repository browser.