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

Last change on this file since 198 was 198, checked in by epyon, 12 years ago
  • warning cleanup for clang and gcc
File size: 5.2 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_raw.hh"
12
13static const char* NLUA_MAP_AREA_METATABLE = "map_area";
14
15typedef nv::flags<512> cell_set;
16
17static nv::uint32 nlua_to_cell_id( lua_State* L, int index, nv::map_area* map )
18{
19        if ( lua_type( L, index ) == LUA_TSTRING )
20                return map->string_to_id( lua_tostring( L, index ) );
21        else
22                return lua_tounsigned( L, index );
23}
24
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// }
47
48bool nlua_is_map_area( lua_State* L, int index )
49{
50        return luaL_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
51}
52
53nv::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
58void 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
66static int nlua_map_area_tostring( lua_State* L )
67{
68        lua_pushstring( L, "map_area" );
69        return 1;
70}
71
72static 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
82static 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
89static 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
96static 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
103static 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
110static 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
117static int nlua_map_area_raw_get_cell( lua_State* L )
118{
119        nv::map_area* ma = nlua_to_map_area( L, 1 );
120        lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
121        return 1;
122}
123
124static int nlua_map_area_raw_set_cell( lua_State* L )
125{
126        nv::map_area* ma = nlua_to_map_area( L, 1 );
127        ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
128        return 0;
129}
130
131static 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        {
142                lua_pushunsigned( L, ma->get_cell( nlua_to_coord( L, 2 ) ) );
143        }
144        return 1;
145}
146
147static int nlua_map_area_newindex( lua_State* L )
148{
149        nv::map_area* ma = nlua_to_map_area( L, 1 );
150        ma->set_cell( nlua_to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
151        return 0;
152}
153
154static 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
161static 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
173static 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
181static int luaopen_map_area( lua_State * L )
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
192void nlua_register_map_area_interface( lua_State* L, int index )
193{
194        nlua_register( L, nlua_map_area_f, index );
195}
196
197void nlua_register_map_area( lua_State* L )
198{
199        luaopen_map_area(L);
200}
Note: See TracBrowser for help on using the repository browser.