shapeImage.cpp 126 KB

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