arcaneFX.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "scene/sceneObject.h"
  26. #include "scene/sceneManager.h"
  27. #include "T3D/gameBase/gameConnection.h"
  28. #include "T3D/gameBase/gameProcess.h"
  29. #include "T3D/player.h"
  30. #include "math/mathUtils.h"
  31. #include "console/engineAPI.h"
  32. #include "console/script.h"
  33. #include "afx/afxChoreographer.h"
  34. #include "afx/afxSelectron.h"
  35. #include "afx/afxResidueMgr.h"
  36. #include "afx/ce/afxZodiacMgr.h"
  37. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  38. #define N_LIGHTING_MODELS 6
  39. //
  40. // "SG - Original Advanced (Lighting Pack)"
  41. // "SG - Original Stock (Lighting Pack)"
  42. // "SG - Inverse Square (Lighting Pack)"
  43. // "SG - Inverse Square Fast Falloff (Lighting Pack)"
  44. // "SG - Near Linear (Lighting Pack)"
  45. // "SG - Near Linear Fast Falloff (Lighting Pack)"
  46. static StringTableEntry lm_old_names[N_LIGHTING_MODELS];
  47. //
  48. // "Original Advanced"
  49. // "Original Stock"
  50. // "Inverse Square"
  51. // "Inverse Square Fast Falloff"
  52. // "Near Linear"
  53. // "Near Linear Fast Falloff"
  54. static StringTableEntry lm_new_names[N_LIGHTING_MODELS];
  55. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  56. class ClientZoneInEvent : public NetEvent
  57. {
  58. typedef NetEvent Parent;
  59. public:
  60. ClientZoneInEvent() { mGuaranteeType = Guaranteed; }
  61. ~ClientZoneInEvent() { }
  62. void pack(NetConnection*, BitStream*bstream) override { }
  63. void write(NetConnection*, BitStream *bstream) override { }
  64. void unpack(NetConnection* /*ps*/, BitStream *bstream) override { }
  65. void process(NetConnection* conn) override
  66. {
  67. GameConnection* game_conn = dynamic_cast<GameConnection*>(conn);
  68. if (game_conn && !game_conn->isZonedIn())
  69. {
  70. arcaneFX::syncToNewConnection(game_conn);
  71. }
  72. }
  73. DECLARE_CONOBJECT(ClientZoneInEvent);
  74. };
  75. IMPLEMENT_CO_SERVEREVENT_V1(ClientZoneInEvent);
  76. ConsoleDocClass( ClientZoneInEvent,
  77. "@brief Event posted when player is fully loaded into the game and ready for interaction.\n\n"
  78. "@internal");
  79. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  80. Vector<afxChoreographer*> arcaneFX::active_choreographers;
  81. Vector<afxChoreographer*> arcaneFX::client_choreographers;
  82. Vector<afxSelectronData*> arcaneFX::selectrons;
  83. Vector<SceneObject*> arcaneFX::scoped_objs;
  84. StringTableEntry arcaneFX::NULLSTRING = 0;
  85. U32 arcaneFX::sTargetSelectionMask = 0;
  86. U32 arcaneFX::sFreeTargetSelectionMask = 0;
  87. bool arcaneFX::sIsFreeTargeting = false;
  88. Point3F arcaneFX::sFreeTargetPos = Point3F(0.0f, 0.0f, 0.0f);
  89. bool arcaneFX::sFreeTargetPosValid = false;
  90. F32 arcaneFX::sTargetSelectionRange = 200.0f;
  91. U32 arcaneFX::sTargetSelectionTimeoutMS = 500;
  92. bool arcaneFX::sClickToTargetSelf = false;
  93. U32 arcaneFX::sMissileCollisionMask = 0;
  94. StringTableEntry arcaneFX::sParameterFieldPrefix = 0;
  95. F32 arcaneFX::sTerrainZodiacZBias = 0.00025f;
  96. F32 arcaneFX::sInteriorZodiacZBias = 0.0001f;
  97. F32 arcaneFX::sPolysoupZodiacZBias = 0.0001f;
  98. U32 arcaneFX::master_choreographer_id = 1;
  99. U16 arcaneFX::master_scope_id = 1;
  100. bool arcaneFX::is_shutdown = true;
  101. bool arcaneFX::sUsePlayerCentricListener = false;
  102. void arcaneFX::init()
  103. {
  104. NULLSTRING = StringTable->insert("");
  105. sParameterFieldPrefix = StringTable->insert("_");
  106. #if defined(TORQUE_OS_MAC)
  107. arcaneFX::sTerrainZodiacZBias = -0.00025f;
  108. arcaneFX::sInteriorZodiacZBias = -0.00025f;
  109. arcaneFX::sPolysoupZodiacZBias = -0.00025f;
  110. #endif
  111. Con::addVariable( "pref::AFX::targetSelectionMask", TypeS32, &sTargetSelectionMask);
  112. Con::addVariable( "pref::AFX::freeTargetSelectionMask", TypeS32, &sFreeTargetSelectionMask);
  113. Con::addVariable( "pref::AFX::targetSelectionRange", TypeF32, &sTargetSelectionRange);
  114. Con::addVariable( "pref::AFX::targetSelectionTimeoutMS", TypeS32, &sTargetSelectionTimeoutMS);
  115. Con::addVariable( "pref::AFX::missileCollisionMask", TypeS32, &sMissileCollisionMask);
  116. Con::addVariable( "pref::AFX::clickToTargetSelf", TypeBool, &sClickToTargetSelf);
  117. Con::addVariable( "Pref::Server::AFX::parameterFieldPrefix", TypeString, &sParameterFieldPrefix);
  118. Con::addVariable( "pref::AFX::terrainZodiacZBias", TypeF32, &sTerrainZodiacZBias);
  119. Con::addVariable( "pref::AFX::interiorZodiacZBias", TypeF32, &sInteriorZodiacZBias);
  120. Con::addVariable( "pref::AFX::polysoupZodiacZBias", TypeF32, &sPolysoupZodiacZBias);
  121. Con::setIntVariable( "$AFX::TARGETING_OFF", TARGETING_OFF);
  122. Con::setIntVariable( "$AFX::TARGETING_STANDARD", TARGETING_STANDARD);
  123. Con::setIntVariable( "$AFX::TARGETING_FREE", TARGETING_FREE);
  124. Con::setIntVariable( "$AFX::TARGET_CHECK_POLL", TARGET_CHECK_POLL);
  125. Con::setIntVariable( "$AFX::TARGET_CHECK_ON_MOUSE_MOVE", TARGET_CHECK_ON_MOUSE_MOVE);
  126. Con::setIntVariable( "$AFX::IMPACTED_SOMETHING", afxEffectDefs::IMPACTED_SOMETHING);
  127. Con::setIntVariable( "$AFX::IMPACTED_TARGET", afxEffectDefs::IMPACTED_TARGET);
  128. Con::setIntVariable( "$AFX::IMPACTED_PRIMARY", afxEffectDefs::IMPACTED_PRIMARY);
  129. Con::setIntVariable( "$AFX::IMPACT_IN_WATER", afxEffectDefs::IMPACT_IN_WATER);
  130. Con::setIntVariable( "$AFX::CASTER_IN_WATER", afxEffectDefs::CASTER_IN_WATER);
  131. Con::setIntVariable( "$AFX::SERVER_ONLY", afxEffectDefs::SERVER_ONLY);
  132. Con::setIntVariable( "$AFX::SCOPE_ALWAYS", afxEffectDefs::SCOPE_ALWAYS);
  133. Con::setIntVariable( "$AFX::GHOSTABLE", afxEffectDefs::GHOSTABLE);
  134. Con::setIntVariable( "$AFX::CLIENT_ONLY", afxEffectDefs::CLIENT_ONLY);
  135. Con::setIntVariable( "$AFX::SERVER_AND_CLIENT", afxEffectDefs::SERVER_AND_CLIENT);
  136. Con::setIntVariable( "$AFX::DELAY", afxEffectDefs::TIMING_DELAY);
  137. Con::setIntVariable( "$AFX::LIFETIME", afxEffectDefs::TIMING_LIFETIME);
  138. Con::setIntVariable( "$AFX::FADE_IN_TIME", afxEffectDefs::TIMING_FADE_IN);
  139. Con::setIntVariable( "$AFX::FADE_OUT_TIME", afxEffectDefs::TIMING_FADE_OUT);
  140. Con::setFloatVariable( "$AFX::INFINITE_TIME", -1.0f);
  141. Con::setIntVariable( "$AFX::INFINITE_REPEATS", -1);
  142. Con::setIntVariable( "$AFX::PLAYER_MOVE_TRIGGER_0", Player::PLAYER_MOVE_TRIGGER_0);
  143. Con::setIntVariable( "$AFX::PLAYER_MOVE_TRIGGER_1", Player::PLAYER_MOVE_TRIGGER_1);
  144. Con::setIntVariable( "$AFX::PLAYER_MOVE_TRIGGER_2", Player::PLAYER_MOVE_TRIGGER_2);
  145. Con::setIntVariable( "$AFX::PLAYER_MOVE_TRIGGER_3", Player::PLAYER_MOVE_TRIGGER_3);
  146. Con::setIntVariable( "$AFX::PLAYER_MOVE_TRIGGER_4", Player::PLAYER_MOVE_TRIGGER_4);
  147. Con::setIntVariable( "$AFX::PLAYER_MOVE_TRIGGER_5", Player::PLAYER_MOVE_TRIGGER_5);
  148. Con::setIntVariable( "$AFX::PLAYER_FIRE_S_TRIGGER", Player::PLAYER_FIRE_S_TRIGGER);
  149. Con::setIntVariable( "$AFX::PLAYER_FIRE_ALT_S_TRIGGER", Player::PLAYER_FIRE_ALT_S_TRIGGER);
  150. Con::setIntVariable( "$AFX::PLAYER_JUMP_S_TRIGGER", Player::PLAYER_JUMP_S_TRIGGER);
  151. Con::setIntVariable( "$AFX::PLAYER_LANDING_S_TRIGGER", Player::PLAYER_LANDING_S_TRIGGER);
  152. Con::setIntVariable( "$AFX::PLAYER_LF_FOOT_C_TRIGGER", Player::PLAYER_LF_FOOT_C_TRIGGER);
  153. Con::setIntVariable( "$AFX::PLAYER_RT_FOOT_C_TRIGGER", Player::PLAYER_RT_FOOT_C_TRIGGER);
  154. Con::setIntVariable( "$AFX::PLAYER_LANDING_C_TRIGGER", Player::PLAYER_LANDING_C_TRIGGER);
  155. Con::setIntVariable( "$AFX::PLAYER_IDLE_C_TRIGGER", Player::PLAYER_IDLE_C_TRIGGER);
  156. Con::setIntVariable( "$AFX::ILLUM_TERRAIN", 0);
  157. Con::setIntVariable( "$AFX::ILLUM_ATLAS", 0);
  158. Con::setIntVariable( "$AFX::ILLUM_DIF", 0);
  159. Con::setIntVariable( "$AFX::ILLUM_DTS", 0);
  160. Con::setIntVariable( "$AFX::ILLUM_ALL", 0);
  161. Con::setIntVariable("$TypeMasks::TerrainLikeObjectType", TerrainLikeObjectType);
  162. Con::setIntVariable("$TypeMasks::InteriorLikeObjectType", InteriorLikeObjectType);
  163. Con::setIntVariable("$TypeMasks::PolysoupObjectType", InteriorLikeObjectType); // deprecated
  164. Con::addVariable("$pref::Audio::usePlayerCentricListener", TypeBool, &sUsePlayerCentricListener);
  165. afxResidueMgr* residue_mgr = new afxResidueMgr;
  166. afxResidueMgr::setMaster(residue_mgr);
  167. master_scope_id = 1;
  168. master_choreographer_id = 1;
  169. is_shutdown = false;
  170. if (lm_old_names[0] == 0)
  171. {
  172. lm_old_names[0] = StringTable->insert("SG - Original Advanced (Lighting Pack)");
  173. lm_old_names[1] = StringTable->insert("SG - Original Stock (Lighting Pack)");
  174. lm_old_names[2] = StringTable->insert("SG - Inverse Square (Lighting Pack)");
  175. lm_old_names[3] = StringTable->insert("SG - Inverse Square Fast Falloff (Lighting Pack)");
  176. lm_old_names[4] = StringTable->insert("SG - Near Linear (Lighting Pack)");
  177. lm_old_names[5] = StringTable->insert("SG - Near Linear Fast Falloff (Lighting Pack)");
  178. //
  179. lm_new_names[0] = StringTable->insert("Original Advanced");
  180. lm_new_names[1] = StringTable->insert("Original Stock");
  181. lm_new_names[2] = StringTable->insert("Inverse Square");
  182. lm_new_names[3] = StringTable->insert("Inverse Square Fast Falloff");
  183. lm_new_names[4] = StringTable->insert("Near Linear");
  184. lm_new_names[5] = StringTable->insert("Near Linear Fast Falloff");
  185. }
  186. }
  187. void arcaneFX::shutdown()
  188. {
  189. is_shutdown = true;
  190. for (S32 i = 0; i < scoped_objs.size(); i++)
  191. if (scoped_objs[i])
  192. scoped_objs[i]->setScopeRegistered(false);
  193. scoped_objs.clear();
  194. for (S32 i = 0; i < client_choreographers.size(); i++)
  195. if (client_choreographers[i])
  196. client_choreographers[i]->clearChoreographerId();
  197. client_choreographers.clear();
  198. for (S32 i = 0; i < selectrons.size(); i++)
  199. if (selectrons[i])
  200. selectrons[i]->registered = false;
  201. selectrons.clear();
  202. afxResidueMgr* residue_mgr = afxResidueMgr::getMaster();
  203. delete residue_mgr;
  204. afxResidueMgr::setMaster(NULL);
  205. }
  206. MODULE_BEGIN( arcaneFX )
  207. MODULE_INIT
  208. {
  209. arcaneFX::init();
  210. }
  211. MODULE_SHUTDOWN
  212. {
  213. arcaneFX::shutdown();
  214. }
  215. MODULE_END;
  216. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  217. void arcaneFX::advanceTime(U32 delta)
  218. {
  219. GameConnection* conn = GameConnection::getConnectionToServer();
  220. if (conn && !conn->isZonedIn() && conn->getCameraObject() != 0)
  221. {
  222. conn->setZonedIn();
  223. conn->postNetEvent(new ClientZoneInEvent());
  224. }
  225. afxZodiacMgr::frameReset();
  226. afxResidueMgr::getMaster()->residueAdvanceTime();
  227. }
  228. //
  229. U32 arcaneFX::registerChoreographer(afxChoreographer* ch)
  230. {
  231. if (!ch)
  232. return 0;
  233. active_choreographers.push_back(ch);
  234. //Con::printf("registerChoreographer() -- size=%d %s", active_choreographers.size(),
  235. // (ch->isServerObject()) ? "server" : "client");
  236. return master_choreographer_id++;
  237. }
  238. void arcaneFX::unregisterChoreographer(afxChoreographer* ch)
  239. {
  240. if (!ch)
  241. return;
  242. for (U32 i = 0; i < active_choreographers.size(); i++)
  243. {
  244. if (ch == active_choreographers[i])
  245. {
  246. active_choreographers.erase_fast(i);
  247. //Con::printf("unregisterChoreographer() -- size=%d %s", active_choreographers.size(),
  248. // (ch->isServerObject()) ? "server" : "client");
  249. return;
  250. }
  251. }
  252. Con::errorf("arcaneFX::unregisterChoreographer() -- failed to find choreographer in list.");
  253. }
  254. void arcaneFX::registerClientChoreographer(afxChoreographer* ch)
  255. {
  256. if (!ch || ch->getChoreographerId() == 0)
  257. return;
  258. client_choreographers.push_back(ch);
  259. }
  260. void arcaneFX::unregisterClientChoreographer(afxChoreographer* ch)
  261. {
  262. if (!ch || ch->getChoreographerId() == 0)
  263. return;
  264. for (U32 i = 0; i < client_choreographers.size(); i++)
  265. {
  266. if (ch == client_choreographers[i])
  267. {
  268. client_choreographers.erase_fast(i);
  269. return;
  270. }
  271. }
  272. Con::errorf("arcaneFX::unregisterClientChoreographer() -- failed to find choreographer in list.");
  273. }
  274. afxChoreographer* arcaneFX::findClientChoreographer(U32 id)
  275. {
  276. for (U32 i = 0; i < client_choreographers.size(); i++)
  277. {
  278. if (id == client_choreographers[i]->getChoreographerId())
  279. return client_choreographers[i];
  280. }
  281. return 0;
  282. }
  283. //
  284. void arcaneFX::registerSelectronData(afxSelectronData* selectron)
  285. {
  286. if (!selectron)
  287. return;
  288. selectrons.push_back(selectron);
  289. }
  290. void arcaneFX::unregisterSelectronData(afxSelectronData* selectron)
  291. {
  292. if (!selectron)
  293. return;
  294. for (U32 i = 0; i < selectrons.size(); i++)
  295. {
  296. if (selectron == selectrons[i])
  297. {
  298. selectrons.erase_fast(i);
  299. return;
  300. }
  301. }
  302. Con::errorf("arcaneFX::unregisterSelectronData() -- failed to find selectron in list.");
  303. }
  304. afxSelectronData* arcaneFX::findSelectronData(U32 mask, U8 style)
  305. {
  306. for (U32 i = 0; i < selectrons.size(); i++)
  307. if (selectrons[i]->matches(mask, style))
  308. return selectrons[i];
  309. return 0;
  310. }
  311. U16 arcaneFX::generateScopeId()
  312. {
  313. U16 ret_id = master_scope_id++;
  314. if (master_scope_id >= BIT(GameBase::SCOPE_ID_BITS))
  315. master_scope_id = 1;
  316. return ret_id;
  317. }
  318. void arcaneFX::registerScopedObject(SceneObject* object)
  319. {
  320. scoped_objs.push_back(object);
  321. object->setScopeRegistered(true);
  322. for (S32 i = 0; i < client_choreographers.size(); i++)
  323. if (client_choreographers[i])
  324. client_choreographers[i]->restoreScopedObject(object);
  325. }
  326. SceneObject* arcaneFX::findScopedObject(U16 scope_id)
  327. {
  328. if (scoped_objs.size() > 0)
  329. {
  330. for (S32 i = scoped_objs.size()-1; i >= 0; i--)
  331. if (scoped_objs[i] && scoped_objs[i]->getScopeId() == scope_id)
  332. return scoped_objs[i];
  333. }
  334. return 0;
  335. }
  336. void arcaneFX::unregisterScopedObject(SceneObject* object)
  337. {
  338. if (scoped_objs.size() > 0)
  339. {
  340. for (S32 i = scoped_objs.size()-1; i >= 0; i--)
  341. if (scoped_objs[i] == object)
  342. {
  343. scoped_objs.erase_fast(i);
  344. if (object)
  345. object->setScopeRegistered(false);
  346. return;
  347. }
  348. }
  349. }
  350. void arcaneFX::syncToNewConnection(GameConnection* conn)
  351. {
  352. if (conn)
  353. conn->setZonedIn();
  354. for (U32 i = 0; i < active_choreographers.size(); i++)
  355. {
  356. if (active_choreographers[i])
  357. active_choreographers[i]->sync_with_clients();
  358. }
  359. }
  360. void arcaneFX::endMissionNotify()
  361. {
  362. for (S32 i = 0; i < scoped_objs.size(); i++)
  363. if (scoped_objs[i])
  364. scoped_objs[i]->setScopeRegistered(false);
  365. scoped_objs.clear();
  366. for (S32 i = 0; i < client_choreographers.size(); i++)
  367. if (client_choreographers[i])
  368. client_choreographers[i]->clearChoreographerId();
  369. client_choreographers.clear();
  370. for (S32 i = 0; i < selectrons.size(); i++)
  371. if (selectrons[i])
  372. selectrons[i]->registered = false;
  373. selectrons.clear();
  374. if (afxResidueMgr::getMaster())
  375. afxResidueMgr::getMaster()->cleanup();
  376. afxZodiacMgr::missionCleanup();
  377. }
  378. S32 arcaneFX::rolloverRayCast(Point3F start, Point3F end, U32 mask)
  379. {
  380. sIsFreeTargeting = false;
  381. #if !defined(AFX_CAP_ROLLOVER_RAYCASTS)
  382. return -1;
  383. #else
  384. GameConnection* conn = GameConnection::getConnectionToServer();
  385. SceneObject* ctrl_obj = NULL;
  386. if (!arcaneFX::sClickToTargetSelf && conn != NULL)
  387. ctrl_obj = conn->getControlObject();
  388. if (ctrl_obj)
  389. ctrl_obj->disableCollision();
  390. SceneObject* rollover_obj = (conn) ? conn->getRolloverObj() : 0;
  391. SceneObject* picked_obj = 0;
  392. RayInfo hit_info;
  393. if (gClientContainer.castRay(start, end, mask, &hit_info))
  394. picked_obj = dynamic_cast<SceneObject*>(hit_info.object);
  395. if (ctrl_obj)
  396. ctrl_obj->enableCollision();
  397. if (picked_obj != rollover_obj)
  398. {
  399. if (rollover_obj)
  400. rollover_obj->setSelectionFlags(rollover_obj->getSelectionFlags() & ~SceneObject::PRE_SELECTED);
  401. if (picked_obj)
  402. picked_obj->setSelectionFlags(picked_obj->getSelectionFlags() | SceneObject::PRE_SELECTED);
  403. rollover_obj = picked_obj;
  404. if (conn)
  405. conn->setRolloverObj(rollover_obj);
  406. }
  407. return (picked_obj) ? picked_obj->getId() : -1;
  408. #endif
  409. }
  410. bool arcaneFX::freeTargetingRayCast(Point3F start, Point3F end, U32 mask)
  411. {
  412. sIsFreeTargeting = true;
  413. RayInfo hit_info;
  414. if (!gClientContainer.castRay(start, end, mask, &hit_info))
  415. {
  416. sFreeTargetPosValid = false;
  417. return false;
  418. }
  419. sFreeTargetPosValid = true;
  420. sFreeTargetPos = hit_info.point;
  421. return true;
  422. }
  423. StringTableEntry arcaneFX::convertLightingModelName(StringTableEntry lm_name)
  424. {
  425. for (U32 i = 0; i < N_LIGHTING_MODELS; i++)
  426. {
  427. if (lm_name == lm_old_names[i])
  428. return lm_new_names[i];
  429. }
  430. return lm_name;
  431. }
  432. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  433. // Console Functions
  434. DefineEngineFunction(afxEndMissionNotify, void, (),,
  435. "...\n\n"
  436. "@ingroup AFX")
  437. {
  438. arcaneFX::endMissionNotify();
  439. }
  440. DefineEngineFunction(afxGetVersion, const char*, (),,
  441. "...\n\n"
  442. "@ingroup AFX")
  443. {
  444. return AFX_VERSION_STRING;
  445. }
  446. DefineEngineFunction(afxGetEngine, const char*, (),,
  447. "...\n\n"
  448. "@ingroup AFX")
  449. {
  450. return "T3D";
  451. }
  452. #if defined(AFX_CAP_ROLLOVER_RAYCASTS)
  453. DefineEngineFunction(rolloverRayCast, S32, (Point3F start, Point3F end, U32 mask),,
  454. "Performs a raycast from points start to end and returns the ID of nearest "
  455. "intersecting object with a type found in the specified mask. "
  456. "Returns -1 if no object is found.\n\n"
  457. "@ingroup AFX")
  458. {
  459. return arcaneFX::rolloverRayCast(start, end, mask);
  460. }
  461. #endif
  462. DefineEngineFunction(getRandomF, F32, (float a, float b), (F32_MAX, F32_MAX),
  463. "Get a random float number between a and b.\n\n"
  464. "@ingroup AFX")
  465. {
  466. if (b == F32_MAX)
  467. {
  468. if (a == F32_MAX)
  469. return gRandGen.randF();
  470. return gRandGen.randF(0.0f, a);
  471. }
  472. return (a + (b-a)*gRandGen.randF());
  473. }
  474. DefineEngineFunction(getRandomDir, Point3F, (Point3F axis, float thetaMin, float thetaMax, float phiMin, float phiMax),
  475. (Point3F(0.0f,0.0f,0.0f), 0.0f, 180.0f, 0.0f, 360.0f),
  476. "Get a random direction vector.\n\n"
  477. "@ingroup AFX")
  478. {
  479. return MathUtils::randomDir(axis, thetaMin, thetaMax, phiMin, phiMax);
  480. }
  481. DefineEngineFunction(MatrixInverseMulVector, Point3F, (MatrixF xfrm, Point3F vector),,
  482. "@brief Multiply the vector by the affine inverse of the transform.\n\n"
  483. "@ingroup AFX")
  484. {
  485. xfrm.affineInverse();
  486. Point3F result;
  487. xfrm.mulV(vector, &result);
  488. return result;
  489. }
  490. DefineEngineFunction(moveTransformAbs, MatrixF, (MatrixF xfrm, Point3F pos),,
  491. "@brief Move the transform to the new absolute position.\n\n"
  492. "@ingroup AFX")
  493. {
  494. xfrm.setPosition(pos);
  495. return xfrm;
  496. }
  497. DefineEngineFunction(moveTransformRel, MatrixF, (MatrixF xfrm, Point3F pos),,
  498. "@brief Move the transform to the new relative position.\n\n"
  499. "@ingroup AFX")
  500. {
  501. pos += xfrm.getPosition();
  502. xfrm.setPosition(pos);
  503. return xfrm;
  504. }
  505. DefineEngineFunction(getFreeTargetPosition, Point3F, (),,
  506. "@brief Returns the current location of the free target.\n\n"
  507. "@ingroup AFX")
  508. {
  509. if (!arcaneFX::sFreeTargetPosValid)
  510. return Point3F(0.0f, 0.0f, 0.0f);
  511. return arcaneFX::sFreeTargetPos;
  512. }
  513. DefineEngineMethod(SceneObject, getSpeed, F32, (),,
  514. "Returns the velocity of a scene-object.\n\n"
  515. "@ingroup AFX")
  516. {
  517. return object->getVelocity().len();
  518. }
  519. static S32 mark_modkey = -1;
  520. DefineEngineFunction(markDataBlocks, void, (),,
  521. "@brief Called before a series of datablocks are reloaded to "
  522. "help distinguish reloaded datablocks from already loaded ones.\n\n"
  523. "@ingroup AFX")
  524. {
  525. mark_modkey = SimDataBlock::getNextModifiedKey();
  526. }
  527. DefineEngineFunction(touchDataBlocks, void, (),,
  528. "@brief Called after a series of datablocks are reloaded to "
  529. "trigger some important actions on the reloaded datablocks.\n\n"
  530. "@ingroup AFX")
  531. {
  532. if (mark_modkey < 0)
  533. return;
  534. SimDataBlockGroup* g = Sim::getDataBlockGroup();
  535. U32 groupCount = g->size();
  536. for (S32 i = groupCount-1; i >= 0; i--)
  537. {
  538. SimDataBlock* simdb = (SimDataBlock*)(*g)[i];
  539. if (simdb->getModifiedKey() > mark_modkey)
  540. {
  541. simdb->unregisterObject();
  542. simdb->registerObject();
  543. }
  544. }
  545. mark_modkey = -1;
  546. }
  547. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  548. // Syntax Error Checking
  549. // (for checking eval() and compile() calls)
  550. DefineEngineFunction(wasSyntaxError, bool, (),,
  551. "@brief Returns true if script compiler had a syntax error. Useful "
  552. "for detecting syntax errors after reloading a script.\n\n"
  553. "@ingroup AFX")
  554. {
  555. return Con::getLastEvalResult().valid == false;
  556. }
  557. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  558. // Network Object Identification
  559. // These useful console methods come from the following code resource:
  560. //
  561. // How to Identify Objects from Client to Server or Server to Client by Nathan Davies
  562. // http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4852
  563. //
  564. DefineEngineMethod(NetConnection, GetGhostIndex, S32, (NetObject* obj),,
  565. "Returns the ghost-index for an object.\n\n"
  566. "@ingroup AFX")
  567. {
  568. if (obj)
  569. return object->getGhostIndex(obj);
  570. return 0;
  571. }
  572. DefineEngineMethod(NetConnection, ResolveGhost, S32, (int ghostIndex),,
  573. "Resolves a ghost-index into an object ID.\n\n"
  574. "@ingroup AFX")
  575. {
  576. if (ghostIndex != -1)
  577. {
  578. NetObject* pObject = NULL;
  579. if( object->isGhostingTo())
  580. pObject = object->resolveGhost(ghostIndex);
  581. else if( object->isGhostingFrom())
  582. pObject = object->resolveObjectFromGhostIndex(ghostIndex);
  583. if (pObject)
  584. return pObject->getId();
  585. }
  586. return 0;
  587. }
  588. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  589. //////////////////////////////////////////////////////////////////////////
  590. // TypeByteRange
  591. //////////////////////////////////////////////////////////////////////////
  592. IMPLEMENT_STRUCT( ByteRange, ByteRange,,
  593. "" )
  594. END_IMPLEMENT_STRUCT;
  595. ConsoleType( ByteRange, TypeByteRange, ByteRange, "")
  596. ConsoleType( ByteRange, TypeByteRange2, ByteRange, "")
  597. ConsoleGetType( TypeByteRange )
  598. {
  599. ByteRange* pt = (ByteRange *) dptr;
  600. char* returnBuffer = Con::getReturnBuffer(256);
  601. dSprintf(returnBuffer, 256, "%u %u", pt->low, pt->high);
  602. return returnBuffer;
  603. }
  604. ConsoleSetType( TypeByteRange )
  605. {
  606. if(argc == 1)
  607. {
  608. ByteRange* range = (ByteRange*) dptr;
  609. U32 lo, hi;
  610. S32 args = dSscanf(argv[0], "%u %u", &lo, &hi);
  611. range->low = (args > 0) ? lo : 0;
  612. range->high = (args > 1) ? hi : 255;
  613. }
  614. else
  615. Con::printf("ByteRange must be set as \"low\" or \"low high\"");
  616. }
  617. ConsoleGetType( TypeByteRange2 )
  618. {
  619. ByteRange* pt = (ByteRange *) dptr;
  620. char* returnBuffer = Con::getReturnBuffer(256);
  621. dSprintf(returnBuffer, 256, "%u %u", pt->low, pt->high);
  622. return returnBuffer;
  623. }
  624. ConsoleSetType( TypeByteRange2 )
  625. {
  626. if(argc == 1)
  627. {
  628. ByteRange* range = (ByteRange*) dptr;
  629. U32 lo, hi;
  630. S32 args = dSscanf(argv[0], "%u %u", &lo, &hi);
  631. range->low = (args > 0) ? lo : 0;
  632. range->high = (args > 1) ? hi : lo;
  633. }
  634. else
  635. Con::printf("ByteRange must be set as \"low\" or \"low high\"");
  636. }
  637. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  638. static void HSVtoRGB(F32 h, F32 s, F32 v, F32& r, F32& g, F32& b)
  639. {
  640. h = mFmod(h, 360.0f);
  641. if (v == 0.0f)
  642. r = g = b = 0.0f;
  643. else if (s == 0.0f)
  644. r = g = b = v;
  645. else
  646. {
  647. F32 hf = h/60.0f;
  648. S32 i = (S32) mFloor(hf);
  649. F32 f = hf - i;
  650. F32 pv = v*(1.0f - s);
  651. F32 qv = v*(1.0f - s*f);
  652. F32 tv = v*(1.0f - s*(1.0f - f));
  653. switch (i)
  654. {
  655. case 0:
  656. r = v; g = tv; b = pv;
  657. break;
  658. case 1:
  659. r = qv; g = v; b = pv;
  660. break;
  661. case 2:
  662. r = pv; g = v; b = tv;
  663. break;
  664. case 3:
  665. r = pv; g = qv; b = v;
  666. break;
  667. case 4:
  668. r = tv; g = pv; b = v;
  669. break;
  670. case 5:
  671. r = v; g = pv; b = qv;
  672. break;
  673. default:
  674. r = g = b = 0.0f;
  675. break;
  676. }
  677. }
  678. }
  679. DefineEngineFunction(getColorFromHSV, const char*, (float hue, float sat, float val, float alpha), (0.0, 0.0, 1.0, 1.0),
  680. "Coverts an HSV formatted color into an RBG color.\n\n"
  681. "@param hue The hue of the color (0-360).\n"
  682. "@param sat The saturation of the color (0-1).\n"
  683. "@param val The value of the color (0-1).\n"
  684. "@param alpha The alpha of the color (0-1).\n"
  685. "@ingroup AFX")
  686. {
  687. LinearColorF rgb;
  688. HSVtoRGB(hue, sat, val, rgb.red, rgb.green, rgb.blue);
  689. rgb.alpha = alpha;
  690. char* returnBuffer = Con::getReturnBuffer(256);
  691. dSprintf(returnBuffer, 256, "%g %g %g %g", rgb.red, rgb.green, rgb.blue, rgb.alpha);
  692. return returnBuffer;
  693. }
  694. DefineEngineFunction(ColorScale, const char*, ( LinearColorF color, float scalar ),,
  695. "Returns color scaled by scalar (color*scalar).\n\n"
  696. "@param color The color to be scaled.\n"
  697. "@param scalar The amount to scale the color.\n"
  698. "@ingroup AFX")
  699. {
  700. color *= scalar;
  701. char* returnBuffer = Con::getReturnBuffer(256);
  702. dSprintf(returnBuffer, 256, "%g %g %g %g", color.red, color.green, color.blue, color.alpha);
  703. return returnBuffer;
  704. }
  705. DefineEngineFunction(getMinF, F32, (float a, float b),,
  706. "Returns the lesser of the two arguments.\n\n"
  707. "@ingroup AFX")
  708. {
  709. return getMin(a, b);
  710. }
  711. DefineEngineFunction(getMaxF, F32, (float a, float b),,
  712. "Returns the greater of the two arguments.\n\n"
  713. "@ingroup AFX")
  714. {
  715. return getMax(a, b);
  716. }
  717. DefineEngineStringlyVariadicFunction(echoThru, const char*, 2, 0, "(string passthru, string text...)"
  718. "Like echo(), but first argument is returned.\n"
  719. "@ingroup AFX")
  720. {
  721. U32 len = 0;
  722. S32 i;
  723. for (i = 2; i < argc; i++)
  724. len += dStrlen(argv[i]);
  725. char *ret = Con::getReturnBuffer(len + 1);
  726. ret[0] = 0;
  727. for (i = 2; i < argc; i++)
  728. dStrcat(ret, argv[i], len + 1);
  729. Con::printf("%s -- [%s]", ret, argv[1].getString());
  730. ret[0] = 0;
  731. return argv[1];
  732. }
  733. DefineEngineStringlyVariadicFunction(warnThru, const char*, 2, 0, "(string passthru, string text...)"
  734. "Like warn(), but first argument is returned.\n"
  735. "@ingroup AFX")
  736. {
  737. U32 len = 0;
  738. S32 i;
  739. for(i = 2; i < argc; i++)
  740. len += dStrlen(argv[i]);
  741. char *ret = Con::getReturnBuffer(len + 1);
  742. ret[0] = 0;
  743. for(i = 2; i < argc; i++)
  744. dStrcat(ret, argv[i], len + 1);
  745. Con::warnf("%s -- [%s]", ret, argv[1].getString());
  746. ret[0] = 0;
  747. return argv[1];
  748. }
  749. DefineEngineStringlyVariadicFunction(errorThru, const char*, 2, 0, "(string passthru, string text...)"
  750. "Like error(), but first argument is returned.\n"
  751. "@ingroup AFX")
  752. {
  753. U32 len = 0;
  754. S32 i;
  755. for(i = 2; i < argc; i++)
  756. len += dStrlen(argv[i]);
  757. char *ret = Con::getReturnBuffer(len + 1);
  758. ret[0] = 0;
  759. for(i = 2; i < argc; i++)
  760. dStrcat(ret, argv[i], len + 1);
  761. Con::errorf("%s -- [%s]", ret, argv[1].getString());
  762. ret[0] = 0;
  763. return argv[1];
  764. }
  765. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//