shapeImage.cpp 124 KB

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