shapeImage.cpp 126 KB

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