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

Last change on this file since 206 was 206, checked in by epyon, 12 years ago
  • lua - major refactoring of the lua state/table
  • lua - added state_wrapper common to state/table
  • lua_values - fixed returning of simple types
  • lua_area - fixed area.coords and area.edges
  • lua_map_area - "fixed" case when object is used as base
File size: 5.6 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 nlua_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* nlua_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
77void 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
85static int nlua_map_area_tostring( lua_State* L )
86{
87        lua_pushstring( L, "map_area" );
88        return 1;
89}
90
91static 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
101static int nlua_map_area_get_area( lua_State* L )
102{
103        nv::map_area* ma = nlua_to_map_area( L, 1 );
104        nv::rectangle r  = ma->get_rectangle();
105        r.lr.x -= 1;
106        r.lr.y -= 1;
107        nlua_push_area( L, r );
108        return 1;
109}
110
111static 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
118static 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
125static 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
132static 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
139static int nlua_map_area_raw_get_cell( lua_State* L )
140{
141        nv::map_area* ma = nlua_to_map_area( L, 1 );
142        lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
143        return 1;
144}
145
146static int nlua_map_area_raw_set_cell( lua_State* L )
147{
148        nv::map_area* ma = nlua_to_map_area( L, 1 );
149        ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
150        return 0;
151}
152
153static 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        {
164                lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
165        }
166        return 1;
167}
168
169static int nlua_map_area_newindex( lua_State* L )
170{
171        nv::map_area* ma = nlua_to_map_area( L, 1 );
172        ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
173        return 0;
174}
175
176static 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
183static 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
195static 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
203static int luaopen_map_area( lua_State * L )
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
214void nlua_register_map_area_interface( lua_State* L, int index )
215{
216        nlua_register( L, nlua_map_area_f, index );
217}
218
219void nlua_register_map_area( lua_State* L )
220{
221        luaopen_map_area(L);
222}
Note: See TracBrowser for help on using the repository browser.