2
0

shapeImage.cpp 127 KB

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