shapeImage.cpp 126 KB

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