2
0

afxConstraint.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  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 "arcaneFX.h"
  25. #include "T3D/aiPlayer.h"
  26. #include "T3D/tsStatic.h"
  27. #include "sim/netConnection.h"
  28. #include "ts/tsShapeInstance.h"
  29. #include "afxConstraint.h"
  30. #include "afxChoreographer.h"
  31. #include "afxEffectWrapper.h"
  32. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  33. // afxConstraintDef
  34. // static
  35. StringTableEntry afxConstraintDef::SCENE_CONS_KEY;
  36. StringTableEntry afxConstraintDef::EFFECT_CONS_KEY;
  37. StringTableEntry afxConstraintDef::GHOST_CONS_KEY;
  38. afxConstraintDef::afxConstraintDef()
  39. {
  40. if (SCENE_CONS_KEY == 0)
  41. {
  42. SCENE_CONS_KEY = StringTable->insert("#scene");
  43. EFFECT_CONS_KEY = StringTable->insert("#effect");
  44. GHOST_CONS_KEY = StringTable->insert("#ghost");
  45. }
  46. reset();
  47. }
  48. bool afxConstraintDef::isDefined()
  49. {
  50. return (mDef_type != CONS_UNDEFINED);
  51. }
  52. bool afxConstraintDef::isArbitraryObject()
  53. {
  54. return ((mCons_src_name != ST_NULLSTRING) && (mDef_type == CONS_SCENE));
  55. }
  56. void afxConstraintDef::reset()
  57. {
  58. mCons_src_name = ST_NULLSTRING;
  59. mCons_node_name = ST_NULLSTRING;
  60. mDef_type = CONS_UNDEFINED;
  61. mHistory_time = 0;
  62. mSample_rate = 30;
  63. mRuns_on_server = false;
  64. mRuns_on_client = false;
  65. mPos_at_box_center = false;
  66. mTreat_as_camera = false;
  67. }
  68. bool afxConstraintDef::parseSpec(const char* spec, bool runs_on_server,
  69. bool runs_on_client)
  70. {
  71. reset();
  72. if (spec == 0 || spec[0] == '\0')
  73. return false;
  74. mHistory_time = 0.0f;
  75. mSample_rate = 30;
  76. mRuns_on_server = runs_on_server;
  77. mRuns_on_client = runs_on_client;
  78. // spec should be in one of these forms:
  79. // CONSTRAINT_NAME (only)
  80. // CONSTRAINT_NAME.NODE (shapeBase objects only)
  81. // CONSTRAINT_NAME.#center
  82. // object.OBJECT_NAME
  83. // object.OBJECT_NAME.NODE (shapeBase objects only)
  84. // object.OBJECT_NAME.#center
  85. // effect.EFFECT_NAME
  86. // effect.EFFECT_NAME.NODE
  87. // effect.EFFECT_NAME.#center
  88. // #ghost.EFFECT_NAME
  89. // #ghost.EFFECT_NAME.NODE
  90. // #ghost.EFFECT_NAME.#center
  91. //
  92. // create scratch buffer by duplicating spec.
  93. char special = '\b';
  94. char* buffer = dStrdup(spec);
  95. // substitute a dots not inside parens with special character
  96. S32 n_nested = 0;
  97. for (char* b = buffer; (*b) != '\0'; b++)
  98. {
  99. if ((*b) == '(')
  100. n_nested++;
  101. else if ((*b) == ')')
  102. n_nested--;
  103. else if ((*b) == '.' && n_nested == 0)
  104. (*b) = special;
  105. }
  106. // divide name into '.' separated tokens (up to 8)
  107. char* words[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  108. char* dot = buffer;
  109. int wdx = 0;
  110. while (wdx < 8)
  111. {
  112. words[wdx] = dot;
  113. dot = dStrchr(words[wdx++], special);
  114. if (!dot)
  115. break;
  116. *(dot++) = '\0';
  117. if ((*dot) == '\0')
  118. break;
  119. }
  120. int n_words = wdx;
  121. // at this point the spec has been split into words.
  122. // n_words indicates how many words we have.
  123. // no words found (must have been all whitespace)
  124. if (n_words < 1)
  125. {
  126. dFree(buffer);
  127. return false;
  128. }
  129. char* hist_spec = 0;
  130. char* words2[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  131. int n_words2 = 0;
  132. // move words to words2 while extracting #center and #history
  133. for (S32 i = 0; i < n_words; i++)
  134. {
  135. if (String::compare(words[i], "#center") == 0)
  136. mPos_at_box_center = true;
  137. else if (dStrncmp(words[i], "#history(", 9) == 0)
  138. hist_spec = words[i];
  139. else
  140. words2[n_words2++] = words[i];
  141. }
  142. // words2[] now contains just the constraint part
  143. // no words found (must have been all #center and #history)
  144. if (n_words2 < 1)
  145. {
  146. dFree(buffer);
  147. return false;
  148. }
  149. if (hist_spec)
  150. {
  151. char* open_paren = dStrchr(hist_spec, '(');
  152. if (open_paren)
  153. {
  154. hist_spec = open_paren+1;
  155. if ((*hist_spec) != '\0')
  156. {
  157. char* close_paren = dStrchr(hist_spec, ')');
  158. if (close_paren)
  159. (*close_paren) = '\0';
  160. char* slash = dStrchr(hist_spec, '/');
  161. if (slash)
  162. (*slash) = ' ';
  163. F32 hist_age = 0.0;
  164. U32 hist_rate = 30;
  165. S32 args = dSscanf(hist_spec,"%g %d", &hist_age, &hist_rate);
  166. if (args > 0)
  167. mHistory_time = hist_age;
  168. if (args > 1)
  169. mSample_rate = hist_rate;
  170. }
  171. }
  172. }
  173. StringTableEntry cons_name_key = StringTable->insert(words2[0]);
  174. // must be in CONSTRAINT_NAME (only) form
  175. if (n_words2 == 1)
  176. {
  177. // arbitrary object/effect constraints must have a name
  178. if (cons_name_key == SCENE_CONS_KEY || cons_name_key == EFFECT_CONS_KEY)
  179. {
  180. dFree(buffer);
  181. return false;
  182. }
  183. mCons_src_name = cons_name_key;
  184. mDef_type = CONS_PREDEFINED;
  185. dFree(buffer);
  186. return true;
  187. }
  188. // "#scene.NAME" or "#scene.NAME.NODE""
  189. if (cons_name_key == SCENE_CONS_KEY)
  190. {
  191. mCons_src_name = StringTable->insert(words2[1]);
  192. if (n_words2 > 2)
  193. mCons_node_name = StringTable->insert(words2[2]);
  194. mDef_type = CONS_SCENE;
  195. dFree(buffer);
  196. return true;
  197. }
  198. // "#effect.NAME" or "#effect.NAME.NODE"
  199. if (cons_name_key == EFFECT_CONS_KEY)
  200. {
  201. mCons_src_name = StringTable->insert(words2[1]);
  202. if (n_words2 > 2)
  203. mCons_node_name = StringTable->insert(words2[2]);
  204. mDef_type = CONS_EFFECT;
  205. dFree(buffer);
  206. return true;
  207. }
  208. // "#ghost.NAME" or "#ghost.NAME.NODE"
  209. if (cons_name_key == GHOST_CONS_KEY)
  210. {
  211. if (runs_on_server)
  212. {
  213. dFree(buffer);
  214. return false;
  215. }
  216. mCons_src_name = StringTable->insert(words2[1]);
  217. if (n_words2 > 2)
  218. mCons_node_name = StringTable->insert(words2[2]);
  219. mDef_type = CONS_GHOST;
  220. dFree(buffer);
  221. return true;
  222. }
  223. // "CONSTRAINT_NAME.NODE"
  224. if (n_words2 == 2)
  225. {
  226. mCons_src_name = cons_name_key;
  227. mCons_node_name = StringTable->insert(words2[1]);
  228. mDef_type = CONS_PREDEFINED;
  229. dFree(buffer);
  230. return true;
  231. }
  232. // must be in unsupported form
  233. dFree(buffer);
  234. return false;
  235. }
  236. void afxConstraintDef::gather_cons_defs(Vector<afxConstraintDef>& defs, afxEffectList& fx)
  237. {
  238. for (S32 i = 0; i < fx.size(); i++)
  239. {
  240. if (fx[i])
  241. fx[i]->gather_cons_defs(defs);
  242. }
  243. }
  244. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  245. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  246. // afxConstraint
  247. afxConstraint::afxConstraint(afxConstraintMgr* mgr)
  248. {
  249. mMgr = mgr;
  250. mIs_defined = false;
  251. mIs_valid = false;
  252. mLast_pos.zero();
  253. mLast_xfm.identity();
  254. mHistory_time = 0.0f;
  255. mIs_alive = true;
  256. mGone_missing = false;
  257. mChange_code = 0;
  258. }
  259. afxConstraint::~afxConstraint()
  260. {
  261. }
  262. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  263. inline afxPointConstraint* newPointCons(afxConstraintMgr* mgr, bool hist)
  264. {
  265. return (hist) ? new afxPointHistConstraint(mgr) : new afxPointConstraint(mgr);
  266. }
  267. inline afxTransformConstraint* newTransformCons(afxConstraintMgr* mgr, bool hist)
  268. {
  269. return (hist) ? new afxTransformHistConstraint(mgr) : new afxTransformConstraint(mgr);
  270. }
  271. inline afxShapeConstraint* newShapeCons(afxConstraintMgr* mgr, bool hist)
  272. {
  273. return (hist) ? new afxShapeHistConstraint(mgr) : new afxShapeConstraint(mgr);
  274. }
  275. inline afxShapeConstraint* newShapeCons(afxConstraintMgr* mgr, StringTableEntry name, bool hist)
  276. {
  277. return (hist) ? new afxShapeHistConstraint(mgr, name) : new afxShapeConstraint(mgr, name);
  278. }
  279. inline afxShapeNodeConstraint* newShapeNodeCons(afxConstraintMgr* mgr, StringTableEntry name, StringTableEntry node, bool hist)
  280. {
  281. return (hist) ? new afxShapeNodeHistConstraint(mgr, name, node) : new afxShapeNodeConstraint(mgr, name, node);
  282. }
  283. inline afxObjectConstraint* newObjectCons(afxConstraintMgr* mgr, bool hist)
  284. {
  285. return (hist) ? new afxObjectHistConstraint(mgr) : new afxObjectConstraint(mgr);
  286. }
  287. inline afxObjectConstraint* newObjectCons(afxConstraintMgr* mgr, StringTableEntry name, bool hist)
  288. {
  289. return (hist) ? new afxObjectHistConstraint(mgr, name) : new afxObjectConstraint(mgr, name);
  290. }
  291. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  292. // afxConstraintMgr
  293. #define CONS_BY_ID(id) ((*mConstraints_v[(id).index])[(id).sub_index])
  294. #define CONS_BY_IJ(i,j) ((*mConstraints_v[(i)])[(j)])
  295. afxConstraintMgr::afxConstraintMgr()
  296. {
  297. mStartTime = 0;
  298. mOn_server = false;
  299. mInitialized = false;
  300. mScoping_dist_sq = 1000.0f*1000.0f;
  301. missing_objs = &missing_objs_a;
  302. missing_objs2 = &missing_objs_b;
  303. }
  304. afxConstraintMgr::~afxConstraintMgr()
  305. {
  306. for (S32 i = 0; i < mConstraints_v.size(); i++)
  307. {
  308. for (S32 j = 0; j < (*mConstraints_v[i]).size(); j++)
  309. delete CONS_BY_IJ(i,j);
  310. delete mConstraints_v[i];
  311. }
  312. }
  313. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  314. S32 afxConstraintMgr::find_cons_idx_from_name(StringTableEntry which)
  315. {
  316. for (S32 i = 0; i < mConstraints_v.size(); i++)
  317. {
  318. afxConstraint* cons = CONS_BY_IJ(i,0);
  319. if (cons && afxConstraintDef::CONS_EFFECT != cons->mCons_def.mDef_type &&
  320. which == cons->mCons_def.mCons_src_name)
  321. {
  322. return i;
  323. }
  324. }
  325. return -1;
  326. }
  327. S32 afxConstraintMgr::find_effect_cons_idx_from_name(StringTableEntry which)
  328. {
  329. for (S32 i = 0; i < mConstraints_v.size(); i++)
  330. {
  331. afxConstraint* cons = CONS_BY_IJ(i,0);
  332. if (cons && afxConstraintDef::CONS_EFFECT == cons->mCons_def.mDef_type &&
  333. which == cons->mCons_def.mCons_src_name)
  334. {
  335. return i;
  336. }
  337. }
  338. return -1;
  339. }
  340. // Defines a predefined constraint with given name and type
  341. void afxConstraintMgr::defineConstraint(U32 type, StringTableEntry name)
  342. {
  343. preDef predef = { name, type };
  344. mPredefs.push_back(predef);
  345. }
  346. afxConstraintID afxConstraintMgr::setReferencePoint(StringTableEntry which, Point3F point,
  347. Point3F vector)
  348. {
  349. S32 idx = find_cons_idx_from_name(which);
  350. if (idx < 0)
  351. return afxConstraintID();
  352. afxConstraintID id = afxConstraintID(idx);
  353. setReferencePoint(id, point, vector);
  354. return id;
  355. }
  356. afxConstraintID afxConstraintMgr::setReferenceTransform(StringTableEntry which, MatrixF& xfm)
  357. {
  358. S32 idx = find_cons_idx_from_name(which);
  359. if (idx < 0)
  360. return afxConstraintID();
  361. afxConstraintID id = afxConstraintID(idx);
  362. setReferenceTransform(id, xfm);
  363. return id;
  364. }
  365. // Assigns an existing scene-object to the named constraint
  366. afxConstraintID afxConstraintMgr::setReferenceObject(StringTableEntry which, SceneObject* obj)
  367. {
  368. S32 idx = find_cons_idx_from_name(which);
  369. if (idx < 0)
  370. return afxConstraintID();
  371. afxConstraintID id = afxConstraintID(idx);
  372. setReferenceObject(id, obj);
  373. return id;
  374. }
  375. // Assigns an un-scoped scene-object by scope_id to the named constraint
  376. afxConstraintID afxConstraintMgr::setReferenceObjectByScopeId(StringTableEntry which, U16 scope_id, bool is_shape)
  377. {
  378. S32 idx = find_cons_idx_from_name(which);
  379. if (idx < 0)
  380. return afxConstraintID();
  381. afxConstraintID id = afxConstraintID(idx);
  382. setReferenceObjectByScopeId(id, scope_id, is_shape);
  383. return id;
  384. }
  385. afxConstraintID afxConstraintMgr::setReferenceEffect(StringTableEntry which, afxEffectWrapper* ew)
  386. {
  387. S32 idx = find_effect_cons_idx_from_name(which);
  388. if (idx < 0)
  389. return afxConstraintID();
  390. afxConstraintID id = afxConstraintID(idx);
  391. setReferenceEffect(id, ew);
  392. return id;
  393. }
  394. afxConstraintID afxConstraintMgr::createReferenceEffect(StringTableEntry which, afxEffectWrapper* ew)
  395. {
  396. afxEffectConstraint* cons = new afxEffectConstraint(this, which);
  397. //cons->cons_def = def;
  398. cons->mCons_def.mDef_type = afxConstraintDef::CONS_EFFECT;
  399. cons->mCons_def.mCons_src_name = which;
  400. afxConstraintList* list = new afxConstraintList();
  401. list->push_back(cons);
  402. mConstraints_v.push_back(list);
  403. return setReferenceEffect(which, ew);
  404. }
  405. void afxConstraintMgr::setReferencePoint(afxConstraintID id, Point3F point, Point3F vector)
  406. {
  407. afxPointConstraint* pt_cons = dynamic_cast<afxPointConstraint*>(CONS_BY_ID(id));
  408. // need to change type
  409. if (!pt_cons)
  410. {
  411. afxConstraint* cons = CONS_BY_ID(id);
  412. pt_cons = newPointCons(this, cons->mCons_def.mHistory_time > 0.0f);
  413. pt_cons->mCons_def = cons->mCons_def;
  414. CONS_BY_ID(id) = pt_cons;
  415. delete cons;
  416. }
  417. pt_cons->set(point, vector);
  418. // nullify all subnodes
  419. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  420. {
  421. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  422. if (cons)
  423. cons->unset();
  424. }
  425. }
  426. void afxConstraintMgr::setReferenceTransform(afxConstraintID id, MatrixF& xfm)
  427. {
  428. afxTransformConstraint* xfm_cons = dynamic_cast<afxTransformConstraint*>(CONS_BY_ID(id));
  429. // need to change type
  430. if (!xfm_cons)
  431. {
  432. afxConstraint* cons = CONS_BY_ID(id);
  433. xfm_cons = newTransformCons(this, cons->mCons_def.mHistory_time > 0.0f);
  434. xfm_cons->mCons_def = cons->mCons_def;
  435. CONS_BY_ID(id) = xfm_cons;
  436. delete cons;
  437. }
  438. xfm_cons->set(xfm);
  439. // nullify all subnodes
  440. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  441. {
  442. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  443. if (cons)
  444. cons->unset();
  445. }
  446. }
  447. void afxConstraintMgr::set_ref_shape(afxConstraintID id, ShapeBase* shape)
  448. {
  449. id.sub_index = 0;
  450. afxShapeConstraint* shape_cons = dynamic_cast<afxShapeConstraint*>(CONS_BY_ID(id));
  451. // need to change type
  452. if (!shape_cons)
  453. {
  454. afxConstraint* cons = CONS_BY_ID(id);
  455. shape_cons = newShapeCons(this, cons->mCons_def.mHistory_time > 0.0f);
  456. shape_cons->mCons_def = cons->mCons_def;
  457. CONS_BY_ID(id) = shape_cons;
  458. delete cons;
  459. }
  460. // set new shape on root
  461. shape_cons->set(shape);
  462. // update all subnodes
  463. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  464. {
  465. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  466. if (cons)
  467. {
  468. if (dynamic_cast<afxShapeNodeConstraint*>(cons))
  469. ((afxShapeNodeConstraint*)cons)->set(shape);
  470. else if (dynamic_cast<afxShapeConstraint*>(cons))
  471. ((afxShapeConstraint*)cons)->set(shape);
  472. else if (dynamic_cast<afxObjectConstraint*>(cons))
  473. ((afxObjectConstraint*)cons)->set(shape);
  474. else
  475. cons->unset();
  476. }
  477. }
  478. }
  479. void afxConstraintMgr::set_ref_shape(afxConstraintID id, U16 scope_id)
  480. {
  481. id.sub_index = 0;
  482. afxShapeConstraint* shape_cons = dynamic_cast<afxShapeConstraint*>(CONS_BY_ID(id));
  483. // need to change type
  484. if (!shape_cons)
  485. {
  486. afxConstraint* cons = CONS_BY_ID(id);
  487. shape_cons = newShapeCons(this, cons->mCons_def.mHistory_time > 0.0f);
  488. shape_cons->mCons_def = cons->mCons_def;
  489. CONS_BY_ID(id) = shape_cons;
  490. delete cons;
  491. }
  492. // set new shape on root
  493. shape_cons->set_scope_id(scope_id);
  494. // update all subnodes
  495. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  496. {
  497. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  498. if (cons)
  499. cons->set_scope_id(scope_id);
  500. }
  501. }
  502. // Assigns an existing scene-object to the constraint matching the given constraint-id.
  503. void afxConstraintMgr::setReferenceObject(afxConstraintID id, SceneObject* obj)
  504. {
  505. if (!mInitialized)
  506. Con::errorf("afxConstraintMgr::setReferenceObject() -- constraint manager not initialized");
  507. if (!CONS_BY_ID(id)->mCons_def.mTreat_as_camera)
  508. {
  509. ShapeBase* shape = dynamic_cast<ShapeBase*>(obj);
  510. if (shape)
  511. {
  512. set_ref_shape(id, shape);
  513. return;
  514. }
  515. }
  516. afxObjectConstraint* obj_cons = dynamic_cast<afxObjectConstraint*>(CONS_BY_ID(id));
  517. // need to change type
  518. if (!obj_cons)
  519. {
  520. afxConstraint* cons = CONS_BY_ID(id);
  521. obj_cons = newObjectCons(this, cons->mCons_def.mHistory_time > 0.0f);
  522. obj_cons->mCons_def = cons->mCons_def;
  523. CONS_BY_ID(id) = obj_cons;
  524. delete cons;
  525. }
  526. obj_cons->set(obj);
  527. // update all subnodes
  528. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  529. {
  530. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  531. if (cons)
  532. {
  533. if (dynamic_cast<afxObjectConstraint*>(cons))
  534. ((afxObjectConstraint*)cons)->set(obj);
  535. else
  536. cons->unset();
  537. }
  538. }
  539. }
  540. // Assigns an un-scoped scene-object by scope_id to the constraint matching the
  541. // given constraint-id.
  542. void afxConstraintMgr::setReferenceObjectByScopeId(afxConstraintID id, U16 scope_id, bool is_shape)
  543. {
  544. if (!mInitialized)
  545. Con::errorf("afxConstraintMgr::setReferenceObject() -- constraint manager not initialized");
  546. if (is_shape)
  547. {
  548. set_ref_shape(id, scope_id);
  549. return;
  550. }
  551. afxObjectConstraint* obj_cons = dynamic_cast<afxObjectConstraint*>(CONS_BY_ID(id));
  552. // need to change type
  553. if (!obj_cons)
  554. {
  555. afxConstraint* cons = CONS_BY_ID(id);
  556. obj_cons = newObjectCons(this, cons->mCons_def.mHistory_time > 0.0f);
  557. obj_cons->mCons_def = cons->mCons_def;
  558. CONS_BY_ID(id) = obj_cons;
  559. delete cons;
  560. }
  561. obj_cons->set_scope_id(scope_id);
  562. // update all subnodes
  563. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  564. {
  565. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  566. if (cons)
  567. cons->set_scope_id(scope_id);
  568. }
  569. }
  570. void afxConstraintMgr::setReferenceEffect(afxConstraintID id, afxEffectWrapper* ew)
  571. {
  572. afxEffectConstraint* eff_cons = dynamic_cast<afxEffectConstraint*>(CONS_BY_ID(id));
  573. if (!eff_cons)
  574. return;
  575. eff_cons->set(ew);
  576. // update all subnodes
  577. for (S32 j = 1; j < (*mConstraints_v[id.index]).size(); j++)
  578. {
  579. afxConstraint* cons = CONS_BY_IJ(id.index,j);
  580. if (cons)
  581. {
  582. if (dynamic_cast<afxEffectNodeConstraint*>(cons))
  583. ((afxEffectNodeConstraint*)cons)->set(ew);
  584. else if (dynamic_cast<afxEffectConstraint*>(cons))
  585. ((afxEffectConstraint*)cons)->set(ew);
  586. else
  587. cons->unset();
  588. }
  589. }
  590. }
  591. void afxConstraintMgr::invalidateReference(afxConstraintID id)
  592. {
  593. afxConstraint* cons = CONS_BY_ID(id);
  594. if (cons)
  595. cons->mIs_valid = false;
  596. }
  597. void afxConstraintMgr::create_constraint(const afxConstraintDef& def)
  598. {
  599. if (def.mDef_type == afxConstraintDef::CONS_UNDEFINED)
  600. return;
  601. //Con::printf("CON - %s [%s] [%s] h=%g", def.cons_type_name, def.cons_src_name, def.cons_node_name, def.history_time);
  602. bool want_history = (def.mHistory_time > 0.0f);
  603. // constraint is an arbitrary named scene object
  604. //
  605. if (def.mDef_type == afxConstraintDef::CONS_SCENE)
  606. {
  607. if (def.mCons_src_name == ST_NULLSTRING)
  608. return;
  609. // find the arbitrary object by name
  610. SceneObject* arb_obj;
  611. if (mOn_server)
  612. {
  613. arb_obj = dynamic_cast<SceneObject*>(Sim::findObject(def.mCons_src_name));
  614. if (!arb_obj)
  615. Con::errorf("afxConstraintMgr -- failed to find scene constraint source, \"%s\" on server.",
  616. def.mCons_src_name);
  617. }
  618. else
  619. {
  620. arb_obj = find_object_from_name(def.mCons_src_name);
  621. if (!arb_obj)
  622. Con::errorf("afxConstraintMgr -- failed to find scene constraint source, \"%s\" on client.",
  623. def.mCons_src_name);
  624. }
  625. // if it's a shapeBase object, create a Shape or ShapeNode constraint
  626. if (dynamic_cast<ShapeBase*>(arb_obj))
  627. {
  628. if (def.mCons_node_name == ST_NULLSTRING && !def.mPos_at_box_center)
  629. {
  630. afxShapeConstraint* cons = newShapeCons(this, def.mCons_src_name, want_history);
  631. cons->mCons_def = def;
  632. cons->set((ShapeBase*)arb_obj);
  633. afxConstraintList* list = new afxConstraintList();
  634. list->push_back(cons);
  635. mConstraints_v.push_back(list);
  636. }
  637. else if (def.mPos_at_box_center)
  638. {
  639. afxShapeConstraint* cons = newShapeCons(this, def.mCons_src_name, want_history);
  640. cons->mCons_def = def;
  641. cons->set((ShapeBase*)arb_obj);
  642. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1]; // SHAPE-NODE CONS-LIST (#scene)(#center)
  643. if (list && (*list)[0])
  644. list->push_back(cons);
  645. }
  646. else
  647. {
  648. afxShapeNodeConstraint* sub = newShapeNodeCons(this, def.mCons_src_name, def.mCons_node_name, want_history);
  649. sub->mCons_def = def;
  650. sub->set((ShapeBase*)arb_obj);
  651. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1];
  652. if (list && (*list)[0])
  653. list->push_back(sub);
  654. }
  655. }
  656. // if it's not a shapeBase object, create an Object constraint
  657. else if (arb_obj)
  658. {
  659. if (!def.mPos_at_box_center)
  660. {
  661. afxObjectConstraint* cons = newObjectCons(this, def.mCons_src_name, want_history);
  662. cons->mCons_def = def;
  663. cons->set(arb_obj);
  664. afxConstraintList* list = new afxConstraintList(); // OBJECT CONS-LIST (#scene)
  665. list->push_back(cons);
  666. mConstraints_v.push_back(list);
  667. }
  668. else // if (def.pos_at_box_center)
  669. {
  670. afxObjectConstraint* cons = newObjectCons(this, def.mCons_src_name, want_history);
  671. cons->mCons_def = def;
  672. cons->set(arb_obj);
  673. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1]; // OBJECT CONS-LIST (#scene)(#center)
  674. if (list && (*list)[0])
  675. list->push_back(cons);
  676. }
  677. }
  678. }
  679. // constraint is an arbitrary named effect
  680. //
  681. else if (def.mDef_type == afxConstraintDef::CONS_EFFECT)
  682. {
  683. if (def.mCons_src_name == ST_NULLSTRING)
  684. return;
  685. // create an Effect constraint
  686. if (def.mCons_node_name == ST_NULLSTRING && !def.mPos_at_box_center)
  687. {
  688. afxEffectConstraint* cons = new afxEffectConstraint(this, def.mCons_src_name);
  689. cons->mCons_def = def;
  690. afxConstraintList* list = new afxConstraintList();
  691. list->push_back(cons);
  692. mConstraints_v.push_back(list);
  693. }
  694. // create an Effect #center constraint
  695. else if (def.mPos_at_box_center)
  696. {
  697. afxEffectConstraint* cons = new afxEffectConstraint(this, def.mCons_src_name);
  698. cons->mCons_def = def;
  699. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1]; // EFFECT-NODE CONS-LIST (#effect)
  700. if (list && (*list)[0])
  701. list->push_back(cons);
  702. }
  703. // create an EffectNode constraint
  704. else
  705. {
  706. afxEffectNodeConstraint* sub = new afxEffectNodeConstraint(this, def.mCons_src_name, def.mCons_node_name);
  707. sub->mCons_def = def;
  708. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1];
  709. if (list && (*list)[0])
  710. list->push_back(sub);
  711. }
  712. }
  713. // constraint is a predefined constraint
  714. //
  715. else
  716. {
  717. afxConstraint* cons = 0;
  718. afxConstraint* cons_ctr = 0;
  719. afxConstraint* sub = 0;
  720. if (def.mDef_type == afxConstraintDef::CONS_GHOST)
  721. {
  722. for (S32 i = 0; i < mPredefs.size(); i++)
  723. {
  724. if (mPredefs[i].name == def.mCons_src_name)
  725. {
  726. if (def.mCons_node_name == ST_NULLSTRING && !def.mPos_at_box_center)
  727. {
  728. cons = newShapeCons(this, want_history);
  729. cons->mCons_def = def;
  730. }
  731. else if (def.mPos_at_box_center)
  732. {
  733. cons_ctr = newShapeCons(this, want_history);
  734. cons_ctr->mCons_def = def;
  735. }
  736. else
  737. {
  738. sub = newShapeNodeCons(this, ST_NULLSTRING, def.mCons_node_name, want_history);
  739. sub->mCons_def = def;
  740. }
  741. break;
  742. }
  743. }
  744. }
  745. else
  746. {
  747. for (S32 i = 0; i < mPredefs.size(); i++)
  748. {
  749. if (mPredefs[i].name == def.mCons_src_name)
  750. {
  751. switch (mPredefs[i].type)
  752. {
  753. case POINT_CONSTRAINT:
  754. cons = newPointCons(this, want_history);
  755. cons->mCons_def = def;
  756. break;
  757. case TRANSFORM_CONSTRAINT:
  758. cons = newTransformCons(this, want_history);
  759. cons->mCons_def = def;
  760. break;
  761. case OBJECT_CONSTRAINT:
  762. if (def.mCons_node_name == ST_NULLSTRING && !def.mPos_at_box_center)
  763. {
  764. cons = newShapeCons(this, want_history);
  765. cons->mCons_def = def;
  766. }
  767. else if (def.mPos_at_box_center)
  768. {
  769. cons_ctr = newShapeCons(this, want_history);
  770. cons_ctr->mCons_def = def;
  771. }
  772. else
  773. {
  774. sub = newShapeNodeCons(this, ST_NULLSTRING, def.mCons_node_name, want_history);
  775. sub->mCons_def = def;
  776. }
  777. break;
  778. case CAMERA_CONSTRAINT:
  779. cons = newObjectCons(this, want_history);
  780. cons->mCons_def = def;
  781. cons->mCons_def.mTreat_as_camera = true;
  782. break;
  783. }
  784. break;
  785. }
  786. }
  787. }
  788. if (cons)
  789. {
  790. afxConstraintList* list = new afxConstraintList();
  791. list->push_back(cons);
  792. mConstraints_v.push_back(list);
  793. }
  794. else if (cons_ctr && mConstraints_v.size() > 0)
  795. {
  796. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1]; // PREDEF-NODE CONS-LIST
  797. if (list && (*list)[0])
  798. list->push_back(cons_ctr);
  799. }
  800. else if (sub && mConstraints_v.size() > 0)
  801. {
  802. afxConstraintList* list = mConstraints_v[mConstraints_v.size()-1];
  803. if (list && (*list)[0])
  804. list->push_back(sub);
  805. }
  806. else
  807. Con::printf("predef not found %s", def.mCons_src_name);
  808. }
  809. }
  810. afxConstraintID afxConstraintMgr::getConstraintId(const afxConstraintDef& def)
  811. {
  812. if (def.mDef_type == afxConstraintDef::CONS_UNDEFINED)
  813. return afxConstraintID();
  814. if (def.mCons_src_name != ST_NULLSTRING)
  815. {
  816. for (S32 i = 0; i < mConstraints_v.size(); i++)
  817. {
  818. afxConstraintList* list = mConstraints_v[i];
  819. afxConstraint* cons = (*list)[0];
  820. if (def.mCons_src_name == cons->mCons_def.mCons_src_name)
  821. {
  822. for (S32 j = 0; j < list->size(); j++)
  823. {
  824. afxConstraint* sub = (*list)[j];
  825. if (def.mCons_node_name == sub->mCons_def.mCons_node_name &&
  826. def.mPos_at_box_center == sub->mCons_def.mPos_at_box_center &&
  827. def.mCons_src_name == sub->mCons_def.mCons_src_name)
  828. {
  829. return afxConstraintID(i, j);
  830. }
  831. }
  832. // if we're here, it means the root object name matched but the node name
  833. // did not.
  834. if (def.mDef_type == afxConstraintDef::CONS_PREDEFINED && !def.mPos_at_box_center)
  835. {
  836. afxShapeConstraint* shape_cons = dynamic_cast<afxShapeConstraint*>(cons);
  837. if (shape_cons)
  838. {
  839. //Con::errorf("Append a Node constraint [%s.%s] [%d,%d]", def.cons_src_name, def.cons_node_name, i, list->size());
  840. bool want_history = (def.mHistory_time > 0.0f);
  841. afxConstraint* sub = newShapeNodeCons(this, ST_NULLSTRING, def.mCons_node_name, want_history);
  842. sub->mCons_def = def;
  843. ((afxShapeConstraint*)sub)->set(shape_cons->mShape);
  844. list->push_back(sub);
  845. return afxConstraintID(i, list->size()-1);
  846. }
  847. }
  848. break;
  849. }
  850. }
  851. }
  852. return afxConstraintID();
  853. }
  854. afxConstraint* afxConstraintMgr::getConstraint(afxConstraintID id)
  855. {
  856. if (id.undefined())
  857. return 0;
  858. afxConstraint* cons = CONS_BY_IJ(id.index,id.sub_index);
  859. if (cons && !cons->isDefined())
  860. return NULL;
  861. return cons;
  862. }
  863. void afxConstraintMgr::sample(F32 dt, U32 now, const Point3F* cam_pos)
  864. {
  865. U32 elapsed = now - mStartTime;
  866. for (S32 i = 0; i < mConstraints_v.size(); i++)
  867. {
  868. afxConstraintList* list = mConstraints_v[i];
  869. for (S32 j = 0; j < list->size(); j++)
  870. (*list)[j]->sample(dt, elapsed, cam_pos);
  871. }
  872. }
  873. S32 QSORT_CALLBACK cmp_cons_defs(const void* a, const void* b)
  874. {
  875. afxConstraintDef* def_a = (afxConstraintDef*) a;
  876. afxConstraintDef* def_b = (afxConstraintDef*) b;
  877. if (def_a->mDef_type == def_b->mDef_type)
  878. {
  879. if (def_a->mCons_src_name == def_b->mCons_src_name)
  880. {
  881. if (def_a->mPos_at_box_center == def_b->mPos_at_box_center)
  882. return (def_a->mCons_node_name - def_b->mCons_node_name);
  883. else
  884. return (def_a->mPos_at_box_center) ? 1 : -1;
  885. }
  886. return (def_a->mCons_src_name - def_b->mCons_src_name);
  887. }
  888. return (def_a->mDef_type - def_b->mDef_type);
  889. }
  890. void afxConstraintMgr::initConstraintDefs(Vector<afxConstraintDef>& all_defs, bool on_server, F32 scoping_dist)
  891. {
  892. mInitialized = true;
  893. mOn_server = on_server;
  894. if (scoping_dist > 0.0)
  895. mScoping_dist_sq = scoping_dist*scoping_dist;
  896. else
  897. {
  898. SceneManager* sg = (on_server) ? gServerSceneGraph : gClientSceneGraph;
  899. F32 vis_dist = (sg) ? sg->getVisibleDistance() : 1000.0f;
  900. mScoping_dist_sq = vis_dist*vis_dist;
  901. }
  902. if (all_defs.size() < 1)
  903. return;
  904. // find effect ghost constraints
  905. if (!on_server)
  906. {
  907. Vector<afxConstraintDef> ghost_defs;
  908. for (S32 i = 0; i < all_defs.size(); i++)
  909. if (all_defs[i].mDef_type == afxConstraintDef::CONS_GHOST && all_defs[i].mCons_src_name != ST_NULLSTRING)
  910. ghost_defs.push_back(all_defs[i]);
  911. if (ghost_defs.size() > 0)
  912. {
  913. // sort the defs
  914. if (ghost_defs.size() > 1)
  915. dQsort(ghost_defs.address(), ghost_defs.size(), sizeof(afxConstraintDef), cmp_cons_defs);
  916. S32 last = 0;
  917. defineConstraint(OBJECT_CONSTRAINT, ghost_defs[0].mCons_src_name);
  918. for (S32 i = 1; i < ghost_defs.size(); i++)
  919. {
  920. if (ghost_defs[last].mCons_src_name != ghost_defs[i].mCons_src_name)
  921. {
  922. defineConstraint(OBJECT_CONSTRAINT, ghost_defs[i].mCons_src_name);
  923. last++;
  924. }
  925. }
  926. }
  927. }
  928. Vector<afxConstraintDef> defs;
  929. // collect defs that run here (server or client)
  930. if (on_server)
  931. {
  932. for (S32 i = 0; i < all_defs.size(); i++)
  933. if (all_defs[i].mRuns_on_server)
  934. defs.push_back(all_defs[i]);
  935. }
  936. else
  937. {
  938. for (S32 i = 0; i < all_defs.size(); i++)
  939. if (all_defs[i].mRuns_on_client)
  940. defs.push_back(all_defs[i]);
  941. }
  942. // create unique set of constraints.
  943. //
  944. if (defs.size() > 0)
  945. {
  946. // sort the defs
  947. if (defs.size() > 1)
  948. dQsort(defs.address(), defs.size(), sizeof(afxConstraintDef), cmp_cons_defs);
  949. Vector<afxConstraintDef> unique_defs;
  950. S32 last = 0;
  951. // manufacture root-object def if absent
  952. if (defs[0].mCons_node_name != ST_NULLSTRING)
  953. {
  954. afxConstraintDef root_def = defs[0];
  955. root_def.mCons_node_name = ST_NULLSTRING;
  956. unique_defs.push_back(root_def);
  957. last++;
  958. }
  959. else if (defs[0].mPos_at_box_center)
  960. {
  961. afxConstraintDef root_def = defs[0];
  962. root_def.mPos_at_box_center = false;
  963. unique_defs.push_back(root_def);
  964. last++;
  965. }
  966. unique_defs.push_back(defs[0]);
  967. for (S32 i = 1; i < defs.size(); i++)
  968. {
  969. if (unique_defs[last].mCons_node_name != defs[i].mCons_node_name ||
  970. unique_defs[last].mCons_src_name != defs[i].mCons_src_name ||
  971. unique_defs[last].mPos_at_box_center != defs[i].mPos_at_box_center ||
  972. unique_defs[last].mDef_type != defs[i].mDef_type)
  973. {
  974. // manufacture root-object def if absent
  975. if (defs[i].mCons_src_name != ST_NULLSTRING && unique_defs[last].mCons_src_name != defs[i].mCons_src_name)
  976. {
  977. if (defs[i].mCons_node_name != ST_NULLSTRING || defs[i].mPos_at_box_center)
  978. {
  979. afxConstraintDef root_def = defs[i];
  980. root_def.mCons_node_name = ST_NULLSTRING;
  981. root_def.mPos_at_box_center = false;
  982. unique_defs.push_back(root_def);
  983. last++;
  984. }
  985. }
  986. unique_defs.push_back(defs[i]);
  987. last++;
  988. }
  989. else
  990. {
  991. if (defs[i].mHistory_time > unique_defs[last].mHistory_time)
  992. unique_defs[last].mHistory_time = defs[i].mHistory_time;
  993. if (defs[i].mSample_rate > unique_defs[last].mSample_rate)
  994. unique_defs[last].mSample_rate = defs[i].mSample_rate;
  995. }
  996. }
  997. //Con::printf("\nConstraints on %s", (on_server) ? "server" : "client");
  998. for (S32 i = 0; i < unique_defs.size(); i++)
  999. create_constraint(unique_defs[i]);
  1000. }
  1001. // collect the names of all the arbitrary object constraints
  1002. // that run on clients and store in names_on_server array.
  1003. //
  1004. if (on_server)
  1005. {
  1006. mNames_on_server.clear();
  1007. defs.clear();
  1008. for (S32 i = 0; i < all_defs.size(); i++)
  1009. if (all_defs[i].mRuns_on_client && all_defs[i].isArbitraryObject())
  1010. defs.push_back(all_defs[i]);
  1011. if (defs.size() < 1)
  1012. return;
  1013. // sort the defs
  1014. if (defs.size() > 1)
  1015. dQsort(defs.address(), defs.size(), sizeof(afxConstraintDef), cmp_cons_defs);
  1016. S32 last = 0;
  1017. mNames_on_server.push_back(defs[0].mCons_src_name);
  1018. for (S32 i = 1; i < defs.size(); i++)
  1019. {
  1020. if (mNames_on_server[last] != defs[i].mCons_src_name)
  1021. {
  1022. mNames_on_server.push_back(defs[i].mCons_src_name);
  1023. last++;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. void afxConstraintMgr::packConstraintNames(NetConnection* conn, BitStream* stream)
  1029. {
  1030. // pack any named constraint names and ghost indices
  1031. if (stream->writeFlag(mNames_on_server.size() > 0)) //-- ANY NAMED CONS_BY_ID?
  1032. {
  1033. stream->write(mNames_on_server.size());
  1034. for (S32 i = 0; i < mNames_on_server.size(); i++)
  1035. {
  1036. stream->writeString(mNames_on_server[i]);
  1037. NetObject* obj = dynamic_cast<NetObject*>(Sim::findObject(mNames_on_server[i]));
  1038. if (!obj)
  1039. {
  1040. //Con::printf("CONSTRAINT-OBJECT %s does not exist.", names_on_server[i]);
  1041. stream->write((S32)-1);
  1042. }
  1043. else
  1044. {
  1045. S32 ghost_id = conn->getGhostIndex(obj);
  1046. /*
  1047. if (ghost_id == -1)
  1048. Con::printf("CONSTRAINT-OBJECT %s does not have a ghost.", names_on_server[i]);
  1049. else
  1050. Con::printf("CONSTRAINT-OBJECT %s name to server.", names_on_server[i]);
  1051. */
  1052. stream->write(ghost_id);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. void afxConstraintMgr::unpackConstraintNames(BitStream* stream)
  1058. {
  1059. if (stream->readFlag()) //-- ANY NAMED CONS_BY_ID?
  1060. {
  1061. mNames_on_server.clear();
  1062. S32 sz; stream->read(&sz);
  1063. for (S32 i = 0; i < sz; i++)
  1064. {
  1065. mNames_on_server.push_back(stream->readSTString());
  1066. S32 ghost_id; stream->read(&ghost_id);
  1067. mGhost_ids.push_back(ghost_id);
  1068. }
  1069. }
  1070. }
  1071. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  1072. SceneObject* afxConstraintMgr::find_object_from_name(StringTableEntry name)
  1073. {
  1074. if (mNames_on_server.size() > 0)
  1075. {
  1076. for (S32 i = 0; i < mNames_on_server.size(); i++)
  1077. if (mNames_on_server[i] == name)
  1078. {
  1079. if (mGhost_ids[i] == -1)
  1080. return 0;
  1081. NetConnection* conn = NetConnection::getConnectionToServer();
  1082. if (!conn)
  1083. return 0;
  1084. return dynamic_cast<SceneObject*>(conn->resolveGhost(mGhost_ids[i]));
  1085. }
  1086. }
  1087. return dynamic_cast<SceneObject*>(Sim::findObject(name));
  1088. }
  1089. void afxConstraintMgr::addScopeableObject(SceneObject* object)
  1090. {
  1091. for (S32 i = 0; i < scopeable_objs.size(); i++)
  1092. {
  1093. if (scopeable_objs[i] == object)
  1094. return;
  1095. }
  1096. object->addScopeRef();
  1097. scopeable_objs.push_back(object);
  1098. }
  1099. void afxConstraintMgr::removeScopeableObject(SceneObject* object)
  1100. {
  1101. for (S32 i = 0; i < scopeable_objs.size(); i++)
  1102. if (scopeable_objs[i] == object)
  1103. {
  1104. object->removeScopeRef();
  1105. scopeable_objs.erase_fast(i);
  1106. return;
  1107. }
  1108. }
  1109. void afxConstraintMgr::clearAllScopeableObjs()
  1110. {
  1111. for (S32 i = 0; i < scopeable_objs.size(); i++)
  1112. scopeable_objs[i]->removeScopeRef();
  1113. scopeable_objs.clear();
  1114. }
  1115. void afxConstraintMgr::postMissingConstraintObject(afxConstraint* cons, bool is_deleting)
  1116. {
  1117. if (cons->mGone_missing)
  1118. return;
  1119. if (!is_deleting)
  1120. {
  1121. SceneObject* obj = arcaneFX::findScopedObject(cons->getScopeId());
  1122. if (obj)
  1123. {
  1124. cons->restoreObject(obj);
  1125. return;
  1126. }
  1127. }
  1128. cons->mGone_missing = true;
  1129. missing_objs->push_back(cons);
  1130. }
  1131. void afxConstraintMgr::restoreScopedObject(SceneObject* obj, afxChoreographer* ch)
  1132. {
  1133. for (S32 i = 0; i < missing_objs->size(); i++)
  1134. {
  1135. if ((*missing_objs)[i]->getScopeId() == obj->getScopeId())
  1136. {
  1137. (*missing_objs)[i]->mGone_missing = false;
  1138. (*missing_objs)[i]->restoreObject(obj);
  1139. if (ch)
  1140. ch->restoreObject(obj);
  1141. }
  1142. else
  1143. missing_objs2->push_back((*missing_objs)[i]);
  1144. }
  1145. Vector<afxConstraint*>* tmp = missing_objs;
  1146. missing_objs = missing_objs2;
  1147. missing_objs2 = tmp;
  1148. missing_objs2->clear();
  1149. }
  1150. void afxConstraintMgr::adjustProcessOrdering(afxChoreographer* ch)
  1151. {
  1152. Vector<ProcessObject*> cons_sources;
  1153. // add choreographer to the list
  1154. cons_sources.push_back(ch);
  1155. // collect all the ProcessObject related constraint sources
  1156. for (S32 i = 0; i < mConstraints_v.size(); i++)
  1157. {
  1158. afxConstraintList* list = mConstraints_v[i];
  1159. afxConstraint* cons = (*list)[0];
  1160. if (cons)
  1161. {
  1162. ProcessObject* pobj = dynamic_cast<ProcessObject*>(cons->getSceneObject());
  1163. if (pobj)
  1164. cons_sources.push_back(pobj);
  1165. }
  1166. }
  1167. ProcessList* proc_list;
  1168. if (ch->isServerObject())
  1169. proc_list = ServerProcessList::get();
  1170. else
  1171. proc_list = ClientProcessList::get();
  1172. if (!proc_list)
  1173. return;
  1174. GameBase* nearest_to_end = dynamic_cast<GameBase*>(proc_list->findNearestToEnd(cons_sources));
  1175. GameBase* chor = (GameBase*) ch;
  1176. // choreographer needs to be processed after the latest process object
  1177. if (chor != nearest_to_end && nearest_to_end != 0)
  1178. {
  1179. //Con::printf("Choreographer processing BEFORE some of its constraints... fixing. [%s] -- %s",
  1180. // (ch->isServerObject()) ? "S" : "C", nearest_to_end->getClassName());
  1181. if (chor->isProperlyAdded())
  1182. ch->processAfter(nearest_to_end);
  1183. else
  1184. ch->postProcessAfterObject(nearest_to_end);
  1185. }
  1186. else
  1187. {
  1188. //Con::printf("Choreographer processing AFTER its constraints... fine. [%s]",
  1189. // (ch->isServerObject()) ? "S" : "C");
  1190. }
  1191. }
  1192. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1193. // afxPointConstraint
  1194. afxPointConstraint::afxPointConstraint(afxConstraintMgr* mgr)
  1195. : afxConstraint(mgr)
  1196. {
  1197. mPoint.zero();
  1198. mVector.set(0,0,1);
  1199. }
  1200. afxPointConstraint::~afxPointConstraint()
  1201. {
  1202. }
  1203. void afxPointConstraint::set(Point3F point, Point3F vector)
  1204. {
  1205. mPoint = point;
  1206. mVector = vector;
  1207. mIs_defined = true;
  1208. mIs_valid = true;
  1209. mChange_code++;
  1210. sample(0.0f, 0, 0);
  1211. }
  1212. void afxPointConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1213. {
  1214. if (cam_pos)
  1215. {
  1216. Point3F dir = (*cam_pos) - mPoint;
  1217. F32 dist_sq = dir.lenSquared();
  1218. if (dist_sq > mMgr->getScopingDistanceSquared())
  1219. {
  1220. mIs_valid = false;
  1221. return;
  1222. }
  1223. mIs_valid = true;
  1224. }
  1225. mLast_pos = mPoint;
  1226. mLast_xfm.identity();
  1227. mLast_xfm.setPosition(mPoint);
  1228. }
  1229. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1230. // afxTransformConstraint
  1231. afxTransformConstraint::afxTransformConstraint(afxConstraintMgr* mgr)
  1232. : afxConstraint(mgr)
  1233. {
  1234. mXfm.identity();
  1235. }
  1236. afxTransformConstraint::~afxTransformConstraint()
  1237. {
  1238. }
  1239. void afxTransformConstraint::set(const MatrixF& xfm)
  1240. {
  1241. mXfm = xfm;
  1242. mIs_defined = true;
  1243. mIs_valid = true;
  1244. mChange_code++;
  1245. sample(0.0f, 0, 0);
  1246. }
  1247. void afxTransformConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1248. {
  1249. if (cam_pos)
  1250. {
  1251. Point3F dir = (*cam_pos) - mXfm.getPosition();
  1252. F32 dist_sq = dir.lenSquared();
  1253. if (dist_sq > mMgr->getScopingDistanceSquared())
  1254. {
  1255. mIs_valid = false;
  1256. return;
  1257. }
  1258. mIs_valid = true;
  1259. }
  1260. mLast_xfm = mXfm;
  1261. mLast_pos = mXfm.getPosition();
  1262. }
  1263. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1264. // afxShapeConstraint
  1265. afxShapeConstraint::afxShapeConstraint(afxConstraintMgr* mgr)
  1266. : afxConstraint(mgr)
  1267. {
  1268. mArb_name = ST_NULLSTRING;
  1269. mShape = 0;
  1270. mScope_id = 0;
  1271. mClip_tag = 0;
  1272. mLock_tag = 0;
  1273. }
  1274. afxShapeConstraint::afxShapeConstraint(afxConstraintMgr* mgr, StringTableEntry arb_name)
  1275. : afxConstraint(mgr)
  1276. {
  1277. mArb_name = arb_name;
  1278. mShape = 0;
  1279. mScope_id = 0;
  1280. mClip_tag = 0;
  1281. mLock_tag = 0;
  1282. }
  1283. afxShapeConstraint::~afxShapeConstraint()
  1284. {
  1285. if (mShape)
  1286. clearNotify(mShape);
  1287. }
  1288. void afxShapeConstraint::set(ShapeBase* shape)
  1289. {
  1290. if (mShape)
  1291. {
  1292. mScope_id = 0;
  1293. clearNotify(mShape);
  1294. if (mClip_tag > 0)
  1295. remapAnimation(mClip_tag, shape);
  1296. if (mLock_tag > 0)
  1297. unlockAnimation(mLock_tag);
  1298. }
  1299. mShape = shape;
  1300. if (mShape)
  1301. {
  1302. deleteNotify(mShape);
  1303. mScope_id = mShape->getScopeId();
  1304. }
  1305. if (mShape != NULL)
  1306. {
  1307. mIs_defined = true;
  1308. mIs_valid = true;
  1309. mChange_code++;
  1310. sample(0.0f, 0, 0);
  1311. }
  1312. else
  1313. mIs_valid = false;
  1314. }
  1315. void afxShapeConstraint::set_scope_id(U16 scope_id)
  1316. {
  1317. if (mShape)
  1318. clearNotify(mShape);
  1319. mShape = 0;
  1320. mScope_id = scope_id;
  1321. mIs_defined = (mScope_id > 0);
  1322. mIs_valid = false;
  1323. mMgr->postMissingConstraintObject(this);
  1324. }
  1325. void afxShapeConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1326. {
  1327. if (mGone_missing)
  1328. return;
  1329. if (mShape)
  1330. {
  1331. mLast_xfm = mShape->getRenderTransform();
  1332. if (mCons_def.mPos_at_box_center)
  1333. mLast_pos = mShape->getBoxCenter();
  1334. else
  1335. mLast_pos = mShape->getRenderPosition();
  1336. }
  1337. }
  1338. void afxShapeConstraint::restoreObject(SceneObject* obj)
  1339. {
  1340. if (mShape)
  1341. {
  1342. mScope_id = 0;
  1343. clearNotify(mShape);
  1344. }
  1345. mShape = (ShapeBase* )obj;
  1346. if (mShape)
  1347. {
  1348. deleteNotify(mShape);
  1349. mScope_id = mShape->getScopeId();
  1350. }
  1351. mIs_valid = (mShape != NULL);
  1352. }
  1353. void afxShapeConstraint::onDeleteNotify(SimObject* obj)
  1354. {
  1355. if (mShape == dynamic_cast<ShapeBase*>(obj))
  1356. {
  1357. mShape = 0;
  1358. mIs_valid = false;
  1359. if (mScope_id > 0)
  1360. mMgr->postMissingConstraintObject(this, true);
  1361. }
  1362. Parent::onDeleteNotify(obj);
  1363. }
  1364. U32 afxShapeConstraint::setAnimClip(const char* clip, F32 pos, F32 rate, F32 trans, bool is_death_anim)
  1365. {
  1366. if (!mShape)
  1367. return 0;
  1368. if (mShape->isServerObject())
  1369. {
  1370. AIPlayer* ai_player = dynamic_cast<AIPlayer*>(mShape);
  1371. if (ai_player && !ai_player->isBlendAnimation(clip))
  1372. {
  1373. ai_player->saveMoveState();
  1374. ai_player->stopMove();
  1375. }
  1376. }
  1377. mClip_tag = mShape->playAnimation(clip, pos, rate, trans, false/*hold*/, true/*wait*/, is_death_anim);
  1378. return mClip_tag;
  1379. }
  1380. void afxShapeConstraint::remapAnimation(U32 tag, ShapeBase* other_shape)
  1381. {
  1382. if (mClip_tag == 0)
  1383. return;
  1384. if (!mShape)
  1385. return;
  1386. if (!other_shape)
  1387. {
  1388. resetAnimation(tag);
  1389. return;
  1390. }
  1391. Con::errorf("remapAnimation -- Clip name, %s.", mShape->getLastClipName(tag));
  1392. if (mShape->isClientObject())
  1393. {
  1394. mShape->restoreAnimation(tag);
  1395. }
  1396. else
  1397. {
  1398. AIPlayer* ai_player = dynamic_cast<AIPlayer*>(mShape);
  1399. if (ai_player)
  1400. ai_player->restartMove(tag);
  1401. else
  1402. mShape->restoreAnimation(tag);
  1403. }
  1404. mClip_tag = 0;
  1405. }
  1406. void afxShapeConstraint::resetAnimation(U32 tag)
  1407. {
  1408. if (mClip_tag == 0)
  1409. return;
  1410. if (!mShape)
  1411. return;
  1412. if (mShape->isClientObject())
  1413. {
  1414. mShape->restoreAnimation(tag);
  1415. }
  1416. else
  1417. {
  1418. AIPlayer* ai_player = dynamic_cast<AIPlayer*>(mShape);
  1419. if (ai_player)
  1420. ai_player->restartMove(tag);
  1421. else
  1422. mShape->restoreAnimation(tag);
  1423. }
  1424. if ((tag & 0x80000000) == 0 && tag == mClip_tag)
  1425. mClip_tag = 0;
  1426. }
  1427. U32 afxShapeConstraint::lockAnimation()
  1428. {
  1429. if (!mShape)
  1430. return 0;
  1431. mLock_tag = mShape->lockAnimation();
  1432. return mLock_tag;
  1433. }
  1434. void afxShapeConstraint::unlockAnimation(U32 tag)
  1435. {
  1436. if (mLock_tag == 0)
  1437. return;
  1438. if (!mShape)
  1439. return;
  1440. mShape->unlockAnimation(tag);
  1441. mLock_tag = 0;
  1442. }
  1443. F32 afxShapeConstraint::getAnimClipDuration(const char* clip)
  1444. {
  1445. return (mShape) ? mShape->getAnimationDuration(clip) : 0.0f;
  1446. }
  1447. S32 afxShapeConstraint::getDamageState()
  1448. {
  1449. return (mShape) ? mShape->getDamageState() : -1;
  1450. }
  1451. U32 afxShapeConstraint::getTriggers()
  1452. {
  1453. return (mShape) ? mShape->getShapeInstance()->getTriggerStateMask() : 0;
  1454. }
  1455. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1456. // afxShapeNodeConstraint
  1457. afxShapeNodeConstraint::afxShapeNodeConstraint(afxConstraintMgr* mgr)
  1458. : afxShapeConstraint(mgr)
  1459. {
  1460. mArb_node = ST_NULLSTRING;
  1461. mShape_node_ID = -1;
  1462. }
  1463. afxShapeNodeConstraint::afxShapeNodeConstraint(afxConstraintMgr* mgr, StringTableEntry arb_name, StringTableEntry arb_node)
  1464. : afxShapeConstraint(mgr, arb_name)
  1465. {
  1466. mArb_node = arb_node;
  1467. mShape_node_ID = -1;
  1468. }
  1469. void afxShapeNodeConstraint::set(ShapeBase* shape)
  1470. {
  1471. if (shape)
  1472. {
  1473. mShape_node_ID = shape->getShape()->findNode(mArb_node);
  1474. if (mShape_node_ID == -1)
  1475. Con::errorf("Failed to find node [%s]", mArb_node);
  1476. }
  1477. else
  1478. mShape_node_ID = -1;
  1479. Parent::set(shape);
  1480. }
  1481. void afxShapeNodeConstraint::set_scope_id(U16 scope_id)
  1482. {
  1483. mShape_node_ID = -1;
  1484. Parent::set_scope_id(scope_id);
  1485. }
  1486. void afxShapeNodeConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1487. {
  1488. if (mShape && mShape_node_ID != -1)
  1489. {
  1490. mLast_xfm = mShape->getRenderTransform();
  1491. mLast_xfm.scale(mShape->getScale());
  1492. mLast_xfm.mul(mShape->getShapeInstance()->mNodeTransforms[mShape_node_ID]);
  1493. mLast_pos = mLast_xfm.getPosition();
  1494. }
  1495. }
  1496. void afxShapeNodeConstraint::restoreObject(SceneObject* obj)
  1497. {
  1498. ShapeBase* shape = dynamic_cast<ShapeBase*>(obj);
  1499. if (shape)
  1500. {
  1501. mShape_node_ID = shape->getShape()->findNode(mArb_node);
  1502. if (mShape_node_ID == -1)
  1503. Con::errorf("Failed to find node [%s]", mArb_node);
  1504. }
  1505. else
  1506. mShape_node_ID = -1;
  1507. Parent::restoreObject(obj);
  1508. }
  1509. void afxShapeNodeConstraint::onDeleteNotify(SimObject* obj)
  1510. {
  1511. Parent::onDeleteNotify(obj);
  1512. }
  1513. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1514. // afxObjectConstraint
  1515. afxObjectConstraint::afxObjectConstraint(afxConstraintMgr* mgr)
  1516. : afxConstraint(mgr)
  1517. {
  1518. mArb_name = ST_NULLSTRING;
  1519. mObj = 0;
  1520. mScope_id = 0;
  1521. mIs_camera = false;
  1522. }
  1523. afxObjectConstraint::afxObjectConstraint(afxConstraintMgr* mgr, StringTableEntry arb_name)
  1524. : afxConstraint(mgr)
  1525. {
  1526. mArb_name = arb_name;
  1527. mObj = 0;
  1528. mScope_id = 0;
  1529. mIs_camera = false;
  1530. }
  1531. afxObjectConstraint::~afxObjectConstraint()
  1532. {
  1533. if (mObj)
  1534. clearNotify(mObj);
  1535. }
  1536. void afxObjectConstraint::set(SceneObject* obj)
  1537. {
  1538. if (mObj)
  1539. {
  1540. mScope_id = 0;
  1541. clearNotify(mObj);
  1542. }
  1543. mObj = obj;
  1544. if (mObj)
  1545. {
  1546. deleteNotify(mObj);
  1547. mScope_id = mObj->getScopeId();
  1548. }
  1549. if (mObj != NULL)
  1550. {
  1551. mIs_camera = mObj->isCamera();
  1552. mIs_defined = true;
  1553. mIs_valid = true;
  1554. mChange_code++;
  1555. sample(0.0f, 0, 0);
  1556. }
  1557. else
  1558. mIs_valid = false;
  1559. }
  1560. void afxObjectConstraint::set_scope_id(U16 scope_id)
  1561. {
  1562. if (mObj)
  1563. clearNotify(mObj);
  1564. mObj = 0;
  1565. mScope_id = scope_id;
  1566. mIs_defined = (scope_id > 0);
  1567. mIs_valid = false;
  1568. mMgr->postMissingConstraintObject(this);
  1569. }
  1570. void afxObjectConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1571. {
  1572. if (mGone_missing)
  1573. return;
  1574. if (mObj)
  1575. {
  1576. if (!mIs_camera && mCons_def.mTreat_as_camera && dynamic_cast<ShapeBase*>(mObj))
  1577. {
  1578. ShapeBase* cam_obj = (ShapeBase*) mObj;
  1579. F32 pov = 1.0f;
  1580. cam_obj->getCameraTransform(&pov, &mLast_xfm);
  1581. mLast_xfm.getColumn(3, &mLast_pos);
  1582. }
  1583. else
  1584. {
  1585. mLast_xfm = mObj->getRenderTransform();
  1586. if (mCons_def.mPos_at_box_center)
  1587. mLast_pos = mObj->getBoxCenter();
  1588. else
  1589. mLast_pos = mObj->getRenderPosition();
  1590. }
  1591. }
  1592. }
  1593. void afxObjectConstraint::restoreObject(SceneObject* obj)
  1594. {
  1595. if (mObj)
  1596. {
  1597. mScope_id = 0;
  1598. clearNotify(mObj);
  1599. }
  1600. mObj = obj;
  1601. if (mObj)
  1602. {
  1603. deleteNotify(mObj);
  1604. mScope_id = mObj->getScopeId();
  1605. }
  1606. mIs_valid = (mObj != NULL);
  1607. }
  1608. void afxObjectConstraint::onDeleteNotify(SimObject* obj)
  1609. {
  1610. if (mObj == dynamic_cast<SceneObject*>(obj))
  1611. {
  1612. mObj = 0;
  1613. mIs_valid = false;
  1614. if (mScope_id > 0)
  1615. mMgr->postMissingConstraintObject(this, true);
  1616. }
  1617. Parent::onDeleteNotify(obj);
  1618. }
  1619. U32 afxObjectConstraint::getTriggers()
  1620. {
  1621. TSStatic* ts_static = dynamic_cast<TSStatic*>(mObj);
  1622. if (ts_static)
  1623. {
  1624. TSShapeInstance* obj_inst = ts_static->getShapeInstance();
  1625. return (obj_inst) ? obj_inst->getTriggerStateMask() : 0;
  1626. }
  1627. ShapeBase* shape_base = dynamic_cast<ShapeBase*>(mObj);
  1628. if (shape_base)
  1629. {
  1630. TSShapeInstance* obj_inst = shape_base->getShapeInstance();
  1631. return (obj_inst) ? obj_inst->getTriggerStateMask() : 0;
  1632. }
  1633. return 0;
  1634. }
  1635. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1636. // afxEffectConstraint
  1637. afxEffectConstraint::afxEffectConstraint(afxConstraintMgr* mgr)
  1638. : afxConstraint(mgr)
  1639. {
  1640. mEffect_name = ST_NULLSTRING;
  1641. mEffect = 0;
  1642. mClip_tag = 0;
  1643. mIs_death_clip = false;
  1644. mLock_tag = 0;
  1645. }
  1646. afxEffectConstraint::afxEffectConstraint(afxConstraintMgr* mgr, StringTableEntry effect_name)
  1647. : afxConstraint(mgr)
  1648. {
  1649. mEffect_name = effect_name;
  1650. mEffect = 0;
  1651. mClip_tag = 0;
  1652. mIs_death_clip = false;
  1653. mLock_tag = 0;
  1654. }
  1655. afxEffectConstraint::~afxEffectConstraint()
  1656. {
  1657. }
  1658. bool afxEffectConstraint::getPosition(Point3F& pos, F32 hist)
  1659. {
  1660. if (!mEffect || !mEffect->inScope())
  1661. return false;
  1662. if (mCons_def.mPos_at_box_center)
  1663. mEffect->getUpdatedBoxCenter(pos);
  1664. else
  1665. mEffect->getUpdatedPosition(pos);
  1666. return true;
  1667. }
  1668. bool afxEffectConstraint::getTransform(MatrixF& xfm, F32 hist)
  1669. {
  1670. if (!mEffect || !mEffect->inScope())
  1671. return false;
  1672. mEffect->getUpdatedTransform(xfm);
  1673. return true;
  1674. }
  1675. bool afxEffectConstraint::getAltitudes(F32& terrain_alt, F32& interior_alt)
  1676. {
  1677. if (!mEffect)
  1678. return false;
  1679. mEffect->getAltitudes(terrain_alt, interior_alt);
  1680. return true;
  1681. }
  1682. void afxEffectConstraint::set(afxEffectWrapper* effect)
  1683. {
  1684. mEffect = effect;
  1685. if (mEffect != NULL)
  1686. {
  1687. mIs_defined = true;
  1688. mIs_valid = true;
  1689. mChange_code++;
  1690. }
  1691. else
  1692. mIs_valid = false;
  1693. }
  1694. U32 afxEffectConstraint::setAnimClip(const char* clip, F32 pos, F32 rate, F32 trans, bool is_death_anim)
  1695. {
  1696. return (mEffect) ? mEffect->setAnimClip(clip, pos, rate, trans) : 0;
  1697. }
  1698. void afxEffectConstraint::resetAnimation(U32 tag)
  1699. {
  1700. if (mEffect)
  1701. mEffect->resetAnimation(tag);
  1702. }
  1703. F32 afxEffectConstraint::getAnimClipDuration(const char* clip)
  1704. {
  1705. return (mEffect) ? getAnimClipDuration(clip) : 0;
  1706. }
  1707. U32 afxEffectConstraint::getTriggers()
  1708. {
  1709. return (mEffect) ? mEffect->getTriggers() : 0;
  1710. }
  1711. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1712. // afxEffectNodeConstraint
  1713. afxEffectNodeConstraint::afxEffectNodeConstraint(afxConstraintMgr* mgr)
  1714. : afxEffectConstraint(mgr)
  1715. {
  1716. mEffect_node = ST_NULLSTRING;
  1717. mEffect_node_ID = -1;
  1718. }
  1719. afxEffectNodeConstraint::afxEffectNodeConstraint(afxConstraintMgr* mgr, StringTableEntry name, StringTableEntry node)
  1720. : afxEffectConstraint(mgr, name)
  1721. {
  1722. mEffect_node = node;
  1723. mEffect_node_ID = -1;
  1724. }
  1725. bool afxEffectNodeConstraint::getPosition(Point3F& pos, F32 hist)
  1726. {
  1727. if (!mEffect || !mEffect->inScope())
  1728. return false;
  1729. TSShapeInstance* ts_shape_inst = mEffect->getTSShapeInstance();
  1730. if (!ts_shape_inst)
  1731. return false;
  1732. if (mEffect_node_ID == -1)
  1733. {
  1734. TSShape* ts_shape = mEffect->getTSShape();
  1735. mEffect_node_ID = (ts_shape) ? ts_shape->findNode(mEffect_node) : -1;
  1736. }
  1737. if (mEffect_node_ID == -1)
  1738. return false;
  1739. mEffect->getUpdatedTransform(mLast_xfm);
  1740. Point3F scale;
  1741. mEffect->getUpdatedScale(scale);
  1742. MatrixF gag = ts_shape_inst->mNodeTransforms[mEffect_node_ID];
  1743. gag.setPosition( gag.getPosition()*scale );
  1744. MatrixF xfm;
  1745. xfm.mul(mLast_xfm, gag);
  1746. //
  1747. pos = xfm.getPosition();
  1748. return true;
  1749. }
  1750. bool afxEffectNodeConstraint::getTransform(MatrixF& xfm, F32 hist)
  1751. {
  1752. if (!mEffect || !mEffect->inScope())
  1753. return false;
  1754. TSShapeInstance* ts_shape_inst = mEffect->getTSShapeInstance();
  1755. if (!ts_shape_inst)
  1756. return false;
  1757. if (mEffect_node_ID == -1)
  1758. {
  1759. TSShape* ts_shape = mEffect->getTSShape();
  1760. mEffect_node_ID = (ts_shape) ? ts_shape->findNode(mEffect_node) : -1;
  1761. }
  1762. if (mEffect_node_ID == -1)
  1763. return false;
  1764. mEffect->getUpdatedTransform(mLast_xfm);
  1765. Point3F scale;
  1766. mEffect->getUpdatedScale(scale);
  1767. MatrixF gag = ts_shape_inst->mNodeTransforms[mEffect_node_ID];
  1768. gag.setPosition( gag.getPosition()*scale );
  1769. xfm.mul(mLast_xfm, gag);
  1770. return true;
  1771. }
  1772. void afxEffectNodeConstraint::set(afxEffectWrapper* effect)
  1773. {
  1774. if (effect)
  1775. {
  1776. TSShape* ts_shape = effect->getTSShape();
  1777. mEffect_node_ID = (ts_shape) ? ts_shape->findNode(mEffect_node) : -1;
  1778. }
  1779. else
  1780. mEffect_node_ID = -1;
  1781. Parent::set(effect);
  1782. }
  1783. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1784. // afxSampleBuffer
  1785. afxSampleBuffer::afxSampleBuffer()
  1786. {
  1787. mBuffer_sz = 0;
  1788. mBuffer_ms = 0;
  1789. mMS_per_sample = 33;
  1790. mElapsed_ms = 0;
  1791. mLast_sample_ms = 0;
  1792. mNext_sample_num = 0;
  1793. mNum_samples = 0;
  1794. }
  1795. afxSampleBuffer::~afxSampleBuffer()
  1796. {
  1797. }
  1798. void afxSampleBuffer::configHistory(F32 hist_len, U8 sample_rate)
  1799. {
  1800. mBuffer_sz = mCeil(hist_len*sample_rate) + 1;
  1801. mMS_per_sample = mCeil(1000.0f/sample_rate);
  1802. mBuffer_ms = mBuffer_sz*mMS_per_sample;
  1803. }
  1804. void afxSampleBuffer::recordSample(F32 dt, U32 elapsed_ms, void* data)
  1805. {
  1806. mElapsed_ms = elapsed_ms;
  1807. if (!data)
  1808. return;
  1809. U32 now_sample_num = elapsed_ms/mMS_per_sample;
  1810. if (mNext_sample_num <= now_sample_num)
  1811. {
  1812. mLast_sample_ms = elapsed_ms;
  1813. while (mNext_sample_num <= now_sample_num)
  1814. {
  1815. recSample(mNext_sample_num % mBuffer_sz, data);
  1816. mNext_sample_num++;
  1817. mNum_samples++;
  1818. }
  1819. }
  1820. }
  1821. inline bool afxSampleBuffer::compute_idx_from_lag(F32 lag, U32& idx)
  1822. {
  1823. bool in_bounds = true;
  1824. U32 lag_ms = lag*1000.0f;
  1825. U32 rec_ms = (mElapsed_ms < mBuffer_ms) ? mElapsed_ms : mBuffer_ms;
  1826. if (lag_ms > rec_ms)
  1827. {
  1828. // hasn't produced enough history
  1829. lag_ms = rec_ms;
  1830. in_bounds = false;
  1831. }
  1832. U32 latest_sample_num = mLast_sample_ms/mMS_per_sample;
  1833. U32 then_sample_num = (mElapsed_ms - lag_ms)/mMS_per_sample;
  1834. if (then_sample_num > latest_sample_num)
  1835. {
  1836. // latest sample is older than lag
  1837. then_sample_num = latest_sample_num;
  1838. in_bounds = false;
  1839. }
  1840. idx = then_sample_num % mBuffer_sz;
  1841. return in_bounds;
  1842. }
  1843. inline bool afxSampleBuffer::compute_idx_from_lag(F32 lag, U32& idx1, U32& idx2, F32& t)
  1844. {
  1845. bool in_bounds = true;
  1846. F32 lag_ms = lag*1000.0f;
  1847. F32 rec_ms = (mElapsed_ms < mBuffer_ms) ? mElapsed_ms : mBuffer_ms;
  1848. if (lag_ms > rec_ms)
  1849. {
  1850. // hasn't produced enough history
  1851. lag_ms = rec_ms;
  1852. in_bounds = false;
  1853. }
  1854. F32 per_samp = mMS_per_sample;
  1855. F32 latest_sample_num = mLast_sample_ms/per_samp;
  1856. F32 then_sample_num = (mElapsed_ms - lag_ms)/per_samp;
  1857. U32 latest_sample_num_i = latest_sample_num;
  1858. U32 then_sample_num_i = then_sample_num;
  1859. if (then_sample_num_i >= latest_sample_num_i)
  1860. {
  1861. if (latest_sample_num_i < then_sample_num_i)
  1862. in_bounds = false;
  1863. t = 0.0;
  1864. idx1 = then_sample_num_i % mBuffer_sz;
  1865. idx2 = idx1;
  1866. }
  1867. else
  1868. {
  1869. t = then_sample_num - then_sample_num_i;
  1870. idx1 = then_sample_num_i % mBuffer_sz;
  1871. idx2 = (then_sample_num_i+1) % mBuffer_sz;
  1872. }
  1873. return in_bounds;
  1874. }
  1875. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1876. // afxSampleXfmBuffer
  1877. afxSampleXfmBuffer::afxSampleXfmBuffer()
  1878. {
  1879. mXfm_buffer = 0;
  1880. }
  1881. afxSampleXfmBuffer::~afxSampleXfmBuffer()
  1882. {
  1883. delete [] mXfm_buffer;
  1884. }
  1885. void afxSampleXfmBuffer::configHistory(F32 hist_len, U8 sample_rate)
  1886. {
  1887. if (!mXfm_buffer)
  1888. {
  1889. afxSampleBuffer::configHistory(hist_len, sample_rate);
  1890. if (mBuffer_sz > 0)
  1891. mXfm_buffer = new MatrixF[mBuffer_sz];
  1892. }
  1893. }
  1894. void afxSampleXfmBuffer::recSample(U32 idx, void* data)
  1895. {
  1896. mXfm_buffer[idx] = *((MatrixF*)data);
  1897. }
  1898. void afxSampleXfmBuffer::getSample(F32 lag, void* data, bool& in_bounds)
  1899. {
  1900. U32 idx1, idx2;
  1901. F32 t;
  1902. in_bounds = compute_idx_from_lag(lag, idx1, idx2, t);
  1903. if (idx1 == idx2)
  1904. {
  1905. MatrixF* m1 = &mXfm_buffer[idx1];
  1906. *((MatrixF*)data) = *m1;
  1907. }
  1908. else
  1909. {
  1910. MatrixF* m1 = &mXfm_buffer[idx1];
  1911. MatrixF* m2 = &mXfm_buffer[idx2];
  1912. Point3F p1 = m1->getPosition();
  1913. Point3F p2 = m2->getPosition();
  1914. Point3F p; p.interpolate(p1, p2, t);
  1915. if (t < 0.5f)
  1916. *((MatrixF*)data) = *m1;
  1917. else
  1918. *((MatrixF*)data) = *m2;
  1919. ((MatrixF*)data)->setPosition(p);
  1920. }
  1921. }
  1922. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1923. // afxPointHistConstraint
  1924. afxPointHistConstraint::afxPointHistConstraint(afxConstraintMgr* mgr)
  1925. : afxPointConstraint(mgr)
  1926. {
  1927. mSamples = 0;
  1928. }
  1929. afxPointHistConstraint::~afxPointHistConstraint()
  1930. {
  1931. delete mSamples;
  1932. }
  1933. void afxPointHistConstraint::set(Point3F point, Point3F vector)
  1934. {
  1935. if (!mSamples)
  1936. {
  1937. mSamples = new afxSampleXfmBuffer;
  1938. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  1939. }
  1940. Parent::set(point, vector);
  1941. }
  1942. void afxPointHistConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1943. {
  1944. Parent::sample(dt, elapsed_ms, cam_pos);
  1945. if (isDefined())
  1946. {
  1947. if (isValid())
  1948. mSamples->recordSample(dt, elapsed_ms, &mLast_xfm);
  1949. else
  1950. mSamples->recordSample(dt, elapsed_ms, 0);
  1951. }
  1952. }
  1953. bool afxPointHistConstraint::getPosition(Point3F& pos, F32 hist)
  1954. {
  1955. bool in_bounds;
  1956. MatrixF xfm;
  1957. mSamples->getSample(hist, &xfm, in_bounds);
  1958. pos = xfm.getPosition();
  1959. return in_bounds;
  1960. }
  1961. bool afxPointHistConstraint::getTransform(MatrixF& xfm, F32 hist)
  1962. {
  1963. bool in_bounds;
  1964. mSamples->getSample(hist, &xfm, in_bounds);
  1965. return in_bounds;
  1966. }
  1967. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  1968. // afxPointHistConstraint
  1969. afxTransformHistConstraint::afxTransformHistConstraint(afxConstraintMgr* mgr)
  1970. : afxTransformConstraint(mgr)
  1971. {
  1972. mSamples = 0;
  1973. }
  1974. afxTransformHistConstraint::~afxTransformHistConstraint()
  1975. {
  1976. delete mSamples;
  1977. }
  1978. void afxTransformHistConstraint::set(const MatrixF& xfm)
  1979. {
  1980. if (!mSamples)
  1981. {
  1982. mSamples = new afxSampleXfmBuffer;
  1983. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  1984. }
  1985. Parent::set(xfm);
  1986. }
  1987. void afxTransformHistConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  1988. {
  1989. Parent::sample(dt, elapsed_ms, cam_pos);
  1990. if (isDefined())
  1991. {
  1992. if (isValid())
  1993. mSamples->recordSample(dt, elapsed_ms, &mLast_xfm);
  1994. else
  1995. mSamples->recordSample(dt, elapsed_ms, 0);
  1996. }
  1997. }
  1998. bool afxTransformHistConstraint::getPosition(Point3F& pos, F32 hist)
  1999. {
  2000. bool in_bounds;
  2001. MatrixF xfm;
  2002. mSamples->getSample(hist, &xfm, in_bounds);
  2003. pos = xfm.getPosition();
  2004. return in_bounds;
  2005. }
  2006. bool afxTransformHistConstraint::getTransform(MatrixF& xfm, F32 hist)
  2007. {
  2008. bool in_bounds;
  2009. mSamples->getSample(hist, &xfm, in_bounds);
  2010. return in_bounds;
  2011. }
  2012. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2013. // afxShapeHistConstraint
  2014. afxShapeHistConstraint::afxShapeHistConstraint(afxConstraintMgr* mgr)
  2015. : afxShapeConstraint(mgr)
  2016. {
  2017. mSamples = 0;
  2018. }
  2019. afxShapeHistConstraint::afxShapeHistConstraint(afxConstraintMgr* mgr, StringTableEntry arb_name)
  2020. : afxShapeConstraint(mgr, arb_name)
  2021. {
  2022. mSamples = 0;
  2023. }
  2024. afxShapeHistConstraint::~afxShapeHistConstraint()
  2025. {
  2026. delete mSamples;
  2027. }
  2028. void afxShapeHistConstraint::set(ShapeBase* shape)
  2029. {
  2030. if (shape && !mSamples)
  2031. {
  2032. mSamples = new afxSampleXfmBuffer;
  2033. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  2034. }
  2035. Parent::set(shape);
  2036. }
  2037. void afxShapeHistConstraint::set_scope_id(U16 scope_id)
  2038. {
  2039. if (scope_id > 0 && !mSamples)
  2040. {
  2041. mSamples = new afxSampleXfmBuffer;
  2042. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  2043. }
  2044. Parent::set_scope_id(scope_id);
  2045. }
  2046. void afxShapeHistConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  2047. {
  2048. Parent::sample(dt, elapsed_ms, cam_pos);
  2049. if (isDefined())
  2050. {
  2051. if (isValid())
  2052. mSamples->recordSample(dt, elapsed_ms, &mLast_xfm);
  2053. else
  2054. mSamples->recordSample(dt, elapsed_ms, 0);
  2055. }
  2056. }
  2057. bool afxShapeHistConstraint::getPosition(Point3F& pos, F32 hist)
  2058. {
  2059. bool in_bounds;
  2060. MatrixF xfm;
  2061. mSamples->getSample(hist, &xfm, in_bounds);
  2062. pos = xfm.getPosition();
  2063. return in_bounds;
  2064. }
  2065. bool afxShapeHistConstraint::getTransform(MatrixF& xfm, F32 hist)
  2066. {
  2067. bool in_bounds;
  2068. mSamples->getSample(hist, &xfm, in_bounds);
  2069. return in_bounds;
  2070. }
  2071. void afxShapeHistConstraint::onDeleteNotify(SimObject* obj)
  2072. {
  2073. Parent::onDeleteNotify(obj);
  2074. }
  2075. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2076. // afxShapeNodeHistConstraint
  2077. afxShapeNodeHistConstraint::afxShapeNodeHistConstraint(afxConstraintMgr* mgr)
  2078. : afxShapeNodeConstraint(mgr)
  2079. {
  2080. mSamples = 0;
  2081. }
  2082. afxShapeNodeHistConstraint::afxShapeNodeHistConstraint(afxConstraintMgr* mgr, StringTableEntry arb_name,
  2083. StringTableEntry arb_node)
  2084. : afxShapeNodeConstraint(mgr, arb_name, arb_node)
  2085. {
  2086. mSamples = 0;
  2087. }
  2088. afxShapeNodeHistConstraint::~afxShapeNodeHistConstraint()
  2089. {
  2090. delete mSamples;
  2091. }
  2092. void afxShapeNodeHistConstraint::set(ShapeBase* shape)
  2093. {
  2094. if (shape && !mSamples)
  2095. {
  2096. mSamples = new afxSampleXfmBuffer;
  2097. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  2098. }
  2099. Parent::set(shape);
  2100. }
  2101. void afxShapeNodeHistConstraint::set_scope_id(U16 scope_id)
  2102. {
  2103. if (scope_id > 0 && !mSamples)
  2104. {
  2105. mSamples = new afxSampleXfmBuffer;
  2106. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  2107. }
  2108. Parent::set_scope_id(scope_id);
  2109. }
  2110. void afxShapeNodeHistConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  2111. {
  2112. Parent::sample(dt, elapsed_ms, cam_pos);
  2113. if (isDefined())
  2114. {
  2115. if (isValid())
  2116. mSamples->recordSample(dt, elapsed_ms, &mLast_xfm);
  2117. else
  2118. mSamples->recordSample(dt, elapsed_ms, 0);
  2119. }
  2120. }
  2121. bool afxShapeNodeHistConstraint::getPosition(Point3F& pos, F32 hist)
  2122. {
  2123. bool in_bounds;
  2124. MatrixF xfm;
  2125. mSamples->getSample(hist, &xfm, in_bounds);
  2126. pos = xfm.getPosition();
  2127. return in_bounds;
  2128. }
  2129. bool afxShapeNodeHistConstraint::getTransform(MatrixF& xfm, F32 hist)
  2130. {
  2131. bool in_bounds;
  2132. mSamples->getSample(hist, &xfm, in_bounds);
  2133. return in_bounds;
  2134. }
  2135. void afxShapeNodeHistConstraint::onDeleteNotify(SimObject* obj)
  2136. {
  2137. Parent::onDeleteNotify(obj);
  2138. }
  2139. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2140. // afxObjectHistConstraint
  2141. afxObjectHistConstraint::afxObjectHistConstraint(afxConstraintMgr* mgr)
  2142. : afxObjectConstraint(mgr)
  2143. {
  2144. mSamples = 0;
  2145. }
  2146. afxObjectHistConstraint::afxObjectHistConstraint(afxConstraintMgr* mgr, StringTableEntry arb_name)
  2147. : afxObjectConstraint(mgr, arb_name)
  2148. {
  2149. mSamples = 0;
  2150. }
  2151. afxObjectHistConstraint::~afxObjectHistConstraint()
  2152. {
  2153. delete mSamples;
  2154. }
  2155. void afxObjectHistConstraint::set(SceneObject* obj)
  2156. {
  2157. if (obj && !mSamples)
  2158. {
  2159. mSamples = new afxSampleXfmBuffer;
  2160. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  2161. }
  2162. Parent::set(obj);
  2163. }
  2164. void afxObjectHistConstraint::set_scope_id(U16 scope_id)
  2165. {
  2166. if (scope_id > 0 && !mSamples)
  2167. {
  2168. mSamples = new afxSampleXfmBuffer;
  2169. mSamples->configHistory(mCons_def.mHistory_time, mCons_def.mSample_rate);
  2170. }
  2171. Parent::set_scope_id(scope_id);
  2172. }
  2173. void afxObjectHistConstraint::sample(F32 dt, U32 elapsed_ms, const Point3F* cam_pos)
  2174. {
  2175. Parent::sample(dt, elapsed_ms, cam_pos);
  2176. if (isDefined())
  2177. {
  2178. if (isValid())
  2179. mSamples->recordSample(dt, elapsed_ms, &mLast_xfm);
  2180. else
  2181. mSamples->recordSample(dt, elapsed_ms, 0);
  2182. }
  2183. }
  2184. bool afxObjectHistConstraint::getPosition(Point3F& pos, F32 hist)
  2185. {
  2186. bool in_bounds;
  2187. MatrixF xfm;
  2188. mSamples->getSample(hist, &xfm, in_bounds);
  2189. pos = xfm.getPosition();
  2190. return in_bounds;
  2191. }
  2192. bool afxObjectHistConstraint::getTransform(MatrixF& xfm, F32 hist)
  2193. {
  2194. bool in_bounds;
  2195. mSamples->getSample(hist, &xfm, in_bounds);
  2196. return in_bounds;
  2197. }
  2198. void afxObjectHistConstraint::onDeleteNotify(SimObject* obj)
  2199. {
  2200. Parent::onDeleteNotify(obj);
  2201. }
  2202. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//