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

Last change on this file since 479 was 452, checked in by epyon, 10 years ago
  • lua_glm -> lua_math
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_math.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        nlua_pushstringview( L, ma->id_to_string( ma->get_cell( to_coord( L, 2 ) ) ) );
140        return 1;
141}
142
143static int nlua_map_area_set_cell( lua_State* L )
144{
145        nv::map_area* ma = to_map_area( L, 1 );
146        ma->set_cell( to_coord( L, 2 ), nlua_to_cell_id( L, 3, ma ) );
147        return 0;
148}
149
150static int nlua_map_area_raw_get_cell( lua_State* L )
151{
152        nv::map_area* ma = to_map_area( L, 1 );
153        lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
154        return 1;
155}
156
157static int nlua_map_area_raw_set_cell( lua_State* L )
158{
159        nv::map_area* ma = to_map_area( L, 1 );
160        ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
161        return 0;
162}
163
164static int nlua_map_area_index( lua_State* L )
165{
166        nv::map_area* ma = to_map_area( L, 1 );
167        if ( lua_type( L, 1 ) == LUA_TSTRING )
168        {
169                luaL_getmetafield( L, 1, "__functions" );
170                lua_pushvalue( L, 2 );
171                lua_rawget( L, -2 );
172        }
173        else
174        {
175                lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
176        }
177        return 1;
178}
179
180static int nlua_map_area_newindex( lua_State* L )
181{
182        nv::map_area* ma = to_map_area( L, 1 );
183        ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
184        return 0;
185}
186
187static int nlua_map_area_new_sub_area( lua_State* L )
188{
189        nv::map_area* ma = to_map_area( L, 1 );
190        push_map_area( L, ma->create_sub_area( to_area( L, 2 ) ) );
191        return 1;
192}
193
194static const luaL_Reg nlua_map_area_f[] = {
195        { "get_area",     nlua_map_area_get_area },
196        { "get_shift",    nlua_map_area_get_shift },
197        { "get_size",     nlua_map_area_get_size },
198        { "get_cell",     nlua_map_area_get_cell },
199        { "set_cell",     nlua_map_area_set_cell },
200        { "raw_get_cell", nlua_map_area_raw_get_cell },
201        { "raw_set_cell", nlua_map_area_raw_set_cell },
202        { "new_sub_area", nlua_map_area_new_sub_area },
203        {NULL, NULL}
204};
205
206static const luaL_Reg nlua_map_area_mt[] = {
207        { "__newindex", nlua_map_area_newindex },
208        { "__index",    nlua_map_area_index },
209        { "__tostring", nlua_map_area_tostring },
210        { "__gc",       nlua_map_area_gc },
211        {NULL, NULL}
212};
213
214static int luaopen_map_area( lua_State * L )
215{
216        luaL_newmetatable( L, NLUA_MAP_AREA_METATABLE );
217        nlua_register( L, nlua_map_area_mt, -1 );
218        lua_createtable( L, 0, 0 );
219        nlua_register( L, nlua_map_area_f, -1 );
220        lua_setfield(L, -2, "__functions" );
221        lua_pop( L, 1 );
222        return 0;
223}
224
225void nv::lua::register_map_area_interface( lua_State* L, int index )
226{
227        nlua_register( L, nlua_map_area_f, index );
228}
229
230void nv::lua::register_map_area( lua_State* L )
231{
232        luaopen_map_area(L);
233}
234
235void nv::lua::register_map_area_instance( lua_State* L, ref object_index, map_area* area )
236{
237        lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() );
238        lua_pushliteral( L, "__map_area_ptr" );
239        lua_pushlightuserdata( L, area );
240        lua_rawset( L, -3 );
241        lua_pop( L, 1 );
242}
Note: See TracBrowser for help on using the repository browser.