missionArea.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "console/typeValidators.h"
  25. #include "core/stream/bitStream.h"
  26. #include "math/mathIO.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CO_NETOBJECT_V1(MissionArea);
  29. ConsoleDocClass( MissionArea,
  30. "@brief Level object which defines the boundaries of the level.\n\n"
  31. "This is a simple box with starting points, width, depth, and height. It does not have "
  32. "any default functionality. Instead, when objects hit the boundaries certain "
  33. "script callbacks will be made allowing you to control the reaction.\n\n"
  34. "@tsexample\n"
  35. "new MissionArea(GlobalMissionArea)\n"
  36. "{\n"
  37. " Area = \"-152 -352 1008 864\";\n"
  38. " flightCeiling = \"300\";\n"
  39. " flightCeilingRange = \"20\";\n"
  40. " canSaveDynamicFields = \"1\";\n"
  41. " enabled = \"1\";\n"
  42. " TypeBool locked = \"false\";\n"
  43. "};\n"
  44. "@endtsexample\n\n"
  45. "@ingroup enviroMisc\n"
  46. );
  47. RectI MissionArea::smMissionArea(Point2I(-100, -100), Point2I(100, 100));
  48. MissionArea * MissionArea::smServerObject = NULL;
  49. //------------------------------------------------------------------------------
  50. MissionArea::MissionArea()
  51. {
  52. mArea.set(Point2I(-100, -100), Point2I(100, 100));
  53. mNetFlags.set(Ghostable | ScopeAlways);
  54. mFlightCeiling = 2000;
  55. mFlightCeilingRange = 50;
  56. }
  57. //------------------------------------------------------------------------------
  58. void MissionArea::setArea(const RectI & area)
  59. {
  60. // set it
  61. mArea = MissionArea::smMissionArea = area;
  62. // pass along..
  63. if(isServerObject())
  64. mNetFlags.set(UpdateMask);
  65. }
  66. //------------------------------------------------------------------------------
  67. MissionArea * MissionArea::getServerObject()
  68. {
  69. return smServerObject;
  70. }
  71. //------------------------------------------------------------------------------
  72. bool MissionArea::onAdd()
  73. {
  74. if(isServerObject())
  75. {
  76. if(MissionArea::getServerObject())
  77. {
  78. Con::errorf(ConsoleLogEntry::General, "MissionArea::onAdd - MissionArea already instantiated!");
  79. return(false);
  80. }
  81. else
  82. {
  83. smServerObject = this;
  84. }
  85. }
  86. if(!Parent::onAdd())
  87. return(false);
  88. setArea(mArea);
  89. return(true);
  90. }
  91. void MissionArea::onRemove()
  92. {
  93. if (smServerObject == this)
  94. smServerObject = NULL;
  95. Parent::onRemove();
  96. }
  97. //------------------------------------------------------------------------------
  98. void MissionArea::inspectPostApply()
  99. {
  100. // Set Parent.
  101. Parent::inspectPostApply();
  102. setMaskBits(UpdateMask);
  103. }
  104. //------------------------------------------------------------------------------
  105. void MissionArea::initPersistFields()
  106. {
  107. docsURL;
  108. addGroup("Dimensions");
  109. addField("area", TypeRectI, Offset(mArea, MissionArea), "Four corners (X1, X2, Y1, Y2) that makes up the level's boundaries");
  110. addFieldV("flightCeiling", TypeRangedF32, Offset(mFlightCeiling, MissionArea),&CommonValidators::F32Range, "Represents the top of the mission area, used by FlyingVehicle. ");
  111. addFieldV("flightCeilingRange", TypeRangedF32, Offset(mFlightCeilingRange, MissionArea), &CommonValidators::F32Range, "Distance from ceiling before FlyingVehicle thrust is cut off. ");
  112. endGroup("Dimensions");
  113. Parent::initPersistFields();
  114. }
  115. //------------------------------------------------------------------------------
  116. void MissionArea::unpackUpdate(NetConnection *, BitStream * stream)
  117. {
  118. // ghost (initial) and regular updates share flag..
  119. if(stream->readFlag())
  120. {
  121. mathRead(*stream, &mArea);
  122. stream->read(&mFlightCeiling);
  123. stream->read(&mFlightCeilingRange);
  124. }
  125. }
  126. U32 MissionArea::packUpdate(NetConnection *, U32 mask, BitStream * stream)
  127. {
  128. if(stream->writeFlag(mask & UpdateMask))
  129. {
  130. mathWrite(*stream, mArea);
  131. stream->write(mFlightCeiling);
  132. stream->write(mFlightCeilingRange);
  133. }
  134. return(0);
  135. }
  136. //-----------------------------------------------------------------------------
  137. DefineEngineFunction(getMissionAreaServerObject, MissionArea*, (),,
  138. "Get the MissionArea object, if any.\n\n"
  139. "@ingroup enviroMisc\n\n")
  140. {
  141. return MissionArea::getServerObject();
  142. }
  143. DefineEngineMethod( MissionArea, getArea, const char *, (),,
  144. "Returns 4 fields: starting x, starting y, extents x, extents y.\n")
  145. {
  146. static const U32 bufSize = 48;
  147. char* returnBuffer = Con::getReturnBuffer(bufSize);
  148. RectI area = object->getArea();
  149. dSprintf(returnBuffer, bufSize, "%d %d %d %d", area.point.x, area.point.y, area.extent.x, area.extent.y);
  150. return(returnBuffer);
  151. }
  152. DefineEngineMethod( MissionArea, setArea, void, (S32 x, S32 y, S32 width, S32 height),,
  153. "@brief - Defines the size of the MissionArea\n\n"
  154. "param x Starting X coordinate position for MissionArea\n"
  155. "param y Starting Y coordinate position for MissionArea\n"
  156. "param width New width of the MissionArea\n"
  157. "param height New height of the MissionArea\n"
  158. "@note Only the server object may be set.\n"
  159. )
  160. {
  161. if(object->isClientObject())
  162. {
  163. Con::errorf(ConsoleLogEntry::General, "MissionArea::cSetArea - cannot alter client object!");
  164. return;
  165. }
  166. RectI rect;
  167. rect.point.x = x;
  168. rect.point.y = y;
  169. rect.extent.x = width;
  170. rect.extent.y = height;
  171. object->setArea(rect);
  172. }
  173. DefineEngineMethod( MissionArea, postApply, void, (),,
  174. "Intended as a helper to developers and editor scripts.\n"
  175. "Force trigger an inspectPostApply. This will transmit "
  176. "material and other fields ( not including nodes ) to client objects."
  177. )
  178. {
  179. object->inspectPostApply();
  180. }