shapeImage.cpp 122 KB

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