shapeImage.cpp 125 KB

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