TriggerRegions.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // region.cpp
  19. // Project: Nostril (aka Postal)
  20. //
  21. // This module facilitates editor usage and creation of region attributes.
  22. // The regions all need to coexist as separate attribute planes, so the
  23. // additional compression of an RMultiGridIndirect was used. For this reason,
  24. // region attribute access is somewhat slower than normal attribute access,
  25. // and should be used sparingly.
  26. //
  27. // History:
  28. // 05/08/97 JRD Started.
  29. //
  30. // 05/14/97 BRH Fixed the reversed Unique ID's in SpewTriggers().
  31. //
  32. ////////////////////////////////////////////////////////////////////////////////
  33. #include "RSPiX.h"
  34. #ifdef PATHS_IN_INCLUDES
  35. #include "ORANGE/MultiGrid/MultiGridIndirect.h"
  36. #else
  37. #include "multigridindirect.h"
  38. #endif
  39. #include "TriggerRegions.h"
  40. #include "TriggerRgn.h"
  41. #include "realm.h"
  42. #include "thing.h"
  43. // Here is a generic set of tools for creating a region map.
  44. // They are a lower level and don't iteratively optimize:
  45. ////////////////////////////////////////////////////////////////////////////////
  46. //
  47. // CreateRegionMap - Begin your session
  48. //
  49. // UINPUT: Size of map in pixels, maximum number of overlapping planes,
  50. // and TILE SIZE for palettes. (Should be as large as possible
  51. // without causing greater than the maximum number of tiles to overlap.]
  52. // sMaxPlanes must NOT exceed MGI_MAX_PLANES!
  53. //
  54. // OUPUT: An allocated RMultiGridIndirect containing an uncompressed
  55. // RMultiGrid within it, ready for writing to.
  56. //
  57. // RETURN VALUE: NULL on failure, else the new grid. Can fail due to memory
  58. // alloc errors.
  59. //
  60. ////////////////////////////////////////////////////////////////////////////////
  61. RMultiGridIndirect* CreateRegionMap(short sWidth,short sHeight,short sMaxPlanes,
  62. short sTileW,short sTileH)
  63. {
  64. ASSERT( (sWidth > 0) && (sHeight > 0) && (sMaxPlanes > 0)
  65. && (sTileW > 0) && (sTileH > 0) );
  66. ASSERT(sMaxPlanes <= MGI_MAX_PLANES);
  67. RMultiGridIndirect* pMGI = new RMultiGridIndirect;
  68. if (!pMGI)
  69. {
  70. TRACE("CreateRegionMap: alloc error!\n");
  71. return NULL;
  72. }
  73. if (pMGI->Alloc(sWidth,sHeight,sMaxPlanes,sTileW,sTileH) != SUCCESS)
  74. {
  75. TRACE("CreateRegionMap: alloc error!\n");
  76. return NULL;
  77. }
  78. // Create and install the MultiGrid.
  79. RMultiGrid* pmg = new RMultiGrid;
  80. if (!pmg)
  81. {
  82. TRACE("CreateRegionMap: alloc error!\n");
  83. return NULL;
  84. }
  85. if (pmg->Alloc(sWidth,sHeight) != SUCCESS)
  86. {
  87. TRACE("CreateRegionMap: alloc error!\n");
  88. delete pmg;
  89. return NULL;
  90. }
  91. pMGI->InstallMultiGrid(pmg);
  92. return pMGI;
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////
  95. //
  96. // StrafeAddRegion - add all members of an array of 256 FSPR1's to the map.
  97. //
  98. // It is assumed that there are 255 trigger regions (1 - 255), whose address
  99. // is also their "palette value"
  100. //
  101. // RETURN VALUE - SUCCESS or FAILURE. Will return failure if there were too
  102. // many overlapping regions in one tile. (It still will create the map, but
  103. // on that one tile there will be some region drop out.) You can fix this by
  104. // increasing the number of max planes or decreasing the palette tile size.
  105. //
  106. // NOTE: When finished, you should compress the MultiGrid as you see fit
  107. // before saving.
  108. //
  109. ////////////////////////////////////////////////////////////////////////////////
  110. short StrafeAddRegion(RMultiGridIndirect* pMGI,TriggerRgn regions[256])
  111. {
  112. ASSERT(pMGI);
  113. short sRet = SUCCESS;
  114. for (short i=1; i < 256;i++)
  115. {
  116. if (regions[i].pimRgn)
  117. {
  118. if (pMGI->AddFSPR1(regions[i].pimRgn,regions[i].sX,regions[i].sY,
  119. UCHAR(i),TriggerRgn::MaxRgnWidth,TriggerRgn::MaxRgnHeight)
  120. != SUCCESS)
  121. {
  122. TRACE("StrafeAddRegion:: Problem installing region %hd\n",i);
  123. sRet = FAILURE;
  124. }
  125. }
  126. }
  127. return sRet;
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////
  130. //
  131. // CompressMap - currently just compresses to your specifications, but may
  132. // be expanded to do some iterative testing for optimizing compression:
  133. //
  134. // NOTE:
  135. //
  136. ////////////////////////////////////////////////////////////////////////////////
  137. short CompressMap(RMultiGridIndirect* pMGI,short sTileW,short sTileH)
  138. {
  139. ASSERT(pMGI);
  140. ASSERT(pMGI->m_pmg);
  141. // use compression results to optimize
  142. pMGI->m_pmg->Compress(sTileW,sTileH);
  143. return SUCCESS;
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////
  146. //
  147. // SpewTriggers - checks for triggers underneath a main dude and
  148. // alerts all relevant pylons to his presence
  149. ////////////////////////////////////////////////////////////////////////////////
  150. //
  151. void SpewTriggers(CRealm* pRealm, USHORT usDudeUID,short sX,short sZ)
  152. {
  153. UCHAR aucHitList[MGI_MAX_PLANES];
  154. if (pRealm->m_pTriggerMap == NULL) return; // No triggers
  155. short sMax = pRealm->m_pTriggerMap->m_sMaxPlanes;
  156. // GET THE ATTRIBUTE MAP FOR THE TRIGGERS:
  157. pRealm->m_pTriggerMap->GetVal(aucHitList,sX,sZ);
  158. UCHAR* pHit = aucHitList;
  159. GameMessage msg;
  160. msg.msg_DudeTrigger.eType = typeDudeTrigger;
  161. msg.msg_DudeTrigger.sPriority = 0;
  162. msg.msg_DudeTrigger.u16DudeUniqueID = usDudeUID;
  163. msg.msg_DudeTrigger.dX = double(sX);
  164. msg.msg_DudeTrigger.dZ = double(sZ);
  165. short sNum = sMax;
  166. while (*pHit && sNum) // got a hit:
  167. {
  168. // send a trigger message out:
  169. CThing* pThing = NULL;
  170. if (pRealm->m_idbank.GetThingByID(&pThing, pRealm->m_asPylonUIDs[*pHit]) == SUCCESS)
  171. {
  172. if (pThing) pThing->SendThingMessage(&msg, 0, pThing); // post the trigger!
  173. }
  174. pHit++;
  175. sMax--;
  176. }
  177. }
  178. ////////////////////////////////////////////////////////////////////////////////
  179. // EOF
  180. ////////////////////////////////////////////////////////////////////////////////