shapeImage.cpp 125 KB

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