/src/moaicore/MOAIGrid.cpp

https://github.com/neojjang/moai-beta · C++ · 479 lines · 231 code · 105 blank · 143 comment · 7 complexity · b6c96a1ea1edb078e6af1f2a6a196bbd MD5 · raw file

  1. // Copyright (c) 2010-2011 Zipline Games, Inc. All Rights Reserved.
  2. // http://getmoai.com
  3. #include "pch.h"
  4. #include <moaicore/MOAIGrid.h>
  5. #include <moaicore/MOAILogMessages.h>
  6. //================================================================//
  7. // local
  8. //================================================================//
  9. //----------------------------------------------------------------//
  10. /** @name clearTileFlags
  11. @text Clears bits specified in mask.
  12. @in MOAIGrid self
  13. @in number xTile
  14. @in number yTile
  15. @in number mask
  16. @out nil
  17. */
  18. int MOAIGrid::_clearTileFlags ( lua_State* L ) {
  19. MOAI_LUA_SETUP ( MOAIGrid, "UNNN" )
  20. int xTile = state.GetValue < int >( 2, 1 ) - 1;
  21. int yTile = state.GetValue < int >( 3, 1 ) - 1;
  22. u32 mask = state.GetValue < u32 >( 4, 0 );
  23. u32 tile = self->GetTile ( xTile, yTile );
  24. tile = tile & ~mask;
  25. self->SetTile ( xTile, yTile, tile );
  26. return 0;
  27. }
  28. //----------------------------------------------------------------//
  29. /** @name getSize
  30. @text Returns the dimensions of the grid (in tiles).
  31. @in MOAIGrid self
  32. @out number width
  33. @out number height
  34. */
  35. int MOAIGrid::_getSize ( lua_State* L ) {
  36. MOAI_LUA_SETUP ( MOAIGrid, "U" )
  37. state.Push ( self->mWidth );
  38. state.Push ( self->mHeight );
  39. return 2;
  40. }
  41. //----------------------------------------------------------------//
  42. /** @name getTile
  43. @text Returns the value of a given tile.
  44. @in MOAIGrid self
  45. @in number xTile
  46. @in number yTile
  47. @out number tile
  48. */
  49. int MOAIGrid::_getTile ( lua_State* L ) {
  50. MOAI_LUA_SETUP ( MOAIGrid, "UNN" )
  51. int xTile = state.GetValue < int >( 2, 1 ) - 1;
  52. int yTile = state.GetValue < int >( 3, 1 ) - 1;
  53. u32 tile = self->GetTile ( xTile, yTile );
  54. state.Push ( tile );
  55. return 1;
  56. }
  57. //----------------------------------------------------------------//
  58. /** @name getTileFlags
  59. @text Returns the masked value of a given tile.
  60. @in MOAIGrid self
  61. @in number xTile
  62. @in number yTile
  63. @in number mask
  64. @out number tile
  65. */
  66. int MOAIGrid::_getTileFlags ( lua_State* L ) {
  67. MOAI_LUA_SETUP ( MOAIGrid, "UNNN" )
  68. int xTile = state.GetValue < int >( 2, 1 ) - 1;
  69. int yTile = state.GetValue < int >( 3, 1 ) - 1;
  70. u32 mask = state.GetValue < u32 >( 4, 0 );
  71. u32 tile = self->GetTile ( xTile, yTile );
  72. tile = tile & mask;
  73. lua_pushnumber ( state, tile );
  74. return 1;
  75. }
  76. //----------------------------------------------------------------//
  77. /** @name getTileLoc
  78. @text Returns the grid space coordinate of the tile. The optional 'position'
  79. flag determines the location of the coordinate within the tile.
  80. @in MOAIGrid self
  81. @in number xTile
  82. @in number yTile
  83. @opt number position See MOAIGrid for list of positions. Default it MOAIGrid.TILE_CENTER.
  84. @out number x
  85. @out number y
  86. */
  87. int MOAIGrid::_getTileLoc ( lua_State* L ) {
  88. MOAI_LUA_SETUP ( MOAIGrid, "UNN" )
  89. MOAICellCoord coord;
  90. coord.mX = state.GetValue < int >( 2, 1 ) - 1;
  91. coord.mY = state.GetValue < int >( 3, 1 ) - 1;
  92. u32 position = state.GetValue < u32 >( 4, MOAIGridSpace::TILE_CENTER );
  93. USVec2D loc = self->GetTilePoint ( coord, position );
  94. state.Push ( loc.mX );
  95. state.Push ( loc.mY );
  96. return 2;
  97. }
  98. //----------------------------------------------------------------//
  99. /** @name locToCoord
  100. @text Transforms a coordinate in grid space into a tile index.
  101. @in MOAIGrid self
  102. @in number x
  103. @in number y
  104. @out number xTile
  105. @out number yTile
  106. */
  107. int MOAIGrid::_locToCoord ( lua_State* L ) {
  108. MOAI_LUA_SETUP ( MOAIGrid, "UNN" )
  109. USVec2D loc;
  110. loc.mX = state.GetValue < float >( 2, 0 );
  111. loc.mY = state.GetValue < float >( 3, 0 );
  112. MOAICellCoord coord;
  113. coord = self->GetCellCoord ( loc );
  114. state.Push ( coord.mX + 1 );
  115. state.Push ( coord.mY + 1);
  116. return 2;
  117. }
  118. //----------------------------------------------------------------//
  119. /** @name setRow
  120. @text Initializes a grid row given a variable argument list of values.
  121. @in MOAIGrid self
  122. @in number row
  123. @in ...
  124. @out nil
  125. */
  126. int MOAIGrid::_setRow ( lua_State* L ) {
  127. MOAI_LUA_SETUP ( MOAIGrid, "UN" )
  128. u32 row = state.GetValue < u32 >( 2, 1 ) - 1;
  129. u32 total = lua_gettop ( state ) - 2;
  130. for ( u32 i = 0; i < total; ++i ) {
  131. u32 tile = state.GetValue < u32 >( 3 + i, 0 );
  132. self->SetTile ( i, row, tile );
  133. }
  134. return 0;
  135. }
  136. //----------------------------------------------------------------//
  137. /** @name setSize
  138. @text Initializes dimensions of grid and reserves storage for tiles.
  139. @in MOAIGrid self
  140. @in number width
  141. @in number height
  142. @in number cellWidth Default value is 1.
  143. @in number cellHeight Default value is 1.
  144. @opt number xOff X offset of the tile from the cell.
  145. @opt number yOff Y offset of the tile from the cell.
  146. @opt number tileWidth Default value is cellWidth.
  147. @opt number tileHeight Default value is cellHeight.
  148. @out nil
  149. */
  150. int MOAIGrid::_setSize ( lua_State* L ) {
  151. MOAI_LUA_SETUP ( MOAIGrid, "UNN" )
  152. u32 width = state.GetValue < u32 >( 2, 0 );
  153. u32 height = state.GetValue < u32 >( 3, 0 );
  154. float cellWidth = state.GetValue < float >( 4, 1.0f );
  155. float cellHeight = state.GetValue < float >( 5, 1.0f );
  156. float xOff = state.GetValue < float >( 6, 0.0f );
  157. float yOff = state.GetValue < float >( 7, 0.0f );
  158. float tileWidth = state.GetValue < float >( 8, cellWidth );
  159. float tileHeight = state.GetValue < float >( 9, cellHeight );
  160. self->SetWidth ( width );
  161. self->SetHeight ( height );
  162. self->SetCellWidth ( cellWidth );
  163. self->SetCellHeight ( cellHeight );
  164. self->SetXOff ( xOff );
  165. self->SetYOff ( yOff );
  166. self->SetTileWidth ( tileWidth );
  167. self->SetTileHeight ( tileHeight );
  168. self->mTiles.Init ( self->GetTotalCells ());
  169. self->mTiles.Fill ( 0 );
  170. return 0;
  171. }
  172. //----------------------------------------------------------------//
  173. /** @name setTile
  174. @text Sets the value of a given tile
  175. @in MOAIGrid self
  176. @in number xTile
  177. @in number yTile
  178. @in number value
  179. @out nil
  180. */
  181. int MOAIGrid::_setTile ( lua_State* L ) {
  182. MOAI_LUA_SETUP ( MOAIGrid, "UNNN" )
  183. int xTile = state.GetValue < int >( 2, 1 ) - 1;
  184. int yTile = state.GetValue < int >( 3, 1 ) - 1;
  185. u32 tile = state.GetValue < u32 >( 4, 0 );
  186. self->SetTile ( xTile, yTile, tile );
  187. return 0;
  188. }
  189. //----------------------------------------------------------------//
  190. /** @name setTileFlags
  191. @text Sets a tile's flags given a mask.
  192. @in MOAIGrid self
  193. @in number xTile
  194. @in number yTile
  195. @in number mask
  196. @out nil
  197. */
  198. int MOAIGrid::_setTileFlags ( lua_State* L ) {
  199. MOAI_LUA_SETUP ( MOAIGrid, "UNNN" )
  200. int xTile = state.GetValue < int >( 2, 1 ) - 1;
  201. int yTile = state.GetValue < int >( 3, 1 ) - 1;
  202. u32 mask = state.GetValue < u32 >( 4, 0 );
  203. u32 tile = self->GetTile ( xTile, yTile );
  204. tile = tile | mask;
  205. self->SetTile ( xTile, yTile, tile );
  206. return 0;
  207. }
  208. //----------------------------------------------------------------//
  209. /** @name toggleTileFlags
  210. @text Toggles a tile's flags given a mask.
  211. @in MOAIGrid self
  212. @in number xTile
  213. @in number yTile
  214. @in number mask
  215. @out nil
  216. */
  217. int MOAIGrid::_toggleTileFlags ( lua_State* L ) {
  218. MOAI_LUA_SETUP ( MOAIGrid, "UNNN" )
  219. int xTile = state.GetValue < int >( 2, 1 ) - 1;
  220. int yTile = state.GetValue < int >( 3, 1 ) - 1;
  221. u32 mask = state.GetValue < u32 >( 4, 0 );
  222. u32 tile = self->GetTile ( xTile, yTile );
  223. tile = tile ^ mask;
  224. self->SetTile ( xTile, yTile, tile );
  225. return 0;
  226. }
  227. //----------------------------------------------------------------//
  228. /** @name wrapCoord
  229. @text Wraps a tile index to the range of the grid.
  230. @in MOAIGrid self
  231. @in number xTile
  232. @in number yTile
  233. @out number xTile
  234. @out number yTile
  235. */
  236. int MOAIGrid::_wrapCoord ( lua_State* L ) {
  237. MOAI_LUA_SETUP ( MOAIGrid, "UNN" )
  238. MOAICellCoord coord;
  239. coord.mX = state.GetValue < int >( 2, 1 ) - 1;
  240. coord.mY = state.GetValue < int >( 3, 1 ) - 1;
  241. self->WrapCellCoord ( coord );
  242. state.Push ( coord.mX + 1 );
  243. state.Push ( coord.mY + 1 );
  244. return 2;
  245. }
  246. //================================================================//
  247. // MOAIGrid
  248. //================================================================//
  249. //----------------------------------------------------------------//
  250. u32 MOAIGrid::GetTile ( int xTile, int yTile ) {
  251. MOAICellCoord coord ( xTile, yTile );
  252. u32 addr = this->GetCellAddr ( coord );
  253. if ( addr < this->mTiles.Size ()) {
  254. return this->mTiles [ addr ];
  255. }
  256. return 0;
  257. }
  258. //----------------------------------------------------------------//
  259. MOAIGrid::MOAIGrid () {
  260. RTTI_BEGIN
  261. RTTI_EXTEND ( USLuaObject )
  262. RTTI_END
  263. }
  264. //----------------------------------------------------------------//
  265. MOAIGrid::~MOAIGrid () {
  266. }
  267. //----------------------------------------------------------------//
  268. void MOAIGrid::RegisterLuaClass ( USLuaState& state ) {
  269. state.SetField ( -1, "TILE_X_FLIP", ( u32 )MOAITileFlags::XFLIP );
  270. state.SetField ( -1, "TILE_Y_FLIP", ( u32 )MOAITileFlags::YFLIP );
  271. state.SetField ( -1, "TILE_XY_FLIP", ( u32 )MOAITileFlags::FLIP_MASK );
  272. state.SetField ( -1, "TILE_HIDE", ( u32 )MOAITileFlags::HIDDEN );
  273. state.SetField ( -1, "TILE_LEFT_TOP", ( u32 )MOAIGridSpace::TILE_LEFT_TOP );
  274. state.SetField ( -1, "TILE_RIGHT_TOP", ( u32 )MOAIGridSpace::TILE_RIGHT_TOP );
  275. state.SetField ( -1, "TILE_LEFT_BOTTOM", ( u32 )MOAIGridSpace::TILE_LEFT_BOTTOM );
  276. state.SetField ( -1, "TILE_RIGHT_BOTTOM", ( u32 )MOAIGridSpace::TILE_RIGHT_BOTTOM );
  277. state.SetField ( -1, "TILE_LEFT_CENTER", ( u32 )MOAIGridSpace::TILE_LEFT_CENTER );
  278. state.SetField ( -1, "TILE_RIGHT_CENTER", ( u32 )MOAIGridSpace::TILE_RIGHT_CENTER );
  279. state.SetField ( -1, "TILE_TOP_CENTER", ( u32 )MOAIGridSpace::TILE_TOP_CENTER );
  280. state.SetField ( -1, "TILE_BOTTOM_CENTER", ( u32 )MOAIGridSpace::TILE_BOTTOM_CENTER );
  281. state.SetField ( -1, "TILE_CENTER", ( u32 )MOAIGridSpace::TILE_CENTER );
  282. }
  283. //----------------------------------------------------------------//
  284. void MOAIGrid::RegisterLuaFuncs ( USLuaState& state ) {
  285. luaL_Reg regTable [] = {
  286. { "clearTileFlags", _clearTileFlags },
  287. { "getSize", _getSize },
  288. { "getTile", _getTile },
  289. { "getTileFlags", _getTileFlags },
  290. { "getTileLoc", _getTileLoc },
  291. { "locToCoord", _locToCoord },
  292. { "setRow", _setRow },
  293. { "setSize", _setSize },
  294. { "setTile", _setTile },
  295. { "setTileFlags", _setTileFlags },
  296. { "toggleTileFlags", _toggleTileFlags },
  297. { "wrapCoord", _wrapCoord },
  298. { NULL, NULL }
  299. };
  300. luaL_register ( state, 0, regTable );
  301. }
  302. //----------------------------------------------------------------//
  303. void MOAIGrid::SerializeIn ( USLuaState& state, USLuaSerializer& serializer ) {
  304. UNUSED ( serializer );
  305. this->MOAIGridSpace::SerializeIn ( state );
  306. this->mTiles.Init ( this->MOAIGridSpace::GetTotalCells ());
  307. state.GetField ( -1, "mData" );
  308. if ( state.IsType ( -1, LUA_TSTRING )) {
  309. void* tiles = this->mTiles;
  310. size_t tilesSize = this->mTiles.Size () * sizeof ( u32 );
  311. STLString base64 = lua_tostring ( state, -1 );
  312. base64.base_64_decode ( tiles, tilesSize );
  313. USLeanArray < u8 > unzip;
  314. USZip::Inflate ( this->mTiles, this->mTiles.Size () * sizeof ( u32 ), unzip );
  315. tiles = unzip.Data ();
  316. if ( unzip.Size () < tilesSize ) {
  317. tilesSize = unzip.Size ();
  318. }
  319. memcpy ( this->mTiles, tiles, tilesSize );
  320. }
  321. lua_pop ( state, 1 );
  322. }
  323. //----------------------------------------------------------------//
  324. void MOAIGrid::SerializeOut ( USLuaState& state, USLuaSerializer& serializer ) {
  325. UNUSED ( serializer );
  326. this->MOAIGridSpace::SerializeOut ( state );
  327. USLeanArray < u8 > zip;
  328. USZip::Deflate ( this->mTiles, this->mTiles.Size () * sizeof ( u32 ), zip );
  329. STLString base64;
  330. base64.base_64_encode ( zip.Data (), zip.Size ());
  331. lua_pushstring ( state, base64.str ());
  332. lua_setfield ( state, -2, "mData" );
  333. }
  334. //----------------------------------------------------------------//
  335. void MOAIGrid::SetTile ( u32 addr, u32 tile ) {
  336. u32 size = this->mTiles.Size ();
  337. if ( size ) {
  338. addr = addr % this->mTiles.Size ();
  339. this->mTiles [ addr ] = tile;
  340. }
  341. }
  342. //----------------------------------------------------------------//
  343. void MOAIGrid::SetTile ( int xTile, int yTile, u32 tile ) {
  344. u32 size = this->mTiles.Size ();
  345. if ( size ) {
  346. MOAICellCoord coord ( xTile, yTile );
  347. u32 addr = this->GetCellAddr ( coord );
  348. if ( addr < this->mTiles.Size ()) {
  349. this->mTiles [ addr ] = tile;
  350. }
  351. }
  352. }
  353. //----------------------------------------------------------------//
  354. u32 MOAIGrid::Size () {
  355. return this->mTiles.Size ();
  356. }
  357. //----------------------------------------------------------------//
  358. STLString MOAIGrid::ToString () {
  359. STLString repr;
  360. //PrettyPrint ( repr, "bounds", this->USTilemap::GetBounds ());
  361. return repr;
  362. }