shapeImage.cpp 125 KB

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