shapeImage.cpp 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/shapeBase.h"
  24. #include "core/resourceManager.h"
  25. #include "core/stream/bitStream.h"
  26. #include "ts/tsShapeInstance.h"
  27. #include "console/consoleInternal.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/engineAPI.h"
  30. #include "lighting/lightInfo.h"
  31. #include "lighting/lightManager.h"
  32. #include "T3D/fx/particleEmitter.h"
  33. #include "T3D/projectile.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "math/mathIO.h"
  36. #include "T3D/debris.h"
  37. #include "math/mathUtils.h"
  38. #include "sim/netObject.h"
  39. #include "sfx/sfxTrack.h"
  40. #include "sfx/sfxSource.h"
  41. #include "sfx/sfxSystem.h"
  42. #include "sfx/sfxTypes.h"
  43. #include "scene/sceneManager.h"
  44. #include "core/stream/fileStream.h"
  45. #include "T3D/fx/cameraFXMgr.h"
  46. //----------------------------------------------------------------------------
  47. ShapeBaseImageData* InvalidImagePtr = (ShapeBaseImageData*) 1;
  48. ImplementEnumType( ShapeBaseImageLoadedState,
  49. "@brief The loaded state of this ShapeBaseImage.\n\n"
  50. "@ingroup gameObjects\n\n")
  51. { ShapeBaseImageData::StateData::IgnoreLoaded, "Ignore", "Ignore the loaded state.\n" },
  52. { ShapeBaseImageData::StateData::Loaded, "Loaded", "ShapeBaseImage is loaded.\n" },
  53. { ShapeBaseImageData::StateData::NotLoaded, "Empty", "ShapeBaseImage is not loaded.\n" },
  54. EndImplementEnumType;
  55. ImplementEnumType( ShapeBaseImageSpinState,
  56. "@brief How the spin animation should be played.\n\n"
  57. "@ingroup gameObjects\n\n")
  58. { ShapeBaseImageData::StateData::IgnoreSpin,"Ignore", "No changes to the spin sequence.\n" },
  59. { ShapeBaseImageData::StateData::NoSpin, "Stop", "Stops the spin sequence at its current position\n" },
  60. { ShapeBaseImageData::StateData::SpinUp, "SpinUp", "Increase spin sequence timeScale from 0 (on state entry) to 1 (after stateTimeoutValue seconds).\n" },
  61. { ShapeBaseImageData::StateData::SpinDown, "SpinDown", "Decrease spin sequence timeScale from 1 (on state entry) to 0 (after stateTimeoutValue seconds).\n" },
  62. { ShapeBaseImageData::StateData::FullSpin, "FullSpeed", "Resume the spin sequence playback at its current position with timeScale = 1.\n"},
  63. EndImplementEnumType;
  64. ImplementEnumType( ShapeBaseImageRecoilState,
  65. "@brief What kind of recoil this ShapeBaseImage should emit when fired.\n\n"
  66. "@ingroup gameObjects\n\n")
  67. { ShapeBaseImageData::StateData::NoRecoil, "NoRecoil", "No recoil occurs.\n" },
  68. { ShapeBaseImageData::StateData::LightRecoil, "LightRecoil", "A light recoil occurs.\n" },
  69. { ShapeBaseImageData::StateData::MediumRecoil, "MediumRecoil", "A medium recoil occurs.\n" },
  70. { ShapeBaseImageData::StateData::HeavyRecoil, "HeavyRecoil", "A heavy recoil occurs.\n" },
  71. EndImplementEnumType;
  72. ImplementEnumType( ShapeBaseImageLightType,
  73. "@brief The type of light to attach to this ShapeBaseImage.\n\n"
  74. "@ingroup gameObjects\n\n")
  75. { ShapeBaseImageData::NoLight, "NoLight", "No light is attached.\n" },
  76. { ShapeBaseImageData::ConstantLight, "ConstantLight", "A constant emitting light is attached.\n" },
  77. { ShapeBaseImageData::SpotLight, "SpotLight", "A spotlight is attached.\n" },
  78. { ShapeBaseImageData::PulsingLight, "PulsingLight", "A pusling light is attached.\n" },
  79. { ShapeBaseImageData::WeaponFireLight, "WeaponFireLight", "Light emits when the weapon is fired, then dissipates.\n" }
  80. EndImplementEnumType;
  81. //----------------------------------------------------------------------------
  82. //----------------------------------------------------------------------------
  83. IMPLEMENT_CO_DATABLOCK_V1(ShapeBaseImageData);
  84. ConsoleDocClass( ShapeBaseImageData,
  85. "@brief Represents geometry to be mounted to a ShapeBase object.\n\n"
  86. "@ingroup gameObjects\n"
  87. );
  88. IMPLEMENT_CALLBACK( ShapeBaseImageData, onMount, void, ( SceneObject* obj, S32 slot, F32 dt ), ( obj, slot, dt ),
  89. "@brief Called when the Image is first mounted to the object.\n\n"
  90. "@param obj object that this Image has been mounted to\n"
  91. "@param slot Image mount slot on the object\n"
  92. "@param dt time remaining in this Image update\n" );
  93. IMPLEMENT_CALLBACK( ShapeBaseImageData, onUnmount, void, ( SceneObject* obj, S32 slot, F32 dt ), ( obj, slot, dt ),
  94. "@brief Called when the Image is unmounted from the object.\n\n"
  95. "@param obj object that this Image has been unmounted from\n"
  96. "@param slot Image mount slot on the object\n"
  97. "@param dt time remaining in this Image update\n" );
  98. ShapeBaseImageData::StateData::StateData()
  99. {
  100. name = 0;
  101. transition.loaded[0] = transition.loaded[1] = -1;
  102. transition.ammo[0] = transition.ammo[1] = -1;
  103. transition.target[0] = transition.target[1] = -1;
  104. transition.trigger[0] = transition.trigger[1] = -1;
  105. transition.altTrigger[0] = transition.altTrigger[1] = -1;
  106. transition.wet[0] = transition.wet[1] = -1;
  107. transition.motion[0] = transition.motion[1] = -1;
  108. transition.timeout = -1;
  109. waitForTimeout = true;
  110. timeoutValue = 0;
  111. fire = false;
  112. altFire = false;
  113. reload = false;
  114. energyDrain = 0;
  115. allowImageChange = true;
  116. loaded = IgnoreLoaded;
  117. spin = IgnoreSpin;
  118. recoil = NoRecoil;
  119. sound = NULL;
  120. soundTrack = NULL;
  121. emitter = NULL;
  122. shapeSequence = NULL;
  123. shapeSequenceScale = true;
  124. script = 0;
  125. ignoreLoadedForReady = false;
  126. ejectShell = false;
  127. scaleAnimation = false;
  128. scaleAnimationFP = false;
  129. sequenceTransitionIn = false;
  130. sequenceTransitionOut = false;
  131. sequenceNeverTransition = false;
  132. sequenceTransitionTime = 0;
  133. direction = false;
  134. emitterTime = 0.0f;
  135. for( U32 i=0; i<MaxShapes; ++i)
  136. {
  137. sequence[i] = -1;
  138. sequenceVis[i] = -1;
  139. flashSequence[i] = false;
  140. emitterNode[i] = -1;
  141. }
  142. }
  143. static ShapeBaseImageData::StateData gDefaultStateData;
  144. //----------------------------------------------------------------------------
  145. ShapeBaseImageData::ShapeBaseImageData()
  146. {
  147. emap = false;
  148. mountPoint = 0;
  149. mountOffset.identity();
  150. eyeOffset.identity();
  151. correctMuzzleVector = true;
  152. correctMuzzleVectorTP = true;
  153. firstPerson = true;
  154. useFirstPersonShape = false;
  155. useEyeOffset = false;
  156. useEyeNode = false;
  157. mass = 0;
  158. usesEnergy = false;
  159. minEnergy = 2;
  160. accuFire = false;
  161. projectile = NULL;
  162. cloakable = true;
  163. lightType = ShapeBaseImageData::NoLight;
  164. lightColor.set(1.f,1.f,1.f,1.f);
  165. lightDuration = 1000;
  166. lightRadius = 10.f;
  167. lightBrightness = 1.0f;
  168. imageAnimPrefix = "";
  169. imageAnimPrefixFP = "";
  170. fireState = -1;
  171. altFireState = -1;
  172. reloadState = -1;
  173. computeCRC = false;
  174. animateAllShapes = true;
  175. animateOnServer = false;
  176. scriptAnimTransitionTime = 0.25f;
  177. //
  178. for (S32 i = 0; i < MaxStates; i++) {
  179. stateName[i] = 0;
  180. stateTransitionLoaded[i] = 0;
  181. stateTransitionNotLoaded[i] = 0;
  182. stateTransitionAmmo[i] = 0;
  183. stateTransitionNoAmmo[i] = 0;
  184. stateTransitionTarget[i] = 0;
  185. stateTransitionNoTarget[i] = 0;
  186. stateTransitionWet[i] = 0;
  187. stateTransitionNotWet[i] = 0;
  188. stateTransitionMotion[i] = 0;
  189. stateTransitionNoMotion[i] = 0;
  190. stateTransitionTriggerUp[i] = 0;
  191. stateTransitionTriggerDown[i] = 0;
  192. stateTransitionAltTriggerUp[i] = 0;
  193. stateTransitionAltTriggerDown[i] = 0;
  194. stateTransitionTimeout[i] = 0;
  195. stateTransitionGeneric0In[i] = 0;
  196. stateTransitionGeneric0Out[i] = 0;
  197. stateTransitionGeneric1In[i] = 0;
  198. stateTransitionGeneric1Out[i] = 0;
  199. stateTransitionGeneric2In[i] = 0;
  200. stateTransitionGeneric2Out[i] = 0;
  201. stateTransitionGeneric3In[i] = 0;
  202. stateTransitionGeneric3Out[i] = 0;
  203. stateWaitForTimeout[i] = true;
  204. stateTimeoutValue[i] = 0;
  205. stateFire[i] = false;
  206. stateAlternateFire[i] = false;
  207. stateReload[i] = false;
  208. stateEjectShell[i] = false;
  209. stateEnergyDrain[i] = 0;
  210. stateAllowImageChange[i] = true;
  211. stateScaleAnimation[i] = true;
  212. stateScaleAnimationFP[i] = true;
  213. stateSequenceTransitionIn[i] = false;
  214. stateSequenceTransitionOut[i] = false;
  215. stateSequenceNeverTransition[i] = false;
  216. stateSequenceTransitionTime[i] = 0.25f;
  217. stateDirection[i] = true;
  218. stateLoaded[i] = StateData::IgnoreLoaded;
  219. stateSpin[i] = StateData::IgnoreSpin;
  220. stateRecoil[i] = StateData::NoRecoil;
  221. stateSequence[i] = 0;
  222. stateSequenceRandomFlash[i] = false;
  223. stateShapeSequence[i] = 0;
  224. stateScaleShapeSequence[i] = false;
  225. INIT_SOUNDASSET_ARRAY(stateSound, i);
  226. stateScript[i] = 0;
  227. stateEmitter[i] = 0;
  228. stateEmitterTime[i] = 0;
  229. stateEmitterNode[i] = 0;
  230. stateIgnoreLoadedForReady[i] = false;
  231. }
  232. statesLoaded = false;
  233. maxConcurrentSounds = 0;
  234. useRemainderDT = false;
  235. casing = NULL;
  236. casingID = 0;
  237. shellExitDir.set( 1.0, 0.0, 1.0 );
  238. shellExitDir.normalize();
  239. shellExitVariance = 20.0;
  240. shellVelocity = 1.0;
  241. fireStateName = NULL;
  242. for(U32 i=0; i<MaxShapes; ++i)
  243. {
  244. mCRC[i] = U32_MAX;
  245. mountTransform[i].identity();
  246. retractNode[i] = -1;
  247. muzzleNode[i] = -1;
  248. ejectNode[i] = -1;
  249. emitterNode[i] = -1;
  250. eyeMountNode[i] = -1;
  251. eyeNode[i] = -1;
  252. spinSequence[i] = -1;
  253. ambientSequence[i] = -1;
  254. isAnimated[i] = false;
  255. hasFlash[i] = false;
  256. shapeIsValid[i] = false;
  257. INIT_ASSET_ARRAY(Shape, i);
  258. }
  259. shakeCamera = false;
  260. camShakeFreq = Point3F::Zero;
  261. camShakeAmp = Point3F::Zero;
  262. camShakeDuration = 1.5f;
  263. camShakeRadius = 3.0f;
  264. camShakeFalloff = 10.0f;
  265. }
  266. ShapeBaseImageData::~ShapeBaseImageData()
  267. {
  268. }
  269. bool ShapeBaseImageData::onAdd()
  270. {
  271. if (!Parent::onAdd())
  272. return false;
  273. // Copy state data from the scripting arrays into the
  274. // state structure array. If we have state data already,
  275. // we are on the client and need to leave it alone.
  276. for (U32 i = 0; i < MaxStates; i++) {
  277. StateData& s = state[i];
  278. if (statesLoaded == false) {
  279. s.name = stateName[i];
  280. s.transition.loaded[0] = lookupState(stateTransitionNotLoaded[i]);
  281. s.transition.loaded[1] = lookupState(stateTransitionLoaded[i]);
  282. s.transition.ammo[0] = lookupState(stateTransitionNoAmmo[i]);
  283. s.transition.ammo[1] = lookupState(stateTransitionAmmo[i]);
  284. s.transition.target[0] = lookupState(stateTransitionNoTarget[i]);
  285. s.transition.target[1] = lookupState(stateTransitionTarget[i]);
  286. s.transition.wet[0] = lookupState(stateTransitionNotWet[i]);
  287. s.transition.wet[1] = lookupState(stateTransitionWet[i]);
  288. s.transition.motion[0] = lookupState(stateTransitionNoMotion[i]);
  289. s.transition.motion[1] = lookupState(stateTransitionMotion[i]);
  290. s.transition.trigger[0] = lookupState(stateTransitionTriggerUp[i]);
  291. s.transition.trigger[1] = lookupState(stateTransitionTriggerDown[i]);
  292. s.transition.altTrigger[0] = lookupState(stateTransitionAltTriggerUp[i]);
  293. s.transition.altTrigger[1] = lookupState(stateTransitionAltTriggerDown[i]);
  294. s.transition.timeout = lookupState(stateTransitionTimeout[i]);
  295. s.transition.genericTrigger[0][0] = lookupState(stateTransitionGeneric0Out[i]);
  296. s.transition.genericTrigger[0][1] = lookupState(stateTransitionGeneric0In[i]);
  297. s.transition.genericTrigger[1][0] = lookupState(stateTransitionGeneric1Out[i]);
  298. s.transition.genericTrigger[1][1] = lookupState(stateTransitionGeneric1In[i]);
  299. s.transition.genericTrigger[2][0] = lookupState(stateTransitionGeneric2Out[i]);
  300. s.transition.genericTrigger[2][1] = lookupState(stateTransitionGeneric2In[i]);
  301. s.transition.genericTrigger[3][0] = lookupState(stateTransitionGeneric3Out[i]);
  302. s.transition.genericTrigger[3][1] = lookupState(stateTransitionGeneric3In[i]);
  303. s.waitForTimeout = stateWaitForTimeout[i];
  304. s.timeoutValue = stateTimeoutValue[i];
  305. s.fire = stateFire[i];
  306. s.altFire = stateAlternateFire[i];
  307. s.reload = stateReload[i];
  308. s.ejectShell = stateEjectShell[i];
  309. s.energyDrain = stateEnergyDrain[i];
  310. s.allowImageChange = stateAllowImageChange[i];
  311. s.scaleAnimation = stateScaleAnimation[i];
  312. s.scaleAnimationFP = stateScaleAnimationFP[i];
  313. s.sequenceTransitionIn = stateSequenceTransitionIn[i];
  314. s.sequenceTransitionOut = stateSequenceTransitionOut[i];
  315. s.sequenceNeverTransition = stateSequenceNeverTransition[i];
  316. s.sequenceTransitionTime = stateSequenceTransitionTime[i];
  317. s.direction = stateDirection[i];
  318. s.loaded = stateLoaded[i];
  319. s.spin = stateSpin[i];
  320. s.recoil = stateRecoil[i];
  321. s.shapeSequence = stateShapeSequence[i];
  322. s.shapeSequenceScale = stateScaleShapeSequence[i];
  323. //_setstateSound(getstateSound(i),i);
  324. handleStateSoundTrack(i);
  325. s.script = stateScript[i];
  326. s.emitter = stateEmitter[i];
  327. s.emitterTime = stateEmitterTime[i];
  328. // Resolved at load time
  329. for( U32 j=0; j<MaxShapes; ++j)
  330. {
  331. s.sequence[j] = -1; // Sequence is resolved in load
  332. s.sequenceVis[j] = -1; // Vis Sequence is resolved in load
  333. s.emitterNode[j] = -1; // Sequnce is resolved in load
  334. }
  335. }
  336. // The first state marked as "fire" is the state entered on the
  337. // client when it recieves a fire event.
  338. if (s.fire && fireState == -1)
  339. fireState = i;
  340. // The first state marked as "alternateFire" is the state entered on the
  341. // client when it recieves an alternate fire event.
  342. if (s.altFire && altFireState == -1)
  343. altFireState = i;
  344. // The first state marked as "reload" is the state entered on the
  345. // client when it recieves a reload event.
  346. if (s.reload && reloadState == -1)
  347. reloadState = i;
  348. }
  349. // Always preload images, this is needed to avoid problems with
  350. // resolving sequences before transmission to a client.
  351. return true;
  352. }
  353. bool ShapeBaseImageData::preload(bool server, String &errorStr)
  354. {
  355. if (!Parent::preload(server, errorStr))
  356. return false;
  357. // Resolve objects transmitted from server
  358. if (!server) {
  359. if (projectile)
  360. if (Sim::findObject(SimObjectId((uintptr_t)projectile), projectile) == false)
  361. Con::errorf(ConsoleLogEntry::General, "Error, unable to load projectile for shapebaseimagedata");
  362. for (U32 i = 0; i < MaxStates; i++) {
  363. if (state[i].emitter)
  364. if (!Sim::findObject(SimObjectId((uintptr_t)state[i].emitter), state[i].emitter))
  365. Con::errorf(ConsoleLogEntry::General, "Error, unable to load emitter for image datablock");
  366. if (getstateSound(i) != StringTable->EmptyString())
  367. {
  368. _setstateSound(getstateSound(i), i);
  369. if (!getstateSoundProfile(i))
  370. Con::errorf("ShapeBaseImageData::preload() - Could not find profile for asset %s on state %d", getstateSound(i), i);
  371. }
  372. }
  373. }
  374. // Use the first person eye offset if it's set.
  375. useEyeOffset = !eyeOffset.isIdentity();
  376. // Go through each of the shapes
  377. for (U32 i=0; i<MaxShapes; ++i)
  378. {
  379. // Shape 0: Standard image shape
  380. // Shape 1: Optional first person image shape
  381. if (i == FirstPersonImageShape)
  382. {
  383. if ((useEyeOffset || useEyeNode) && !mShapeAsset[i].isNull())
  384. {
  385. // Make use of the first person shape
  386. useFirstPersonShape = true;
  387. }
  388. else
  389. {
  390. // Skip the first person shape
  391. continue;
  392. }
  393. }
  394. if (!mShapeAsset[i].isNull())
  395. {
  396. // Resolve shapename
  397. mShape[i] = mShapeAsset[i]->getShapeResource();
  398. if (!bool(mShape[i])) {
  399. errorStr = String::ToString("Unable to load shape asset: %s", mShapeAsset[i]->getAssetId());
  400. return false;
  401. }
  402. if(computeCRC)
  403. {
  404. Con::printf("Validation required for shape asset: %s", mShapeAsset[i]->getAssetId());
  405. Torque::FS::FileNodeRef fileRef = Torque::FS::GetFileNode(mShape[i].getPath());
  406. if (!fileRef)
  407. {
  408. errorStr = String::ToString("ShapeBaseImageData: Couldn't load shape asset\"%s\"", mShapeAsset[i]->getAssetId());
  409. return false;
  410. }
  411. if(server)
  412. {
  413. mCRC[i] = fileRef->getChecksum();
  414. }
  415. else if(mCRC[i] != fileRef->getChecksum())
  416. {
  417. errorStr = String::ToString("Shape asset\"%s\" does not match version on server.", mShapeAsset[i]->getAssetId());
  418. return false;
  419. }
  420. }
  421. // Resolve nodes & build mount transform
  422. eyeMountNode[i] = mShape[i]->findNode("eyeMount");
  423. eyeNode[i] = mShape[i]->findNode("eye");
  424. if (eyeNode[i] == -1)
  425. eyeNode[i] = eyeMountNode[i];
  426. ejectNode[i] = mShape[i]->findNode("ejectPoint");
  427. muzzleNode[i] = mShape[i]->findNode("muzzlePoint");
  428. retractNode[i] = mShape[i]->findNode("retractionPoint");
  429. mountTransform[i] = mountOffset;
  430. S32 node = mShape[i]->findNode("mountPoint");
  431. if (node != -1) {
  432. MatrixF total(1);
  433. do {
  434. MatrixF nmat;
  435. QuatF q;
  436. TSTransform::setMatrix(mShape[i]->defaultRotations[node].getQuatF(&q), mShape[i]->defaultTranslations[node],&nmat);
  437. total.mul(nmat);
  438. node = mShape[i]->nodes[node].parentIndex;
  439. }
  440. while(node != -1);
  441. total.inverse();
  442. mountTransform[i].mul(total);
  443. }
  444. // Resolve state sequence names & emitter nodes
  445. isAnimated[i] = false;
  446. hasFlash[i] = false;
  447. for (U32 j = 0; j < MaxStates; j++) {
  448. StateData& s = state[j];
  449. if (stateSequence[j] && stateSequence[j][0])
  450. s.sequence[i] = mShape[i]->findSequence(stateSequence[j]);
  451. if (s.sequence[i] != -1)
  452. {
  453. // This state has an animation sequence
  454. isAnimated[i] = true;
  455. }
  456. if (stateSequence[j] && stateSequence[j][0] && stateSequenceRandomFlash[j]) {
  457. char bufferVis[128];
  458. dStrncpy(bufferVis, stateSequence[j], 100);
  459. dStrcat(bufferVis, "_vis", 128);
  460. s.sequenceVis[i] = mShape[i]->findSequence(bufferVis);
  461. }
  462. if (s.sequenceVis[i] != -1)
  463. {
  464. // This state has a flash animation sequence
  465. s.flashSequence[i] = true;
  466. hasFlash[i] = true;
  467. }
  468. s.ignoreLoadedForReady = stateIgnoreLoadedForReady[j];
  469. if (stateEmitterNode[j] && stateEmitterNode[j][0])
  470. s.emitterNode[i] = mShape[i]->findNode(stateEmitterNode[j]);
  471. if (s.emitterNode[i] == -1)
  472. s.emitterNode[i] = muzzleNode[i];
  473. }
  474. ambientSequence[i] = mShape[i]->findSequence("ambient");
  475. spinSequence[i] = mShape[i]->findSequence("spin");
  476. shapeIsValid[i] = true;
  477. }
  478. else {
  479. errorStr = "Bad Datablock from server";
  480. return false;
  481. }
  482. }
  483. if( !casing && casingID != 0 )
  484. {
  485. if( !Sim::findObject( SimObjectId( casingID ), casing ) )
  486. {
  487. Con::errorf( ConsoleLogEntry::General, "ShapeBaseImageData::preload: Invalid packet, bad datablockId(casing): 0x%x", casingID );
  488. }
  489. }
  490. // Preload the shapes
  491. for( U32 i=0; i<MaxShapes; ++i)
  492. {
  493. if( shapeIsValid[i] )
  494. {
  495. TSShapeInstance* pDummy = new TSShapeInstance(mShape[i], !server);
  496. delete pDummy;
  497. }
  498. }
  499. return true;
  500. }
  501. void ShapeBaseImageData::handleStateSoundTrack(const U32& stateId)
  502. {
  503. if (stateId > MaxStates)
  504. return;
  505. StateData& s = state[stateId];
  506. s.sound = getstateSoundAsset(stateId);
  507. if (s.sound == NULL)
  508. {
  509. if (mstateSoundName[stateId] != StringTable->EmptyString())
  510. {
  511. //ok, so we've got some sort of special-case here like a fallback or SFXPlaylist. So do the hook-up now
  512. SFXTrack* sndTrack;
  513. if (!Sim::findObject(mstateSoundName[stateId], sndTrack))
  514. {
  515. Con::errorf("ShapeBaseImageData::onAdd() - attempted to find sound %s but failed!", mstateSoundName[stateId]);
  516. }
  517. else
  518. {
  519. s.soundTrack = sndTrack;
  520. }
  521. }
  522. else if (mstateSoundSFXId[stateId] != 0)
  523. {
  524. SFXTrack* sndTrack;
  525. if (!Sim::findObject(mstateSoundSFXId[stateId], sndTrack))
  526. {
  527. Con::errorf("ShapeBaseImageData::onAdd() - attempted to find sound %i but failed!", mstateSoundSFXId[stateId]);
  528. }
  529. else
  530. {
  531. s.soundTrack = sndTrack;
  532. }
  533. }
  534. }
  535. }
  536. S32 ShapeBaseImageData::lookupState(const char* name)
  537. {
  538. if (!name || !name[0])
  539. return -1;
  540. for (U32 i = 0; i < MaxStates; i++)
  541. if (stateName[i] && !dStricmp(name,stateName[i]))
  542. return i;
  543. Con::errorf(ConsoleLogEntry::General,"ShapeBaseImageData:: Could not resolve state \"%s\" for image \"%s\"",name,getName());
  544. return 0;
  545. }
  546. void ShapeBaseImageData::initPersistFields()
  547. {
  548. addField( "emap", TypeBool, Offset(emap, ShapeBaseImageData),
  549. "@brief Whether to enable environment mapping on this Image.\n\n" );
  550. INITPERSISTFIELD_SHAPEASSET_ARRAY(Shape, MaxShapes, ShapeBaseImageData, "The shape asset to use for this image in the third person")
  551. //addProtectedField("shapeFileFP", TypeShapeFilename, Offset(mShapeName[1], ShapeBaseImageData), _setShapeData, defaultProtectedGetFn, "deprecated alias for ShapeFPFile/Asset", AbstractClassRep::FIELD_HideInInspectors);
  552. addField( "imageAnimPrefix", TypeCaseString, Offset(imageAnimPrefix, ShapeBaseImageData),
  553. "@brief Passed along to the mounting shape to modify animation sequences played in third person. [optional]\n\n" );
  554. addField( "imageAnimPrefixFP", TypeCaseString, Offset(imageAnimPrefixFP, ShapeBaseImageData),
  555. "@brief Passed along to the mounting shape to modify animation sequences played in first person. [optional]\n\n" );
  556. addField( "animateAllShapes", TypeBool, Offset(animateAllShapes, ShapeBaseImageData),
  557. "@brief Indicates that all shapes should be animated in sync.\n\n"
  558. "When multiple shapes are defined for this image datablock, each of them are automatically "
  559. "animated in step with each other. This allows for easy switching between between shapes "
  560. "when some other condition changes, such as going from first person to third person, and "
  561. "keeping their look consistent. If you know that you'll never switch between shapes on the "
  562. "fly, such as players only being allowed in a first person view, then you could set this to "
  563. "false to save some calculations.\n\n"
  564. "There are other circumstances internal to the engine that determine that only the current shape "
  565. "should be animated rather than all defined shapes. In those cases, this property is ignored.\n\n"
  566. "@note This property is only important if you have more than one shape defined, such as shapeFileFP.\n\n"
  567. "@see shapeFileFP\n");
  568. addField( "animateOnServer", TypeBool, Offset(animateOnServer, ShapeBaseImageData),
  569. "@brief Indicates that the image should be animated on the server.\n\n"
  570. "In most cases you'll want this set if you're using useEyeNode. You may also want to "
  571. "set this if the muzzlePoint is animated while it shoots. You can set this "
  572. "to false even if these previous cases are true if the image's shape is set "
  573. "up in the correct position and orientation in the 'root' pose and none of "
  574. "the nodes are animated at key times, such as the muzzlePoint essentially "
  575. "remaining at the same position at the start of the fire state (it could "
  576. "animate just fine after the projectile is away as the muzzle vector is only "
  577. "calculated at the start of the state).\n\n"
  578. "You'll also want to set this to true if you're animating the camera using the "
  579. "image's 'eye' node -- unless the movement is very subtle and doesn't need to "
  580. "be reflected on the server.\n\n"
  581. "@note Setting this to true causes up to four animation threads to be advanced on the server "
  582. "for each instance in use, although for most images only one or two are actually defined.\n\n"
  583. "@see useEyeNode\n");
  584. addField( "scriptAnimTransitionTime", TypeF32, Offset(scriptAnimTransitionTime, ShapeBaseImageData),
  585. "@brief The amount of time to transition between the previous sequence and new sequence when the script prefix has changed.\n\n"
  586. "When setImageScriptAnimPrefix() is used on a ShapeBase that has this image mounted, the image "
  587. "will attempt to switch to the new animation sequence based on the given script prefix. This is "
  588. "the amount of time it takes to transition from the previously playing animation sequence to"
  589. "the new script prefix-based animation sequence.\n"
  590. "@see ShapeBase::setImageScriptAnimPrefix()");
  591. addField( "projectile", TYPEID< ProjectileData >(), Offset(projectile, ShapeBaseImageData),
  592. "@brief The projectile fired by this Image\n\n" );
  593. addField( "cloakable", TypeBool, Offset(cloakable, ShapeBaseImageData),
  594. "@brief Whether this Image can be cloaked.\n\n"
  595. "Currently unused." );
  596. addField( "mountPoint", TypeS32, Offset(mountPoint, ShapeBaseImageData),
  597. "@brief Mount node # to mount this Image to.\n\n"
  598. "This should correspond to a mount# node on the ShapeBase derived object we are mounting to." );
  599. addField( "offset", TypeMatrixPosition, Offset(mountOffset, ShapeBaseImageData),
  600. "@brief \"X Y Z\" translation offset from this Image's <i>mountPoint</i> node to "
  601. "attach to.\n\n"
  602. "Defaults to \"0 0 0\". ie. attach this Image's "
  603. "<i>mountPoint</i> node to the ShapeBase model's mount# node without any offset.\n"
  604. "@see rotation");
  605. addField( "rotation", TypeMatrixRotation, Offset(mountOffset, ShapeBaseImageData),
  606. "@brief \"X Y Z ANGLE\" rotation offset from this Image's <i>mountPoint</i> node "
  607. "to attach to.\n\n"
  608. "Defaults to \"0 0 0\". ie. attach this Image's "
  609. "<i>mountPoint</i> node to the ShapeBase model's mount# node without any additional rotation.\n"
  610. "@see offset");
  611. addField( "eyeOffset", TypeMatrixPosition, Offset(eyeOffset, ShapeBaseImageData),
  612. "@brief \"X Y Z\" translation offset from the ShapeBase model's eye node.\n\n"
  613. "When in first person view, this is the offset from the eye node to place the gun. This "
  614. "gives the gun a fixed point in space, typical of a lot of FPS games.\n"
  615. "@see eyeRotation");
  616. addField( "eyeRotation", TypeMatrixRotation, Offset(eyeOffset, ShapeBaseImageData),
  617. "@brief \"X Y Z ANGLE\" rotation offset from the ShapeBase model's eye node.\n\n"
  618. "When in first person view, this is the rotation from the eye node to place the gun.\n"
  619. "@see eyeOffset");
  620. addField( "useEyeNode", TypeBool, Offset(useEyeNode, ShapeBaseImageData),
  621. "@brief Mount image using image's eyeMount node and place the camera at the image's eye node (or "
  622. "at the eyeMount node if the eye node is missing).\n\n"
  623. "When in first person view, if an 'eyeMount' node is present in the image's shape, this indicates "
  624. "that the image should mount eyeMount node to Player eye node for image placement. The "
  625. "Player's camera should also mount to the image's eye node to inherit any animation (or the eyeMount "
  626. "node if the image doesn't have an eye node).\n\n"
  627. "@note Used instead of eyeOffset.\n\n"
  628. "@note Read about the animateOnServer field as you may want to set it to true if you're using useEyeNode.\n\n"
  629. "@see eyeOffset\n\n"
  630. "@see animateOnServer\n\n");
  631. addField( "correctMuzzleVector", TypeBool, Offset(correctMuzzleVector, ShapeBaseImageData),
  632. "@brief Flag to adjust the aiming vector to the eye's LOS point when in 1st person view.\n\n"
  633. "@see ShapeBase::getMuzzleVector()" );
  634. addField( "correctMuzzleVectorTP", TypeBool, Offset(correctMuzzleVectorTP, ShapeBaseImageData),
  635. "@brief Flag to adjust the aiming vector to the camera's LOS point when in 3rd person view.\n\n"
  636. "@see ShapeBase::getMuzzleVector()" );
  637. addField( "firstPerson", TypeBool, Offset(firstPerson, ShapeBaseImageData),
  638. "@brief Set to true to render the image in first person." );
  639. addField( "mass", TypeF32, Offset(mass, ShapeBaseImageData),
  640. "@brief Mass of this Image.\n\n"
  641. "This is added to the total mass of the ShapeBase object." );
  642. addField( "usesEnergy", TypeBool, Offset(usesEnergy,ShapeBaseImageData),
  643. "@brief Flag indicating whether this Image uses energy instead of ammo. The energy level comes from the ShapeBase object we're mounted to.\n\n"
  644. "@see ShapeBase::setEnergyLevel()");
  645. addField( "minEnergy", TypeF32, Offset(minEnergy, ShapeBaseImageData),
  646. "@brief Minimum Image energy for it to be operable.\n\n"
  647. "@see usesEnergy");
  648. addField( "accuFire", TypeBool, Offset(accuFire, ShapeBaseImageData),
  649. "@brief Flag to control whether the Image's aim is automatically converged with "
  650. "the crosshair.\n\n"
  651. "Currently unused." );
  652. addField( "lightType", TYPEID< ShapeBaseImageData::LightType >(), Offset(lightType, ShapeBaseImageData),
  653. "@brief The type of light this Image emits.\n\n"
  654. "@see ShapeBaseImageLightType");
  655. addField( "lightColor", TypeColorF, Offset(lightColor, ShapeBaseImageData),
  656. "@brief The color of light this Image emits.\n\n"
  657. "@see lightType");
  658. addField( "lightDuration", TypeS32, Offset(lightDuration, ShapeBaseImageData),
  659. "@brief Duration in SimTime of Pulsing and WeaponFire type lights.\n\n"
  660. "@see lightType");
  661. addField( "lightRadius", TypeF32, Offset(lightRadius, ShapeBaseImageData),
  662. "@brief Radius of the light this Image emits.\n\n"
  663. "@see lightType");
  664. addField( "lightBrightness", TypeF32, Offset(lightBrightness, ShapeBaseImageData),
  665. "@brief Brightness of the light this Image emits.\n\n"
  666. "Only valid for WeaponFireLight."
  667. "@see lightType");
  668. addField( "shakeCamera", TypeBool, Offset(shakeCamera, ShapeBaseImageData),
  669. "@brief Flag indicating whether the camera should shake when this Image fires.\n\n" );
  670. addField( "camShakeFreq", TypePoint3F, Offset(camShakeFreq, ShapeBaseImageData),
  671. "@brief Frequency of the camera shaking effect.\n\n"
  672. "@see shakeCamera" );
  673. addField( "camShakeAmp", TypePoint3F, Offset(camShakeAmp, ShapeBaseImageData),
  674. "@brief Amplitude of the camera shaking effect.\n\n"
  675. "@see shakeCamera" );
  676. addField( "camShakeDuration", TypeF32, Offset(camShakeDuration, ShapeBaseImageData),
  677. "Duration (in seconds) to shake the camera." );
  678. addField( "camShakeRadius", TypeF32, Offset(camShakeRadius, ShapeBaseImageData),
  679. "Radial distance that a camera's position must be within relative to the "
  680. "center of the explosion to be shaken." );
  681. addField( "camShakeFalloff", TypeF32, Offset(camShakeFalloff, ShapeBaseImageData),
  682. "Falloff value for the camera shake." );
  683. addField( "casing", TYPEID< DebrisData >(), Offset(casing, ShapeBaseImageData),
  684. "@brief DebrisData datablock to use for ejected casings.\n\n"
  685. "@see stateEjectShell" );
  686. addField( "shellExitDir", TypePoint3F, Offset(shellExitDir, ShapeBaseImageData),
  687. "@brief Vector direction to eject shell casings.\n\n"
  688. "@see casing");
  689. addField( "shellExitVariance", TypeF32, Offset(shellExitVariance, ShapeBaseImageData),
  690. "@brief Variance (in degrees) from the shellExitDir vector to eject casings.\n\n"
  691. "@see shellExitDir");
  692. addField( "shellVelocity", TypeF32, Offset(shellVelocity, ShapeBaseImageData),
  693. "@brief Speed at which to eject casings.\n\n"
  694. "@see casing");
  695. // State arrays
  696. addArray( "States", MaxStates );
  697. addField( "stateName", TypeCaseString, Offset(stateName, ShapeBaseImageData), MaxStates,
  698. "Name of this state." );
  699. addField( "stateTransitionOnLoaded", TypeString, Offset(stateTransitionLoaded, ShapeBaseImageData), MaxStates,
  700. "Name of the state to transition to when the loaded state of the Image "
  701. "changes to 'Loaded'." );
  702. addField( "stateTransitionOnNotLoaded", TypeString, Offset(stateTransitionNotLoaded, ShapeBaseImageData), MaxStates,
  703. "Name of the state to transition to when the loaded state of the Image "
  704. "changes to 'Empty'." );
  705. addField( "stateTransitionOnAmmo", TypeString, Offset(stateTransitionAmmo, ShapeBaseImageData), MaxStates,
  706. "Name of the state to transition to when the ammo state of the Image "
  707. "changes to true." );
  708. addField( "stateTransitionOnNoAmmo", TypeString, Offset(stateTransitionNoAmmo, ShapeBaseImageData), MaxStates,
  709. "Name of the state to transition to when the ammo state of the Image "
  710. "changes to false." );
  711. addField( "stateTransitionOnTarget", TypeString, Offset(stateTransitionTarget, ShapeBaseImageData), MaxStates,
  712. "Name of the state to transition to when the Image gains a target." );
  713. addField( "stateTransitionOnNoTarget", TypeString, Offset(stateTransitionNoTarget, ShapeBaseImageData), MaxStates,
  714. "Name of the state to transition to when the Image loses a target." );
  715. addField( "stateTransitionOnWet", TypeString, Offset(stateTransitionWet, ShapeBaseImageData), MaxStates,
  716. "Name of the state to transition to when the Image enters the water." );
  717. addField( "stateTransitionOnNotWet", TypeString, Offset(stateTransitionNotWet, ShapeBaseImageData), MaxStates,
  718. "Name of the state to transition to when the Image exits the water." );
  719. addField( "stateTransitionOnMotion", TypeString, Offset(stateTransitionMotion, ShapeBaseImageData), MaxStates,
  720. "Name of the state to transition to when the Player moves." );
  721. addField( "stateTransitionOnNoMotion", TypeString, Offset(stateTransitionNoMotion, ShapeBaseImageData), MaxStates,
  722. "Name of the state to transition to when the Player stops moving." );
  723. addField( "stateTransitionOnTriggerUp", TypeString, Offset(stateTransitionTriggerUp, ShapeBaseImageData), MaxStates,
  724. "Name of the state to transition to when the trigger state of the Image "
  725. "changes to true (fire button down)." );
  726. addField( "stateTransitionOnTriggerDown", TypeString, Offset(stateTransitionTriggerDown, ShapeBaseImageData), MaxStates,
  727. "Name of the state to transition to when the trigger state of the Image "
  728. "changes to false (fire button released)." );
  729. addField( "stateTransitionOnAltTriggerUp", TypeString, Offset(stateTransitionAltTriggerUp, ShapeBaseImageData), MaxStates,
  730. "Name of the state to transition to when the alt trigger state of the "
  731. "Image changes to true (alt fire button down)." );
  732. addField( "stateTransitionOnAltTriggerDown", TypeString, Offset(stateTransitionAltTriggerDown, ShapeBaseImageData), MaxStates,
  733. "Name of the state to transition to when the alt trigger state of the "
  734. "Image changes to false (alt fire button up)." );
  735. addField( "stateTransitionOnTimeout", TypeString, Offset(stateTransitionTimeout, ShapeBaseImageData), MaxStates,
  736. "Name of the state to transition to when we have been in this state "
  737. "for stateTimeoutValue seconds." );
  738. addField( "stateTransitionGeneric0In", TypeString, Offset(stateTransitionGeneric0In, ShapeBaseImageData), MaxStates,
  739. "Name of the state to transition to when the generic trigger 0 state "
  740. "changes to true." );
  741. addField( "stateTransitionGeneric0Out", TypeString, Offset(stateTransitionGeneric0Out, ShapeBaseImageData), MaxStates,
  742. "Name of the state to transition to when the generic trigger 0 state "
  743. "changes to false." );
  744. addField( "stateTransitionGeneric1In", TypeString, Offset(stateTransitionGeneric1In, ShapeBaseImageData), MaxStates,
  745. "Name of the state to transition to when the generic trigger 1 state "
  746. "changes to true." );
  747. addField( "stateTransitionGeneric1Out", TypeString, Offset(stateTransitionGeneric1Out, ShapeBaseImageData), MaxStates,
  748. "Name of the state to transition to when the generic trigger 1 state "
  749. "changes to false." );
  750. addField( "stateTransitionGeneric2In", TypeString, Offset(stateTransitionGeneric2In, ShapeBaseImageData), MaxStates,
  751. "Name of the state to transition to when the generic trigger 2 state "
  752. "changes to true." );
  753. addField( "stateTransitionGeneric2Out", TypeString, Offset(stateTransitionGeneric2Out, ShapeBaseImageData), MaxStates,
  754. "Name of the state to transition to when the generic trigger 2 state "
  755. "changes to false." );
  756. addField( "stateTransitionGeneric3In", TypeString, Offset(stateTransitionGeneric3In, ShapeBaseImageData), MaxStates,
  757. "Name of the state to transition to when the generic trigger 3 state "
  758. "changes to true." );
  759. addField( "stateTransitionGeneric3Out", TypeString, Offset(stateTransitionGeneric3Out, ShapeBaseImageData), MaxStates,
  760. "Name of the state to transition to when the generic trigger 3 state "
  761. "changes to false." );
  762. addField( "stateTimeoutValue", TypeF32, Offset(stateTimeoutValue, ShapeBaseImageData), MaxStates,
  763. "Time in seconds to wait before transitioning to stateTransitionOnTimeout." );
  764. addField( "stateWaitForTimeout", TypeBool, Offset(stateWaitForTimeout, ShapeBaseImageData), MaxStates,
  765. "If false, this state ignores stateTimeoutValue and transitions "
  766. "immediately if other transition conditions are met." );
  767. addField( "stateFire", TypeBool, Offset(stateFire, ShapeBaseImageData), MaxStates,
  768. "The first state with this set to true is the state entered by the "
  769. "client when it receives the 'fire' event." );
  770. addField( "stateAlternateFire", TypeBool, Offset(stateAlternateFire, ShapeBaseImageData), MaxStates,
  771. "The first state with this set to true is the state entered by the "
  772. "client when it receives the 'altFire' event." );
  773. addField( "stateReload", TypeBool, Offset(stateReload, ShapeBaseImageData), MaxStates,
  774. "The first state with this set to true is the state entered by the "
  775. "client when it receives the 'reload' event." );
  776. addField( "stateEjectShell", TypeBool, Offset(stateEjectShell, ShapeBaseImageData), MaxStates,
  777. "If true, a shell casing will be ejected in this state." );
  778. addField( "stateEnergyDrain", TypeF32, Offset(stateEnergyDrain, ShapeBaseImageData), MaxStates,
  779. "@brief Amount of energy to subtract from the Image in this state.\n\n"
  780. "Energy is drained at stateEnergyDrain units/tick as long as we are in "
  781. "this state.\n"
  782. "@see usesEnergy");
  783. addField( "stateAllowImageChange", TypeBool, Offset(stateAllowImageChange, ShapeBaseImageData), MaxStates,
  784. "@brief If false, other Images will temporarily be blocked from mounting "
  785. "while the state machine is executing the tasks in this state.\n\n"
  786. "For instance, if we have a rocket launcher, the player shouldn't "
  787. "be able to switch out <i>while</i> firing. So, you'd set "
  788. "stateAllowImageChange to false in firing states, and true the rest "
  789. "of the time." );
  790. addField( "stateDirection", TypeBool, Offset(stateDirection, ShapeBaseImageData), MaxStates,
  791. "@brief Direction of the animation to play in this state.\n\n"
  792. "True is forward, false is backward." );
  793. addField( "stateLoadedFlag", TYPEID< ShapeBaseImageData::StateData::LoadedState >(), Offset(stateLoaded, ShapeBaseImageData), MaxStates,
  794. "@brief Set the loaded state of the Image.\n\n"
  795. "<ul><li>IgnoreLoaded: Don't change Image loaded state.</li>"
  796. "<li>Loaded: Set Image loaded state to true.</li>"
  797. "<li>NotLoaded: Set Image loaded state to false.</li></ul>\n"
  798. "@see ShapeBaseImageLoadedState");
  799. addField( "stateSpinThread", TYPEID< ShapeBaseImageData::StateData::SpinState >(), Offset(stateSpin, ShapeBaseImageData), MaxStates,
  800. "@brief Controls how fast the 'spin' animation sequence will be played in "
  801. "this state.\n\n"
  802. "<ul><li>Ignore: No change to the spin sequence.</li>"
  803. "<li>Stop: Stops the spin sequence at its current position.</li>"
  804. "<li>SpinUp: Increase spin sequence timeScale from 0 (on state entry) "
  805. "to 1 (after stateTimeoutValue seconds).</li>"
  806. "<li>SpinDown: Decrease spin sequence timeScale from 1 (on state entry) "
  807. "to 0 (after stateTimeoutValue seconds).</li>"
  808. "<li>FullSpeed: Resume the spin sequence playback at its current "
  809. "position with timeScale=1.</li></ul>\n"
  810. "@see ShapeBaseImageSpinState");
  811. addField( "stateRecoil", TYPEID< ShapeBaseImageData::StateData::RecoilState >(), Offset(stateRecoil, ShapeBaseImageData), MaxStates,
  812. "@brief Type of recoil sequence to play on the ShapeBase object on entry to "
  813. "this state.\n\n"
  814. "<ul><li>NoRecoil: Do not play a recoil sequence.</li>"
  815. "<li>LightRecoil: Play the light_recoil sequence.</li>"
  816. "<li>MediumRecoil: Play the medium_recoil sequence.</li>"
  817. "<li>HeavyRecoil: Play the heavy_recoil sequence.</li></ul>\n"
  818. "@see ShapeBaseImageRecoilState");
  819. addField( "stateSequence", TypeString, Offset(stateSequence, ShapeBaseImageData), MaxStates,
  820. "Name of the sequence to play on entry to this state." );
  821. addField( "stateSequenceRandomFlash", TypeBool, Offset(stateSequenceRandomFlash, ShapeBaseImageData), MaxStates,
  822. "@brief If true, the muzzle flash sequence will be played while in this state.\n\n"
  823. "The name of the muzzle flash sequence is the same as stateSequence, "
  824. "with \"_vis\" at the end." );
  825. addField( "stateScaleAnimation", TypeBool, Offset(stateScaleAnimation, ShapeBaseImageData), MaxStates,
  826. "If true, the timeScale of the stateSequence animation will be adjusted "
  827. "such that the sequence plays for stateTimeoutValue seconds. " );
  828. addField( "stateScaleAnimationFP", TypeBool, Offset(stateScaleAnimationFP, ShapeBaseImageData), MaxStates,
  829. "If true, the timeScale of the first person stateSequence animation will be adjusted "
  830. "such that the sequence plays for stateTimeoutValue seconds. " );
  831. addField( "stateSequenceTransitionIn", TypeBool, Offset(stateSequenceTransitionIn, ShapeBaseImageData), MaxStates,
  832. "Do we transition to the state's sequence when we enter the state?" );
  833. addField( "stateSequenceTransitionOut", TypeBool, Offset(stateSequenceTransitionOut, ShapeBaseImageData), MaxStates,
  834. "Do we transition to the new state's sequence when we leave the state?" );
  835. addField( "stateSequenceNeverTransition", TypeBool, Offset(stateSequenceNeverTransition, ShapeBaseImageData), MaxStates,
  836. "Never allow a transition to this sequence. Often used for a fire sequence." );
  837. addField( "stateSequenceTransitionTime", TypeF32, Offset(stateSequenceTransitionTime, ShapeBaseImageData), MaxStates,
  838. "The time to transition in or out of a sequence." );
  839. addField( "stateShapeSequence", TypeString, Offset(stateShapeSequence, ShapeBaseImageData), MaxStates,
  840. "Name of the sequence that is played on the mounting shape." );
  841. addField( "stateScaleShapeSequence", TypeBool, Offset(stateScaleShapeSequence, ShapeBaseImageData), MaxStates,
  842. "Indicates if the sequence to be played on the mounting shape should be scaled to the length of the state." );
  843. INITPERSISTFIELD_SOUNDASSET_ARRAY(stateSound, MaxStates, ShapeBaseImageData, "State sound.");
  844. addField( "stateScript", TypeCaseString, Offset(stateScript, ShapeBaseImageData), MaxStates,
  845. "@brief Method to execute on entering this state.\n\n"
  846. "Scoped to this image class name, then ShapeBaseImageData. The script "
  847. "callback function takes the same arguments as the onMount callback.\n"
  848. "@see onMount() for the same arguments as this callback.");
  849. addField( "stateEmitter", TYPEID< ParticleEmitterData >(), Offset(stateEmitter, ShapeBaseImageData), MaxStates,
  850. "@brief Emitter to generate particles in this state (from muzzle point or "
  851. "specified node).\n\n"
  852. "@see stateEmitterNode" );
  853. addField( "stateEmitterTime", TypeF32, Offset(stateEmitterTime, ShapeBaseImageData), MaxStates,
  854. "How long (in seconds) to emit particles on entry to this state." );
  855. addField( "stateEmitterNode", TypeString, Offset(stateEmitterNode, ShapeBaseImageData), MaxStates,
  856. "@brief Name of the node to emit particles from.\n\n"
  857. "@see stateEmitter" );
  858. addField( "stateIgnoreLoadedForReady", TypeBool, Offset(stateIgnoreLoadedForReady, ShapeBaseImageData), MaxStates,
  859. "@brief If set to true, and both ready and loaded transitions are true, the "
  860. "ready transition will be taken instead of the loaded transition.\n\n"
  861. "A state is 'ready' if pressing the fire trigger in that state would "
  862. "transition to the fire state." );
  863. endArray( "States" );
  864. addField( "computeCRC", TypeBool, Offset(computeCRC, ShapeBaseImageData),
  865. "If true, verify that the CRC of the client's Image matches the server's "
  866. "CRC for the Image when loaded by the client." );
  867. addField( "maxConcurrentSounds", TypeS32, Offset(maxConcurrentSounds, ShapeBaseImageData),
  868. "@brief Maximum number of sounds this Image can play at a time.\n\n"
  869. "Any value <= 0 indicates that it can play an infinite number of sounds." );
  870. addField( "useRemainderDT", TypeBool, Offset(useRemainderDT, ShapeBaseImageData),
  871. "@brief If true, allow multiple timeout transitions to occur within a single "
  872. "tick (useful if states have a very small timeout).\n\n" );
  873. Parent::initPersistFields();
  874. }
  875. void ShapeBaseImageData::packData(BitStream* stream)
  876. {
  877. Parent::packData(stream);
  878. if(stream->writeFlag(computeCRC))
  879. {
  880. for( U32 j=0; j<MaxShapes; ++j )
  881. {
  882. stream->write(mCRC[j]);
  883. }
  884. }
  885. for (U32 j = 0; j < MaxShapes; ++j)
  886. {
  887. PACKDATA_ASSET_ARRAY(Shape, j); // shape 0 for normal use, shape 1 for first person use (optional)
  888. }
  889. stream->writeString(imageAnimPrefix);
  890. stream->writeString(imageAnimPrefixFP);
  891. stream->write(mountPoint);
  892. if (!stream->writeFlag(mountOffset.isIdentity()))
  893. stream->writeAffineTransform(mountOffset);
  894. if (!stream->writeFlag(eyeOffset.isIdentity()))
  895. stream->writeAffineTransform(eyeOffset);
  896. stream->writeFlag(animateOnServer);
  897. stream->write(scriptAnimTransitionTime);
  898. stream->writeFlag(useEyeNode);
  899. stream->writeFlag(correctMuzzleVector);
  900. stream->writeFlag(correctMuzzleVectorTP);
  901. stream->writeFlag(firstPerson);
  902. stream->write(mass);
  903. stream->writeFlag(usesEnergy);
  904. stream->write(minEnergy);
  905. for( U32 j=0; j<MaxShapes; ++j)
  906. {
  907. stream->writeFlag(hasFlash[j]);
  908. }
  909. // Client doesn't need accuFire
  910. // Write the projectile datablock
  911. if (stream->writeFlag(projectile))
  912. stream->writeRangedU32(mPacked ? SimObjectId((uintptr_t)projectile):
  913. projectile->getId(),DataBlockObjectIdFirst,DataBlockObjectIdLast);
  914. stream->writeFlag(cloakable);
  915. stream->writeRangedU32(lightType, 0, NumLightTypes-1);
  916. if(lightType != NoLight)
  917. {
  918. stream->write(lightRadius);
  919. stream->write(lightDuration);
  920. stream->writeFloat(lightColor.red, 7);
  921. stream->writeFloat(lightColor.green, 7);
  922. stream->writeFloat(lightColor.blue, 7);
  923. stream->writeFloat(lightColor.alpha, 7);
  924. stream->write(lightBrightness);
  925. }
  926. if ( stream->writeFlag( shakeCamera ) )
  927. {
  928. mathWrite( *stream, camShakeFreq );
  929. mathWrite( *stream, camShakeAmp );
  930. stream->write( camShakeDuration );
  931. stream->write( camShakeRadius );
  932. stream->write( camShakeFalloff );
  933. }
  934. mathWrite( *stream, shellExitDir );
  935. stream->write(shellExitVariance);
  936. stream->write(shellVelocity);
  937. if( stream->writeFlag( casing ) )
  938. {
  939. stream->writeRangedU32(mPacked ? SimObjectId((uintptr_t)casing):
  940. casing->getId(),DataBlockObjectIdFirst,DataBlockObjectIdLast);
  941. }
  942. for (U32 i = 0; i < MaxStates; i++)
  943. if (stream->writeFlag(state[i].name && state[i].name[0])) {
  944. StateData& s = state[i];
  945. // States info not needed on the client:
  946. // s.allowImageChange
  947. // s.scriptNames
  948. // Transitions are inc. one to account for -1 values
  949. stream->writeString(state[i].name);
  950. stream->writeInt(s.transition.loaded[0]+1,NumStateBits);
  951. stream->writeInt(s.transition.loaded[1]+1,NumStateBits);
  952. stream->writeInt(s.transition.ammo[0]+1,NumStateBits);
  953. stream->writeInt(s.transition.ammo[1]+1,NumStateBits);
  954. stream->writeInt(s.transition.target[0]+1,NumStateBits);
  955. stream->writeInt(s.transition.target[1]+1,NumStateBits);
  956. stream->writeInt(s.transition.wet[0]+1,NumStateBits);
  957. stream->writeInt(s.transition.wet[1]+1,NumStateBits);
  958. stream->writeInt(s.transition.trigger[0]+1,NumStateBits);
  959. stream->writeInt(s.transition.trigger[1]+1,NumStateBits);
  960. stream->writeInt(s.transition.altTrigger[0]+1,NumStateBits);
  961. stream->writeInt(s.transition.altTrigger[1]+1,NumStateBits);
  962. stream->writeInt(s.transition.timeout+1,NumStateBits);
  963. // Most states don't make use of the motion transition.
  964. if (stream->writeFlag(s.transition.motion[0] != -1 || s.transition.motion[1] != -1))
  965. {
  966. // This state does
  967. stream->writeInt(s.transition.motion[0]+1,NumStateBits);
  968. stream->writeInt(s.transition.motion[1]+1,NumStateBits);
  969. }
  970. // Most states don't make use of the generic trigger transitions. Don't transmit
  971. // if that is the case here.
  972. for (U32 j=0; j<MaxGenericTriggers; ++j)
  973. {
  974. if (stream->writeFlag(s.transition.genericTrigger[j][0] != -1 || s.transition.genericTrigger[j][1] != -1))
  975. {
  976. stream->writeInt(s.transition.genericTrigger[j][0]+1,NumStateBits);
  977. stream->writeInt(s.transition.genericTrigger[j][1]+1,NumStateBits);
  978. }
  979. }
  980. if(stream->writeFlag(s.timeoutValue != gDefaultStateData.timeoutValue))
  981. stream->write(s.timeoutValue);
  982. stream->writeFlag(s.waitForTimeout);
  983. stream->writeFlag(s.fire);
  984. stream->writeFlag(s.altFire);
  985. stream->writeFlag(s.reload);
  986. stream->writeFlag(s.ejectShell);
  987. stream->writeFlag(s.scaleAnimation);
  988. stream->writeFlag(s.scaleAnimationFP);
  989. stream->writeFlag(s.direction);
  990. stream->writeFlag(s.sequenceTransitionIn);
  991. stream->writeFlag(s.sequenceTransitionOut);
  992. stream->writeFlag(s.sequenceNeverTransition);
  993. if(stream->writeFlag(s.sequenceTransitionTime != gDefaultStateData.sequenceTransitionTime))
  994. stream->write(s.sequenceTransitionTime);
  995. stream->writeString(s.shapeSequence);
  996. stream->writeFlag(s.shapeSequenceScale);
  997. if(stream->writeFlag(s.energyDrain != gDefaultStateData.energyDrain))
  998. stream->write(s.energyDrain);
  999. stream->writeInt(s.loaded,StateData::NumLoadedBits);
  1000. stream->writeInt(s.spin,StateData::NumSpinBits);
  1001. stream->writeInt(s.recoil,StateData::NumRecoilBits);
  1002. for( U32 j=0; j<MaxShapes; ++j )
  1003. {
  1004. if(stream->writeFlag(s.sequence[j] != gDefaultStateData.sequence[j]))
  1005. stream->writeSignedInt(s.sequence[j], 16);
  1006. if(stream->writeFlag(s.sequenceVis[j] != gDefaultStateData.sequenceVis[j]))
  1007. stream->writeSignedInt(s.sequenceVis[j],16);
  1008. stream->writeFlag(s.flashSequence[j]);
  1009. }
  1010. stream->writeFlag(s.ignoreLoadedForReady);
  1011. if (stream->writeFlag(s.emitter))
  1012. {
  1013. stream->writeRangedU32(mPacked ? SimObjectId((uintptr_t)s.emitter):
  1014. s.emitter->getId(),DataBlockObjectIdFirst,DataBlockObjectIdLast);
  1015. stream->write(s.emitterTime);
  1016. for( U32 j=0; j<MaxShapes; ++j )
  1017. {
  1018. stream->write(s.emitterNode[j]);
  1019. }
  1020. }
  1021. PACKDATA_SOUNDASSET_ARRAY(stateSound, i);
  1022. }
  1023. stream->write(maxConcurrentSounds);
  1024. stream->writeFlag(useRemainderDT);
  1025. }
  1026. void ShapeBaseImageData::unpackData(BitStream* stream)
  1027. {
  1028. Parent::unpackData(stream);
  1029. computeCRC = stream->readFlag();
  1030. if(computeCRC)
  1031. {
  1032. for( U32 j=0; j<MaxShapes; ++j )
  1033. {
  1034. stream->read(&mCRC[j]);
  1035. }
  1036. }
  1037. for (U32 j = 0; j < MaxShapes; ++j)
  1038. {
  1039. UNPACKDATA_ASSET_ARRAY(Shape, j); // shape 0 for normal use, shape 1 for first person use (optional)
  1040. }
  1041. imageAnimPrefix = stream->readSTString();
  1042. imageAnimPrefixFP = stream->readSTString();
  1043. stream->read(&mountPoint);
  1044. if (stream->readFlag())
  1045. mountOffset.identity();
  1046. else
  1047. stream->readAffineTransform(&mountOffset);
  1048. if (stream->readFlag())
  1049. eyeOffset.identity();
  1050. else
  1051. stream->readAffineTransform(&eyeOffset);
  1052. animateOnServer = stream->readFlag();
  1053. stream->read(&scriptAnimTransitionTime);
  1054. useEyeNode = stream->readFlag();
  1055. correctMuzzleVector = stream->readFlag();
  1056. correctMuzzleVectorTP = stream->readFlag();
  1057. firstPerson = stream->readFlag();
  1058. stream->read(&mass);
  1059. usesEnergy = stream->readFlag();
  1060. stream->read(&minEnergy);
  1061. for( U32 j=0; j<MaxShapes; ++j )
  1062. {
  1063. hasFlash[j] = stream->readFlag();
  1064. }
  1065. projectile = (stream->readFlag() ?
  1066. (ProjectileData*)(uintptr_t)stream->readRangedU32(DataBlockObjectIdFirst,
  1067. DataBlockObjectIdLast) : 0);
  1068. cloakable = stream->readFlag();
  1069. lightType = stream->readRangedU32(0, NumLightTypes-1);
  1070. if(lightType != NoLight)
  1071. {
  1072. stream->read(&lightRadius);
  1073. stream->read(&lightDuration);
  1074. lightColor.red = stream->readFloat(7);
  1075. lightColor.green = stream->readFloat(7);
  1076. lightColor.blue = stream->readFloat(7);
  1077. lightColor.alpha = stream->readFloat(7);
  1078. stream->read( &lightBrightness );
  1079. }
  1080. shakeCamera = stream->readFlag();
  1081. if ( shakeCamera )
  1082. {
  1083. mathRead( *stream, &camShakeFreq );
  1084. mathRead( *stream, &camShakeAmp );
  1085. stream->read( &camShakeDuration );
  1086. stream->read( &camShakeRadius );
  1087. stream->read( &camShakeFalloff );
  1088. }
  1089. mathRead( *stream, &shellExitDir );
  1090. stream->read(&shellExitVariance);
  1091. stream->read(&shellVelocity);
  1092. if(stream->readFlag())
  1093. {
  1094. casingID = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  1095. }
  1096. for (U32 i = 0; i < MaxStates; i++) {
  1097. if (stream->readFlag()) {
  1098. StateData& s = state[i];
  1099. // States info not needed on the client:
  1100. // s.allowImageChange
  1101. // s.scriptNames
  1102. // Transitions are dec. one to restore -1 values
  1103. s.name = stream->readSTString();
  1104. s.transition.loaded[0] = stream->readInt(NumStateBits) - 1;
  1105. s.transition.loaded[1] = stream->readInt(NumStateBits) - 1;
  1106. s.transition.ammo[0] = stream->readInt(NumStateBits) - 1;
  1107. s.transition.ammo[1] = stream->readInt(NumStateBits) - 1;
  1108. s.transition.target[0] = stream->readInt(NumStateBits) - 1;
  1109. s.transition.target[1] = stream->readInt(NumStateBits) - 1;
  1110. s.transition.wet[0] = stream->readInt(NumStateBits) - 1;
  1111. s.transition.wet[1] = stream->readInt(NumStateBits) - 1;
  1112. s.transition.trigger[0] = stream->readInt(NumStateBits) - 1;
  1113. s.transition.trigger[1] = stream->readInt(NumStateBits) - 1;
  1114. s.transition.altTrigger[0] = stream->readInt(NumStateBits) - 1;
  1115. s.transition.altTrigger[1] = stream->readInt(NumStateBits) - 1;
  1116. s.transition.timeout = stream->readInt(NumStateBits) - 1;
  1117. // Motion trigger
  1118. if (stream->readFlag())
  1119. {
  1120. s.transition.motion[0] = stream->readInt(NumStateBits) - 1;
  1121. s.transition.motion[1] = stream->readInt(NumStateBits) - 1;
  1122. }
  1123. else
  1124. {
  1125. s.transition.motion[0] = -1;
  1126. s.transition.motion[1] = -1;
  1127. }
  1128. // Generic triggers
  1129. for (U32 j=0; j<MaxGenericTriggers; ++j)
  1130. {
  1131. if (stream->readFlag())
  1132. {
  1133. s.transition.genericTrigger[j][0] = stream->readInt(NumStateBits) - 1;
  1134. s.transition.genericTrigger[j][1] = stream->readInt(NumStateBits) - 1;
  1135. }
  1136. else
  1137. {
  1138. s.transition.genericTrigger[j][0] = -1;
  1139. s.transition.genericTrigger[j][1] = -1;
  1140. }
  1141. }
  1142. if(stream->readFlag())
  1143. stream->read(&s.timeoutValue);
  1144. else
  1145. s.timeoutValue = gDefaultStateData.timeoutValue;
  1146. s.waitForTimeout = stream->readFlag();
  1147. s.fire = stream->readFlag();
  1148. s.altFire = stream->readFlag();
  1149. s.reload = stream->readFlag();
  1150. s.ejectShell = stream->readFlag();
  1151. s.scaleAnimation = stream->readFlag();
  1152. s.scaleAnimationFP = stream->readFlag();
  1153. s.direction = stream->readFlag();
  1154. s.sequenceTransitionIn = stream->readFlag();
  1155. s.sequenceTransitionOut = stream->readFlag();
  1156. s.sequenceNeverTransition = stream->readFlag();
  1157. if (stream->readFlag())
  1158. stream->read(&s.sequenceTransitionTime);
  1159. else
  1160. s.sequenceTransitionTime = gDefaultStateData.sequenceTransitionTime;
  1161. s.shapeSequence = stream->readSTString();
  1162. s.shapeSequenceScale = stream->readFlag();
  1163. if(stream->readFlag())
  1164. stream->read(&s.energyDrain);
  1165. else
  1166. s.energyDrain = gDefaultStateData.energyDrain;
  1167. s.loaded = (StateData::LoadedState)stream->readInt(StateData::NumLoadedBits);
  1168. s.spin = (StateData::SpinState)stream->readInt(StateData::NumSpinBits);
  1169. s.recoil = (StateData::RecoilState)stream->readInt(StateData::NumRecoilBits);
  1170. for( U32 j=0; j<MaxShapes; ++j )
  1171. {
  1172. if(stream->readFlag())
  1173. s.sequence[j] = stream->readSignedInt(16);
  1174. else
  1175. s.sequence[j] = gDefaultStateData.sequence[j];
  1176. if(stream->readFlag())
  1177. s.sequenceVis[j] = stream->readSignedInt(16);
  1178. else
  1179. s.sequenceVis[j] = gDefaultStateData.sequenceVis[j];
  1180. s.flashSequence[j] = stream->readFlag();
  1181. }
  1182. s.ignoreLoadedForReady = stream->readFlag();
  1183. if (stream->readFlag())
  1184. {
  1185. s.emitter = (ParticleEmitterData*)(uintptr_t)stream->readRangedU32(DataBlockObjectIdFirst,
  1186. DataBlockObjectIdLast);
  1187. stream->read(&s.emitterTime);
  1188. for( U32 j=0; j<MaxShapes; ++j )
  1189. {
  1190. stream->read(&(s.emitterNode[j]));
  1191. }
  1192. }
  1193. else
  1194. s.emitter = 0;
  1195. UNPACKDATA_SOUNDASSET_ARRAY(stateSound, i);
  1196. handleStateSoundTrack(i);
  1197. }
  1198. }
  1199. stream->read(&maxConcurrentSounds);
  1200. useRemainderDT = stream->readFlag();
  1201. statesLoaded = true;
  1202. }
  1203. void ShapeBaseImageData::inspectPostApply()
  1204. {
  1205. Parent::inspectPostApply();
  1206. // This does not do a very good job of applying changes to states
  1207. // which may have occured in the editor, but at least we can do this...
  1208. useEyeOffset = !eyeOffset.isIdentity();
  1209. }
  1210. //----------------------------------------------------------------------------
  1211. //----------------------------------------------------------------------------
  1212. //----------------------------------------------------------------------------
  1213. ShapeBase::MountedImage::MountedImage()
  1214. {
  1215. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  1216. {
  1217. shapeInstance[i] = 0;
  1218. ambientThread[i] = NULL;
  1219. visThread[i] = NULL;
  1220. animThread[i] = NULL;
  1221. flashThread[i] = NULL;
  1222. spinThread[i] = NULL;
  1223. }
  1224. doAnimateAllShapes = false;
  1225. forceAnimateAllShapes = false;
  1226. lastShapeIndex = 0;
  1227. state = 0;
  1228. dataBlock = 0;
  1229. nextImage = InvalidImagePtr;
  1230. delayTime = 0;
  1231. rDT = 0.0f;
  1232. ammo = false;
  1233. target = false;
  1234. triggerDown = false;
  1235. altTriggerDown = false;
  1236. loaded = false;
  1237. fireCount = 0;
  1238. altFireCount = 0;
  1239. reloadCount = 0;
  1240. wet = false;
  1241. motion = false;
  1242. lightStart = 0;
  1243. lightInfo = NULL;
  1244. dMemset(emitter, 0, sizeof(emitter));
  1245. for (U32 i=0; i<ShapeBaseImageData::MaxGenericTriggers; ++i)
  1246. {
  1247. genericTrigger[i] = false;
  1248. }
  1249. nextLoaded = false;
  1250. }
  1251. ShapeBase::MountedImage::~MountedImage()
  1252. {
  1253. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  1254. {
  1255. delete shapeInstance[i];
  1256. shapeInstance[i] = 0;
  1257. }
  1258. // stop sound
  1259. for(Vector<SFXSource*>::iterator i = mSoundSources.begin(); i != mSoundSources.end(); i++)
  1260. {
  1261. SFX_DELETE((*i));
  1262. }
  1263. mSoundSources.clear();
  1264. for (S32 i = 0; i < MaxImageEmitters; i++)
  1265. if (bool(emitter[i].emitter))
  1266. emitter[i].emitter->deleteWhenEmpty();
  1267. if ( lightInfo != NULL )
  1268. delete lightInfo;
  1269. }
  1270. void ShapeBase::MountedImage::addSoundSource(SFXSource* source)
  1271. {
  1272. if(source != NULL)
  1273. {
  1274. if(dataBlock->maxConcurrentSounds > 0 && mSoundSources.size() > dataBlock->maxConcurrentSounds)
  1275. {
  1276. SFX_DELETE(mSoundSources.first());
  1277. mSoundSources.pop_front();
  1278. }
  1279. source->play();
  1280. mSoundSources.push_back(source);
  1281. }
  1282. }
  1283. void ShapeBase::MountedImage::updateSoundSources( const MatrixF &renderTransform )
  1284. {
  1285. // Update all the sounds removing any ones that have stopped.
  1286. for ( U32 i=0; i < mSoundSources.size(); )
  1287. {
  1288. SFXSource *source = mSoundSources[i];
  1289. if ( source->isStopped() )
  1290. {
  1291. SFX_DELETE( source );
  1292. mSoundSources.erase_fast( i );
  1293. continue;
  1294. }
  1295. source->setTransform(renderTransform);
  1296. i++;
  1297. }
  1298. }
  1299. void ShapeBase::MountedImage::updateDoAnimateAllShapes(const ShapeBase* owner)
  1300. {
  1301. doAnimateAllShapes = false;
  1302. if (!dataBlock)
  1303. return;
  1304. // According to ShapeBase::isFirstPerson() the server is always in first person mode.
  1305. // Therefore we don't need to animate any other shapes but the one that will be
  1306. // used for first person.
  1307. // Sometimes this is forced externally, so honour it.
  1308. if (forceAnimateAllShapes)
  1309. {
  1310. doAnimateAllShapes = true;
  1311. return;
  1312. }
  1313. if (owner->isClientObject())
  1314. {
  1315. // If this client object doesn't have a controlling client, then according to
  1316. // ShapeBase::isFirstPerson() it cannot ever be in first person mode. So no need
  1317. // to animate any shapes beyond the current one.
  1318. if (!owner->getControllingClient())
  1319. {
  1320. return;
  1321. }
  1322. doAnimateAllShapes = dataBlock->animateAllShapes;
  1323. }
  1324. }
  1325. //----------------------------------------------------------------------------
  1326. //----------------------------------------------------------------------------
  1327. //----------------------------------------------------------------------------
  1328. // Any item with an item image is selectable
  1329. bool ShapeBase::mountImage(ShapeBaseImageData* imageData,U32 imageSlot,bool loaded,NetStringHandle &skinNameHandle)
  1330. {
  1331. AssertFatal(imageSlot<MaxMountedImages,"Out of range image slot");
  1332. MountedImage& image = mMountedImageList[imageSlot];
  1333. if (image.dataBlock) {
  1334. if ((image.dataBlock == imageData) && (image.skinNameHandle == skinNameHandle)) {
  1335. // Image already loaded
  1336. image.nextImage = InvalidImagePtr;
  1337. return true;
  1338. }
  1339. }
  1340. //
  1341. setImage(imageSlot,imageData,skinNameHandle,loaded);
  1342. return true;
  1343. }
  1344. bool ShapeBase::unmountImage(U32 imageSlot)
  1345. {
  1346. AssertFatal(imageSlot<MaxMountedImages,"Out of range image slot");
  1347. bool returnValue = false;
  1348. MountedImage& image = mMountedImageList[imageSlot];
  1349. if (image.dataBlock)
  1350. {
  1351. NetStringHandle temp;
  1352. setImage(imageSlot,0, temp);
  1353. returnValue = true;
  1354. }
  1355. return returnValue;
  1356. }
  1357. //----------------------------------------------------------------------------
  1358. ShapeBaseImageData* ShapeBase::getMountedImage(U32 imageSlot)
  1359. {
  1360. AssertFatal(imageSlot<MaxMountedImages,"Out of range image slot");
  1361. return mMountedImageList[imageSlot].dataBlock;
  1362. }
  1363. ShapeBase::MountedImage* ShapeBase::getImageStruct(U32 imageSlot)
  1364. {
  1365. return &mMountedImageList[imageSlot];
  1366. }
  1367. ShapeBaseImageData* ShapeBase::getPendingImage(U32 imageSlot)
  1368. {
  1369. ShapeBaseImageData* data = mMountedImageList[imageSlot].nextImage;
  1370. return (data == InvalidImagePtr)? 0: data;
  1371. }
  1372. bool ShapeBase::isImageFiring(U32 imageSlot)
  1373. {
  1374. MountedImage& image = mMountedImageList[imageSlot];
  1375. return image.dataBlock && image.state->fire;
  1376. }
  1377. bool ShapeBase::isImageAltFiring(U32 imageSlot)
  1378. {
  1379. MountedImage& image = mMountedImageList[imageSlot];
  1380. return image.dataBlock && image.state->altFire;
  1381. }
  1382. bool ShapeBase::isImageReloading(U32 imageSlot)
  1383. {
  1384. MountedImage& image = mMountedImageList[imageSlot];
  1385. return image.dataBlock && image.state->reload;
  1386. }
  1387. bool ShapeBase::isImageReady(U32 imageSlot,U32 ns,U32 depth)
  1388. {
  1389. // Will pressing the trigger lead to a fire state?
  1390. MountedImage& image = mMountedImageList[imageSlot];
  1391. if (depth++ > 5 || !image.dataBlock)
  1392. return false;
  1393. ShapeBaseImageData::StateData& stateData = (ns == -1) ?
  1394. *image.state : image.dataBlock->state[ns];
  1395. if (stateData.fire)
  1396. return true;
  1397. // Try the transitions...
  1398. if (stateData.ignoreLoadedForReady == true) {
  1399. if ((ns = stateData.transition.loaded[true]) != -1)
  1400. if (isImageReady(imageSlot,ns,depth))
  1401. return true;
  1402. } else {
  1403. if ((ns = stateData.transition.loaded[image.loaded]) != -1)
  1404. if (isImageReady(imageSlot,ns,depth))
  1405. return true;
  1406. }
  1407. for (U32 i=0; i<ShapeBaseImageData::MaxGenericTriggers; ++i)
  1408. {
  1409. if ((ns = stateData.transition.genericTrigger[i][image.genericTrigger[i]]) != -1)
  1410. if (isImageReady(imageSlot,ns,depth))
  1411. return true;
  1412. }
  1413. if ((ns = stateData.transition.ammo[image.ammo]) != -1)
  1414. if (isImageReady(imageSlot,ns,depth))
  1415. return true;
  1416. if ((ns = stateData.transition.target[image.target]) != -1)
  1417. if (isImageReady(imageSlot,ns,depth))
  1418. return true;
  1419. if ((ns = stateData.transition.wet[image.wet]) != -1)
  1420. if (isImageReady(imageSlot,ns,depth))
  1421. return true;
  1422. if ((ns = stateData.transition.motion[image.motion]) != -1)
  1423. if (isImageReady(imageSlot,ns,depth))
  1424. return true;
  1425. if ((ns = stateData.transition.trigger[1]) != -1)
  1426. if (isImageReady(imageSlot,ns,depth))
  1427. return true;
  1428. if ((ns = stateData.transition.altTrigger[1]) != -1)
  1429. if (isImageReady(imageSlot,ns,depth))
  1430. return true;
  1431. if ((ns = stateData.transition.timeout) != -1)
  1432. if (isImageReady(imageSlot,ns,depth))
  1433. return true;
  1434. return false;
  1435. }
  1436. bool ShapeBase::isImageMounted(ShapeBaseImageData* imageData)
  1437. {
  1438. for (U32 i = 0; i < MaxMountedImages; i++)
  1439. if (imageData == mMountedImageList[i].dataBlock)
  1440. return true;
  1441. return false;
  1442. }
  1443. S32 ShapeBase::getMountSlot(ShapeBaseImageData* imageData)
  1444. {
  1445. for (U32 i = 0; i < MaxMountedImages; i++)
  1446. if (imageData == mMountedImageList[i].dataBlock)
  1447. return i;
  1448. return -1;
  1449. }
  1450. NetStringHandle ShapeBase::getImageSkinTag(U32 imageSlot)
  1451. {
  1452. MountedImage& image = mMountedImageList[imageSlot];
  1453. return image.dataBlock? image.skinNameHandle : NetStringHandle();
  1454. }
  1455. const char* ShapeBase::getImageState(U32 imageSlot)
  1456. {
  1457. MountedImage& image = mMountedImageList[imageSlot];
  1458. return image.dataBlock? image.state->name: 0;
  1459. }
  1460. void ShapeBase::setImageGenericTriggerState(U32 imageSlot, U32 trigger, bool state)
  1461. {
  1462. MountedImage& image = mMountedImageList[imageSlot];
  1463. if (image.dataBlock && image.genericTrigger[trigger] != state) {
  1464. setMaskBits(ImageMaskN << imageSlot);
  1465. image.genericTrigger[trigger] = state;
  1466. }
  1467. }
  1468. bool ShapeBase::getImageGenericTriggerState(U32 imageSlot, U32 trigger)
  1469. {
  1470. MountedImage& image = mMountedImageList[imageSlot];
  1471. if (!image.dataBlock)
  1472. return false;
  1473. return image.genericTrigger[trigger];
  1474. }
  1475. void ShapeBase::setImageAmmoState(U32 imageSlot,bool ammo)
  1476. {
  1477. MountedImage& image = mMountedImageList[imageSlot];
  1478. if (image.dataBlock && !image.dataBlock->usesEnergy && image.ammo != ammo) {
  1479. setMaskBits(ImageMaskN << imageSlot);
  1480. image.ammo = ammo;
  1481. }
  1482. }
  1483. bool ShapeBase::getImageAmmoState(U32 imageSlot)
  1484. {
  1485. MountedImage& image = mMountedImageList[imageSlot];
  1486. if (!image.dataBlock)
  1487. return false;
  1488. return image.ammo;
  1489. }
  1490. void ShapeBase::setImageWetState(U32 imageSlot,bool wet)
  1491. {
  1492. MountedImage& image = mMountedImageList[imageSlot];
  1493. if (image.dataBlock && image.wet != wet) {
  1494. setMaskBits(ImageMaskN << imageSlot);
  1495. image.wet = wet;
  1496. }
  1497. }
  1498. bool ShapeBase::getImageWetState(U32 imageSlot)
  1499. {
  1500. MountedImage& image = mMountedImageList[imageSlot];
  1501. if (!image.dataBlock)
  1502. return false;
  1503. return image.wet;
  1504. }
  1505. void ShapeBase::setImageMotionState(U32 imageSlot,bool motion)
  1506. {
  1507. MountedImage& image = mMountedImageList[imageSlot];
  1508. if (image.dataBlock && image.motion != motion) {
  1509. setMaskBits(ImageMaskN << imageSlot);
  1510. image.motion = motion;
  1511. }
  1512. }
  1513. bool ShapeBase::getImageMotionState(U32 imageSlot)
  1514. {
  1515. MountedImage& image = mMountedImageList[imageSlot];
  1516. if (!image.dataBlock)
  1517. return false;
  1518. return image.motion;
  1519. }
  1520. void ShapeBase::setImageTargetState(U32 imageSlot,bool target)
  1521. {
  1522. MountedImage& image = mMountedImageList[imageSlot];
  1523. if (image.dataBlock && image.target != target) {
  1524. setMaskBits(ImageMaskN << imageSlot);
  1525. image.target = target;
  1526. }
  1527. }
  1528. bool ShapeBase::getImageTargetState(U32 imageSlot)
  1529. {
  1530. MountedImage& image = mMountedImageList[imageSlot];
  1531. if (!image.dataBlock)
  1532. return false;
  1533. return image.target;
  1534. }
  1535. void ShapeBase::setImageLoadedState(U32 imageSlot,bool loaded)
  1536. {
  1537. MountedImage& image = mMountedImageList[imageSlot];
  1538. if (image.dataBlock && image.loaded != loaded) {
  1539. setMaskBits(ImageMaskN << imageSlot);
  1540. image.loaded = loaded;
  1541. }
  1542. }
  1543. bool ShapeBase::getImageLoadedState(U32 imageSlot)
  1544. {
  1545. MountedImage& image = mMountedImageList[imageSlot];
  1546. if (!image.dataBlock)
  1547. return false;
  1548. return image.loaded;
  1549. }
  1550. void ShapeBase::getMuzzleVector(U32 imageSlot,VectorF* vec)
  1551. {
  1552. MatrixF mat;
  1553. getMuzzleTransform(imageSlot,&mat);
  1554. GameConnection * gc = getControllingClient();
  1555. if (gc && !gc->isAIControlled())
  1556. {
  1557. MountedImage& image = mMountedImageList[imageSlot];
  1558. bool fp = gc->isFirstPerson();
  1559. if ((fp && image.dataBlock->correctMuzzleVector) ||
  1560. (!fp && image.dataBlock->correctMuzzleVectorTP))
  1561. if (getCorrectedAim(mat, vec))
  1562. return;
  1563. }
  1564. mat.getColumn(1,vec);
  1565. }
  1566. void ShapeBase::getMuzzlePoint(U32 imageSlot,Point3F* pos)
  1567. {
  1568. MatrixF mat;
  1569. getMuzzleTransform(imageSlot,&mat);
  1570. mat.getColumn(3,pos);
  1571. }
  1572. void ShapeBase::getRenderMuzzleVector(U32 imageSlot,VectorF* vec)
  1573. {
  1574. MatrixF mat;
  1575. getRenderMuzzleTransform(imageSlot,&mat);
  1576. GameConnection * gc = getControllingClient();
  1577. if (gc && !gc->isAIControlled())
  1578. {
  1579. MountedImage& image = mMountedImageList[imageSlot];
  1580. bool fp = gc->isFirstPerson();
  1581. if ((fp && image.dataBlock->correctMuzzleVector) ||
  1582. (!fp && image.dataBlock->correctMuzzleVectorTP))
  1583. if (getCorrectedAim(mat, vec))
  1584. return;
  1585. }
  1586. mat.getColumn(1,vec);
  1587. }
  1588. void ShapeBase::getRenderMuzzlePoint(U32 imageSlot,Point3F* pos)
  1589. {
  1590. MatrixF mat;
  1591. getRenderMuzzleTransform(imageSlot,&mat);
  1592. mat.getColumn(3,pos);
  1593. }
  1594. //----------------------------------------------------------------------------
  1595. void ShapeBase::scriptCallback(U32 imageSlot,const char* function)
  1596. {
  1597. MountedImage &image = mMountedImageList[imageSlot];
  1598. char buff1[32];
  1599. dSprintf( buff1, 32, "%d", imageSlot );
  1600. char buff2[32];
  1601. dSprintf( buff2, 32, "%f", image.dataBlock->useRemainderDT ? image.rDT : 0.0f );
  1602. Con::executef( image.dataBlock, function, getIdString(), buff1, buff2 );
  1603. }
  1604. //----------------------------------------------------------------------------
  1605. void ShapeBase::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  1606. {
  1607. // Returns mount point to world space transform
  1608. if ( index >= 0 && index < SceneObject::NumMountPoints) {
  1609. S32 ni = mDataBlock->mountPointNode[index];
  1610. if (ni != -1) {
  1611. MatrixF mountTransform = mShapeInstance->mNodeTransforms[ni];
  1612. mountTransform.mul( xfm );
  1613. const Point3F& scale = getScale();
  1614. // The position of the mount point needs to be scaled.
  1615. Point3F position = mountTransform.getPosition();
  1616. position.convolve( scale );
  1617. mountTransform.setPosition( position );
  1618. // Also we would like the object to be scaled to the model.
  1619. outMat->mul(mObjToWorld, mountTransform);
  1620. return;
  1621. }
  1622. }
  1623. // Then let SceneObject handle it.
  1624. Parent::getMountTransform( index, xfm, outMat );
  1625. }
  1626. void ShapeBase::getImageTransform(U32 imageSlot,MatrixF* mat)
  1627. {
  1628. // Image transform in world space
  1629. MountedImage& image = mMountedImageList[imageSlot];
  1630. if (image.dataBlock) {
  1631. ShapeBaseImageData& data = *image.dataBlock;
  1632. U32 shapeIndex = getImageShapeIndex(image);
  1633. MatrixF nmat;
  1634. if (data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1) {
  1635. // We need to animate, even on the server, to make sure the nodes are in the correct location.
  1636. image.shapeInstance[shapeIndex]->animate();
  1637. getEyeBaseTransform(&nmat, mDataBlock->mountedImagesBank);
  1638. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1639. mat->mul(nmat, mountTransform);
  1640. }
  1641. else if (data.useEyeOffset && isFirstPerson()) {
  1642. getEyeTransform(&nmat);
  1643. mat->mul(nmat,data.eyeOffset);
  1644. }
  1645. else {
  1646. getMountTransform( image.dataBlock->mountPoint, MatrixF::Identity, &nmat );
  1647. mat->mul(nmat,data.mountTransform[shapeIndex]);
  1648. }
  1649. }
  1650. else
  1651. *mat = mObjToWorld;
  1652. }
  1653. void ShapeBase::getImageTransform(U32 imageSlot,S32 node,MatrixF* mat)
  1654. {
  1655. // Image transform in world space
  1656. MountedImage& image = mMountedImageList[imageSlot];
  1657. if (image.dataBlock)
  1658. {
  1659. if (node != -1)
  1660. {
  1661. ShapeBaseImageData& data = *image.dataBlock;
  1662. U32 shapeIndex = getImageShapeIndex(image);
  1663. MatrixF nmat = image.shapeInstance[shapeIndex]->mNodeTransforms[node];
  1664. MatrixF mmat;
  1665. if (data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1)
  1666. {
  1667. // We need to animate, even on the server, to make sure the nodes are in the correct location.
  1668. image.shapeInstance[shapeIndex]->animate();
  1669. MatrixF emat;
  1670. getEyeBaseTransform(&emat, mDataBlock->mountedImagesBank);
  1671. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1672. mountTransform.affineInverse();
  1673. mmat.mul(emat, mountTransform);
  1674. }
  1675. else if (data.useEyeOffset && isFirstPerson())
  1676. {
  1677. MatrixF emat;
  1678. getEyeTransform(&emat);
  1679. mmat.mul(emat,data.eyeOffset);
  1680. }
  1681. else
  1682. {
  1683. MatrixF emat;
  1684. getMountTransform( image.dataBlock->mountPoint, MatrixF::Identity, &emat );
  1685. mmat.mul(emat,data.mountTransform[shapeIndex]);
  1686. }
  1687. mat->mul(mmat, nmat);
  1688. }
  1689. else
  1690. getImageTransform(imageSlot,mat);
  1691. }
  1692. else
  1693. *mat = mObjToWorld;
  1694. }
  1695. void ShapeBase::getImageTransform(U32 imageSlot,StringTableEntry nodeName,MatrixF* mat)
  1696. {
  1697. getImageTransform( imageSlot, getNodeIndex( imageSlot, nodeName ), mat );
  1698. }
  1699. void ShapeBase::getMuzzleTransform(U32 imageSlot,MatrixF* mat)
  1700. {
  1701. // Muzzle transform in world space
  1702. MountedImage& image = mMountedImageList[imageSlot];
  1703. if (image.dataBlock)
  1704. getImageTransform(imageSlot,image.dataBlock->muzzleNode[getImageShapeIndex(image)],mat);
  1705. else
  1706. *mat = mObjToWorld;
  1707. }
  1708. //----------------------------------------------------------------------------
  1709. void ShapeBase::getRenderMountTransform( F32 delta, S32 mountPoint, const MatrixF &xfm, MatrixF *outMat )
  1710. {
  1711. // Returns mount point to world space transform
  1712. if ( mountPoint >= 0 && mountPoint < SceneObject::NumMountPoints) {
  1713. S32 ni = mDataBlock->mountPointNode[mountPoint];
  1714. if (ni != -1) {
  1715. MatrixF mountTransform = mShapeInstance->mNodeTransforms[ni];
  1716. mountTransform.mul( xfm );
  1717. const Point3F& scale = getScale();
  1718. // The position of the mount point needs to be scaled.
  1719. Point3F position = mountTransform.getPosition();
  1720. position.convolve( scale );
  1721. mountTransform.setPosition( position );
  1722. // Also we would like the object to be scaled to the model.
  1723. mountTransform.scale( scale );
  1724. outMat->mul(getRenderTransform(), mountTransform);
  1725. return;
  1726. }
  1727. }
  1728. // Then let SceneObject handle it.
  1729. Parent::getRenderMountTransform( delta, mountPoint, xfm, outMat );
  1730. }
  1731. void ShapeBase::getRenderImageTransform( U32 imageSlot, MatrixF* mat, bool noEyeOffset )
  1732. {
  1733. // Image transform in world space
  1734. MountedImage& image = mMountedImageList[imageSlot];
  1735. if (image.dataBlock)
  1736. {
  1737. ShapeBaseImageData& data = *image.dataBlock;
  1738. U32 shapeIndex = getImageShapeIndex(image);
  1739. MatrixF nmat;
  1740. if ( data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1 ) {
  1741. getRenderEyeBaseTransform(&nmat, mDataBlock->mountedImagesBank);
  1742. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1743. mat->mul(nmat, mountTransform);
  1744. }
  1745. else if ( !noEyeOffset && data.useEyeOffset && isFirstPerson() )
  1746. {
  1747. getRenderEyeTransform(&nmat);
  1748. mat->mul(nmat,data.eyeOffset);
  1749. }
  1750. else
  1751. {
  1752. getRenderMountTransform( 0.0f, data.mountPoint, MatrixF::Identity, &nmat );
  1753. mat->mul(nmat,data.mountTransform[shapeIndex]);
  1754. }
  1755. }
  1756. else
  1757. *mat = getRenderTransform();
  1758. }
  1759. void ShapeBase::getRenderImageTransform(U32 imageSlot,S32 node,MatrixF* mat)
  1760. {
  1761. // Image transform in world space
  1762. MountedImage& image = mMountedImageList[imageSlot];
  1763. if (image.dataBlock)
  1764. {
  1765. if (node != -1)
  1766. {
  1767. ShapeBaseImageData& data = *image.dataBlock;
  1768. U32 shapeIndex = getImageShapeIndex(image);
  1769. MatrixF nmat = image.shapeInstance[shapeIndex]->mNodeTransforms[node];
  1770. MatrixF mmat;
  1771. if ( data.useEyeNode && isFirstPerson() && data.eyeMountNode[shapeIndex] != -1 )
  1772. {
  1773. MatrixF emat;
  1774. getRenderEyeBaseTransform(&emat, mDataBlock->mountedImagesBank);
  1775. MatrixF mountTransform = image.shapeInstance[shapeIndex]->mNodeTransforms[data.eyeMountNode[shapeIndex]];
  1776. mountTransform.affineInverse();
  1777. mmat.mul(emat, mountTransform);
  1778. }
  1779. else if ( data.useEyeOffset && isFirstPerson() )
  1780. {
  1781. MatrixF emat;
  1782. getRenderEyeTransform(&emat);
  1783. mmat.mul(emat,data.eyeOffset);
  1784. }
  1785. else
  1786. {
  1787. MatrixF emat;
  1788. getRenderMountTransform( 0.0f, data.mountPoint, MatrixF::Identity, &emat );
  1789. mmat.mul(emat,data.mountTransform[shapeIndex]);
  1790. }
  1791. mat->mul(mmat, nmat);
  1792. }
  1793. else
  1794. getRenderImageTransform(imageSlot,mat);
  1795. }
  1796. else
  1797. *mat = getRenderTransform();
  1798. }
  1799. void ShapeBase::getRenderImageTransform(U32 imageSlot,StringTableEntry nodeName,MatrixF* mat)
  1800. {
  1801. getRenderImageTransform( imageSlot, getNodeIndex( imageSlot, nodeName ), mat );
  1802. }
  1803. void ShapeBase::getRenderMuzzleTransform(U32 imageSlot,MatrixF* mat)
  1804. {
  1805. // Muzzle transform in world space
  1806. MountedImage& image = mMountedImageList[imageSlot];
  1807. if (image.dataBlock)
  1808. getRenderImageTransform(imageSlot,image.dataBlock->muzzleNode[getImageShapeIndex(image)],mat);
  1809. else
  1810. *mat = getRenderTransform();
  1811. }
  1812. void ShapeBase::getRetractionTransform(U32 imageSlot,MatrixF* mat)
  1813. {
  1814. // Muzzle transform in world space
  1815. MountedImage& image = mMountedImageList[imageSlot];
  1816. if (image.dataBlock) {
  1817. ShapeBaseImageData& data = *image.dataBlock;
  1818. U32 imageShapeIndex = getImageShapeIndex(image);
  1819. if (data.retractNode[imageShapeIndex] != -1)
  1820. getImageTransform(imageSlot,data.retractNode[imageShapeIndex],mat);
  1821. else
  1822. getImageTransform(imageSlot,data.muzzleNode[imageShapeIndex],mat);
  1823. } else {
  1824. *mat = getTransform();
  1825. }
  1826. }
  1827. void ShapeBase::getRenderRetractionTransform(U32 imageSlot,MatrixF* mat)
  1828. {
  1829. // Muzzle transform in world space
  1830. MountedImage& image = mMountedImageList[imageSlot];
  1831. if (image.dataBlock) {
  1832. ShapeBaseImageData& data = *image.dataBlock;
  1833. U32 imageShapeIndex = getImageShapeIndex(image);
  1834. if (data.retractNode[imageShapeIndex] != -1)
  1835. getRenderImageTransform(imageSlot,data.retractNode[imageShapeIndex],mat);
  1836. else
  1837. getRenderImageTransform(imageSlot,data.muzzleNode[imageShapeIndex],mat);
  1838. } else {
  1839. *mat = getRenderTransform();
  1840. }
  1841. }
  1842. //----------------------------------------------------------------------------
  1843. S32 ShapeBase::getNodeIndex(U32 imageSlot,StringTableEntry nodeName)
  1844. {
  1845. MountedImage& image = mMountedImageList[imageSlot];
  1846. if (image.dataBlock)
  1847. return image.dataBlock->mShape[getImageShapeIndex(image)]->findNode(nodeName);
  1848. else
  1849. return -1;
  1850. }
  1851. // Modify muzzle if needed to aim at whatever is straight in front of the camera. Let the
  1852. // caller know if we actually modified the result.
  1853. bool ShapeBase::getCorrectedAim(const MatrixF& muzzleMat, VectorF* result)
  1854. {
  1855. F32 pullInD = sFullCorrectionDistance;
  1856. const F32 maxAdjD = 500;
  1857. VectorF aheadVec(0, maxAdjD, 0);
  1858. MatrixF camMat;
  1859. Point3F camPos;
  1860. F32 pos = 0;
  1861. GameConnection * gc = getControllingClient();
  1862. if (gc && !gc->isFirstPerson())
  1863. pos = 1.0f;
  1864. getCameraTransform(&pos, &camMat);
  1865. camMat.getColumn(3, &camPos);
  1866. camMat.mulV(aheadVec);
  1867. Point3F aheadPoint = (camPos + aheadVec);
  1868. // Should we check if muzzle point is really close to camera? Does that happen?
  1869. Point3F muzzlePos;
  1870. muzzleMat.getColumn(3, &muzzlePos);
  1871. Point3F collidePoint;
  1872. VectorF collideVector;
  1873. disableCollision();
  1874. RayInfo rinfo;
  1875. if (getContainer()->castRay(camPos, aheadPoint, STATIC_COLLISION_TYPEMASK|DAMAGEABLE_TYPEMASK, &rinfo) &&
  1876. (mDot(rinfo.point - mObjToWorld.getPosition(), mObjToWorld.getForwardVector()) > 0)) // Check if point is behind us (could happen in 3rd person view)
  1877. collideVector = ((collidePoint = rinfo.point) - camPos);
  1878. else
  1879. collideVector = ((collidePoint = aheadPoint) - camPos);
  1880. enableCollision();
  1881. // For close collision we want to NOT aim at ground since we're bending
  1882. // the ray here as it is. But we don't want to pop, so adjust continuously.
  1883. F32 lenSq = collideVector.lenSquared();
  1884. if (lenSq < (pullInD * pullInD) && lenSq > 0.04)
  1885. {
  1886. F32 len = mSqrt(lenSq);
  1887. F32 mid = pullInD; // (pullInD + len) / 2.0;
  1888. // This gives us point beyond to focus on-
  1889. collideVector *= (mid / len);
  1890. collidePoint = (camPos + collideVector);
  1891. }
  1892. VectorF muzzleToCollide = (collidePoint - muzzlePos);
  1893. lenSq = muzzleToCollide.lenSquared();
  1894. if (lenSq > 0.04)
  1895. {
  1896. muzzleToCollide *= (1 / mSqrt(lenSq));
  1897. * result = muzzleToCollide;
  1898. return true;
  1899. }
  1900. return false;
  1901. }
  1902. //----------------------------------------------------------------------------
  1903. void ShapeBase::updateMass()
  1904. {
  1905. if (mDataBlock) {
  1906. F32 imass = 0;
  1907. for (U32 i = 0; i < MaxMountedImages; i++) {
  1908. MountedImage& image = mMountedImageList[i];
  1909. if (image.dataBlock)
  1910. imass += image.dataBlock->mass;
  1911. }
  1912. //
  1913. mMass = mDataBlock->mass + imass;
  1914. mOneOverMass = 1 / mMass;
  1915. }
  1916. }
  1917. void ShapeBase::onImage(U32 imageSlot, bool unmount)
  1918. {
  1919. }
  1920. void ShapeBase::onImageRecoil(U32,ShapeBaseImageData::StateData::RecoilState)
  1921. {
  1922. }
  1923. void ShapeBase::onImageStateAnimation(U32 imageSlot, const char* seqName, bool direction, bool scaleToState, F32 stateTimeOutValue)
  1924. {
  1925. }
  1926. void ShapeBase::onImageAnimThreadChange(U32 imageSlot, S32 imageShapeIndex, ShapeBaseImageData::StateData* lastState, const char* anim, F32 pos, F32 timeScale, bool reset)
  1927. {
  1928. }
  1929. void ShapeBase::onImageAnimThreadUpdate(U32 imageSlot, S32 imageShapeIndex, F32 dt)
  1930. {
  1931. }
  1932. //----------------------------------------------------------------------------
  1933. void ShapeBase::setImage( U32 imageSlot,
  1934. ShapeBaseImageData* imageData,
  1935. NetStringHandle& skinNameHandle,
  1936. bool loaded,
  1937. bool ammo,
  1938. bool triggerDown,
  1939. bool altTriggerDown,
  1940. bool motion,
  1941. bool genericTrigger0,
  1942. bool genericTrigger1,
  1943. bool genericTrigger2,
  1944. bool genericTrigger3,
  1945. bool target)
  1946. {
  1947. AssertFatal(imageSlot<MaxMountedImages,"Out of range image slot");
  1948. MountedImage& image = mMountedImageList[imageSlot];
  1949. // If we already have this datablock...
  1950. if (image.dataBlock == imageData) {
  1951. // Mark that there is not a datablock change pending.
  1952. image.nextImage = InvalidImagePtr;
  1953. // Change the skin handle if necessary.
  1954. if (image.skinNameHandle != skinNameHandle) {
  1955. if (!isGhost()) {
  1956. // Serverside, note the skin handle and tell the client.
  1957. image.skinNameHandle = skinNameHandle;
  1958. setMaskBits(ImageMaskN << imageSlot);
  1959. }
  1960. else {
  1961. // Clientside, do the reskin.
  1962. image.skinNameHandle = skinNameHandle;
  1963. for( U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  1964. {
  1965. if (image.shapeInstance[i])
  1966. {
  1967. String newSkin = skinNameHandle.getString();
  1968. image.shapeInstance[i]->reSkin(newSkin, image.appliedSkinName);
  1969. image.appliedSkinName = newSkin;
  1970. }
  1971. }
  1972. }
  1973. }
  1974. return;
  1975. }
  1976. // Check to see if we need to delay image changes until state change.
  1977. if (!isGhost()) {
  1978. if (imageData && image.dataBlock && !image.state->allowImageChange) {
  1979. image.nextImage = imageData;
  1980. image.nextSkinNameHandle = skinNameHandle;
  1981. image.nextLoaded = loaded;
  1982. return;
  1983. }
  1984. }
  1985. // Mark that updates are happenin'.
  1986. setMaskBits(ImageMaskN << imageSlot);
  1987. // Notify script unmount since we're swapping datablocks.
  1988. if (image.dataBlock && !isGhost()) {
  1989. F32 dt = image.dataBlock->useRemainderDT ? image.rDT : 0.0f;
  1990. image.dataBlock->onUnmount_callback( this, imageSlot, dt );
  1991. }
  1992. // Stop anything currently going on with the image.
  1993. resetImageSlot(imageSlot);
  1994. // If we're just unselecting the current shape without swapping
  1995. // in a new one, then bail.
  1996. if (!imageData) {
  1997. onImage( imageSlot, true);
  1998. return;
  1999. }
  2000. // Otherwise, init the new shape.
  2001. image.dataBlock = imageData;
  2002. image.state = &image.dataBlock->state[0];
  2003. image.skinNameHandle = skinNameHandle;
  2004. image.updateDoAnimateAllShapes(this);
  2005. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2006. {
  2007. if (image.dataBlock->shapeIsValid[i])
  2008. image.shapeInstance[i] = new TSShapeInstance(image.dataBlock->mShape[i], isClientObject());
  2009. }
  2010. if (isClientObject())
  2011. {
  2012. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2013. {
  2014. if (image.shapeInstance[i])
  2015. {
  2016. image.shapeInstance[i]->cloneMaterialList();
  2017. String newSkin = skinNameHandle.getString();
  2018. image.shapeInstance[i]->reSkin(newSkin, image.appliedSkinName);
  2019. image.appliedSkinName = newSkin;
  2020. }
  2021. }
  2022. }
  2023. image.loaded = loaded;
  2024. image.ammo = ammo;
  2025. image.triggerDown = triggerDown;
  2026. image.altTriggerDown = altTriggerDown;
  2027. image.target = target;
  2028. image.motion = motion;
  2029. image.genericTrigger[0] = genericTrigger0;
  2030. image.genericTrigger[1] = genericTrigger1;
  2031. image.genericTrigger[2] = genericTrigger2;
  2032. image.genericTrigger[3] = genericTrigger3;
  2033. // The server needs the shape loaded for muzzle mount nodes
  2034. // but it doesn't need to run any of the animations, unless the image
  2035. // has animateOnServer set. Then the server needs to animate as well.
  2036. // This is often set when using useEyeNode.
  2037. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2038. {
  2039. image.ambientThread[i] = 0;
  2040. image.animThread[i] = 0;
  2041. image.flashThread[i] = 0;
  2042. image.spinThread[i] = 0;
  2043. }
  2044. if (imageData->animateOnServer || isGhost())
  2045. {
  2046. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2047. {
  2048. if (!image.shapeInstance[i])
  2049. continue;
  2050. if (image.dataBlock->isAnimated[i]) {
  2051. image.animThread[i] = image.shapeInstance[i]->addThread();
  2052. image.shapeInstance[i]->setTimeScale(image.animThread[i],0);
  2053. }
  2054. if (image.dataBlock->hasFlash[i]) {
  2055. image.flashThread[i] = image.shapeInstance[i]->addThread();
  2056. image.shapeInstance[i]->setTimeScale(image.flashThread[i],0);
  2057. }
  2058. if (image.dataBlock->ambientSequence[i] != -1) {
  2059. image.ambientThread[i] = image.shapeInstance[i]->addThread();
  2060. image.shapeInstance[i]->setTimeScale(image.ambientThread[i],1);
  2061. image.shapeInstance[i]->setSequence(image.ambientThread[i],
  2062. image.dataBlock->ambientSequence[i],0);
  2063. }
  2064. if (image.dataBlock->spinSequence[i] != -1) {
  2065. image.spinThread[i] = image.shapeInstance[i]->addThread();
  2066. image.shapeInstance[i]->setTimeScale(image.spinThread[i],1);
  2067. image.shapeInstance[i]->setSequence(image.spinThread[i],
  2068. image.dataBlock->spinSequence[i],0);
  2069. }
  2070. }
  2071. }
  2072. // Set the image to its starting state.
  2073. setImageState(imageSlot, (U32)0, true);
  2074. // Update the mass for the mount object.
  2075. updateMass();
  2076. // Notify script mount.
  2077. if ( !isGhost() )
  2078. {
  2079. F32 dt = image.dataBlock->useRemainderDT ? image.rDT : 0.0f;
  2080. image.dataBlock->onMount_callback( this, imageSlot, dt );
  2081. }
  2082. else
  2083. {
  2084. if ( imageData->lightType == ShapeBaseImageData::PulsingLight )
  2085. image.lightStart = Sim::getCurrentTime();
  2086. }
  2087. onImage(imageSlot, false);
  2088. // Done.
  2089. }
  2090. //----------------------------------------------------------------------------
  2091. void ShapeBase::resetImageSlot(U32 imageSlot)
  2092. {
  2093. AssertFatal(imageSlot<MaxMountedImages,"Out of range image slot");
  2094. // Clear out current image
  2095. MountedImage& image = mMountedImageList[imageSlot];
  2096. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2097. {
  2098. delete image.shapeInstance[i];
  2099. image.shapeInstance[i] = 0;
  2100. }
  2101. // stop sound
  2102. for(Vector<SFXSource*>::iterator i = image.mSoundSources.begin(); i != image.mSoundSources.end(); i++)
  2103. {
  2104. SFX_DELETE((*i));
  2105. }
  2106. image.mSoundSources.clear();
  2107. for (S32 i = 0; i < MaxImageEmitters; i++) {
  2108. MountedImage::ImageEmitter& em = image.emitter[i];
  2109. if (bool(em.emitter)) {
  2110. em.emitter->deleteWhenEmpty();
  2111. em.emitter = 0;
  2112. }
  2113. }
  2114. image.dataBlock = 0;
  2115. image.nextImage = InvalidImagePtr;
  2116. image.skinNameHandle = NetStringHandle();
  2117. image.nextSkinNameHandle = NetStringHandle();
  2118. image.state = 0;
  2119. image.delayTime = 0;
  2120. image.rDT = 0;
  2121. image.ammo = false;
  2122. image.triggerDown = false;
  2123. image.altTriggerDown = false;
  2124. image.loaded = false;
  2125. image.motion = false;
  2126. for (U32 i=0; i<ShapeBaseImageData::MaxGenericTriggers; ++i)
  2127. {
  2128. image.genericTrigger[i] = false;
  2129. }
  2130. image.lightStart = 0;
  2131. if ( image.lightInfo != NULL )
  2132. SAFE_DELETE( image.lightInfo );
  2133. updateMass();
  2134. }
  2135. //----------------------------------------------------------------------------
  2136. bool ShapeBase::getImageTriggerState(U32 imageSlot)
  2137. {
  2138. if (isGhost() || !mMountedImageList[imageSlot].dataBlock)
  2139. return false;
  2140. return mMountedImageList[imageSlot].triggerDown;
  2141. }
  2142. void ShapeBase::setImageTriggerState(U32 imageSlot,bool trigger)
  2143. {
  2144. if (isGhost() || !mMountedImageList[imageSlot].dataBlock)
  2145. return;
  2146. MountedImage& image = mMountedImageList[imageSlot];
  2147. if (trigger) {
  2148. if (!image.triggerDown && image.dataBlock) {
  2149. image.triggerDown = true;
  2150. if (!isGhost()) {
  2151. setMaskBits(ImageMaskN << imageSlot);
  2152. updateImageState(imageSlot,0);
  2153. }
  2154. }
  2155. }
  2156. else
  2157. if (image.triggerDown) {
  2158. image.triggerDown = false;
  2159. if (!isGhost()) {
  2160. setMaskBits(ImageMaskN << imageSlot);
  2161. updateImageState(imageSlot,0);
  2162. }
  2163. }
  2164. }
  2165. bool ShapeBase::getImageAltTriggerState(U32 imageSlot)
  2166. {
  2167. if (isGhost() || !mMountedImageList[imageSlot].dataBlock)
  2168. return false;
  2169. return mMountedImageList[imageSlot].altTriggerDown;
  2170. }
  2171. void ShapeBase::setImageAltTriggerState(U32 imageSlot,bool trigger)
  2172. {
  2173. if (isGhost() || !mMountedImageList[imageSlot].dataBlock)
  2174. return;
  2175. MountedImage& image = mMountedImageList[imageSlot];
  2176. if (trigger) {
  2177. if (!image.altTriggerDown && image.dataBlock) {
  2178. image.altTriggerDown = true;
  2179. if (!isGhost()) {
  2180. setMaskBits(ImageMaskN << imageSlot);
  2181. updateImageState(imageSlot,0);
  2182. }
  2183. }
  2184. }
  2185. else
  2186. if (image.altTriggerDown) {
  2187. image.altTriggerDown = false;
  2188. if (!isGhost()) {
  2189. setMaskBits(ImageMaskN << imageSlot);
  2190. updateImageState(imageSlot,0);
  2191. }
  2192. }
  2193. }
  2194. //----------------------------------------------------------------------------
  2195. U32 ShapeBase::getImageFireState(U32 imageSlot)
  2196. {
  2197. MountedImage& image = mMountedImageList[imageSlot];
  2198. // If there is no fire state, then try state 0
  2199. if (image.dataBlock && image.dataBlock->fireState != -1)
  2200. return image.dataBlock->fireState;
  2201. return 0;
  2202. }
  2203. U32 ShapeBase::getImageAltFireState(U32 imageSlot)
  2204. {
  2205. MountedImage& image = mMountedImageList[imageSlot];
  2206. // If there is no alternate fire state, then try state 0
  2207. if (image.dataBlock && image.dataBlock->altFireState != -1)
  2208. return image.dataBlock->altFireState;
  2209. return 0;
  2210. }
  2211. U32 ShapeBase::getImageReloadState(U32 imageSlot)
  2212. {
  2213. MountedImage& image = mMountedImageList[imageSlot];
  2214. // If there is no reload state, then try state 0
  2215. if (image.dataBlock && image.dataBlock->reloadState != -1)
  2216. return image.dataBlock->reloadState;
  2217. return 0;
  2218. }
  2219. //----------------------------------------------------------------------------
  2220. bool ShapeBase::hasImageState(U32 imageSlot, const char* state)
  2221. {
  2222. if (!state || !state[0])
  2223. return false;
  2224. MountedImage& image = mMountedImageList[imageSlot];
  2225. if (image.dataBlock)
  2226. {
  2227. for (U32 i = 0; i < ShapeBaseImageData::MaxStates; i++)
  2228. {
  2229. ShapeBaseImageData::StateData& sd = image.dataBlock->state[i];
  2230. if (sd.name && !dStricmp(state, sd.name))
  2231. return true;
  2232. }
  2233. }
  2234. return false;
  2235. }
  2236. void ShapeBase::setImageState(U32 imageSlot, U32 newState, bool force)
  2237. {
  2238. if (!mMountedImageList[imageSlot].dataBlock)
  2239. return;
  2240. MountedImage& image = mMountedImageList[imageSlot];
  2241. // The client never enters the initial fire state on its own, but it
  2242. // will continue to set that state...
  2243. if (isGhost() && !force && newState == image.dataBlock->fireState) {
  2244. if (image.state != &image.dataBlock->state[newState])
  2245. return;
  2246. }
  2247. // The client never enters the initial alternate fire state on its own, but it
  2248. // will continue to set that state...
  2249. if (isGhost() && !force && newState == image.dataBlock->altFireState) {
  2250. if (image.state != &image.dataBlock->state[newState])
  2251. return;
  2252. }
  2253. // The client never enters the initial reload state on its own, but it
  2254. // will continue to set that state...
  2255. if (isGhost() && !force && newState == image.dataBlock->reloadState) {
  2256. if (image.state != &image.dataBlock->state[newState])
  2257. return;
  2258. }
  2259. // Eject shell casing on every state change (client side only)
  2260. ShapeBaseImageData::StateData& nextStateData = image.dataBlock->state[newState];
  2261. if (isGhost() && nextStateData.ejectShell) {
  2262. ejectShellCasing(imageSlot);
  2263. }
  2264. // Shake camera on client.
  2265. if (isGhost() && nextStateData.fire && image.dataBlock->shakeCamera) {
  2266. shakeCamera(imageSlot);
  2267. }
  2268. // Server must animate the shape if it is a firestate...
  2269. if (isServerObject() && (image.dataBlock->state[newState].fire || image.dataBlock->state[newState].altFire))
  2270. mShapeInstance->animate();
  2271. // Obtain the image's shape index for future use.
  2272. U32 imageShapeIndex = getImageShapeIndex(image);
  2273. image.lastShapeIndex = imageShapeIndex;
  2274. // If going back into the same state, just reset the timer
  2275. // and invoke the script callback
  2276. if (!force && image.state == &image.dataBlock->state[newState]) {
  2277. image.delayTime = image.state->timeoutValue;
  2278. if (image.state->script && !isGhost())
  2279. scriptCallback(imageSlot, image.state->script);
  2280. // If this is a flash sequence, we need to select a new position for the
  2281. // animation if we're returning to that state...
  2282. F32 randomPos = Platform::getRandom();
  2283. for (U32 i = 0; i < ShapeBaseImageData::MaxShapes; ++i)
  2284. {
  2285. if (!image.dataBlock->shapeIsValid[i] || (i != imageShapeIndex && !image.doAnimateAllShapes))
  2286. continue;
  2287. if (image.animThread[i] && image.state->sequence[i] != -1 && image.state->flashSequence[i]) {
  2288. image.shapeInstance[i]->setPos(image.animThread[i], randomPos);
  2289. image.shapeInstance[i]->setTimeScale(image.animThread[i], 0);
  2290. if (image.flashThread[i])
  2291. image.shapeInstance[i]->setPos(image.flashThread[i], 0);
  2292. }
  2293. }
  2294. return;
  2295. }
  2296. F32 lastDelay = image.delayTime;
  2297. ShapeBaseImageData::StateData* lastState = image.state;
  2298. image.state = &image.dataBlock->state[newState];
  2299. //
  2300. // Do state cleanup first...
  2301. //
  2302. ShapeBaseImageData::StateData& stateData = *image.state;
  2303. image.delayTime = stateData.timeoutValue;
  2304. // Mount pending images
  2305. if (image.nextImage != InvalidImagePtr && stateData.allowImageChange) {
  2306. setImage(imageSlot, image.nextImage, image.nextSkinNameHandle, image.nextLoaded);
  2307. return;
  2308. }
  2309. // Reset cyclic sequences back to the first frame to turn it off
  2310. // (the first key frame should be it's off state).
  2311. // We need to do this across all image shapes to make sure we have no hold overs when switching
  2312. // rendering shapes while in the middle of a state change.
  2313. for (U32 i = 0; i < ShapeBaseImageData::MaxShapes; ++i)
  2314. {
  2315. // If we are to do a sequence transition then we need to keep the previous animThread active
  2316. if (image.animThread[i] && image.animThread[i]->getSequence()->isCyclic() && (stateData.sequenceNeverTransition || !(stateData.sequenceTransitionIn || lastState->sequenceTransitionOut))) {
  2317. image.shapeInstance[i]->setPos(image.animThread[i], 0);
  2318. image.shapeInstance[i]->setTimeScale(image.animThread[i], 0);
  2319. }
  2320. if (image.flashThread[i]) {
  2321. image.shapeInstance[i]->setPos(image.flashThread[i], 0);
  2322. image.shapeInstance[i]->setTimeScale(image.flashThread[i], 0);
  2323. }
  2324. }
  2325. // Broadcast the reset
  2326. onImageAnimThreadChange(imageSlot, imageShapeIndex, lastState, NULL, 0, 0, true);
  2327. // Check for immediate transitions, but only if we don't need to wait for
  2328. // a time out. Only perform this wait if we're not forced to change.
  2329. S32 ns;
  2330. if (image.delayTime <= 0 || !stateData.waitForTimeout)
  2331. {
  2332. if ((ns = stateData.transition.loaded[image.loaded]) != -1) {
  2333. setImageState(imageSlot, ns);
  2334. return;
  2335. }
  2336. for (U32 i = 0; i < ShapeBaseImageData::MaxGenericTriggers; ++i)
  2337. {
  2338. if ((ns = stateData.transition.genericTrigger[i][image.genericTrigger[i]]) != -1) {
  2339. setImageState(imageSlot, ns);
  2340. return;
  2341. }
  2342. }
  2343. //if (!imageData.usesEnergy)
  2344. if ((ns = stateData.transition.ammo[image.ammo]) != -1) {
  2345. setImageState(imageSlot, ns);
  2346. return;
  2347. }
  2348. if ((ns = stateData.transition.target[image.target]) != -1) {
  2349. setImageState(imageSlot, ns);
  2350. return;
  2351. }
  2352. if ((ns = stateData.transition.wet[image.wet]) != -1) {
  2353. setImageState(imageSlot, ns);
  2354. return;
  2355. }
  2356. if ((ns = stateData.transition.motion[image.motion]) != -1) {
  2357. setImageState(imageSlot, ns);
  2358. return;
  2359. }
  2360. if ((ns = stateData.transition.trigger[image.triggerDown]) != -1) {
  2361. setImageState(imageSlot, ns);
  2362. return;
  2363. }
  2364. if ((ns = stateData.transition.altTrigger[image.altTriggerDown]) != -1) {
  2365. setImageState(imageSlot, ns);
  2366. return;
  2367. }
  2368. }
  2369. //
  2370. // Initialize the new state...
  2371. //
  2372. if (stateData.loaded != ShapeBaseImageData::StateData::IgnoreLoaded)
  2373. image.loaded = stateData.loaded == ShapeBaseImageData::StateData::Loaded;
  2374. if (!isGhost() && image.dataBlock->state[newState].fire) {
  2375. setMaskBits(ImageMaskN << imageSlot);
  2376. image.fireCount = (image.fireCount + 1) & 0x7;
  2377. }
  2378. if (!isGhost() && image.dataBlock->state[newState].altFire) {
  2379. setMaskBits(ImageMaskN << imageSlot);
  2380. image.altFireCount = (image.altFireCount + 1) & 0x7;
  2381. }
  2382. if (!isGhost() && image.dataBlock->state[newState].reload) {
  2383. setMaskBits(ImageMaskN << imageSlot);
  2384. image.reloadCount = (image.reloadCount + 1) & 0x7;
  2385. }
  2386. // Apply recoil
  2387. if (stateData.recoil != ShapeBaseImageData::StateData::NoRecoil)
  2388. onImageRecoil(imageSlot, stateData.recoil);
  2389. // Apply image state animation on mounting shape
  2390. if (stateData.shapeSequence && stateData.shapeSequence[0])
  2391. {
  2392. onImageStateAnimation(imageSlot, stateData.shapeSequence, stateData.direction, stateData.shapeSequenceScale, stateData.timeoutValue);
  2393. }
  2394. // Delete any loooping sounds that were in the previous state.
  2395. // this is the crazy bit =/ needs to know prev state in order to stop sounds.
  2396. // lastState does not return an id for the prev state so we keep track of it.
  2397. if (lastState->sound && lastState->sound->getSfxProfile()->getDescription()->mIsLooping)
  2398. {
  2399. for (Vector<SFXSource*>::iterator i = image.mSoundSources.begin(); i != image.mSoundSources.end(); i++)
  2400. SFX_DELETE((*i));
  2401. image.mSoundSources.clear();
  2402. }
  2403. // Play sound
  2404. if (isGhost())
  2405. {
  2406. if (stateData.sound)
  2407. {
  2408. const Point3F& velocity = getVelocity();
  2409. image.addSoundSource(SFX->createSource(stateData.sound->getSfxProfile(), &getRenderTransform(), &velocity));
  2410. }
  2411. if (stateData.soundTrack)
  2412. {
  2413. const Point3F& velocity = getVelocity();
  2414. image.addSoundSource(SFX->createSource(stateData.soundTrack, &getRenderTransform(), &velocity));
  2415. }
  2416. }
  2417. // Play animation
  2418. updateAnimThread(imageSlot, imageShapeIndex, lastState);
  2419. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2420. {
  2421. if (!image.dataBlock->shapeIsValid[i] || (i != imageShapeIndex && !image.doAnimateAllShapes))
  2422. continue;
  2423. // Start spin thread
  2424. if (image.spinThread[i]) {
  2425. switch (stateData.spin) {
  2426. case ShapeBaseImageData::StateData::IgnoreSpin:
  2427. image.shapeInstance[i]->setTimeScale(image.spinThread[i], image.shapeInstance[i]->getTimeScale(image.spinThread[i]));
  2428. break;
  2429. case ShapeBaseImageData::StateData::NoSpin:
  2430. image.shapeInstance[i]->setTimeScale(image.spinThread[i],0);
  2431. break;
  2432. case ShapeBaseImageData::StateData::SpinUp:
  2433. if (lastState->spin == ShapeBaseImageData::StateData::SpinDown)
  2434. image.delayTime *= 1.0f - (lastDelay / stateData.timeoutValue);
  2435. break;
  2436. case ShapeBaseImageData::StateData::SpinDown:
  2437. if (lastState->spin == ShapeBaseImageData::StateData::SpinUp)
  2438. image.delayTime *= 1.0f - (lastDelay / stateData.timeoutValue);
  2439. break;
  2440. case ShapeBaseImageData::StateData::FullSpin:
  2441. image.shapeInstance[i]->setTimeScale(image.spinThread[i],1);
  2442. break;
  2443. }
  2444. }
  2445. }
  2446. // Start particle emitter on the client (client side only)
  2447. if (isGhost() && stateData.emitter)
  2448. startImageEmitter(image,stateData);
  2449. // Script callback on server
  2450. if (stateData.script && stateData.script[0] && !isGhost())
  2451. scriptCallback(imageSlot,stateData.script);
  2452. // If there is a zero timeout, and a timeout transition, then
  2453. // go ahead and transition imediately.
  2454. if (!image.delayTime)
  2455. {
  2456. if ((ns = stateData.transition.timeout) != -1)
  2457. {
  2458. setImageState(imageSlot,ns);
  2459. return;
  2460. }
  2461. }
  2462. }
  2463. void ShapeBase::updateAnimThread(U32 imageSlot, S32 imageShapeIndex, ShapeBaseImageData::StateData* lastState)
  2464. {
  2465. MountedImage& image = mMountedImageList[imageSlot];
  2466. ShapeBaseImageData::StateData& stateData = *image.state;
  2467. F32 randomPos = Platform::getRandom();
  2468. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2469. {
  2470. if (!image.dataBlock->shapeIsValid[i] || (i != imageShapeIndex && !image.doAnimateAllShapes))
  2471. continue;
  2472. if (image.animThread[i] && stateData.sequence[i] != -1)
  2473. {
  2474. S32 seqIndex = stateData.sequence[i]; // Standard index without any prefix
  2475. bool scaleAnim = stateData.scaleAnimation;
  2476. if (i == ShapeBaseImageData::FirstPersonImageShape)
  2477. scaleAnim = stateData.scaleAnimationFP;
  2478. // We're going to apply various prefixes to determine the final sequence to use.
  2479. // Here is the order:
  2480. // shapeBasePrefix_scriptPrefix_baseAnimName
  2481. // shapeBasePrefix_baseAnimName
  2482. // scriptPrefix_baseAnimName
  2483. // baseAnimName
  2484. // Collect the prefixes
  2485. const char* shapeBasePrefix = getImageAnimPrefix(imageSlot, i);
  2486. bool hasShapeBasePrefix = shapeBasePrefix && shapeBasePrefix[0];
  2487. const char* scriptPrefix = getImageScriptAnimPrefix(imageSlot).getString();
  2488. bool hasScriptPrefix = scriptPrefix && scriptPrefix[0];
  2489. // Find the final sequence based on the prefix combinations
  2490. if (hasShapeBasePrefix || hasScriptPrefix)
  2491. {
  2492. bool found = false;
  2493. String baseSeqName(image.shapeInstance[i]->getShape()->getSequenceName(stateData.sequence[i]));
  2494. if (!found && hasShapeBasePrefix && hasScriptPrefix)
  2495. {
  2496. String seqName = String(shapeBasePrefix) + String("_") + String(scriptPrefix) + String("_") + baseSeqName;
  2497. S32 index = image.shapeInstance[i]->getShape()->findSequence(seqName);
  2498. if (index != -1)
  2499. {
  2500. seqIndex = index;
  2501. found = true;
  2502. }
  2503. }
  2504. if (!found && hasShapeBasePrefix)
  2505. {
  2506. String seqName = String(shapeBasePrefix) + String("_") + baseSeqName;
  2507. S32 index = image.shapeInstance[i]->getShape()->findSequence(seqName);
  2508. if (index != -1)
  2509. {
  2510. seqIndex = index;
  2511. found = true;
  2512. }
  2513. }
  2514. if (!found && hasScriptPrefix)
  2515. {
  2516. String seqName = String(scriptPrefix) + String("_") + baseSeqName;
  2517. S32 index = image.shapeInstance[i]->getShape()->findSequence(seqName);
  2518. if (index != -1)
  2519. {
  2520. seqIndex = index;
  2521. found = true;
  2522. }
  2523. }
  2524. }
  2525. if (seqIndex != -1)
  2526. {
  2527. if (!lastState)
  2528. {
  2529. // No lastState indicates that we are just switching animation sequences, not states. Transition into this new sequence, but only
  2530. // if it is different than what we're currently playing.
  2531. S32 prevSeq = -1;
  2532. if (image.animThread[i]->hasSequence())
  2533. {
  2534. prevSeq = image.shapeInstance[i]->getSequence(image.animThread[i]);
  2535. }
  2536. if (seqIndex != prevSeq)
  2537. {
  2538. image.shapeInstance[i]->transitionToSequence(image.animThread[i], seqIndex, stateData.direction ? 0.0f : 1.0f, image.dataBlock->scriptAnimTransitionTime, true);
  2539. }
  2540. }
  2541. else if (!stateData.sequenceNeverTransition && stateData.sequenceTransitionTime && (stateData.sequenceTransitionIn || lastState->sequenceTransitionOut))
  2542. {
  2543. image.shapeInstance[i]->transitionToSequence(image.animThread[i], seqIndex, stateData.direction ? 0.0f : 1.0f, stateData.sequenceTransitionTime, true);
  2544. }
  2545. else
  2546. {
  2547. image.shapeInstance[i]->setSequence(image.animThread[i], seqIndex, stateData.direction ? 0.0f : 1.0f);
  2548. }
  2549. if (stateData.flashSequence[i] == false)
  2550. {
  2551. F32 timeScale = (scaleAnim && stateData.timeoutValue) ?
  2552. image.shapeInstance[i]->getDuration(image.animThread[i]) / stateData.timeoutValue : 1.0f;
  2553. image.shapeInstance[i]->setTimeScale(image.animThread[i], stateData.direction ? timeScale : -timeScale);
  2554. // Broadcast the sequence change
  2555. String seqName = image.shapeInstance[i]->getShape()->getSequenceName(stateData.sequence[i]);
  2556. onImageAnimThreadChange(imageSlot, imageShapeIndex, lastState, seqName, stateData.direction ? 0.0f : 1.0f, stateData.direction ? timeScale : -timeScale);
  2557. }
  2558. else
  2559. {
  2560. image.shapeInstance[i]->setPos(image.animThread[i], randomPos);
  2561. image.shapeInstance[i]->setTimeScale(image.animThread[i], 0);
  2562. S32 seqVisIndex = stateData.sequenceVis[i];
  2563. // Go through the same process as the animThread sequence to find the flashThread sequence
  2564. if (hasShapeBasePrefix || hasScriptPrefix)
  2565. {
  2566. bool found = false;
  2567. String baseVisSeqName(image.shapeInstance[i]->getShape()->getSequenceName(stateData.sequenceVis[i]));
  2568. if (!found && hasShapeBasePrefix && hasScriptPrefix)
  2569. {
  2570. String seqName = String(shapeBasePrefix) + String("_") + String(scriptPrefix) + String("_") + baseVisSeqName;
  2571. S32 index = image.shapeInstance[i]->getShape()->findSequence(seqName);
  2572. if (index != -1)
  2573. {
  2574. seqVisIndex = index;
  2575. found = true;
  2576. }
  2577. }
  2578. if (!found && hasShapeBasePrefix)
  2579. {
  2580. String seqName = String(shapeBasePrefix) + String("_") + baseVisSeqName;
  2581. S32 index = image.shapeInstance[i]->getShape()->findSequence(seqName);
  2582. if (index != -1)
  2583. {
  2584. seqVisIndex = index;
  2585. found = true;
  2586. }
  2587. }
  2588. if (!found && hasScriptPrefix)
  2589. {
  2590. String seqName = String(scriptPrefix) + String("_") + baseVisSeqName;
  2591. S32 index = image.shapeInstance[i]->getShape()->findSequence(seqName);
  2592. if (index != -1)
  2593. {
  2594. seqVisIndex = index;
  2595. found = true;
  2596. }
  2597. }
  2598. }
  2599. image.shapeInstance[i]->setSequence(image.flashThread[i], seqVisIndex, 0);
  2600. image.shapeInstance[i]->setPos(image.flashThread[i], 0);
  2601. F32 timeScale = (scaleAnim && stateData.timeoutValue) ?
  2602. image.shapeInstance[i]->getDuration(image.flashThread[i]) / stateData.timeoutValue : 1.0f;
  2603. image.shapeInstance[i]->setTimeScale(image.flashThread[i], timeScale);
  2604. // Broadcast the sequence change
  2605. String seqName = image.shapeInstance[i]->getShape()->getSequenceName(stateData.sequenceVis[i]);
  2606. onImageAnimThreadChange(imageSlot, imageShapeIndex, lastState, seqName, stateData.direction ? 0.0f : 1.0f, stateData.direction ? timeScale : -timeScale);
  2607. }
  2608. }
  2609. }
  2610. }
  2611. }
  2612. //----------------------------------------------------------------------------
  2613. void ShapeBase::updateImageState(U32 imageSlot,F32 dt)
  2614. {
  2615. if (!mMountedImageList[imageSlot].dataBlock)
  2616. return;
  2617. MountedImage& image = mMountedImageList[imageSlot];
  2618. ShapeBaseImageData& imageData = *image.dataBlock;
  2619. image.rDT = dt;
  2620. F32 elapsed;
  2621. TICKAGAIN:
  2622. ShapeBaseImageData::StateData& stateData = *image.state;
  2623. if ( image.delayTime > dt )
  2624. elapsed = dt;
  2625. else
  2626. elapsed = image.delayTime;
  2627. dt = elapsed;
  2628. image.rDT -= elapsed;
  2629. image.delayTime -= dt;
  2630. // Energy management
  2631. if (imageData.usesEnergy)
  2632. {
  2633. F32 newEnergy = getEnergyLevel() - stateData.energyDrain * dt;
  2634. if (newEnergy < 0)
  2635. newEnergy = 0;
  2636. setEnergyLevel(newEnergy);
  2637. if (!isGhost())
  2638. {
  2639. bool ammo = newEnergy > imageData.minEnergy;
  2640. if (ammo != image.ammo)
  2641. {
  2642. setMaskBits(ImageMaskN << imageSlot);
  2643. image.ammo = ammo;
  2644. }
  2645. }
  2646. }
  2647. // Check for transitions. On some states we must wait for the
  2648. // full timeout value before moving on.
  2649. if (image.delayTime <= 0 || !stateData.waitForTimeout)
  2650. {
  2651. S32 ns;
  2652. if ((ns = stateData.transition.loaded[image.loaded]) != -1)
  2653. setImageState(imageSlot,ns);
  2654. else if ((ns = stateData.transition.genericTrigger[0][image.genericTrigger[0]]) != -1)
  2655. setImageState(imageSlot,ns);
  2656. else if ((ns = stateData.transition.genericTrigger[1][image.genericTrigger[1]]) != -1)
  2657. setImageState(imageSlot,ns);
  2658. else if ((ns = stateData.transition.genericTrigger[2][image.genericTrigger[2]]) != -1)
  2659. setImageState(imageSlot,ns);
  2660. else if ((ns = stateData.transition.genericTrigger[3][image.genericTrigger[3]]) != -1)
  2661. setImageState(imageSlot,ns);
  2662. else if ((ns = stateData.transition.ammo[image.ammo]) != -1)
  2663. setImageState(imageSlot,ns);
  2664. else if ((ns = stateData.transition.target[image.target]) != -1)
  2665. setImageState(imageSlot,ns);
  2666. else if ((ns = stateData.transition.wet[image.wet]) != -1)
  2667. setImageState(imageSlot,ns);
  2668. else if ((ns = stateData.transition.motion[image.motion]) != -1)
  2669. setImageState(imageSlot,ns);
  2670. else if ((ns = stateData.transition.trigger[image.triggerDown]) != -1)
  2671. setImageState(imageSlot,ns);
  2672. else if ((ns = stateData.transition.altTrigger[image.altTriggerDown]) != -1)
  2673. setImageState(imageSlot,ns);
  2674. else if (image.delayTime <= 0 && (ns = stateData.transition.timeout) != -1)
  2675. setImageState(imageSlot,ns);
  2676. }
  2677. // Update the spinning thread timeScale
  2678. U32 imageShapeIndex = getImageShapeIndex(image);
  2679. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2680. {
  2681. if (!image.dataBlock->shapeIsValid[i] || (i != imageShapeIndex && !image.doAnimateAllShapes))
  2682. continue;
  2683. if (image.spinThread[i])
  2684. {
  2685. F32 timeScale;
  2686. switch (stateData.spin)
  2687. {
  2688. case ShapeBaseImageData::StateData::IgnoreSpin:
  2689. case ShapeBaseImageData::StateData::NoSpin:
  2690. case ShapeBaseImageData::StateData::FullSpin:
  2691. {
  2692. timeScale = 0;
  2693. image.shapeInstance[i]->setTimeScale(image.spinThread[i], image.shapeInstance[i]->getTimeScale(image.spinThread[i]));
  2694. break;
  2695. }
  2696. case ShapeBaseImageData::StateData::SpinUp:
  2697. {
  2698. timeScale = 1.0f - image.delayTime / stateData.timeoutValue;
  2699. image.shapeInstance[i]->setTimeScale(image.spinThread[i],timeScale);
  2700. break;
  2701. }
  2702. case ShapeBaseImageData::StateData::SpinDown:
  2703. {
  2704. timeScale = image.delayTime / stateData.timeoutValue;
  2705. image.shapeInstance[i]->setTimeScale(image.spinThread[i],timeScale);
  2706. break;
  2707. }
  2708. }
  2709. }
  2710. }
  2711. if ( image.rDT > 0.0f && image.delayTime > 0.0f && imageData.useRemainderDT && dt != 0.0f )
  2712. {
  2713. dt = image.rDT;
  2714. goto TICKAGAIN;
  2715. }
  2716. }
  2717. //----------------------------------------------------------------------------
  2718. void ShapeBase::updateImageAnimation(U32 imageSlot, F32 dt)
  2719. {
  2720. if (!mMountedImageList[imageSlot].dataBlock)
  2721. return;
  2722. MountedImage& image = mMountedImageList[imageSlot];
  2723. U32 imageShapeIndex = getImageShapeIndex(image);
  2724. // Advance animation threads
  2725. for (U32 i=0; i<ShapeBaseImageData::MaxShapes; ++i)
  2726. {
  2727. if (!image.dataBlock->shapeIsValid[i] || (i != imageShapeIndex && !image.doAnimateAllShapes))
  2728. continue;
  2729. if (image.ambientThread[i])
  2730. image.shapeInstance[i]->advanceTime(dt,image.ambientThread[i]);
  2731. if (image.animThread[i])
  2732. image.shapeInstance[i]->advanceTime(dt,image.animThread[i]);
  2733. if (image.spinThread[i])
  2734. image.shapeInstance[i]->advanceTime(dt,image.spinThread[i]);
  2735. if (image.flashThread[i])
  2736. image.shapeInstance[i]->advanceTime(dt,image.flashThread[i]);
  2737. }
  2738. // Broadcast the update
  2739. onImageAnimThreadUpdate(imageSlot, imageShapeIndex, dt);
  2740. image.updateSoundSources(getRenderTransform());
  2741. // Particle emission
  2742. for (S32 i = 0; i < MaxImageEmitters; i++) {
  2743. MountedImage::ImageEmitter& em = image.emitter[i];
  2744. if (bool(em.emitter)) {
  2745. if (em.time > 0) {
  2746. em.time -= dt;
  2747. // Do we need to update the emitter's node due to the current shape changing?
  2748. if (imageShapeIndex != image.lastShapeIndex)
  2749. {
  2750. em.node = image.state->emitterNode[imageShapeIndex];
  2751. }
  2752. MatrixF mat;
  2753. getRenderImageTransform(imageSlot,em.node,&mat);
  2754. Point3F pos,axis;
  2755. mat.getColumn(3,&pos);
  2756. mat.getColumn(1,&axis);
  2757. em.emitter->emitParticles(pos,true,axis,getVelocity(),(U32) (dt * 1000));
  2758. }
  2759. else {
  2760. em.emitter->deleteWhenEmpty();
  2761. em.emitter = 0;
  2762. }
  2763. }
  2764. }
  2765. image.lastShapeIndex = imageShapeIndex;
  2766. }
  2767. //----------------------------------------------------------------------------
  2768. void ShapeBase::setImageScriptAnimPrefix(U32 imageSlot, NetStringHandle prefix)
  2769. {
  2770. MountedImage& image = mMountedImageList[imageSlot];
  2771. if (image.dataBlock) {
  2772. setMaskBits(ImageMaskN << imageSlot);
  2773. image.scriptAnimPrefix = prefix;
  2774. }
  2775. }
  2776. NetStringHandle ShapeBase::getImageScriptAnimPrefix(U32 imageSlot)
  2777. {
  2778. MountedImage& image = mMountedImageList[imageSlot];
  2779. return image.dataBlock? image.scriptAnimPrefix : NetStringHandle();
  2780. }
  2781. //----------------------------------------------------------------------------
  2782. U32 ShapeBase::getImageShapeIndex(const MountedImage& image) const
  2783. {
  2784. U32 shapeIndex = ShapeBaseImageData::StandardImageShape;
  2785. const ShapeBaseImageData* data = image.dataBlock;
  2786. if (data && data->useFirstPersonShape && isFirstPerson())
  2787. shapeIndex = ShapeBaseImageData::FirstPersonImageShape;
  2788. return shapeIndex;
  2789. }
  2790. //----------------------------------------------------------------------------
  2791. void ShapeBase::startImageEmitter(MountedImage& image,ShapeBaseImageData::StateData& state)
  2792. {
  2793. MountedImage::ImageEmitter* bem = 0;
  2794. MountedImage::ImageEmitter* em = image.emitter;
  2795. MountedImage::ImageEmitter* ee = &image.emitter[MaxImageEmitters];
  2796. U32 imageShapeIndex = getImageShapeIndex(image);
  2797. // If we are already emitting the same particles from the same
  2798. // node, then simply extend the time. Otherwise, find an empty
  2799. // emitter slot, or grab the one with the least amount of time left.
  2800. for (; em != ee; em++) {
  2801. if (bool(em->emitter)) {
  2802. if (state.emitter == em->emitter->getDataBlock() && state.emitterNode[imageShapeIndex] == em->node) {
  2803. if (state.emitterTime > em->time)
  2804. em->time = state.emitterTime;
  2805. return;
  2806. }
  2807. if (!bem || (bool(bem->emitter) && bem->time > em->time))
  2808. bem = em;
  2809. }
  2810. else
  2811. bem = em;
  2812. }
  2813. bem->time = state.emitterTime;
  2814. bem->node = state.emitterNode[imageShapeIndex];
  2815. bem->emitter = new ParticleEmitter;
  2816. bem->emitter->onNewDataBlock(state.emitter,false);
  2817. if( !bem->emitter->registerObject() )
  2818. {
  2819. bem->emitter.getPointer()->destroySelf();
  2820. bem->emitter = NULL;
  2821. }
  2822. }
  2823. void ShapeBase::submitLights( LightManager *lm, bool staticLighting )
  2824. {
  2825. if ( staticLighting )
  2826. return;
  2827. // Submit lights for MountedImage(s)
  2828. for ( S32 i = 0; i < MaxMountedImages; i++ )
  2829. {
  2830. ShapeBaseImageData *imageData = getMountedImage( i );
  2831. if ( imageData != NULL && imageData->lightType != ShapeBaseImageData::NoLight )
  2832. {
  2833. MountedImage &image = mMountedImageList[i];
  2834. F32 intensity;
  2835. switch ( imageData->lightType )
  2836. {
  2837. case ShapeBaseImageData::ConstantLight:
  2838. case ShapeBaseImageData::SpotLight:
  2839. intensity = 1.0f;
  2840. break;
  2841. case ShapeBaseImageData::PulsingLight:
  2842. intensity = 0.5f + 0.5f * mSin( M_PI_F * (F32)Sim::getCurrentTime() / (F32)imageData->lightDuration + image.lightStart );
  2843. intensity = 0.15f + intensity * 0.85f;
  2844. break;
  2845. case ShapeBaseImageData::WeaponFireLight:
  2846. {
  2847. S32 elapsed = Sim::getCurrentTime() - image.lightStart;
  2848. if ( elapsed > imageData->lightDuration )
  2849. continue;
  2850. intensity = ( 1.0 - (F32)elapsed / (F32)imageData->lightDuration ) * imageData->lightBrightness;
  2851. break;
  2852. }
  2853. default:
  2854. intensity = 1.0f;
  2855. return;
  2856. }
  2857. if ( !image.lightInfo )
  2858. image.lightInfo = LightManager::createLightInfo();
  2859. image.lightInfo->setColor( imageData->lightColor );
  2860. image.lightInfo->setBrightness( intensity );
  2861. image.lightInfo->setRange( imageData->lightRadius );
  2862. if ( imageData->lightType == ShapeBaseImageData::SpotLight )
  2863. {
  2864. image.lightInfo->setType( LightInfo::Spot );
  2865. // Do we want to expose these or not?
  2866. image.lightInfo->setInnerConeAngle( 15 );
  2867. image.lightInfo->setOuterConeAngle( 40 );
  2868. }
  2869. else
  2870. image.lightInfo->setType( LightInfo::Point );
  2871. MatrixF imageMat;
  2872. getRenderImageTransform( i, &imageMat );
  2873. image.lightInfo->setTransform( imageMat );
  2874. lm->registerGlobalLight( image.lightInfo, NULL );
  2875. }
  2876. }
  2877. }
  2878. //----------------------------------------------------------------------------
  2879. void ShapeBase::ejectShellCasing( U32 imageSlot )
  2880. {
  2881. MountedImage& image = mMountedImageList[imageSlot];
  2882. ShapeBaseImageData* imageData = image.dataBlock;
  2883. if (!imageData->casing)
  2884. return;
  2885. // Shell casings are client-side only, so use the render transform.
  2886. MatrixF ejectTrans;
  2887. getRenderImageTransform( imageSlot, imageData->ejectNode[getImageShapeIndex(image)], &ejectTrans );
  2888. Point3F ejectDir = imageData->shellExitDir;
  2889. ejectDir.normalize();
  2890. F32 ejectSpread = mDegToRad( imageData->shellExitVariance );
  2891. MatrixF ejectOrient = MathUtils::createOrientFromDir( ejectDir );
  2892. Point3F randomDir;
  2893. randomDir.x = mSin( gRandGen.randF( -ejectSpread, ejectSpread ) );
  2894. randomDir.y = 1.0;
  2895. randomDir.z = mSin( gRandGen.randF( -ejectSpread, ejectSpread ) );
  2896. randomDir.normalizeSafe();
  2897. ejectOrient.mulV( randomDir );
  2898. MatrixF imageTrans = getRenderTransform();
  2899. imageTrans.mulV( randomDir );
  2900. Point3F shellVel = randomDir * imageData->shellVelocity;
  2901. Point3F shellPos = ejectTrans.getPosition();
  2902. Debris *casing = new Debris;
  2903. casing->onNewDataBlock( imageData->casing, false );
  2904. casing->setTransform( imageTrans );
  2905. if (!casing->registerObject())
  2906. delete casing;
  2907. else
  2908. casing->init( shellPos, shellVel );
  2909. }
  2910. void ShapeBase::shakeCamera( U32 imageSlot )
  2911. {
  2912. MountedImage& image = mMountedImageList[imageSlot];
  2913. ShapeBaseImageData* imageData = image.dataBlock;
  2914. if (!imageData->shakeCamera)
  2915. return;
  2916. // Warning: this logic was duplicated from Explosion.
  2917. // first check if explosion is near camera
  2918. GameConnection* connection = GameConnection::getConnectionToServer();
  2919. ShapeBase *obj = dynamic_cast<ShapeBase*>(connection->getControlObject());
  2920. bool applyShake = true;
  2921. if (obj)
  2922. {
  2923. ShapeBase* cObj = obj;
  2924. while ((cObj = cObj->getControlObject()) != 0)
  2925. {
  2926. if (cObj->useObjsEyePoint())
  2927. {
  2928. applyShake = false;
  2929. break;
  2930. }
  2931. }
  2932. }
  2933. if (applyShake && obj)
  2934. {
  2935. VectorF diff;
  2936. getMuzzlePoint(imageSlot, &diff);
  2937. diff = obj->getPosition() - diff;
  2938. F32 dist = diff.len();
  2939. if (dist < imageData->camShakeRadius)
  2940. {
  2941. CameraShake *camShake = new CameraShake;
  2942. camShake->setDuration(imageData->camShakeDuration);
  2943. camShake->setFrequency(imageData->camShakeFreq);
  2944. F32 falloff = dist / imageData->camShakeRadius;
  2945. falloff = 1.0f + falloff * 10.0f;
  2946. falloff = 1.0f / (falloff * falloff);
  2947. VectorF shakeAmp = imageData->camShakeAmp * falloff;
  2948. camShake->setAmplitude(shakeAmp);
  2949. camShake->setFalloff(imageData->camShakeFalloff);
  2950. camShake->init();
  2951. gCamFXMgr.addFX(camShake);
  2952. }
  2953. }
  2954. }