missionArea.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. //------------------------------------------------------------------------------
  48. MissionArea::MissionArea()
  49. {
  50. mArea.set(Point2I(768, 768), Point2I(512, 512));
  51. mNetFlags.set(Ghostable | ScopeAlways);
  52. mFlightCeiling = 2000;
  53. mFlightCeilingRange = 50;
  54. }
  55. //------------------------------------------------------------------------------
  56. void MissionArea::setArea(const RectI & area)
  57. {
  58. // set it
  59. mArea = MissionArea::smMissionArea = area;
  60. // pass along..
  61. if(isServerObject())
  62. mNetFlags.set(UpdateMask);
  63. }
  64. //------------------------------------------------------------------------------
  65. MissionArea * MissionArea::getServerObject()
  66. {
  67. SimSet * scopeAlwaysSet = Sim::getGhostAlwaysSet();
  68. for(SimSet::iterator itr = scopeAlwaysSet->begin(); itr != scopeAlwaysSet->end(); itr++)
  69. {
  70. MissionArea * ma = dynamic_cast<MissionArea*>(*itr);
  71. if(ma)
  72. {
  73. AssertFatal(ma->isServerObject(), "MissionArea::getServerObject: found client object in ghost always set!");
  74. return(ma);
  75. }
  76. }
  77. return(0);
  78. }
  79. //------------------------------------------------------------------------------
  80. bool MissionArea::onAdd()
  81. {
  82. if(isServerObject() && MissionArea::getServerObject())
  83. {
  84. Con::errorf(ConsoleLogEntry::General, "MissionArea::onAdd - MissionArea already instantiated!");
  85. return(false);
  86. }
  87. if(!Parent::onAdd())
  88. return(false);
  89. setArea(mArea);
  90. return(true);
  91. }
  92. //------------------------------------------------------------------------------
  93. void MissionArea::inspectPostApply()
  94. {
  95. // Set Parent.
  96. Parent::inspectPostApply();
  97. setMaskBits(UpdateMask);
  98. }
  99. //------------------------------------------------------------------------------
  100. void MissionArea::initPersistFields()
  101. {
  102. addGroup("Dimensions");
  103. addField("area", TypeRectI, Offset(mArea, MissionArea), "Four corners (X1, X2, Y1, Y2) that makes up the level's boundaries");
  104. addField("flightCeiling", TypeF32, Offset(mFlightCeiling, MissionArea), "Represents the top of the mission area, used by FlyingVehicle. ");
  105. addField("flightCeilingRange", TypeF32, Offset(mFlightCeilingRange, MissionArea), "Distance from ceiling before FlyingVehicle thrust is cut off. ");
  106. endGroup("Dimensions");
  107. Parent::initPersistFields();
  108. }
  109. //------------------------------------------------------------------------------
  110. void MissionArea::unpackUpdate(NetConnection *, BitStream * stream)
  111. {
  112. // ghost (initial) and regular updates share flag..
  113. if(stream->readFlag())
  114. {
  115. mathRead(*stream, &mArea);
  116. stream->read(&mFlightCeiling);
  117. stream->read(&mFlightCeilingRange);
  118. }
  119. }
  120. U32 MissionArea::packUpdate(NetConnection *, U32 mask, BitStream * stream)
  121. {
  122. if(stream->writeFlag(mask & UpdateMask))
  123. {
  124. mathWrite(*stream, mArea);
  125. stream->write(mFlightCeiling);
  126. stream->write(mFlightCeilingRange);
  127. }
  128. return(0);
  129. }
  130. //-----------------------------------------------------------------------------
  131. DefineEngineFunction(getMissionAreaServerObject, MissionArea*, (),,
  132. "Get the MissionArea object, if any.\n\n"
  133. "@ingroup enviroMisc\n\n")
  134. {
  135. return MissionArea::getServerObject();
  136. }
  137. DefineEngineMethod( MissionArea, getArea, const char *, (),,
  138. "Returns 4 fields: starting x, starting y, extents x, extents y.\n")
  139. {
  140. char* returnBuffer = Con::getReturnBuffer(48);
  141. RectI area = object->getArea();
  142. dSprintf(returnBuffer, sizeof(returnBuffer), "%d %d %d %d", area.point.x, area.point.y, area.extent.x, area.extent.y);
  143. return(returnBuffer);
  144. }
  145. DefineEngineMethod( MissionArea, setArea, void, (S32 x, S32 y, S32 width, S32 height),,
  146. "@brief - Defines the size of the MissionArea\n\n"
  147. "param x Starting X coordinate position for MissionArea\n"
  148. "param y Starting Y coordinate position for MissionArea\n"
  149. "param width New width of the MissionArea\n"
  150. "param height New height of the MissionArea\n"
  151. "@note Only the server object may be set.\n"
  152. )
  153. {
  154. if(object->isClientObject())
  155. {
  156. Con::errorf(ConsoleLogEntry::General, "MissionArea::cSetArea - cannot alter client object!");
  157. return;
  158. }
  159. RectI rect;
  160. rect.point.x = x;
  161. rect.point.y = y;
  162. rect.extent.x = width;
  163. rect.extent.y = height;
  164. object->setArea(rect);
  165. }
  166. DefineEngineMethod( MissionArea, postApply, void, (),,
  167. "Intended as a helper to developers and editor scripts.\n"
  168. "Force trigger an inspectPostApply. This will transmit "
  169. "material and other fields ( not including nodes ) to client objects."
  170. )
  171. {
  172. object->inspectPostApply();
  173. }