2
0

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