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