shapeImage.cpp 125 KB

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