shapeImage.cpp 122 KB

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