Area.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /******************************************************************************/
  2. namespace Game{
  3. /******************************************************************************/
  4. enum AREA_STATE
  5. {
  6. AREA_UNLOADED, // the area is completely unloaded, it doesn't contain any data at all
  7. AREA_CACHE , // the area has data loaded from the disk to the temporary memory, but not used in the game, objects are not listed in object containers, actors are not created
  8. AREA_INACTIVE, // the area has data loaded and used in the game, objects are listed in object containers however they're not updated or drawn, all actors are frozen (set to kinematic and cannot move)
  9. AREA_ACTIVE , // the area has data loaded and used in the game, objects are listed in object containers and are updated and drawn, actors can be dynamic
  10. AREA_LOAD, // this state can be specified only for 'WorldManager.areaSetState', it converts state from AREA_UNLOADED to AREA_CACHE (if the state was not AREA_UNLOADED then it remains unmodified)
  11. };
  12. /******************************************************************************/
  13. struct AreaPath2D
  14. {
  15. Int resolution()C {return _map.w();}
  16. Bool walkable(Int x, Int y )C; // get if pixel is walkable, x=0..world.settings().path2DRes()-1, y=0..world.settings().path2DRes()-1
  17. AreaPath2D& walkable(Int x, Int y, Bool walkable) ; // set if pixel is walkable, x=0..world.settings().path2DRes()-1, y=0..world.settings().path2DRes()-1, after making all desired changes to paths you must call World.pathBuild once, any changes made aren't stored in the SaveGame (custom modifications need to be applied every time an Area is reloaded)
  18. #if !EE_PRIVATE
  19. private:
  20. #endif
  21. struct Neighbor
  22. {
  23. Byte a, b, cost;
  24. #if EE_PRIVATE
  25. void set(Byte a, Byte b, Byte cost) {T.a=a; T.b=b; T.cost=cost;}
  26. #endif
  27. };
  28. Bool _changed;
  29. Byte _groups;
  30. Image _map;
  31. Memc<Neighbor> _neighbor;
  32. #if EE_PRIVATE
  33. #define MAX_PATH_RES 64
  34. void zero ();
  35. void del ();
  36. void create (Int size);
  37. void createFromQuarter(AreaPath2D &src, Bool right, Bool forward , WorldSettings &settings);
  38. void createFromQuad (AreaPath2D *lb, AreaPath2D *rb, AreaPath2D *lf, AreaPath2D *rf, WorldSettings &settings);
  39. Bool fullyWalkable ();
  40. void group ();
  41. void resize (Int size);
  42. // io
  43. Bool save(File &f)C;
  44. Bool load(File &f) ;
  45. #endif
  46. AreaPath2D();
  47. };
  48. struct Path2DWalker
  49. {
  50. Vec target;
  51. Memc<Vec2> points;
  52. Path2DWalker& clear();
  53. Bool find (C Vec &start, C Vec &target, C Vec *alternate_start=null);
  54. };
  55. /******************************************************************************/
  56. struct Area // World Area
  57. {
  58. struct Data // Data of an World Area
  59. {
  60. STRUCT(AreaObj , Object)
  61. //{
  62. UID id;
  63. Bool save(File &f, CChar *path=null)C; // 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  64. Bool load(File &f, CChar *path=null) ; // 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  65. #if EE_PRIVATE
  66. Bool loadOld(File &f);
  67. #endif
  68. };
  69. struct TerrainObj
  70. {
  71. ObjectPtr obj;
  72. Matrix matrix;
  73. Actor actor;
  74. Int mesh_variation;
  75. };
  76. struct GrassObj
  77. {
  78. struct Instance
  79. {
  80. Matrix matrix;
  81. Actor actor ;
  82. };
  83. Bool shrink;
  84. MeshPtr mesh;
  85. Int mesh_variation;
  86. PhysBodyPtr phys;
  87. Memc<Instance> instances;
  88. GrassObj() {shrink=false;}
  89. };
  90. MeshGroup mesh ; // area mesh , this can contain terrain and all terrain objects which are too big and needed to be split into smaller parts
  91. PhysBody phys ; // area physical body, this can contain terrain and all terrain objects which are too big and needed to be split into smaller parts
  92. Actor actor ; // area actor, created from the 'phys' member
  93. Memc<AreaObj > objs ; // list of all area objects which are not dynamic (their access is not OBJ_ACCESS_CUSTOM), this does not include terrain objects which are too big and needed to be split into smaller parts (those objects are stored in the 'mesh' and 'phys' members)
  94. Memc<TerrainObj > terrain_objs ; // list of all area objects of OBJ_ACCESS_TERRAIN access, these objects are always taken from the 'objs' container ('terrain_objs' container is not stored in the area data file, instead it is always copied from 'objs' member at area load)
  95. Box terrain_objs_box; // this covers all 'terrain_objs'
  96. Memc<GrassObj > foliage_objs ; // list of all area objects of OBJ_ACCESS_GRASS access, these objects are always taken from the 'objs' container ('foliage_objs' container is not stored in the area data file, instead it is always copied from 'objs' member at area load)
  97. Box foliage_objs_box; // this covers all 'foliage_objs'
  98. Image height , // area height map, each pixel value stores information about the height of the terrain
  99. material_map ; // area material map, each pixel value stores information about index of the most significant material in the 'materials' container, must be in sync with 'materials' member !!
  100. Mems<MaterialPtr> materials ; // list of materials used by the terrain, must be in sync with 'material_map' member !!
  101. Memc<WaterMesh > waters ; // area water meshes
  102. Memc<Decal > decals ; // area decals
  103. Memc<MeshOverlay> mesh_overlays ; // area mesh overlays
  104. Area & area () {return *_area ;} // get area containing this data
  105. PathMesh* pathMesh() {return _path_mesh;} // get area path mesh
  106. // custom interface
  107. // operations
  108. virtual void customSetShader() {} // reset shader of custom meshes stored inside area data
  109. // draw
  110. virtual UInt customDrawPrepare () {return 0;} // draw custom area data
  111. virtual void customDrawShadow () { } // draw custom area data
  112. virtual void customDrawBlend () { } // draw custom area data in RM_BLEND mode
  113. virtual void customDrawPalette () { } // draw custom area data in RM_PALETTE mode
  114. virtual void customDrawPalette1() { } // draw custom area data in RM_PALETTE1 mode
  115. // switching states
  116. virtual void customLoad (File &f, CChar *chunk_name, UInt chunk_ver) {} // this is called when area is switching state from (AREA_UNLOADED ) to (AREA_CACHE ), in this method you should load custom data using chunks from "game world", this method will be called by the engine on secondary thread, since physics simulation may be running on the main thread - you may not create/modify any physical objects in this method (actors, joints, ragdolls, ..) the only exception are physical bodies which can be created/loaded
  117. virtual void customUnloadToCache( ) {} // this is called when area is switching state from (AREA_ACTIVE or AREA_INACTIVE) to (AREA_CACHE ), in this method you must delete all actors
  118. virtual void customLoadFromCache( ) {} // this is called when area is switching state from (AREA_CACHE ) to (AREA_ACTIVE or AREA_INACTIVE), in this method you must create all actors
  119. virtual void customDeactivate ( ) {} // this is called when area is switching state from (AREA_ACTIVE ) to (AREA_INACTIVE ), in this method you must turn dynamic actors into kinematic actors
  120. virtual void customActivate ( ) {} // this is called when area is switching state from (AREA_INACTIVE ) to (AREA_ACTIVE ), in this method you may turn kinematic actors into dynamic actors
  121. // saving area changes
  122. virtual Bool customSaveWant( ) {return false;} // you can override this method and return true, this will cause saving the full Area into the game world folder
  123. virtual Bool customSave (ChunkWriter &cw) {return true ;} // you can override this method, save custom chunks of data and return true, this method will be called only if 'customSaveWant' returned true
  124. Data(Area &area);
  125. virtual ~Data(); // set virtual destructor so 'Delete' can be used together with extended classes
  126. AreaPath2D* path2D() {return _path2D;} // get area path 2D
  127. #if !EE_PRIVATE
  128. private:
  129. #endif
  130. Area *_area;
  131. Int _path_node_offset;
  132. PathMesh *_path_mesh;
  133. AreaPath2D *_path2D;
  134. #if EE_PRIVATE
  135. Bool save(File &f);
  136. Bool load(File &f);
  137. #endif
  138. NO_COPY_CONSTRUCTOR(Data);
  139. };
  140. Bool visited ( )C {return _visited ;} // if area has been visited before
  141. C VecI2& xz ( )C {return _xz ;} // get area coordinates
  142. AREA_STATE state ( )C {return _state ;} // get current area state
  143. Int objs ( )C {return _objs.elms();} // get number of objects in the area (this includes only dynamic objects of OBJ_ACCESS_CUSTOM access, all other objects are stored in 'Game.Area.Data')
  144. Obj& obj ( Int i )C {return *_objs[i] ;} // get i-th object in the area (this includes only dynamic objects of OBJ_ACCESS_CUSTOM access, all other objects are stored in 'Game.Area.Data')
  145. Flt hmHeight (C Vec2 &xz, Bool smooth=true)C; // get heightmap height at 'xz' world position, 0 on fail, this method is fast because it uses lookup table ('Game.Area.Data.height' Image), 'smooth'=if calculate smooth value using linear interpolation
  146. C MaterialPtr& hmMaterial(C Vec2 &xz )C; // get heightmap material at 'xz' world position, null on fail, this method is fast because it uses lookup table ('Game.Area.Data.material_map' Image)
  147. Data* data() {return _data;} T1(TYPE) TYPE* data() {return CAST(TYPE, _data );} // get 'Area.Data' (don't create data if it's null)
  148. Data* getData(); T1(TYPE) TYPE* getData() {return CAST(TYPE, getData());} // get 'Area.Data' ( create data if it's null, null can still be returned if this area has AREA_UNLOADED state)
  149. WorldManager* world()C {return _world;} // get World containing this area
  150. #if EE_PRIVATE
  151. Bool loaded ()C {return _state>AREA_CACHE;}
  152. void setShader();
  153. void drawObjAndTerrain();
  154. void drawTerrainShadow();
  155. void drawObjShadow ();
  156. void drawOverlay ();
  157. Bool saveObj (Obj &obj);
  158. Bool saveObjs();
  159. Bool save (File &f, C VecI2 &xy);
  160. Bool load (File &f);
  161. #endif
  162. virtual ~Area(); // force virtual class to enable memory container auto-casting when extending this class with another virtual class
  163. explicit Area(C VecI2 &xy, Ptr grid_user);
  164. #if !EE_PRIVATE
  165. private:
  166. #endif
  167. Bool _visited, _temp;
  168. VecI2 _xz ;
  169. AREA_STATE _state ;
  170. Memc<Obj*> _objs ;
  171. File _saved_obj;
  172. Data *_data ;
  173. WorldManager *_world ;
  174. NO_COPY_CONSTRUCTOR(Area);
  175. };
  176. /******************************************************************************/
  177. #if EE_PRIVATE
  178. enum PATH_NODE_TYPE
  179. {
  180. PN_NODE ,
  181. PN_AREA ,
  182. PN_CONVEX,
  183. };
  184. struct PathNode
  185. {
  186. Int node_index , // used in path finding
  187. added_in_step; // used in path finding
  188. UInt iteration , // used in path finding
  189. length ; // used in path finding
  190. PathNode *src ; // used in path finding
  191. Byte type , // PATH_NODE_TYPE
  192. temp , // temporary index used in path finding algorithm
  193. nghb_num; // number of neighbors
  194. Int nghb_ofs, // neighbors offset in global neighbor storage
  195. parent , // parent index in global PathNode storage (-1=none)
  196. sibling ; // next parents child (looped)
  197. struct Node // PN_NODE
  198. {
  199. Int child;
  200. };
  201. struct Area // PN_AREA
  202. {
  203. VecI2 xy;
  204. Int index;
  205. };
  206. struct Convex // PN_CONVEX
  207. {
  208. VecI2 xy;
  209. Int index;
  210. };
  211. union
  212. {
  213. Node node ; // PN_NODE
  214. Area area ; // PN_AREA
  215. Convex convex; // PN_CONVEX
  216. };
  217. PathNode() {iteration=0;}
  218. };
  219. struct PathNodeNeighbor
  220. {
  221. UInt index;
  222. Byte cost;
  223. void set(UInt index, Byte cost) {T.index=index; T.cost=cost;}
  224. };
  225. #endif
  226. /******************************************************************************/
  227. } // namespace
  228. /******************************************************************************/