missionArea.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/missionArea.h"
  23. #include "console/consoleTypes.h"
  24. #include "core/stream/bitStream.h"
  25. #include "math/mathIO.h"
  26. #include "console/engineAPI.h"
  27. IMPLEMENT_CO_NETOBJECT_V1(MissionArea);
  28. ConsoleDocClass( MissionArea,
  29. "@brief Level object which defines the boundaries of the level.\n\n"
  30. "This is a simple box with starting points, width, depth, and height. It does not have "
  31. "any default functionality. Instead, when objects hit the boundaries certain "
  32. "script callbacks will be made allowing you to control the reaction.\n\n"
  33. "@tsexample\n"
  34. "new MissionArea(GlobalMissionArea)\n"
  35. "{\n"
  36. " Area = \"-152 -352 1008 864\";\n"
  37. " flightCeiling = \"300\";\n"
  38. " flightCeilingRange = \"20\";\n"
  39. " canSaveDynamicFields = \"1\";\n"
  40. " enabled = \"1\";\n"
  41. " TypeBool locked = \"false\";\n"
  42. "};\n"
  43. "@endtsexample\n\n"
  44. "@ingroup enviroMisc\n"
  45. );
  46. RectI MissionArea::smMissionArea(Point2I(768, 768), Point2I(512, 512));
  47. MissionArea * MissionArea::smServerObject = NULL;
  48. //------------------------------------------------------------------------------
  49. MissionArea::MissionArea()
  50. {
  51. mArea.set(Point2I(768, 768), Point2I(512, 512));
  52. mNetFlags.set(Ghostable | ScopeAlways);
  53. mFlightCeiling = 2000;
  54. mFlightCeilingRange = 50;
  55. }
  56. //------------------------------------------------------------------------------
  57. void MissionArea::setArea(const RectI & area)
  58. {
  59. // set it
  60. mArea = MissionArea::smMissionArea = area;
  61. // pass along..
  62. if(isServerObject())
  63. mNetFlags.set(UpdateMask);
  64. }
  65. //------------------------------------------------------------------------------
  66. MissionArea * MissionArea::getServerObject()
  67. {
  68. return smServerObject;
  69. }
  70. //------------------------------------------------------------------------------
  71. bool MissionArea::onAdd()
  72. {
  73. if(isServerObject())
  74. {
  75. if(MissionArea::getServerObject())
  76. {
  77. Con::errorf(ConsoleLogEntry::General, "MissionArea::onAdd - MissionArea already instantiated!");
  78. return(false);
  79. }
  80. else
  81. {
  82. smServerObject = this;
  83. }
  84. }
  85. if(!Parent::onAdd())
  86. return(false);
  87. setArea(mArea);
  88. return(true);
  89. }
  90. void MissionArea::onRemove()
  91. {
  92. if (smServerObject == this)
  93. smServerObject = NULL;
  94. Parent::onRemove();
  95. }
  96. //------------------------------------------------------------------------------
  97. void MissionArea::inspectPostApply()
  98. {
  99. // Set Parent.
  100. Parent::inspectPostApply();
  101. setMaskBits(UpdateMask);
  102. }
  103. //------------------------------------------------------------------------------
  104. void MissionArea::initPersistFields()
  105. {
  106. docsURL;
  107. addGroup("Dimensions");
  108. addField("area", TypeRectI, Offset(mArea, MissionArea), "Four corners (X1, X2, Y1, Y2) that makes up the level's boundaries");
  109. addField("flightCeiling", TypeF32, Offset(mFlightCeiling, MissionArea), "Represents the top of the mission area, used by FlyingVehicle. ");
  110. addField("flightCeilingRange", TypeF32, Offset(mFlightCeilingRange, MissionArea), "Distance from ceiling before FlyingVehicle thrust is cut off. ");
  111. endGroup("Dimensions");
  112. Parent::initPersistFields();
  113. }
  114. //------------------------------------------------------------------------------
  115. void MissionArea::unpackUpdate(NetConnection *, BitStream * stream)
  116. {
  117. // ghost (initial) and regular updates share flag..
  118. if(stream->readFlag())
  119. {
  120. mathRead(*stream, &mArea);
  121. stream->read(&mFlightCeiling);
  122. stream->read(&mFlightCeilingRange);
  123. }
  124. }
  125. U32 MissionArea::packUpdate(NetConnection *, U32 mask, BitStream * stream)
  126. {
  127. if(stream->writeFlag(mask & UpdateMask))
  128. {
  129. mathWrite(*stream, mArea);
  130. stream->write(mFlightCeiling);
  131. stream->write(mFlightCeilingRange);
  132. }
  133. return(0);
  134. }
  135. //-----------------------------------------------------------------------------
  136. DefineEngineFunction(getMissionAreaServerObject, MissionArea*, (),,
  137. "Get the MissionArea object, if any.\n\n"
  138. "@ingroup enviroMisc\n\n")
  139. {
  140. return MissionArea::getServerObject();
  141. }
  142. DefineEngineMethod( MissionArea, getArea, const char *, (),,
  143. "Returns 4 fields: starting x, starting y, extents x, extents y.\n")
  144. {
  145. static const U32 bufSize = 48;
  146. char* returnBuffer = Con::getReturnBuffer(bufSize);
  147. RectI area = object->getArea();
  148. dSprintf(returnBuffer, bufSize, "%d %d %d %d", area.point.x, area.point.y, area.extent.x, area.extent.y);
  149. return(returnBuffer);
  150. }
  151. DefineEngineMethod( MissionArea, setArea, void, (S32 x, S32 y, S32 width, S32 height),,
  152. "@brief - Defines the size of the MissionArea\n\n"
  153. "param x Starting X coordinate position for MissionArea\n"
  154. "param y Starting Y coordinate position for MissionArea\n"
  155. "param width New width of the MissionArea\n"
  156. "param height New height of the MissionArea\n"
  157. "@note Only the server object may be set.\n"
  158. )
  159. {
  160. if(object->isClientObject())
  161. {
  162. Con::errorf(ConsoleLogEntry::General, "MissionArea::cSetArea - cannot alter client object!");
  163. return;
  164. }
  165. RectI rect;
  166. rect.point.x = x;
  167. rect.point.y = y;
  168. rect.extent.x = width;
  169. rect.extent.y = height;
  170. object->setArea(rect);
  171. }
  172. DefineEngineMethod( MissionArea, postApply, void, (),,
  173. "Intended as a helper to developers and editor scripts.\n"
  174. "Force trigger an inspectPostApply. This will transmit "
  175. "material and other fields ( not including nodes ) to client objects."
  176. )
  177. {
  178. object->inspectPostApply();
  179. }