shapeImage.cpp 127 KB

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