action.cpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/action.cpp $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 3/19/02 11:31a $*
  29. * *
  30. * $Revision:: 266 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "action.h"
  36. #include "input.h"
  37. #include "persistfactory.h"
  38. #include "combatchunkid.h"
  39. #include "path.h"
  40. #include "pathsolve.h"
  41. #include "pathmgr.h"
  42. #include "vehicledriver.h"
  43. #include "pilot.h"
  44. #include "soldier.h"
  45. #include "vehicle.h"
  46. #include "weapons.h"
  47. #include "movephys.h"
  48. #include "pathfind.h"
  49. #include "pathfindportal.h"
  50. #include "debug.h"
  51. #include "animcontrol.h"
  52. #include "conversationmgr.h"
  53. #include "activeconversation.h"
  54. #include "orator.h"
  55. #include "soldierobserver.h"
  56. #include "physcon.h"
  57. #include "phys3.h"
  58. #include "humanphys.h"
  59. #include "pscene.h"
  60. #include "staticphys.h"
  61. #include "elevator.h"
  62. #include "doors.h"
  63. #include "pathaction.h"
  64. #include "crandom.h"
  65. #include "playertype.h"
  66. #include "wwprofile.h"
  67. #include "combat.h"
  68. #include "waypath.h"
  69. #include "waypoint.h"
  70. #include "gameobjmanager.h"
  71. #include "colmathaabox.h"
  72. #include "dinput.h"
  73. int _ActionActCalls = 0;
  74. int _ActionCodeChanges = 0;
  75. /*
  76. **
  77. */
  78. static bool Display_Findpaths = false;
  79. void Toggle_Display_Findpaths( void ) { Display_Findpaths = !Display_Findpaths; }
  80. /*
  81. ** ActionCodeClass
  82. */
  83. class ActionCodeClass : public PersistClass {
  84. public:
  85. typedef enum {
  86. ACTION_DONE,
  87. ACTION_IN_PROGRESS,
  88. } ActResult;
  89. ActionCodeClass( void ) : Action( NULL ) {}
  90. virtual ~ActionCodeClass( void ) {}
  91. /*
  92. ** Save / Load
  93. */
  94. enum {
  95. CHUNKID_PARENT = 1031001350,
  96. };
  97. virtual bool Save( ChunkSaveClass & csave )
  98. {
  99. csave.Begin_Chunk( CHUNKID_PARENT );
  100. PersistClass::Save( csave );
  101. csave.End_Chunk();
  102. return true;
  103. }
  104. virtual bool Load( ChunkLoadClass & cload )
  105. {
  106. while (cload.Open_Chunk()) {
  107. switch(cload.Cur_Chunk_ID()) {
  108. case CHUNKID_PARENT:
  109. PersistClass::Load( cload );
  110. break;
  111. default:
  112. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  113. break;
  114. }
  115. cload.Close_Chunk();
  116. }
  117. return true;
  118. }
  119. virtual void Init( ActionClass * action )
  120. {
  121. WWASSERT( action );
  122. WWASSERT( Action == NULL );
  123. Action = action;
  124. //
  125. // Check to see if we need to change the soldier's AI state
  126. //
  127. if ( Action->Get_Parameters().AIState != NO_AI_STATE_CHANGE ) {
  128. SmartGameObj * obj = Action->Get_Action_Obj();
  129. if ( obj != NULL ) {
  130. //
  131. // Check to ensure this is really a soldier
  132. //
  133. SoldierGameObj * soldier = obj->As_SoldierGameObj();
  134. if ( soldier != NULL ) {
  135. soldier->Set_AI_State( Action->Get_Parameters().AIState );
  136. }
  137. }
  138. }
  139. }
  140. virtual void Modify_Parameters( const SafeActionParamsStruct & parameters, bool modify_move, bool modify_attack ) {}
  141. virtual void Set_Action( ActionClass * action ) { WWASSERT( action ); WWASSERT( Action == NULL ); Action = action; }
  142. virtual void Shutdown( void ) { Action = NULL; }
  143. virtual ActResult Act( void ) { return ACTION_IN_PROGRESS; }
  144. virtual bool Is_Animating( void ) { return false; }
  145. virtual bool Is_Busy( void ) { return false; }
  146. virtual void Begin_Hibernation( void ) {}
  147. virtual void End_Hibernation( void ) {}
  148. protected:
  149. ActionClass * Action;
  150. };
  151. /*
  152. **
  153. */
  154. SimpleDynVecClass<ActionCodeClass*> ActionDeleteList;
  155. void Register_Action_Code_Deletion( ActionCodeClass * action )
  156. {
  157. ActionDeleteList.Add( action );
  158. }
  159. void Delete_Action_Code( void )
  160. {
  161. for ( int i = 0; i < ActionDeleteList.Count(); i++ ) {
  162. delete ActionDeleteList[i];
  163. }
  164. // Clear vector but don't allow shrink.
  165. ActionDeleteList.Delete_All(false);
  166. }
  167. /*
  168. ** Follow Input Action Code Class
  169. */
  170. class FollowInputActionCodeClass;
  171. SimplePersistFactoryClass<FollowInputActionCodeClass, CHUNKID_ACTION_CODE_FOLLOW_INPUT> _FollowInputActionCodeClassFactory;
  172. class FollowInputActionCodeClass : public ActionCodeClass {
  173. public:
  174. virtual const PersistFactoryClass & Get_Factory( void ) const { return _FollowInputActionCodeClassFactory; }
  175. /*
  176. ** Save / Load
  177. */
  178. enum {
  179. CHUNKID_PARENT = 1031001349,
  180. };
  181. virtual bool Save( ChunkSaveClass & csave )
  182. {
  183. csave.Begin_Chunk( CHUNKID_PARENT );
  184. ActionCodeClass::Save( csave );
  185. csave.End_Chunk();
  186. return true;
  187. }
  188. virtual bool Load( ChunkLoadClass & cload )
  189. {
  190. while (cload.Open_Chunk()) {
  191. switch(cload.Cur_Chunk_ID()) {
  192. case CHUNKID_PARENT:
  193. ActionCodeClass::Load( cload );
  194. break;
  195. default:
  196. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  197. break;
  198. }
  199. cload.Close_Chunk();
  200. }
  201. return true;
  202. }
  203. virtual ActResult Act( void )
  204. {
  205. float amount;
  206. SmartGameObj * obj = Action->Get_Action_Obj();
  207. // If we're in a vehicle, use the input function VEHICLE_TURN_LEFT...
  208. if ((obj->As_SoldierGameObj() != NULL) && (obj->As_SoldierGameObj()->Get_Vehicle() != NULL)) {
  209. amount = Input::Get_Amount( INPUT_FUNCTION_VEHICLE_TURN_LEFT ) - Input::Get_Amount( INPUT_FUNCTION_VEHICLE_TURN_RIGHT );
  210. } else {
  211. amount = Input::Get_Amount( INPUT_FUNCTION_TURN_LEFT ) - Input::Get_Amount( INPUT_FUNCTION_TURN_RIGHT );
  212. }
  213. obj->Set_Analog_Control( ControlClass::ANALOG_TURN_LEFT, amount );
  214. float forward_amount = Input::Get_Amount( INPUT_FUNCTION_MOVE_FORWARD ) - Input::Get_Amount( INPUT_FUNCTION_MOVE_BACKWARD );
  215. float left_amount = Input::Get_Amount( INPUT_FUNCTION_MOVE_LEFT ) - Input::Get_Amount( INPUT_FUNCTION_MOVE_RIGHT );
  216. float up_amount = Input::Get_Amount( INPUT_FUNCTION_MOVE_UP ) - Input::Get_Amount( INPUT_FUNCTION_MOVE_DOWN );
  217. if ( obj->As_SoldierGameObj() && obj->As_SoldierGameObj()->Get_Vehicle() ) {
  218. VehicleGameObj * vehicle = obj->As_SoldierGameObj()->Get_Vehicle();
  219. // Convert turn to straffe
  220. if ( vehicle->Is_Aircraft() && (Input::Is_Button_Down (DIK_LCONTROL) || Input::Is_Button_Down (DIK_LMENU) ) ) {
  221. left_amount = amount;
  222. obj->Set_Analog_Control( ControlClass::ANALOG_TURN_LEFT, 0 );
  223. }
  224. }
  225. Vector3 move( forward_amount, left_amount, up_amount );
  226. float length = move.Length();
  227. if ( length > 1 ) {
  228. forward_amount /= length;
  229. left_amount /= length;
  230. }
  231. #if 0
  232. if ( Input::Get_State( INPUT_FUNCTION_WALK_MODE ) ) {
  233. forward_amount /= 4;
  234. left_amount /= 4;
  235. }
  236. #else
  237. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WALK, Input::Get_State( INPUT_FUNCTION_WALK_MODE ) );
  238. #endif
  239. obj->Set_Analog_Control( ControlClass::ANALOG_MOVE_FORWARD, forward_amount );
  240. obj->Set_Analog_Control( ControlClass::ANALOG_MOVE_LEFT, left_amount );
  241. obj->Set_Analog_Control( ControlClass::ANALOG_MOVE_UP, up_amount );
  242. obj->Set_Boolean_Control( ControlClass::BOOLEAN_JUMP, Input::Get_State( INPUT_FUNCTION_JUMP ) );
  243. obj->Set_Boolean_Control( ControlClass::BOOLEAN_VEHICLE_TOGGLE_GUNNER, Input::Get_State( INPUT_FUNCTION_VEHICLE_TOGGLE_GUNNER ) );
  244. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_NEXT, Input::Get_State( INPUT_FUNCTION_NEXT_WEAPON ) );
  245. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_PREV, Input::Get_State( INPUT_FUNCTION_PREV_WEAPON ) );
  246. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_FIRE_PRIMARY, Input::Get_State( INPUT_FUNCTION_FIRE_WEAPON_PRIMARY ) );
  247. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_FIRE_SECONDARY, Input::Get_State( INPUT_FUNCTION_FIRE_WEAPON_SECONDARY ) );
  248. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_USE, Input::Get_State( INPUT_FUNCTION_USE_WEAPON ) );
  249. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_RELOAD, Input::Get_State( INPUT_FUNCTION_RELOAD_WEAPON ) );
  250. obj->Set_Boolean_Control( ControlClass::BOOLEAN_DIVE_FORWARD, Input::Get_State( INPUT_FUNCTION_DIVE_FORWARD ) );
  251. obj->Set_Boolean_Control( ControlClass::BOOLEAN_DIVE_BACKWARD, Input::Get_State( INPUT_FUNCTION_DIVE_BACKWARD ) );
  252. obj->Set_Boolean_Control( ControlClass::BOOLEAN_DIVE_LEFT, Input::Get_State( INPUT_FUNCTION_DIVE_LEFT ) );
  253. obj->Set_Boolean_Control( ControlClass::BOOLEAN_DIVE_RIGHT, Input::Get_State( INPUT_FUNCTION_DIVE_RIGHT ) );
  254. obj->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH, Input::Get_State( INPUT_FUNCTION_CROUCH ) );
  255. obj->Set_Boolean_Control( ControlClass::BOOLEAN_ACTION, Input::Get_State( INPUT_FUNCTION_ACTION ) );
  256. obj->Set_Boolean_Control( ControlClass::BOOLEAN_DROP_FLAG, Input::Get_State( INPUT_FUNCTION_DROP_FLAG ) );
  257. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_NO_WEAPON, Input::Get_State( INPUT_FUNCTION_SELECT_NO_WEAPON ) );
  258. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_0, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_0 ) );
  259. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_1, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_1 ) );
  260. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_2, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_2 ) );
  261. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_3, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_3 ) );
  262. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_4, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_4 ) );
  263. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_5, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_5 ) );
  264. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_6, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_6 ) );
  265. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_7, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_7 ) );
  266. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_8, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_8 ) );
  267. obj->Set_Boolean_Control( ControlClass::BOOLEAN_SELECT_WEAPON_9, Input::Get_State( INPUT_FUNCTION_SELECT_WEAPON_9 ) );
  268. return ACTION_IN_PROGRESS;
  269. }
  270. };
  271. /*
  272. ** Stand Action Code Class
  273. */
  274. class StandActionCodeClass;
  275. SimplePersistFactoryClass<StandActionCodeClass, CHUNKID_ACTION_CODE_STAND> _StandActionCodeClassFactory;
  276. class StandActionCodeClass : public ActionCodeClass {
  277. public:
  278. virtual const PersistFactoryClass & Get_Factory( void ) const { return _StandActionCodeClassFactory; }
  279. /*
  280. ** Save / Load
  281. */
  282. enum {
  283. CHUNKID_PARENT = 1031001348,
  284. };
  285. virtual bool Save( ChunkSaveClass & csave )
  286. {
  287. csave.Begin_Chunk( CHUNKID_PARENT );
  288. ActionCodeClass::Save( csave );
  289. csave.End_Chunk();
  290. return true;
  291. }
  292. virtual bool Load( ChunkLoadClass & cload )
  293. {
  294. while (cload.Open_Chunk()) {
  295. switch(cload.Cur_Chunk_ID()) {
  296. case CHUNKID_PARENT:
  297. ActionCodeClass::Load( cload );
  298. break;
  299. default:
  300. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  301. break;
  302. }
  303. cload.Close_Chunk();
  304. }
  305. return true;
  306. }
  307. virtual ActResult Act( void )
  308. {
  309. return ACTION_IN_PROGRESS;
  310. }
  311. };
  312. /*
  313. ** Play Animation Action Code Class
  314. */
  315. class PlayAnimationActionCodeClass;
  316. SimplePersistFactoryClass<PlayAnimationActionCodeClass, CHUNKID_ACTION_CODE_PLAY_ANIMATION> _PlayAnimationActionCodeClassFactory;
  317. class PlayAnimationActionCodeClass : public ActionCodeClass {
  318. public:
  319. virtual const PersistFactoryClass & Get_Factory( void ) const { return _PlayAnimationActionCodeClassFactory; }
  320. /*
  321. ** Save / Load
  322. */
  323. enum {
  324. CHUNKID_PARENT = 1031001347,
  325. };
  326. virtual bool Save( ChunkSaveClass & csave )
  327. {
  328. csave.Begin_Chunk( CHUNKID_PARENT );
  329. ActionCodeClass::Save( csave );
  330. csave.End_Chunk();
  331. return true;
  332. }
  333. virtual bool Load( ChunkLoadClass & cload )
  334. {
  335. while (cload.Open_Chunk()) {
  336. switch(cload.Cur_Chunk_ID()) {
  337. case CHUNKID_PARENT:
  338. ActionCodeClass::Load( cload );
  339. break;
  340. default:
  341. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  342. break;
  343. }
  344. cload.Close_Chunk();
  345. }
  346. return true;
  347. }
  348. virtual bool Is_Animating( void ) { return true; }
  349. virtual void Init( ActionClass * action )
  350. {
  351. ActionCodeClass::Init( action );
  352. SmartGameObj * obj = Action->Get_Action_Obj();
  353. WWASSERT( obj->Get_Anim_Control() != NULL );
  354. obj->Set_Animation( action->Get_Parameters().SafeAnimationName, action->Get_Parameters().AnimationLooping );
  355. }
  356. virtual void Shutdown( void )
  357. {
  358. SmartGameObj * obj = Action->Get_Action_Obj();
  359. WWASSERT( obj->Get_Anim_Control() != NULL );
  360. // WWASSERT( obj->Get_Anim_Control()->Get_Animation_Name()[0] != 0 );
  361. obj->Set_Animation( NULL, false );
  362. ActionCodeClass::Shutdown();
  363. }
  364. virtual ActResult Act( void )
  365. {
  366. SmartGameObj * obj = Action->Get_Action_Obj();
  367. WWASSERT( obj->Get_Anim_Control() != NULL );
  368. if ( obj->Get_Anim_Control()->Get_Animation_Name()[0] == 0 ) {
  369. Debug_Say(( "Not playing an anim when we should be playing %s\n", Action->Get_Parameters().SafeAnimationName ));
  370. Action->Done( ACTION_COMPLETE_NORMAL );
  371. return ACTION_DONE;
  372. }
  373. // WWASSERT( obj->Get_Anim_Control()->Get_Animation_Name()[0] != 0 );
  374. if ( obj->Get_Anim_Control()->Is_Complete() ) {
  375. Action->Done( ACTION_COMPLETE_NORMAL );
  376. return ACTION_DONE;
  377. }
  378. return ACTION_IN_PROGRESS;
  379. }
  380. };
  381. /*
  382. ** Base Action Code Class
  383. **
  384. ** This action will move and/or shoot context Object to the context Locations
  385. ** From the outside, you can set the moveto location, and you can set the
  386. ** shootAt location. The last one you set sets the moving flag, which
  387. ** is ignored if object can move and shoot independently.
  388. ** Need to know:
  389. Can object move and fire independantly( if turreted || moves laterally )
  390. Is Object Turreted ( gun can face a different direction than object, just tanks )
  391. Can object move laterally (tanks and cars cannot, humans can)
  392. Can object turn without moving ( cars cannot )
  393. */
  394. class BaseActionCodeClass : public ActionCodeClass {
  395. public:
  396. BaseActionCodeClass( void )
  397. {
  398. IsMoving = false;
  399. IsAttacking = false;
  400. }
  401. /*
  402. ** Save / Load
  403. */
  404. enum {
  405. CHUNKID_PARENT = 1031001345,
  406. CHUNKID_VARIABLES,
  407. CHUNKID_VEHICLE_DRIVER,
  408. CHUNKID_PILOT,
  409. MICROCHUNKID_IS_ATTACKING = 1,
  410. MICROCHUNKID_IS_MOVING = 2,
  411. };
  412. virtual bool Save( ChunkSaveClass & csave )
  413. {
  414. csave.Begin_Chunk( CHUNKID_PARENT );
  415. ActionCodeClass::Save( csave );
  416. csave.End_Chunk();
  417. csave.Begin_Chunk( CHUNKID_VARIABLES );
  418. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IS_ATTACKING, IsAttacking );
  419. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IS_MOVING, IsMoving );
  420. csave.End_Chunk();
  421. csave.Begin_Chunk( CHUNKID_VEHICLE_DRIVER );
  422. VehicleDriver.Save( csave );
  423. csave.End_Chunk();
  424. csave.Begin_Chunk( CHUNKID_PILOT );
  425. Pilot.Save( csave );
  426. csave.End_Chunk();
  427. return true;
  428. }
  429. virtual bool Load( ChunkLoadClass & cload )
  430. {
  431. while (cload.Open_Chunk()) {
  432. switch(cload.Cur_Chunk_ID()) {
  433. case CHUNKID_PARENT:
  434. ActionCodeClass::Load( cload );
  435. break;
  436. case CHUNKID_VARIABLES:
  437. while (cload.Open_Micro_Chunk()) {
  438. switch(cload.Cur_Micro_Chunk_ID()) {
  439. READ_MICRO_CHUNK( cload, MICROCHUNKID_IS_ATTACKING, IsAttacking );
  440. READ_MICRO_CHUNK( cload, MICROCHUNKID_IS_MOVING, IsMoving );
  441. default:
  442. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  443. break;
  444. }
  445. cload.Close_Micro_Chunk();
  446. }
  447. break;
  448. case CHUNKID_VEHICLE_DRIVER:
  449. VehicleDriver.Load( cload );
  450. break;
  451. case CHUNKID_PILOT:
  452. Pilot.Load( cload );
  453. break;
  454. default:
  455. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  456. break;
  457. }
  458. cload.Close_Chunk();
  459. }
  460. return true;
  461. }
  462. // Return a pointer to the object that will be doing the movement
  463. SmartGameObj *Peek_Transport_Object( SmartGameObj *game_obj )
  464. {
  465. SmartGameObj *transport = game_obj;
  466. if ( game_obj->As_SoldierGameObj() != NULL &&
  467. game_obj->As_SoldierGameObj()->Get_Profile_Vehicle() != NULL ) {
  468. transport = game_obj->As_SoldierGameObj()->Get_Profile_Vehicle();
  469. }
  470. return transport;
  471. }
  472. void Clamped_Units( Vector3& value, float rate )
  473. {
  474. // convert to "units", and clamp
  475. rate *= TimeManager::Get_Frame_Seconds();
  476. if ( rate > 0.0001f ) {
  477. value /= rate;
  478. }
  479. if ( (rate <= 0.0001f) || value.Length() > 1.0 ) {
  480. value.Normalize();
  481. }
  482. }
  483. // Move Human object to given relative position
  484. void Human_Move_To_Relative( Vector3 & orig_rel_pos )
  485. {
  486. //Debug_Say(( "Mov To Rel %f %f %f\n", rel_pos.X, rel_pos.Y, rel_pos.Z ));
  487. SmartGameObj * obj = Action->Get_Action_Obj();
  488. float speed = Action->Get_Parameters().MoveSpeed;
  489. Vector3 move = orig_rel_pos;
  490. //
  491. // Zero out the relative distance in the z axis (if necessary)
  492. //
  493. SoldierGameObj * soldier = obj->As_SoldierGameObj();
  494. if ( soldier == NULL || soldier->Is_On_Ladder() == false) {
  495. move.Z = 0;
  496. }
  497. Clamped_Units( move, obj->Get_Max_Speed() * speed );
  498. if ( move.Length2() > 1 ) {
  499. move.Normalize();
  500. }
  501. obj->Set_Analog_Control( ControlClass::ANALOG_MOVE_FORWARD, move[0] * speed );
  502. obj->Set_Analog_Control( ControlClass::ANALOG_MOVE_LEFT, move[1] * speed );
  503. // Special case for soldier moving
  504. if ( soldier && soldier->Is_On_Ladder() ) {
  505. obj->Set_Analog_Control( ControlClass::ANALOG_MOVE_FORWARD, (move[2] >= 0) ? 1 : -1 ); // Could we make this move up?
  506. }
  507. }
  508. // Move object to given relative position, return if complete
  509. bool Move_To_Absolute( const Vector3 & abs_pos, bool ignore_arrived_dist = false )
  510. {
  511. SmartGameObj * obj = Action->Get_Action_Obj();
  512. // Should the vehicle AI code take over?
  513. SmartGameObj *transport_obj = Peek_Transport_Object( obj );
  514. VehicleGameObj *vehicle = transport_obj->As_VehicleGameObj ();
  515. if ( vehicle != NULL ) {
  516. bool retval = false;
  517. if (vehicle->Is_Aircraft ()) {
  518. Pilot.Set_Speed_Factor( Action->Get_Parameters().MoveSpeed );
  519. if ( Action->Get_Parameters().AttackObject != NULL ) {
  520. Vector3 target_pos;
  521. Action->Get_Parameters().AttackObject->Get_Position( &target_pos );
  522. Pilot.Set_Target( &target_pos );
  523. }
  524. Pilot.Set_Speed_Factor( Action->Get_Parameters().MoveSpeed );
  525. Pilot.Set_Arrived_Dist( Action->Get_Parameters().MoveArrivedDistance );
  526. retval = Pilot.Think();
  527. } else {
  528. VehicleDriver.Set_Next_Position( abs_pos );
  529. VehicleDriver.Set_Speed_Factor( Action->Get_Parameters().MoveSpeed );
  530. VehicleDriver.Set_Arrived_Dist( Action->Get_Parameters().MoveArrivedDistance );
  531. VehicleDriver.Force_Backup( Action->Get_Parameters().MoveBackup );
  532. retval = VehicleDriver.Drive();
  533. }
  534. IsMoving = true;
  535. return retval;
  536. }
  537. // If we want to be facing a particular position, set targeting to it.
  538. if ( Action->Get_Parameters().ForceFacing ) {
  539. obj->Set_Targeting( Action->Get_Parameters().FaceLocation, false ); // Don't tilt
  540. }
  541. Vector3 curr_pos;
  542. obj->Get_Position( &curr_pos );
  543. curr_pos -= abs_pos;
  544. // I'm having soldiers get stuck because they are directly below
  545. // the goal point. Lets try ignoring Z
  546. curr_pos.Z = 0;
  547. float range = curr_pos.Length();
  548. SoldierGameObj *soldier = obj->As_SoldierGameObj();
  549. bool done_facing = true; // Assume we are done facing
  550. //
  551. // If we are not attacking, and not force facing, and are a soldier and
  552. // are not on a ladder and not very close
  553. //
  554. if ( !IsAttacking && !Action->Get_Parameters().ForceFacing &&
  555. !Action->Get_Parameters().IgnoreFacing && soldier &&
  556. !soldier->Is_On_Ladder() && (range > 0.1f ) )
  557. {
  558. // Then try to face the point we are moving to
  559. Vector3 face_pt = abs_pos + Vector3( 0,0, obj->Get_Bullseye_Offset_Z() );
  560. done_facing = obj->Set_Targeting( face_pt, false );
  561. }
  562. //
  563. // Determine how close we want to get to the end-point before
  564. // we stop moving
  565. //
  566. float arrived_dist = Action->Get_Parameters().MoveArrivedDistance;
  567. if ( ignore_arrived_dist ) {
  568. arrived_dist = 0.05F;
  569. }
  570. //
  571. // If we are close enough
  572. //
  573. if ( (Get_Distance_From_Goal() + range) <= arrived_dist) {
  574. IsMoving = false;
  575. return done_facing; // complete;
  576. }
  577. IsMoving = true;
  578. // Find the relitive location
  579. Vector3 rel_pos;
  580. Matrix3D::Inverse_Transform_Vector( obj->Get_Transform(), abs_pos, &rel_pos );
  581. // we done facing, or facing close enough, move
  582. float goal_bearing = WWMath::Atan2( rel_pos.Y, rel_pos.X );
  583. if ( done_facing || WWMath::Fabs(goal_bearing) < DEG_TO_RAD( 40 ) ) {
  584. Human_Move_To_Relative( rel_pos );
  585. }
  586. return false;
  587. }
  588. virtual float Get_Distance_From_Goal (void)
  589. {
  590. SmartGameObj * obj = Action->Get_Action_Obj();
  591. //
  592. // Get the object's current position
  593. //
  594. Vector3 curr_pos;
  595. obj->Get_Position( &curr_pos );
  596. //
  597. // Find the difference between the current position and
  598. // the destination
  599. //
  600. curr_pos -= Action->Get_Parameters().MoveLocation;
  601. curr_pos.Z = 0;
  602. //
  603. // Return the length
  604. //
  605. return curr_pos.Length ();
  606. }
  607. protected:
  608. bool IsAttacking;
  609. bool IsMoving;
  610. VehicleDriverClass VehicleDriver;
  611. PilotClass Pilot;
  612. };
  613. /*
  614. ** Goto Action Code Class
  615. */
  616. class GotoActionCodeClass;
  617. SimplePersistFactoryClass<GotoActionCodeClass, CHUNKID_ACTION_CODE_GOTO> _GotoActionCodeClassFactory;
  618. class GotoActionCodeClass : public BaseActionCodeClass {
  619. public:
  620. virtual const PersistFactoryClass & Get_Factory( void ) const { return _GotoActionCodeClassFactory; }
  621. GotoActionCodeClass( void ) :
  622. UseMoveObject( false ),
  623. FirstCall( true ),
  624. PathSolver( NULL ),
  625. Path( NULL )
  626. {
  627. }
  628. ~GotoActionCodeClass( void )
  629. {
  630. return ;
  631. }
  632. /*
  633. ** Save / Load
  634. */
  635. enum {
  636. CHUNKID_PARENT = 1031001308,
  637. CHUNKID_VARIABLES,
  638. CHUNKID_PATH,
  639. CHUNKID_PATH_SOLVER,
  640. CHUNKID_PATH_ACTION,
  641. MICROCHUNKID_FIRST_CALL = 1,
  642. MICROCHUNKID_USE_MOVE_OBJECT = 2,
  643. MICROCHUNKID_TARGET_PREV_POS = 3,
  644. MICROCHUNKID_PATH_SOLVE_PTR = 4,
  645. };
  646. virtual bool Save( ChunkSaveClass & csave )
  647. {
  648. csave.Begin_Chunk( CHUNKID_PARENT );
  649. BaseActionCodeClass::Save( csave );
  650. csave.End_Chunk();
  651. if ( Path != NULL ) {
  652. csave.Begin_Chunk( CHUNKID_PATH );
  653. Path->Save( csave );
  654. csave.End_Chunk();
  655. }
  656. csave.Begin_Chunk( CHUNKID_VARIABLES );
  657. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_FIRST_CALL, FirstCall );
  658. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_USE_MOVE_OBJECT, UseMoveObject );
  659. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_TARGET_PREV_POS, TargetPrevPos );
  660. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_PATH_SOLVE_PTR, PathSolver );
  661. csave.End_Chunk();
  662. csave.Begin_Chunk( CHUNKID_PATH_ACTION );
  663. PathAction.Save( csave );
  664. csave.End_Chunk();
  665. return true;
  666. }
  667. virtual bool Load( ChunkLoadClass & cload )
  668. {
  669. while (cload.Open_Chunk()) {
  670. switch(cload.Cur_Chunk_ID()) {
  671. case CHUNKID_PARENT:
  672. BaseActionCodeClass::Load( cload );
  673. break;
  674. case CHUNKID_VARIABLES:
  675. while (cload.Open_Micro_Chunk()) {
  676. switch(cload.Cur_Micro_Chunk_ID()) {
  677. READ_MICRO_CHUNK( cload, MICROCHUNKID_FIRST_CALL, FirstCall );
  678. READ_MICRO_CHUNK( cload, MICROCHUNKID_USE_MOVE_OBJECT, UseMoveObject );
  679. READ_MICRO_CHUNK( cload, MICROCHUNKID_TARGET_PREV_POS, TargetPrevPos );
  680. case MICROCHUNKID_PATH_SOLVE_PTR:
  681. LOAD_MICRO_CHUNK( cload, PathSolver );
  682. if (PathSolver != NULL) {
  683. REQUEST_REF_COUNTED_POINTER_REMAP( (RefCountClass **)&PathSolver );
  684. }
  685. break;
  686. default:
  687. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  688. break;
  689. }
  690. cload.Close_Micro_Chunk();
  691. }
  692. break;
  693. case CHUNKID_PATH:
  694. {
  695. Path = new PathClass;
  696. Path->Load( cload );
  697. }
  698. break;
  699. case CHUNKID_PATH_ACTION:
  700. PathAction.Load( cload );
  701. break;
  702. default:
  703. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  704. break;
  705. }
  706. cload.Close_Chunk();
  707. }
  708. return true;
  709. }
  710. virtual void Begin_Hibernation( void )
  711. {
  712. BaseActionCodeClass::Begin_Hibernation();
  713. FirstCall = true;
  714. //
  715. // Return the path we got from the manager
  716. //
  717. if ( PathSolver != NULL ) {
  718. PathMgrClass::Return_Path_Object( PathSolver );
  719. PathSolver = NULL;
  720. }
  721. return ;
  722. }
  723. virtual void Init( ActionClass * action )
  724. {
  725. BaseActionCodeClass::Init( action );
  726. FirstCall = true;
  727. SoldierGameObj *soldier_obj = Action->Get_Action_Obj()->As_SoldierGameObj();
  728. if ( soldier_obj && Action->Get_Parameters().LookDuration != 0 ) {
  729. soldier_obj->Look_At( Action->Get_Parameters().LookLocation, Action->Get_Parameters().LookDuration );
  730. }
  731. UseMoveObject = (Action->Get_Parameters().MoveObjectRef.Get_Ptr() != NULL);
  732. }
  733. virtual void Shutdown( void )
  734. {
  735. //
  736. // Return the path we got from the manager
  737. //
  738. if (PathSolver != NULL) {
  739. PathMgrClass::Return_Path_Object (PathSolver);
  740. PathSolver = NULL;
  741. }
  742. Release_Path();
  743. BaseActionCodeClass::Shutdown();
  744. return ;
  745. }
  746. virtual void Init( WaypathClass *waypath )
  747. {
  748. //
  749. // Return the path we got from the manager
  750. //
  751. if (PathSolver != NULL) {
  752. PathMgrClass::Return_Path_Object (PathSolver);
  753. PathSolver = NULL;
  754. }
  755. Release_Path();
  756. Prepare_Path ();
  757. Path->Set_Traversal_Type( PathClass::LINEAR );
  758. Path->Initialize( waypath, -1, -1 );
  759. Initialize_Vehicle_AI();
  760. return ;
  761. }
  762. virtual bool Is_Busy( void )
  763. {
  764. return ( PathAction.Get_State () != PathActionClass::STATE_FINISHED );
  765. }
  766. virtual ActResult Arrived()
  767. {
  768. if ( Action->Get_Parameters().MoveFollow ) {
  769. return ACTION_IN_PROGRESS; // Do nothing special when we arrive
  770. }
  771. if ( Action->Get_Action_Obj() && Action->Get_Parameters().ShutdownEngineOnArrival ) {
  772. SmartGameObj *transport_obj = Peek_Transport_Object( Action->Get_Action_Obj() );
  773. if ( transport_obj && transport_obj->As_VehicleGameObj() ) {
  774. transport_obj->As_VehicleGameObj()->Enable_Engine (false);
  775. }
  776. }
  777. Action->Done( ACTION_COMPLETE_NORMAL );
  778. return ACTION_DONE; // We are done
  779. }
  780. virtual ActResult Beeline( void )
  781. {
  782. //
  783. // Make sure we have a valid path object to follow
  784. //
  785. if ( Path == NULL ) {
  786. Prepare_Path();
  787. Vector3 curr_pos;
  788. Action->Get_Action_Obj()->Get_Position( &curr_pos );
  789. Path->Initialize( curr_pos, Action->Get_Parameters().MoveLocation );
  790. Initialize_Vehicle_AI();
  791. }
  792. ActResult result = ACTION_IN_PROGRESS;
  793. if ( Path != NULL ) {
  794. result = Traverse_Path ();
  795. }
  796. // Tell the unit to beeline w/o using pathfind
  797. /*ActResult result = ACTION_IN_PROGRESS;
  798. if ( Move_To_Absolute( Action->Get_Parameters().MoveLocation ) ) {
  799. result = Arrived();
  800. }*/
  801. return result;
  802. }
  803. void Prepare_Path( void )
  804. {
  805. //
  806. // Allocate a path object (if necessary)
  807. //
  808. if ( Path == NULL ) {
  809. Path = new PathClass;
  810. }
  811. //
  812. // Setup the path traverser
  813. //
  814. PathObjectClass path_obj;
  815. Configure_Path_Object( path_obj );
  816. //
  817. // Initialize the path
  818. //
  819. Path->Set_Path_Object( path_obj );
  820. return ;
  821. }
  822. void Initialize_Vehicle_AI( void )
  823. {
  824. //
  825. // Get the transport object
  826. //
  827. SmartGameObj *transport_obj = Peek_Transport_Object( Action->Get_Action_Obj() );
  828. //
  829. // Initialize the different driving/flying AI's
  830. //
  831. VehicleDriver.Initialize( transport_obj, Path );
  832. VehicleDriver.Force_Backup( Action->Get_Parameters().MoveBackup );
  833. Pilot.Initialize( transport_obj );
  834. Pilot.Set_Mode( PilotClass::MODE_FLY_TO_POINT );
  835. Pilot.Set_Current_Path ( Path );
  836. Pilot.Set_Arrived_Dist( Action->Get_Parameters().MoveArrivedDistance );
  837. Pilot.Set_Is_Exact_Z_Important( bool(Action->Get_Parameters().WaypathID != 0) );
  838. //
  839. // Check to make sure we aren't trying to drive a "simple" vehicle around
  840. //
  841. if ( transport_obj != NULL && transport_obj->As_VehicleGameObj () != NULL &&
  842. transport_obj->Peek_Physical_Object () != NULL &&
  843. transport_obj->Peek_Physical_Object ()->As_MoveablePhysClass () == NULL )
  844. {
  845. //
  846. // Warn the user that they are trying to drive a non-driveable object
  847. //
  848. Debug_Say (("Warning! Attempting to drive a non-moveable vehicle. ID = %d.\n", transport_obj->Get_ID ()));
  849. }
  850. return ;
  851. }
  852. void Initialize_Waypath( void )
  853. {
  854. Prepare_Path ();
  855. //
  856. // Lookup the waypath object
  857. //
  858. WaypathClass *waypath = PathfindClass::Get_Instance()->Find_Waypath( Action->Get_Parameters().WaypathID );
  859. if ( waypath == NULL ) {
  860. Debug_Say (("Warning! Waypath %d does not exist.\n", Action->Get_Parameters().WaypathID));
  861. }
  862. WWASSERT( waypath != NULL );
  863. //
  864. // Initialize the path
  865. //
  866. Path->Set_Traversal_Type( Action->Get_Parameters().WaypathSplined ? PathClass::SPLINE : PathClass::LINEAR );
  867. Path->Initialize( waypath, Action->Get_Parameters().WaypointStartID, Action->Get_Parameters().WaypointEndID );
  868. return ;
  869. }
  870. void Configure_Path_Object( PathObjectClass &path_obj )
  871. {
  872. //
  873. // Determine which game object will actually be doing the moving along the path
  874. //
  875. SmartGameObj *transport_obj = Peek_Transport_Object( Action->Get_Action_Obj() );
  876. WWASSERT( transport_obj != NULL );
  877. //
  878. // Determine the turn radius
  879. //
  880. float turn_radius = 0;
  881. VehicleGameObj *vehicle_obj = transport_obj->As_VehicleGameObj();
  882. if ( vehicle_obj != NULL ) {
  883. turn_radius = vehicle_obj->Get_Turn_Radius();
  884. }
  885. //
  886. // Dig out the physics object that will be moving along the path
  887. //
  888. MoveablePhysClass *phys_obj = transport_obj->Peek_Physical_Object()->As_MoveablePhysClass();
  889. if (phys_obj != NULL) {
  890. //
  891. // Configure the path-traverser object
  892. //
  893. path_obj.Initialize( *phys_obj );
  894. path_obj.Set_Max_Speed( transport_obj->Get_Max_Speed () );
  895. path_obj.Set_Turn_Radius( turn_radius );
  896. path_obj.Set_Flag( PathObjectClass::IS_VEHICLE, bool(vehicle_obj != NULL) );
  897. }
  898. //
  899. // Pass the key information onto the path object
  900. //
  901. SoldierGameObj *soldier = Action->Get_Action_Obj()->As_SoldierGameObj();
  902. if ( soldier != NULL ) {
  903. path_obj.Set_Key_Ring( soldier->Get_Key_Ring () );
  904. }
  905. return ;
  906. }
  907. void Initialize_Path_Solver( void )
  908. {
  909. //
  910. // Get a new path object from the manager (if necessary)
  911. //
  912. if (PathSolver == NULL) {
  913. PathSolver = PathMgrClass::Request_Path_Object();
  914. }
  915. //
  916. // Get the destination position
  917. //
  918. Vector3 dest_pos = Action->Get_Parameters().MoveLocation;
  919. TargetPrevPos = Action->Get_Parameters().MoveLocation;
  920. //
  921. // Setup the path traverser
  922. //
  923. PathObjectClass path_obj;
  924. Configure_Path_Object( path_obj );
  925. //
  926. // Initialize the path itself
  927. //
  928. Vector3 cur_pos;
  929. Peek_Transport_Object (Action->Get_Action_Obj())->Get_Position( &cur_pos );
  930. PathSolver->Set_Path_Object( path_obj );
  931. PathSolver->Reset( cur_pos, dest_pos, 2.0F );
  932. return ;
  933. }
  934. void Initialize_Path( void )
  935. {
  936. Release_Path();
  937. //
  938. // Determine if we should use a waypath instead of trying to solve
  939. // the path dynamically
  940. //
  941. if ( Action->Get_Parameters().WaypathID != 0 ) {
  942. Initialize_Waypath();
  943. Initialize_Vehicle_AI();
  944. } else {
  945. Initialize_Path_Solver();
  946. }
  947. return ;
  948. }
  949. void Release_Path( void )
  950. {
  951. Pilot.Reset();
  952. VehicleDriver.Reset();
  953. REF_PTR_RELEASE( Path );
  954. // Reset the PathAction class too - otherwise it keeps the pointer we game it to Path. ST - 9/6/2001 10:36AM
  955. PathAction.Reset();
  956. return ;
  957. }
  958. bool
  959. Clip_Point (Vector3 *point, const AABoxClass &box)
  960. {
  961. Vector3 temp_point = *point;
  962. //
  963. // Clip the temporary point
  964. //
  965. temp_point.X = max (temp_point.X, box.Center.X - box.Extent.X);
  966. temp_point.Y = max (temp_point.Y, box.Center.Y - box.Extent.Y);
  967. temp_point.Z = max (temp_point.Z, box.Center.Z - box.Extent.Z);
  968. temp_point.X = min (temp_point.X, box.Center.X + box.Extent.X);
  969. temp_point.Y = min (temp_point.Y, box.Center.Y + box.Extent.Y);
  970. temp_point.Z = min (temp_point.Z, box.Center.Z + box.Extent.Z);
  971. //
  972. // Did the clip change the point?
  973. //
  974. bool retval = (point->X != temp_point.X);
  975. retval |= (point->Y != temp_point.Y);
  976. retval |= (point->Z != temp_point.Z);
  977. //
  978. // Pass the new point back to the caller
  979. //
  980. (*point) = temp_point;
  981. return retval;
  982. }
  983. virtual ActResult Traverse_Path ( void )
  984. {
  985. ActResult act_result = ACTION_IN_PROGRESS;
  986. //
  987. // Let the pathfind system know which directions we can (and are) moving
  988. //
  989. Path->Set_Movement_Directions( PathClass::MOVE_X | PathClass::MOVE_Y );
  990. //
  991. // Is the soldier only moving in Z?
  992. //
  993. SmartGameObj *obj = Action->Get_Action_Obj();
  994. SoldierGameObj *soldier = obj->As_SoldierGameObj();
  995. if( soldier != NULL ) {
  996. if ( soldier->Is_On_Ladder() ) {
  997. Path->Set_Movement_Directions( PathClass::MOVE_Z );
  998. }
  999. }
  1000. //
  1001. // Get the object's current position
  1002. //
  1003. Vector3 curr_pos;
  1004. obj->Get_Position( &curr_pos );
  1005. //
  1006. // Determine where the object should go next
  1007. //
  1008. Vector3 next_pos;
  1009. if( Path->Evaluate_Next_Point( curr_pos, next_pos ) ) {
  1010. /*return act_result;
  1011. }*/
  1012. //
  1013. // Allow debug display of the path
  1014. //
  1015. Path->Display_Path( Display_Findpaths );
  1016. //
  1017. // What should we be doing?
  1018. //
  1019. switch( Path->Get_State() )
  1020. {
  1021. case PathClass::STATE_PATH_COMPLETE:
  1022. case PathClass::STATE_TRAVERSING_PATH:
  1023. //
  1024. // Don't allow any movement if something is in our way
  1025. //
  1026. if ( soldier == NULL || soldier->Is_Soldier_Blocked( curr_pos ) == false ) {
  1027. if ( Move_To_Absolute( next_pos ) ) {
  1028. act_result = Arrived();
  1029. }
  1030. }
  1031. break;
  1032. case PathClass::STATE_ACTION_REQUIRED:
  1033. act_result = Perform_Action();
  1034. break;
  1035. /*case PathClass::STATE_PATH_COMPLETE:
  1036. act_result = Arrived();
  1037. break; */
  1038. }
  1039. } else {
  1040. //
  1041. // Error, free the path object
  1042. //
  1043. Release_Path();
  1044. act_result = ACTION_DONE;
  1045. Action->Done( ACTION_COMPLETE_MOVE_NO_PROGRESS_MADE );
  1046. }
  1047. return act_result;
  1048. }
  1049. virtual ActResult Perform_Action ( void )
  1050. {
  1051. SmartGameObj *obj = Action->Get_Action_Obj();
  1052. //
  1053. // Determine what action we need to perform
  1054. //
  1055. PathClass::ACTION_ID action_id = Path->Get_Action_Type();
  1056. switch( action_id )
  1057. {
  1058. case PathClass::ACTION_LADDER:
  1059. {
  1060. //
  1061. // Initialize the action object that will be responsible for
  1062. // controlling this action
  1063. //
  1064. PathAction.Initialize( PathActionClass::TYPE_LADDER, Path, obj, NULL );
  1065. PathAction.Set_Ladder_Index( Path->Get_Action_Mechanism() );
  1066. }
  1067. break;
  1068. case PathClass::ACTION_MECHANISM:
  1069. {
  1070. //
  1071. // Lookup the physics object we will be interacting with
  1072. //
  1073. uint32 mechanism_id = Path->Get_Action_Mechanism();
  1074. StaticPhysClass *mechanism = PhysicsSceneClass::Get_Instance ()->Find_Static_Object( mechanism_id );
  1075. if (mechanism != NULL) {
  1076. PathActionClass::TYPE type = PathActionClass::TYPE_UNKNOWN;
  1077. //
  1078. // Determine what type of mechanism this is
  1079. //
  1080. if( mechanism->As_ElevatorPhysClass () != NULL ) {
  1081. type = PathActionClass::TYPE_ELEVATOR;
  1082. } else if( mechanism->As_DoorPhysClass () != NULL ) {
  1083. type = PathActionClass::TYPE_DOOR;
  1084. }
  1085. //
  1086. // Initialize the action object that will be responsible for
  1087. // controlling this action
  1088. //
  1089. PathAction.Initialize( type, Path, obj, mechanism );
  1090. }
  1091. }
  1092. break;
  1093. case PathClass::ACTION_LEAP:
  1094. case PathClass::ACTION_JUMP:
  1095. {
  1096. //
  1097. // Get the point where we will be jumping to
  1098. //
  1099. Vector3 dest_pos;
  1100. Path->Get_Action_Destination( dest_pos );
  1101. //
  1102. // Instruct the physics object to jump to this point
  1103. //
  1104. HumanPhysClass *human_phys_obj = obj->Peek_Physical_Object()->As_HumanPhysClass ();
  1105. if( human_phys_obj != NULL ) {
  1106. human_phys_obj->Jump_To_Point( dest_pos );
  1107. }
  1108. //
  1109. // Initialize the action object that will be responsible for
  1110. // controlling this action
  1111. //
  1112. PathAction.Initialize( PathActionClass::TYPE_JUMPING, Path, obj, NULL );
  1113. }
  1114. break;
  1115. }
  1116. return ACTION_IN_PROGRESS;
  1117. }
  1118. virtual ActResult Process_Action ( void )
  1119. {
  1120. //
  1121. // Continue to let the action take place
  1122. //
  1123. PathAction.Process ();
  1124. //
  1125. // Does this action require us to move somewhere?
  1126. //
  1127. if( PathAction.Get_State () == PathActionClass::STATE_MOVING ) {
  1128. Move_To_Absolute( PathAction.Get_Destination (), true );
  1129. }
  1130. return ACTION_IN_PROGRESS;
  1131. }
  1132. virtual ActResult Solve_Path ( void )
  1133. {
  1134. ActResult act_result = ACTION_IN_PROGRESS;
  1135. //
  1136. // Evaluate the path
  1137. //
  1138. PathSolveClass::STATE_DESC result = PathSolver->Get_State();
  1139. //
  1140. // Has the path finished solving?
  1141. //
  1142. bool is_finished = true;
  1143. switch( result ) {
  1144. case PathSolveClass::THINKING:
  1145. is_finished = false;
  1146. break;
  1147. case PathSolveClass::SOLVED_PATH:
  1148. //
  1149. // Allocate and initialize the path object
  1150. //
  1151. Prepare_Path();
  1152. Initialize_Vehicle_AI();
  1153. //
  1154. // Configure the path object with information we learned
  1155. // during the path solve
  1156. //
  1157. Path->Set_Traversal_Type( PathClass::SPLINE );
  1158. Path->Initialize( *PathSolver );
  1159. break;
  1160. case PathSolveClass::ERROR_INVALID_START_POS:
  1161. case PathSolveClass::ERROR_INVALID_DEST_POS:
  1162. case PathSolveClass::ERROR_NO_PATH:
  1163. break;
  1164. }
  1165. //
  1166. // If we are finished solving the path, then release our
  1167. // hold on the path solver.
  1168. //
  1169. if ( is_finished ) {
  1170. PathMgrClass::Return_Path_Object( PathSolver );
  1171. PathSolver = NULL;
  1172. }
  1173. return act_result;
  1174. }
  1175. virtual ActResult Pathfind( void )
  1176. {
  1177. ActResult act_result = ACTION_IN_PROGRESS;
  1178. if( PathAction.Get_State () != PathActionClass::STATE_FINISHED ) {
  1179. //
  1180. // Keep performing the action until we've completed it
  1181. //
  1182. act_result = Process_Action();
  1183. } else {
  1184. if( PathSolver != NULL ) {
  1185. //
  1186. // Try to find a path to the destination
  1187. //
  1188. act_result = Solve_Path();
  1189. }
  1190. if( PathSolver == NULL) {
  1191. if( Path != NULL ) {
  1192. //
  1193. // Move along the path we found
  1194. //
  1195. act_result = Traverse_Path();
  1196. } else if ( PathSolver == NULL ){
  1197. //
  1198. // Unable to solve path...
  1199. //
  1200. // If this level doesn't have any pathfind data, then allow the unit
  1201. // to beeline -- otherwise, kill the action...
  1202. //
  1203. if (PathfindClass::Get_Instance ()->Does_Pathfind_Data_Exist ()) {
  1204. //
  1205. // Kill the action
  1206. //
  1207. Action->Done( ACTION_COMPLETE_PATH_BAD_DEST );
  1208. act_result = ACTION_DONE;
  1209. } else {
  1210. act_result = Beeline ();
  1211. }
  1212. }
  1213. }
  1214. }
  1215. return act_result;
  1216. }
  1217. virtual float Get_Distance_From_Goal (void)
  1218. {
  1219. float dist_to_goal = 0;
  1220. //
  1221. // Get the distance remaining from the path object (if possible)
  1222. //
  1223. if ( Path != NULL ) {
  1224. dist_to_goal = Path->Get_Remaining_Path_Length();
  1225. } else {
  1226. dist_to_goal = BaseActionCodeClass::Get_Distance_From_Goal();
  1227. }
  1228. //
  1229. // Return the length
  1230. //
  1231. return dist_to_goal;
  1232. }
  1233. void Update_Move_Location( void )
  1234. {
  1235. ScriptableGameObj *target = Action->Get_Parameters().MoveObjectRef.Get_Ptr();
  1236. if ( target == NULL ) { // If the target is gone
  1237. // Debug_Say(( "No Movement Target\n" ));
  1238. // Move_Target_Gone();
  1239. // Action->Reset( Action->Get_Parameters().Priority ); // Stop current Command (this may get this deleted!)
  1240. // Do we need to notify observers?
  1241. // return ACTION_DONE;
  1242. return;
  1243. }
  1244. // Action->Get_Parameters().MoveLocation = target->Get_Transform() * Action->Get_Parameters().MoveObjectOffset;
  1245. target->Get_Position( &(Action->Get_Parameters().MoveLocation) );
  1246. if ( FirstCall == false ) {
  1247. SmartGameObj * obj = Action->Get_Action_Obj();
  1248. SoldierGameObj * soldier = NULL;
  1249. if ( target->As_PhysicalGameObj() != NULL ) {
  1250. soldier = target->As_PhysicalGameObj()->As_SoldierGameObj();
  1251. }
  1252. // Get the current positions of the action object and the target object
  1253. Vector3 curr_pos;
  1254. Vector3 target_pos;
  1255. target->Get_Position( &target_pos );
  1256. obj->Get_Position( &curr_pos );
  1257. // Determine how far the target has moved since we last
  1258. // did a pathfind.
  1259. float target_move_dist = (target_pos - TargetPrevPos).Length ();
  1260. float to_target_dist = (curr_pos - target_pos).Length ();
  1261. if ( to_target_dist > 0 &&
  1262. (soldier == NULL || (soldier->Is_Airborne () == false && PathAction.Get_State () == PathActionClass::STATE_FINISHED)))
  1263. {
  1264. // Get the ratio of the distance the target has moved relative
  1265. // to the distance we are from the target.
  1266. float ratio = target_move_dist / to_target_dist;
  1267. if ((target_move_dist > 2.0F && ratio > 0.1F)) {
  1268. Initialize_Path();
  1269. }
  1270. }
  1271. }
  1272. }
  1273. virtual ActResult Act()
  1274. {
  1275. WWPROFILE( "Goto Act" );
  1276. SoldierGameObj * soldier = Action->Get_Action_Obj()->As_SoldierGameObj();
  1277. ScriptableGameObj * look_obj = Action->Get_Parameters().LookObjectRef.Get_Ptr();
  1278. if ( soldier != NULL && look_obj != NULL ) {
  1279. Vector3 pos;
  1280. look_obj->Get_Position( &pos );
  1281. pos.Z += 1; // Offset bullseye
  1282. soldier->Update_Look_At( pos );
  1283. }
  1284. if ( UseMoveObject ) {
  1285. Update_Move_Location();
  1286. }
  1287. if ( FirstCall ) {
  1288. Initialize_Path();
  1289. FirstCall = false;
  1290. }
  1291. #if 0
  1292. // Make the soldier crouch (if necessary)
  1293. if ( soldier && (soldier->Is_Crouched() != Action->Get_Parameters().MoveCrouched) ) {
  1294. soldier->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH );
  1295. }
  1296. #else
  1297. // Make the soldier crouch (if necessary)
  1298. if ( soldier ) {
  1299. soldier->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH, Action->Get_Parameters().MoveCrouched );
  1300. }
  1301. #endif
  1302. ActResult result = ACTION_IN_PROGRESS;
  1303. // Determine whether we should use pathfinding or
  1304. // simply beeline to the destination
  1305. if ( Action->Get_Parameters().MovePathfind ) {
  1306. result = Pathfind();
  1307. } else {
  1308. result = Beeline();
  1309. }
  1310. return result;
  1311. }
  1312. private:
  1313. PathClass * Path;
  1314. PathSolveClass * PathSolver;
  1315. PathActionClass PathAction;
  1316. bool FirstCall;
  1317. bool UseMoveObject;
  1318. Vector3 TargetPrevPos;
  1319. };
  1320. /*
  1321. ** EnterExit Action Code Class
  1322. */
  1323. class EnterExitActionCodeClass;
  1324. SimplePersistFactoryClass<EnterExitActionCodeClass, CHUNKID_ACTION_CODE_ENTER_EXIT> _EnterExitActionCodeClassFactory;
  1325. class EnterExitActionCodeClass : public BaseActionCodeClass {
  1326. public:
  1327. virtual const PersistFactoryClass & Get_Factory( void ) const { return _EnterExitActionCodeClassFactory; }
  1328. /*
  1329. ** Save / Load
  1330. */
  1331. enum {
  1332. CHUNKID_PARENT = 1031001312,
  1333. };
  1334. virtual bool Save( ChunkSaveClass & csave )
  1335. {
  1336. csave.Begin_Chunk( CHUNKID_PARENT );
  1337. BaseActionCodeClass::Save( csave );
  1338. csave.End_Chunk();
  1339. return true;
  1340. }
  1341. virtual bool Load( ChunkLoadClass & cload )
  1342. {
  1343. while (cload.Open_Chunk()) {
  1344. switch(cload.Cur_Chunk_ID()) {
  1345. case CHUNKID_PARENT:
  1346. BaseActionCodeClass::Load( cload );
  1347. break;
  1348. default:
  1349. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  1350. break;
  1351. }
  1352. cload.Close_Chunk();
  1353. }
  1354. return true;
  1355. }
  1356. virtual ActResult Act( void )
  1357. {
  1358. SoldierGameObj * obj = Action->Get_Action_Obj()->As_SoldierGameObj();
  1359. WWASSERT( obj );
  1360. obj->Set_Boolean_Control( ControlClass::BOOLEAN_ACTION );
  1361. Action->Done( ACTION_COMPLETE_NORMAL );
  1362. return ACTION_DONE;
  1363. }
  1364. };
  1365. /*
  1366. ** Dive Action Code Class
  1367. */
  1368. class DiveActionCodeClass;
  1369. SimplePersistFactoryClass<DiveActionCodeClass, CHUNKID_ACTION_CODE_DIVE> _DiveActionCodeClassFactory;
  1370. class DiveActionCodeClass : public BaseActionCodeClass {
  1371. public:
  1372. virtual const PersistFactoryClass & Get_Factory( void ) const { return _DiveActionCodeClassFactory; }
  1373. /*
  1374. ** Save / Load
  1375. */
  1376. enum {
  1377. CHUNKID_PARENT = 1031001310,
  1378. };
  1379. virtual bool Save( ChunkSaveClass & csave )
  1380. {
  1381. csave.Begin_Chunk( CHUNKID_PARENT );
  1382. BaseActionCodeClass::Save( csave );
  1383. csave.End_Chunk();
  1384. return true;
  1385. }
  1386. virtual bool Load( ChunkLoadClass & cload )
  1387. {
  1388. while (cload.Open_Chunk()) {
  1389. switch(cload.Cur_Chunk_ID()) {
  1390. case CHUNKID_PARENT:
  1391. BaseActionCodeClass::Load( cload );
  1392. break;
  1393. default:
  1394. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  1395. break;
  1396. }
  1397. cload.Close_Chunk();
  1398. }
  1399. return true;
  1400. }
  1401. virtual ActResult Act( void )
  1402. {
  1403. SoldierGameObj * obj = Action->Get_Action_Obj()->As_SoldierGameObj();
  1404. WWASSERT( obj );
  1405. Vector3 rel_pos;
  1406. Matrix3D::Inverse_Transform_Vector( obj->Get_Transform(), Action->Get_Parameters().MoveLocation, &rel_pos );
  1407. if ( WWMath::Fabs( rel_pos.X ) > WWMath::Fabs( rel_pos.Y ) ) {
  1408. obj->Set_Boolean_Control( rel_pos.X > 0 ? ControlClass::BOOLEAN_DIVE_FORWARD : ControlClass::BOOLEAN_DIVE_BACKWARD );
  1409. } else {
  1410. obj->Set_Boolean_Control( rel_pos.Y > 0 ? ControlClass::BOOLEAN_DIVE_LEFT : ControlClass::BOOLEAN_DIVE_RIGHT );
  1411. }
  1412. #if 0
  1413. if ( !obj->Is_Crouched() ) {
  1414. obj->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH );
  1415. }
  1416. #else
  1417. obj->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH );
  1418. #endif
  1419. Action->Done( ACTION_COMPLETE_NORMAL );
  1420. return ACTION_DONE;
  1421. }
  1422. };
  1423. /*
  1424. ** Attack - Location
  1425. */
  1426. class AttackActionCodeClass;
  1427. SimplePersistFactoryClass<AttackActionCodeClass, CHUNKID_ACTION_CODE_ATTACK> _AttackActionCodeClassFactory;
  1428. class AttackActionCodeClass : public GotoActionCodeClass {
  1429. public:
  1430. virtual const PersistFactoryClass & Get_Factory( void ) const { return _AttackActionCodeClassFactory; }
  1431. AttackActionCodeClass( void ) :
  1432. UseAttackObject( false ),
  1433. WanderPos(0,0,0),
  1434. WanderTimer(0),
  1435. HasArrived( false ),
  1436. HoldPrimaryTriggerTimer( 0 )
  1437. {
  1438. ErrorScale = FreeRandom.Get_Float( 0.75f, 1.25f );
  1439. }
  1440. /*
  1441. ** Save / Load
  1442. */
  1443. enum {
  1444. CHUNKID_PARENT = 1031001308,
  1445. CHUNKID_VARIABLES,
  1446. MICROCHUNKID_USE_ATTACK_OBJECT = 1,
  1447. MICROCHUNKID_IS_BLOCKED = 2,
  1448. MICROCHUNKID_CHECK_BLOCKED_TIMER = 3,
  1449. };
  1450. virtual bool Save( ChunkSaveClass & csave )
  1451. {
  1452. csave.Begin_Chunk( CHUNKID_PARENT );
  1453. GotoActionCodeClass::Save( csave );
  1454. csave.End_Chunk();
  1455. csave.Begin_Chunk( CHUNKID_VARIABLES );
  1456. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_USE_ATTACK_OBJECT, UseAttackObject );
  1457. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IS_BLOCKED, IsBlocked );
  1458. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_CHECK_BLOCKED_TIMER, CheckBlockedTimer );
  1459. csave.End_Chunk();
  1460. return true;
  1461. }
  1462. virtual bool Load( ChunkLoadClass & cload )
  1463. {
  1464. while (cload.Open_Chunk()) {
  1465. switch(cload.Cur_Chunk_ID()) {
  1466. case CHUNKID_PARENT:
  1467. GotoActionCodeClass::Load( cload );
  1468. break;
  1469. case CHUNKID_VARIABLES:
  1470. while (cload.Open_Micro_Chunk()) {
  1471. switch(cload.Cur_Micro_Chunk_ID()) {
  1472. READ_MICRO_CHUNK( cload, MICROCHUNKID_USE_ATTACK_OBJECT, UseAttackObject );
  1473. READ_MICRO_CHUNK( cload, MICROCHUNKID_IS_BLOCKED, IsBlocked );
  1474. READ_MICRO_CHUNK( cload, MICROCHUNKID_CHECK_BLOCKED_TIMER, CheckBlockedTimer );
  1475. default:
  1476. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  1477. break;
  1478. }
  1479. cload.Close_Micro_Chunk();
  1480. }
  1481. break;
  1482. default:
  1483. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  1484. break;
  1485. }
  1486. cload.Close_Chunk();
  1487. }
  1488. return true;
  1489. }
  1490. virtual void Init( ActionClass * action )
  1491. {
  1492. GotoActionCodeClass::Init( action );
  1493. UseAttackObject = (Action->Get_Parameters().AttackObjectRef.Get_Ptr() != NULL);
  1494. IsBlocked = action->Get_Parameters().AttackCheckBlocked;
  1495. CheckBlockedTimer = 0;
  1496. WanderPos = Vector3(0,0,0);
  1497. }
  1498. virtual ActResult Arrived()
  1499. {
  1500. if ( !HasArrived ) {
  1501. // Debug_Say(( "Attack Arrived %d\n", HasArrived ));
  1502. HasArrived = true;
  1503. SoldierGameObj * obj = Action->Get_Action_Obj()->As_SoldierGameObj();
  1504. if ( obj != NULL ) {
  1505. int observer_id = Action->Get_Parameters ().ObserverID;
  1506. if ( observer_id != 0 ) {
  1507. const GameObjObserverList & observer_list = obj->Get_Observers();
  1508. for( int index = 0; index < observer_list.Count(); index++ ) {
  1509. if ( observer_list[ index ]->Get_ID() == observer_id ) {
  1510. observer_list[ index ]->Custom( obj, CUSTOM_EVENT_ATTACK_ARRIVED, Action->Get_Parameters().ActionID, NULL );
  1511. }
  1512. }
  1513. }
  1514. }
  1515. }
  1516. return ACTION_IN_PROGRESS; // Do nothing special when we arrive
  1517. }
  1518. // Attack the given absolute position, return nothing
  1519. void Attack_Absolute( Vector3 & abs_pos )
  1520. {
  1521. WWPROFILE( "Attack Absolute" );
  1522. IsAttacking = true;
  1523. float range = Action->Get_Parameters().AttackRange;
  1524. SmartGameObj * obj = Action->Get_Action_Obj();
  1525. Vector3 my_pos;
  1526. obj->Get_Position( &my_pos );
  1527. // move the abs_pos around a little for AttackError
  1528. Vector3 attack_pos = abs_pos;
  1529. Matrix3D error;
  1530. error.Obj_Look_At( my_pos, abs_pos, 0 );
  1531. float dist = (my_pos - abs_pos).Length();
  1532. float move = TimeManager::Get_Total_Seconds() * ErrorScale;
  1533. float error_angle = WWMath::Fabs(Action->Get_Parameters().AttackError) +
  1534. WWMath::Fabs(obj->Get_Weapon_Error());
  1535. #if 0
  1536. // Modify error angle based on difficulty
  1537. switch ( CombatManager::Get_Difficulty_Level() ) {
  1538. case 0:
  1539. error_angle += 20;
  1540. break;
  1541. case 1:
  1542. // no change
  1543. break;
  1544. case 2:
  1545. error_angle -= 20;
  1546. break;
  1547. };
  1548. if ( error_angle < 0 ) {
  1549. error_angle = 0;
  1550. }
  1551. #endif
  1552. // If AttackErrorOverride, only use AttackError
  1553. if ( Action->Get_Parameters().AttackErrorOverride ) {
  1554. error_angle = WWMath::Fabs(Action->Get_Parameters().AttackError);
  1555. }
  1556. error.Rotate_Z( DEG_TO_RADF( WWMath::Sin( move * 1.7f ) * error_angle ) );
  1557. error.Rotate_Y( DEG_TO_RADF( WWMath::Cos( move ) * error_angle ) * 0.5f );
  1558. attack_pos = error * Vector3( dist,0,0 );
  1559. if ( obj->Set_Targeting( attack_pos ) == false && !Action->Get_Parameters().AttackForceFire ) {
  1560. // we are not facing it yet, don't fire
  1561. return;
  1562. }
  1563. WeaponClass * weapon = obj->Get_Weapon();
  1564. WWASSERT( weapon );
  1565. if ( Action->Get_Parameters().AttackCheckBlocked ) {
  1566. WWPROFILE( "Check Blocked" );
  1567. CheckBlockedTimer -= TimeManager::Get_Frame_Seconds();
  1568. if ( CheckBlockedTimer <= 0 ) {
  1569. CheckBlockedTimer = FreeRandom.Get_Float( 2, 3 ); // Re check every 2 - 3 sec
  1570. // cast a ray from muzzle to target
  1571. PhysicalGameObj * hit_obj = weapon->Cast_Weapon( abs_pos );
  1572. if ( hit_obj != NULL ) {
  1573. if ( UseAttackObject ) {
  1574. PhysicalGameObj * target = (PhysicalGameObj *)Action->Get_Parameters().AttackObjectRef.Get_Ptr();
  1575. IsBlocked = ( hit_obj != target );
  1576. if ( IsBlocked && target->As_SoldierGameObj() && target->As_SoldierGameObj()->Get_Vehicle() == hit_obj ) {
  1577. IsBlocked = false;
  1578. }
  1579. } else {
  1580. IsBlocked = !hit_obj->Is_Enemy( obj );
  1581. }
  1582. } else {
  1583. IsBlocked = true;
  1584. }
  1585. }
  1586. }
  1587. if ( IsBlocked && !Action->Get_Parameters().AttackForceFire ) {
  1588. return;
  1589. }
  1590. // Update Weapons Dynamic Error
  1591. if ( weapon ) {
  1592. if ( Action->Get_Parameters().AttackCheckBlocked != false ) {
  1593. range = MIN( range, weapon->Get_Range() ); // dont fire if not in range
  1594. }
  1595. }
  1596. my_pos -= abs_pos;
  1597. my_pos.Z = 0;
  1598. if ( my_pos.Length() < range || Action->Get_Parameters().AttackForceFire ) {
  1599. if ( Action->Get_Parameters().AttackPrimaryFire ) {
  1600. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_FIRE_PRIMARY );
  1601. // Keep the trigger down (helps obelisk)
  1602. float rate = 0;
  1603. if ( obj->Get_Weapon() ) {
  1604. rate = obj->Get_Weapon()->Get_Primary_Fire_Rate();
  1605. }
  1606. if ( rate != 0 ) {
  1607. HoldPrimaryTriggerTimer = (1 / rate) * 0.75f;
  1608. }
  1609. } else {
  1610. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_FIRE_SECONDARY );
  1611. }
  1612. }
  1613. }
  1614. virtual void Modify_Parameters( const SafeActionParamsStruct & parameters, bool modify_move, bool modify_attack )
  1615. {
  1616. //
  1617. // Copy the parameters that are safe to change in the middle of an action
  1618. //
  1619. if ( modify_attack ) {
  1620. Action->Get_Parameters().AttackLocation = parameters.AttackLocation;
  1621. Action->Get_Parameters().AttackObjectRef = parameters.AttackObjectRef;
  1622. Action->Get_Parameters().AttackCrouched = parameters.AttackCrouched;
  1623. Action->Get_Parameters().AttackRange = parameters.AttackRange;
  1624. Action->Get_Parameters().AttackCheckBlocked = parameters.AttackCheckBlocked;
  1625. Action->Get_Parameters().AttackError = parameters.AttackError;
  1626. Action->Get_Parameters().AttackErrorOverride = parameters.AttackErrorOverride;
  1627. Action->Get_Parameters().AttackPrimaryFire = parameters.AttackPrimaryFire;
  1628. Action->Get_Parameters().AttackActive = parameters.AttackActive;
  1629. Action->Get_Parameters().AttackWanderAllowed = parameters.AttackWanderAllowed;
  1630. Action->Get_Parameters().AttackFaceTarget = parameters.AttackFaceTarget;
  1631. Action->Get_Parameters().AttackForceFire = parameters.AttackForceFire;
  1632. }
  1633. if ( modify_move ) {
  1634. Action->Get_Parameters().MoveObject = parameters.MoveObject;
  1635. Action->Get_Parameters().MoveObjectOffset = parameters.MoveObjectOffset;
  1636. Action->Get_Parameters().MoveSpeed = parameters.MoveSpeed;
  1637. Action->Get_Parameters().MoveArrivedDistance = parameters.MoveArrivedDistance;
  1638. Action->Get_Parameters().MoveBackup = parameters.MoveBackup;
  1639. Action->Get_Parameters().MoveFollow = parameters.MoveFollow;
  1640. Action->Get_Parameters().MoveCrouched = parameters.MoveCrouched;
  1641. Action->Get_Parameters().ShutdownEngineOnArrival = parameters.ShutdownEngineOnArrival;
  1642. Action->Get_Parameters().MovePathfind = parameters.MovePathfind;
  1643. }
  1644. //
  1645. // Reset some of the internal flags
  1646. //
  1647. UseAttackObject = (parameters.AttackObjectRef.Get_Ptr() != NULL);
  1648. IsBlocked = false;
  1649. CheckBlockedTimer = 0;
  1650. return ;
  1651. }
  1652. virtual ActResult Act( void )
  1653. {
  1654. WWPROFILE( "Attack Act" );
  1655. //
  1656. // Do the movement (if necessary)
  1657. //
  1658. if ( Action->Get_Parameters().MoveArrivedDistance < DONT_MOVE_ARRIVED_DIST ) {
  1659. GotoActionCodeClass::Act();
  1660. }
  1661. ActResult result = ACTION_IN_PROGRESS;
  1662. //
  1663. // Only "do" the attack if the attack state is currently active
  1664. //
  1665. if ( Action != NULL && Action->Get_Parameters().AttackActive ) {
  1666. Vector3 set_target;
  1667. result = Process_Attack(&set_target);
  1668. if ( Action != NULL && Action->Get_Parameters().AttackFaceTarget) {
  1669. Pilot.Set_Target (&set_target);
  1670. }
  1671. } else {
  1672. Pilot.Set_Target (NULL);
  1673. }
  1674. return result;
  1675. }
  1676. ActResult Process_Attack( Vector3 * set_target )
  1677. {
  1678. SmartGameObj * obj = Action->Get_Action_Obj();
  1679. if ( HoldPrimaryTriggerTimer > 0 ) {
  1680. HoldPrimaryTriggerTimer -= TimeManager::Get_Frame_Seconds();
  1681. obj->Set_Boolean_Control( ControlClass::BOOLEAN_WEAPON_FIRE_PRIMARY );
  1682. }
  1683. Vector3 target_pos = Action->Get_Parameters().AttackLocation;
  1684. WeaponClass * weapon = obj->Get_Weapon();
  1685. if ( weapon == NULL ) {
  1686. Action->Done( ACTION_COMPLETE_NORMAL ); // Stop current Command (this may get this deleted!)
  1687. return ACTION_DONE;
  1688. }
  1689. if ( UseAttackObject ) {
  1690. WWPROFILE( "Attack Object" );
  1691. // See if our target still exists
  1692. PhysicalGameObj * target = (PhysicalGameObj *)Action->Get_Parameters().AttackObjectRef.Get_Ptr();
  1693. if ( target ) {
  1694. if ( target->Get_Defense_Object()->Get_Health() <= 0 ) {
  1695. // Debug_Say(( "Attack target dead\n" ));
  1696. target = NULL;
  1697. }
  1698. }
  1699. if ( target == NULL ) { // If the target is gone or dead
  1700. static int count = 0;
  1701. if (count++ < 20) {
  1702. // Debug_Say(( "No Attack Target\n" ));
  1703. }
  1704. Action->Done( ACTION_COMPLETE_NORMAL ); // Stop current Command (this may get this deleted!)
  1705. return ACTION_DONE;
  1706. }
  1707. // Goto our target's location
  1708. target_pos = target->Get_Bullseye_Position();
  1709. if ( target->As_SoldierGameObj() != NULL && target->As_SoldierGameObj()->Peek_Human_Phys() != NULL ) {
  1710. Vector3 vel;
  1711. target->As_SoldierGameObj()->Peek_Human_Phys()->Get_Velocity( &vel );
  1712. vel *= TimeManager::Get_Frame_Seconds();
  1713. target_pos += vel;
  1714. }
  1715. }
  1716. // If not moving, use attack crouch
  1717. if ( !IsMoving ) {
  1718. SoldierGameObj * soldier = Action->Get_Action_Obj()->As_SoldierGameObj();
  1719. if ( soldier != NULL ) {
  1720. #if 0
  1721. if ( soldier->Is_Crouched() != Action->Get_Parameters().AttackCrouched ) {
  1722. soldier->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH );
  1723. }
  1724. #else
  1725. soldier->Set_Boolean_Control( ControlClass::BOOLEAN_CROUCH, Action->Get_Parameters().AttackCrouched );
  1726. #endif
  1727. }
  1728. }
  1729. Attack_Absolute( target_pos );
  1730. #if 0
  1731. // Elie Wander!
  1732. // Always wander if attacking!
  1733. WanderTimer -= TimeManager::Get_Frame_Seconds();
  1734. SoldierGameObj * soldier = Action->Get_Action_Obj()->As_SoldierGameObj();
  1735. if ( !IsMoving && WanderTimer <= 0 && soldier != NULL && Action->Get_Parameters().AttackWanderAllowed ) {
  1736. WWPROFILE( "Wander" );
  1737. Vector3 rel_pos;
  1738. Matrix3D::Inverse_Transform_Vector( obj->Get_Transform(), WanderPos, &rel_pos );
  1739. // Debug_Say(( "Wander Pos %f %f %f\n", WanderPos.X, WanderPos.Y, WanderPos.Z ));
  1740. // Debug_Say(( "Rel Pos %f %f %f\n", rel_pos.X, rel_pos.Y, rel_pos.Z ));
  1741. // If he is near the wander position, or very far from it, pick a new wander pos
  1742. float dist = rel_pos.Length();
  1743. if ( dist < 0.5 || dist > 8 ) {
  1744. WanderTimer = FreeRandom.Get_Float( 0.4f, 1.2f ); // random pause
  1745. // If near the move loc, wander away, else wander back
  1746. Vector3 dif = WanderPos - Action->Get_Parameters().MoveLocation;
  1747. WanderPos = Action->Get_Parameters().MoveLocation;
  1748. if ( dif.Length() < 2 ) {
  1749. #if 1
  1750. PathfindClass *pathfind = PathfindClass::Get_Instance(); // Lookup a safe random position to walk to
  1751. pathfind->Find_Random_Spot( Action->Get_Parameters().MoveLocation, 5, &WanderPos );
  1752. #else
  1753. float angle = FreeRandom.Get_Float( 0, DEG_TO_RADF( 360.0f ) );
  1754. Vector3 move( ::sinf(angle), ::cosf(angle), 0 );
  1755. // Debug_Say(( " Move to %f %f %f\n", move.X, move.Y, move.Z ));
  1756. WanderPos += move * FreeRandom.Get_Float( 3, 5 );
  1757. #endif
  1758. soldier->Look_At( WanderPos + Vector3( 0,0,1 ), FreeRandom.Get_Float( 0.2f, 0.6f ) );
  1759. }
  1760. }
  1761. Human_Move_To_Relative( rel_pos );
  1762. }
  1763. #endif
  1764. *set_target = target_pos;
  1765. return ACTION_IN_PROGRESS;
  1766. }
  1767. bool UseAttackObject;
  1768. bool IsBlocked;
  1769. float CheckBlockedTimer;
  1770. bool HasArrived;
  1771. float ErrorScale;
  1772. float HoldPrimaryTriggerTimer;
  1773. Vector3 WanderPos;
  1774. float WanderTimer;
  1775. };
  1776. /*
  1777. ** Face - Location
  1778. */
  1779. class FaceLocationActionCodeClass;
  1780. SimplePersistFactoryClass<FaceLocationActionCodeClass, CHUNKID_ACTION_CODE_FACE_LOCATION> _FaceLocationActionCodeClassFactory;
  1781. class FaceLocationActionCodeClass : public BaseActionCodeClass {
  1782. public:
  1783. virtual const PersistFactoryClass & Get_Factory( void ) const { return _FaceLocationActionCodeClassFactory; }
  1784. FaceLocationActionCodeClass( void ) :
  1785. EndTime( 0 )
  1786. {
  1787. }
  1788. /*
  1789. ** Save / Load
  1790. */
  1791. enum {
  1792. CHUNKID_PARENT = 1031001305,
  1793. CHUNKID_VARIABLES,
  1794. MICROCHUNKID_END_TIME = 1,
  1795. };
  1796. virtual bool Save( ChunkSaveClass & csave )
  1797. {
  1798. csave.Begin_Chunk( CHUNKID_PARENT );
  1799. BaseActionCodeClass::Save( csave );
  1800. csave.End_Chunk();
  1801. csave.Begin_Chunk( CHUNKID_VARIABLES );
  1802. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_END_TIME, EndTime );
  1803. csave.End_Chunk();
  1804. return true;
  1805. }
  1806. virtual bool Load( ChunkLoadClass & cload )
  1807. {
  1808. while (cload.Open_Chunk()) {
  1809. switch(cload.Cur_Chunk_ID()) {
  1810. case CHUNKID_PARENT:
  1811. BaseActionCodeClass::Load( cload );
  1812. break;
  1813. case CHUNKID_VARIABLES:
  1814. while (cload.Open_Micro_Chunk()) {
  1815. switch(cload.Cur_Micro_Chunk_ID()) {
  1816. READ_MICRO_CHUNK( cload, MICROCHUNKID_END_TIME, EndTime );
  1817. default:
  1818. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  1819. break;
  1820. }
  1821. cload.Close_Micro_Chunk();
  1822. }
  1823. break;
  1824. default:
  1825. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  1826. break;
  1827. }
  1828. cload.Close_Chunk();
  1829. }
  1830. return true;
  1831. }
  1832. virtual void Init( ActionClass * action )
  1833. {
  1834. BaseActionCodeClass::Init( action );
  1835. SoldierGameObj *soldier_obj = Action->Get_Action_Obj()->As_SoldierGameObj();
  1836. if ( soldier_obj && Action->Get_Parameters().LookDuration != 0 ) {
  1837. soldier_obj->Look_At( Action->Get_Parameters().LookLocation, Action->Get_Parameters().LookDuration );
  1838. }
  1839. EndTime = TimeManager::Get_Seconds() +
  1840. MAX(action->Get_Parameters().FaceDuration, Action->Get_Parameters().LookDuration);
  1841. }
  1842. virtual ActResult Act( void )
  1843. {
  1844. BaseActionCodeClass::Act();
  1845. // Look at the moving object
  1846. SoldierGameObj * soldier = Action->Get_Action_Obj()->As_SoldierGameObj();
  1847. ScriptableGameObj * look_obj = Action->Get_Parameters().LookObjectRef.Get_Ptr();
  1848. if ( soldier != NULL && look_obj != NULL ) {
  1849. Vector3 pos;
  1850. look_obj->Get_Position( &pos );
  1851. pos.Z += 1; // Offset bullseye
  1852. soldier->Update_Look_At( pos );
  1853. }
  1854. if ( Action->Get_Parameters().FaceDuration != 0 ) {
  1855. //
  1856. // Make the object target (face for humans) the given location
  1857. //
  1858. SmartGameObj * obj = Action->Get_Action_Obj();
  1859. Vector3 face_location = Action->Get_Parameters().FaceLocation;
  1860. obj->Set_Targeting( face_location, false );
  1861. }
  1862. //
  1863. // Have we timed out yet?
  1864. //
  1865. ActResult result = ACTION_IN_PROGRESS;
  1866. if ( EndTime < TimeManager::Get_Seconds() ) {
  1867. //
  1868. // Stop this action. Note: The following call may cause the
  1869. // object to be deleted...
  1870. //
  1871. Action->Done( ACTION_COMPLETE_NORMAL );
  1872. result = ACTION_DONE;
  1873. }
  1874. return result;
  1875. }
  1876. float EndTime;
  1877. };
  1878. /*
  1879. ** Have a conversation
  1880. */
  1881. class ConversationActionCodeClass;
  1882. SimplePersistFactoryClass<ConversationActionCodeClass, CHUNKID_ACTION_CODE_CONVERSATION> _ConversationActionCodeClassFactory;
  1883. class ConversationActionCodeClass : public GotoActionCodeClass {
  1884. public:
  1885. virtual const PersistFactoryClass & Get_Factory( void ) const { return _ConversationActionCodeClassFactory; }
  1886. enum
  1887. {
  1888. STATE_WALKING_TO_START_POS = 0,
  1889. STATE_HAVING_CONVERSATION,
  1890. STATE_FINISHED
  1891. };
  1892. ConversationActionCodeClass( void ) :
  1893. Conversation( NULL ),
  1894. State( STATE_WALKING_TO_START_POS ),
  1895. OriginalPos( 0, 0, 0 )
  1896. {
  1897. }
  1898. ~ConversationActionCodeClass( void )
  1899. {
  1900. return ;
  1901. }
  1902. /*
  1903. ** Save / Load
  1904. */
  1905. enum {
  1906. CHUNKID_PARENT = 1031001258,
  1907. CHUNKID_VARIABLES,
  1908. MICROCHUNKID_STATE = 1,
  1909. MICROCHUNKID_ORIGINAL_POS = 2,
  1910. MICROCHUNKID_CONVERSATION_PTR = 3,
  1911. };
  1912. virtual bool Save( ChunkSaveClass & csave )
  1913. {
  1914. csave.Begin_Chunk( CHUNKID_PARENT );
  1915. GotoActionCodeClass::Save( csave );
  1916. csave.End_Chunk();
  1917. csave.Begin_Chunk( CHUNKID_VARIABLES );
  1918. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_STATE, State );
  1919. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ORIGINAL_POS, OriginalPos );
  1920. //
  1921. // Only save the conversation ptr if it exists in the manager
  1922. //
  1923. if (Conversation != NULL) {
  1924. ConversationClass *conv = ConversationMgrClass::Find_Conversation (Conversation->Get_ID ());
  1925. if (conv != NULL) {
  1926. //
  1927. // Save the conversation pointer
  1928. //
  1929. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_CONVERSATION_PTR, Conversation );
  1930. REF_PTR_RELEASE( conv );
  1931. }
  1932. }
  1933. csave.End_Chunk();
  1934. return true;
  1935. }
  1936. virtual bool Load( ChunkLoadClass & cload )
  1937. {
  1938. while (cload.Open_Chunk()) {
  1939. switch(cload.Cur_Chunk_ID()) {
  1940. case CHUNKID_PARENT:
  1941. GotoActionCodeClass::Load( cload );
  1942. break;
  1943. case CHUNKID_VARIABLES:
  1944. while (cload.Open_Micro_Chunk()) {
  1945. switch(cload.Cur_Micro_Chunk_ID()) {
  1946. READ_MICRO_CHUNK( cload, MICROCHUNKID_STATE, State );
  1947. READ_MICRO_CHUNK( cload, MICROCHUNKID_ORIGINAL_POS, OriginalPos );
  1948. case MICROCHUNKID_CONVERSATION_PTR:
  1949. LOAD_MICRO_CHUNK( cload, Conversation );
  1950. //
  1951. // Fixup the pointer
  1952. //
  1953. if (Conversation != NULL) {
  1954. REQUEST_REF_COUNTED_POINTER_REMAP( (RefCountClass **)&Conversation );
  1955. }
  1956. break;
  1957. default:
  1958. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  1959. break;
  1960. }
  1961. cload.Close_Micro_Chunk();
  1962. }
  1963. break;
  1964. default:
  1965. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  1966. break;
  1967. }
  1968. cload.Close_Chunk();
  1969. }
  1970. return true;
  1971. }
  1972. virtual void Begin_Hibernation( void )
  1973. {
  1974. //
  1975. // Stop this action. Note: The following call may cause the
  1976. // object to be deleted...
  1977. //
  1978. if ( Action != NULL ) {
  1979. Action->Done( ACTION_COMPLETE_NORMAL );
  1980. }
  1981. return ;
  1982. }
  1983. virtual void Init( ActionClass * action )
  1984. {
  1985. State = STATE_WALKING_TO_START_POS;
  1986. REF_PTR_RELEASE( Conversation );
  1987. OriginalPos.Set( 0, 0, 0 );
  1988. SmartGameObj *obj = action->Get_Action_Obj();
  1989. if ( obj != NULL ) {
  1990. SoldierGameObj *soldier = obj->As_SoldierGameObj ();
  1991. if( soldier != NULL ) {
  1992. //
  1993. // Should we join an already active conversation or should
  1994. // we start our own conversation?
  1995. //
  1996. if ( action->Get_Parameters().ActiveConversationID != 0 ) {
  1997. Conversation = ConversationMgrClass::Find_Active_Conversation( action->Get_Parameters().ActiveConversationID );
  1998. } else {
  1999. //
  2000. // Start a new conversation
  2001. //
  2002. Conversation = ConversationMgrClass::Start_Conversation( soldier, action->Get_Parameters().SafeConversationName );
  2003. }
  2004. //
  2005. // By default our new position is our current position
  2006. //
  2007. soldier->Get_Position( &OriginalPos );
  2008. if ( Conversation != NULL ) {
  2009. OratorClass *orator_info = Conversation->Get_Orator_Information( soldier );
  2010. //
  2011. // Determine if the soldier needs to move to a new location
  2012. //
  2013. if (orator_info->Get_Flag (OratorClass::FLAG_DONT_MOVE) == false) {
  2014. //
  2015. // Get the soldier's expected position
  2016. //
  2017. Vector3 new_pos = OriginalPos;
  2018. Conversation->Get_Orator_Location( soldier, &new_pos );
  2019. //
  2020. // Set our movement location
  2021. //
  2022. action->Get_Parameters().MoveLocation = new_pos;
  2023. action->Get_Parameters().MoveSpeed = 0.15F;
  2024. action->Get_Parameters().MoveArrivedDistance = 0.25F;
  2025. } else {
  2026. State = STATE_HAVING_CONVERSATION;
  2027. }
  2028. }
  2029. }
  2030. }
  2031. GotoActionCodeClass::Init( action );
  2032. return ;
  2033. }
  2034. virtual void Shutdown( void )
  2035. {
  2036. //
  2037. // Release our hold on the conversation (but don't terminate it)
  2038. //
  2039. if ( Conversation != NULL ) {
  2040. //Conversation->Stop_Conversation ();
  2041. REF_PTR_RELEASE( Conversation );
  2042. }
  2043. GotoActionCodeClass::Shutdown();
  2044. return ;
  2045. }
  2046. virtual ActResult Act( void )
  2047. {
  2048. SmartGameObj *obj = Action->Get_Action_Obj();
  2049. if ( obj != NULL ) {
  2050. SoldierGameObj *soldier = obj->As_SoldierGameObj ();
  2051. if( soldier != NULL ) {
  2052. if( State == STATE_WALKING_TO_START_POS ) {
  2053. GotoActionCodeClass::Act();
  2054. } else if ( State == STATE_HAVING_CONVERSATION ) {
  2055. //
  2056. // Check to see if the conversation has finished yet
  2057. //
  2058. if ( Conversation == NULL || Conversation->Is_Finished () ) {
  2059. State ++;
  2060. //
  2061. // Tell this soldier to return to his original position
  2062. //
  2063. ActionParamsStruct parameters;
  2064. parameters.Priority = Action->Get_Parameters().Priority;
  2065. parameters.ObserverID = Action->Get_Parameters().ObserverID;
  2066. parameters.MoveLocation = OriginalPos;
  2067. parameters.MoveSpeed = 0.15F;
  2068. parameters.MoveArrivedDistance = 1.0F;
  2069. soldier->Get_Action()->Goto (parameters);
  2070. }
  2071. }
  2072. }
  2073. }
  2074. //
  2075. // Have we timed out yet?
  2076. //
  2077. ActResult result = ACTION_IN_PROGRESS;
  2078. if ( Conversation == NULL || Conversation->Is_Finished () ) {
  2079. //
  2080. // Stop this action. Note: The following call may cause the
  2081. // object to be deleted...
  2082. //
  2083. if ( Action != NULL ) {
  2084. Action->Done( ACTION_COMPLETE_NORMAL );
  2085. }
  2086. result = ACTION_DONE;
  2087. }
  2088. return result;
  2089. }
  2090. virtual ActResult Arrived (void)
  2091. {
  2092. if ( Conversation != NULL && State == STATE_WALKING_TO_START_POS ) {
  2093. //
  2094. // Let the conversation object know this soldier has arrived
  2095. //
  2096. SmartGameObj *obj = Action->Get_Action_Obj();
  2097. if ( obj != NULL ) {
  2098. SoldierGameObj *soldier = obj->As_SoldierGameObj();
  2099. if( soldier != NULL ) {
  2100. OratorClass *orator_info = Conversation->Get_Orator_Information( soldier );
  2101. if ( orator_info != NULL ) {
  2102. orator_info->Set_Flag( OratorClass::FLAG_TEMP_DONT_FACE, false );
  2103. }
  2104. Conversation->Set_Orator_Arrived( soldier, true );
  2105. }
  2106. }
  2107. State ++;
  2108. }
  2109. return ACTION_IN_PROGRESS;
  2110. }
  2111. private:
  2112. ActiveConversationClass * Conversation;
  2113. int State;
  2114. Vector3 OriginalPos;
  2115. };
  2116. /*
  2117. ** Dock vehicle (backup) into a bay
  2118. */
  2119. class DockActionCodeClass;
  2120. SimplePersistFactoryClass<DockActionCodeClass, CHUNKID_ACTION_CODE_DOCK> _DockActionCodeClassFactory;
  2121. class DockActionCodeClass : public GotoActionCodeClass {
  2122. public:
  2123. virtual const PersistFactoryClass & Get_Factory( void ) const { return _DockActionCodeClassFactory; }
  2124. enum
  2125. {
  2126. STATE_DRIVING_TO_ENTRANCE = 0,
  2127. STATE_DRIVING_TO_SIDE,
  2128. STATE_BACKING_INTO_BAY
  2129. };
  2130. DockActionCodeClass( void ) :
  2131. State( STATE_DRIVING_TO_ENTRANCE )
  2132. {
  2133. }
  2134. ~DockActionCodeClass( void )
  2135. {
  2136. return ;
  2137. }
  2138. /*
  2139. ** Save / Load
  2140. */
  2141. enum {
  2142. CHUNKID_PARENT = 1031001258,
  2143. CHUNKID_VARIABLES,
  2144. MICROCHUNKID_STATE = 1,
  2145. };
  2146. virtual bool Save( ChunkSaveClass & csave )
  2147. {
  2148. csave.Begin_Chunk( CHUNKID_PARENT );
  2149. GotoActionCodeClass::Save( csave );
  2150. csave.End_Chunk();
  2151. csave.Begin_Chunk( CHUNKID_VARIABLES );
  2152. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_STATE, State );
  2153. csave.End_Chunk();
  2154. return true;
  2155. }
  2156. virtual bool Load( ChunkLoadClass & cload )
  2157. {
  2158. while (cload.Open_Chunk()) {
  2159. switch(cload.Cur_Chunk_ID()) {
  2160. case CHUNKID_PARENT:
  2161. GotoActionCodeClass::Load( cload );
  2162. break;
  2163. case CHUNKID_VARIABLES:
  2164. while (cload.Open_Micro_Chunk()) {
  2165. switch(cload.Cur_Micro_Chunk_ID()) {
  2166. READ_MICRO_CHUNK( cload, MICROCHUNKID_STATE, State );
  2167. default:
  2168. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  2169. break;
  2170. }
  2171. cload.Close_Micro_Chunk();
  2172. }
  2173. break;
  2174. default:
  2175. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  2176. break;
  2177. }
  2178. cload.Close_Chunk();
  2179. }
  2180. return true;
  2181. }
  2182. virtual void Init( ActionClass * action )
  2183. {
  2184. State = STATE_DRIVING_TO_ENTRANCE;
  2185. //
  2186. // Skip the "driving to side" state if we're a tracked vehicle which
  2187. // can turn in place.
  2188. //
  2189. SmartGameObj *obj = action->Get_Action_Obj();
  2190. VehicleGameObj *vehicle = obj->As_VehicleGameObj();
  2191. if ( vehicle != NULL && vehicle->Get_Turn_Radius() == 0 ) {
  2192. State = STATE_DRIVING_TO_SIDE;
  2193. }
  2194. //
  2195. // Setup the action structure so the vehicle will drive
  2196. // to the entrance of the docking bay
  2197. //
  2198. action->Get_Parameters().MoveLocation = action->Get_Parameters().DockEntrance;
  2199. action->Get_Parameters().MovePathfind = true;
  2200. action->Get_Parameters().MoveBackup = false;
  2201. OriginalMoveSpeed = action->Get_Parameters().MoveSpeed;
  2202. //
  2203. // Ensure that the arrived distance is valid
  2204. //
  2205. if (action->Get_Parameters().MoveArrivedDistance == DONT_MOVE_ARRIVED_DIST) {
  2206. action->Get_Parameters().MoveArrivedDistance = 1.0F;
  2207. }
  2208. GotoActionCodeClass::Init( action );
  2209. return ;
  2210. }
  2211. virtual ActResult Arrived (void)
  2212. {
  2213. SafeActionParamsStruct &params = Action->Get_Parameters();
  2214. ActResult result = ACTION_IN_PROGRESS;
  2215. //
  2216. // Determine which action we just finished
  2217. //
  2218. if ( State == STATE_DRIVING_TO_ENTRANCE ) {
  2219. //
  2220. // Terminate the action, we have arrived successfully
  2221. //
  2222. SmartGameObj *obj = Action->Get_Action_Obj();
  2223. VehicleGameObj *vehicle = obj->As_VehicleGameObj();
  2224. //
  2225. // Get the vector which points the direction the vehicle is
  2226. // currently facing
  2227. //
  2228. const Matrix3D &tm = obj->Get_Transform();
  2229. Vector3 facing_vector;
  2230. tm.Get_X_Vector( &facing_vector );
  2231. //
  2232. // Build a vector which points the way into the docking bay
  2233. //
  2234. Vector3 entrance_vector = params.DockEntrance - params.DockLocation;
  2235. entrance_vector.Normalize();
  2236. //
  2237. // Lookup the vehicle's turn radius
  2238. //
  2239. float turn_radius = 2.0F;
  2240. if ( vehicle != NULL ) {
  2241. turn_radius = vehicle->Get_Turn_Radius();
  2242. }
  2243. if (turn_radius == 0) {
  2244. //
  2245. // Instruct the object to drive to the side of the docking bay
  2246. //
  2247. ActionClass *action = Action;
  2248. action->Get_Parameters().MoveLocation = params.DockEntrance;
  2249. action->Get_Parameters().MovePathfind = false;
  2250. action->Get_Parameters().MoveBackup = true;
  2251. action->Get_Parameters().MoveSpeed = OriginalMoveSpeed * 0.25F;
  2252. GotoActionCodeClass::Init( action );
  2253. } else {
  2254. WaypathClass *test_waypath = new WaypathClass;
  2255. Vector3 side_pos = params.DockLocation + (params.DockEntrance - params.DockLocation) * 0.8F;
  2256. //
  2257. // Determine which side we should drive to in order to be oriented
  2258. // correctly for backing up
  2259. //
  2260. float dot_product = Vector3::Dot_Product (facing_vector, entrance_vector);
  2261. if (dot_product > 0) {
  2262. Vector3 vector1 = entrance_vector;
  2263. Vector3 vector2 = Vector3 (0, 0, 1.0F);
  2264. Vector3 side_vector = Vector3::Cross_Product (vector1, vector2);
  2265. test_waypath->Add_Point (side_pos + (side_vector * (turn_radius * 0.5F)));
  2266. side_pos += (side_vector * (turn_radius * 1.4F));
  2267. test_waypath->Add_Point (side_pos);
  2268. } else {
  2269. Vector3 vector1 = entrance_vector;
  2270. Vector3 vector2 = Vector3 (0, 0, 1.0F);
  2271. Vector3 side_vector = Vector3::Cross_Product (vector1, vector2);
  2272. test_waypath->Add_Point (side_pos - (side_vector * (turn_radius * 0.5F)));
  2273. side_pos -= (side_vector * (turn_radius * 1.4F));
  2274. test_waypath->Add_Point (side_pos);
  2275. }
  2276. //
  2277. // Instruct the object to drive to the side of the docking bay
  2278. //
  2279. ActionClass *action = Action;
  2280. action->Get_Parameters().MoveLocation = side_pos;
  2281. action->Get_Parameters().MovePathfind = true;
  2282. action->Get_Parameters().MoveBackup = false;
  2283. action->Get_Parameters().MoveSpeed = OriginalMoveSpeed * 0.25F;
  2284. GotoActionCodeClass::Init( test_waypath );
  2285. //
  2286. // Free our temporary waypath
  2287. //
  2288. REF_PTR_RELEASE( test_waypath );
  2289. }
  2290. //
  2291. // Move onto the next state
  2292. //
  2293. State ++;
  2294. } else if ( State == STATE_DRIVING_TO_SIDE ) {
  2295. WaypathClass *test_waypath = new WaypathClass;
  2296. SmartGameObj *obj = Action->Get_Action_Obj();
  2297. VehicleGameObj *vehicle = obj->As_VehicleGameObj();
  2298. float turn_radius = 2.0F;
  2299. if ( vehicle != NULL ) {
  2300. turn_radius = vehicle->Get_Turn_Radius();
  2301. }
  2302. Vector3 entrance_vector = params.DockEntrance - params.DockLocation;
  2303. entrance_vector.Normalize();
  2304. Vector3 vector1 = entrance_vector;
  2305. Vector3 vector2 = Vector3 (0, 0, 1.0F);
  2306. Vector3 side_vector = Vector3::Cross_Product (vector1, vector2);
  2307. test_waypath->Add_Point (params.DockLocation + (params.DockEntrance - params.DockLocation) * 0.7F - (side_vector * turn_radius * 0.6F));
  2308. test_waypath->Add_Point (params.DockLocation + (params.DockEntrance - params.DockLocation) * 0.3F);
  2309. test_waypath->Add_Point (params.DockLocation);
  2310. //
  2311. // Instruct the object to back into the docking bay
  2312. //
  2313. ActionClass *action = Action;
  2314. action->Get_Parameters().MoveLocation = params.DockLocation;
  2315. action->Get_Parameters().MovePathfind = true;
  2316. action->Get_Parameters().MoveBackup = true;
  2317. action->Get_Parameters().MoveSpeed = OriginalMoveSpeed * 0.1F;
  2318. GotoActionCodeClass::Init( test_waypath );
  2319. //
  2320. // Move onto the next state
  2321. //
  2322. State ++;
  2323. //
  2324. // Free our temporary waypath
  2325. //
  2326. REF_PTR_RELEASE( test_waypath );
  2327. //
  2328. // Let the observer know that the vehicle will be
  2329. // backing into the bay
  2330. //
  2331. int observer_id = Action->Get_Parameters ().ObserverID;
  2332. if ( observer_id != 0 ) {
  2333. const GameObjObserverList & observer_list = obj->Get_Observers();
  2334. for( int index = 0; index < observer_list.Count(); index++ ) {
  2335. if ( observer_list[ index ]->Get_ID() == observer_id ) {
  2336. observer_list[ index ]->Custom( obj, CUSTOM_EVENT_DOCK_BACKING_IN, 0, NULL );
  2337. }
  2338. }
  2339. }
  2340. } else if ( State == STATE_BACKING_INTO_BAY ) {
  2341. //
  2342. // Terminate the action, we have arrived successfully
  2343. //
  2344. State ++;
  2345. Action->Done( ACTION_COMPLETE_NORMAL );
  2346. result = ACTION_DONE;
  2347. }
  2348. return result;
  2349. }
  2350. private:
  2351. int State;
  2352. float OriginalMoveSpeed;
  2353. };
  2354. /*
  2355. **
  2356. */
  2357. SafeActionParamsStruct & SafeActionParamsStruct::operator = (const ActionParamsStruct & src)
  2358. {
  2359. Priority = src.Priority;
  2360. ActionID = src.ActionID;
  2361. ObserverID = src.ObserverID;
  2362. LookLocation = src.LookLocation;
  2363. LookObjectRef = src.LookObject;
  2364. LookDuration = src.LookDuration;
  2365. MoveLocation = src.MoveLocation;
  2366. MoveObjectRef = src.MoveObject;
  2367. MoveObjectOffset = src.MoveObjectOffset;
  2368. MoveSpeed = src.MoveSpeed;
  2369. MoveArrivedDistance = src.MoveArrivedDistance;
  2370. MoveBackup = src.MoveBackup;
  2371. MoveFollow = src.MoveFollow;
  2372. MoveCrouched = src.MoveCrouched;
  2373. ShutdownEngineOnArrival = src.ShutdownEngineOnArrival;
  2374. MovePathfind = src.MovePathfind;
  2375. AttackRange = src.AttackRange;
  2376. AttackError = src.AttackError;
  2377. AttackErrorOverride = src.AttackErrorOverride;
  2378. AttackPrimaryFire = src.AttackPrimaryFire;
  2379. AttackCrouched = src.AttackCrouched;
  2380. AttackObjectRef = src.AttackObject;
  2381. AttackLocation = src.AttackLocation;
  2382. AttackCheckBlocked = src.AttackCheckBlocked;
  2383. AttackActive = src.AttackActive;
  2384. AttackWanderAllowed = src.AttackWanderAllowed;
  2385. AttackFaceTarget = src.AttackFaceTarget;
  2386. AttackForceFire = src.AttackForceFire;
  2387. WaypathID = src.WaypathID;
  2388. WaypointStartID = src.WaypointStartID;
  2389. WaypointEndID = src.WaypointEndID;
  2390. WaypathSplined = src.WaypathSplined;
  2391. AnimationLooping = src.AnimationLooping;
  2392. ForceFacing = src.ForceFacing;
  2393. IgnoreFacing = src.IgnoreFacing;
  2394. FaceLocation = src.FaceLocation;
  2395. FaceDuration = src.FaceDuration;
  2396. SafeConversationName = src.ConversationName;
  2397. ActiveConversationID = src.ActiveConversationID;
  2398. SafeAnimationName = src.AnimationName;
  2399. AIState = src.AIState;
  2400. DockLocation = src.DockLocation;
  2401. DockEntrance = src.DockEntrance;
  2402. return *this;
  2403. }
  2404. enum {
  2405. CHUNKID_VARIABLES = 712001855,
  2406. CHUNKID_MOVE_OBJECT,
  2407. CHUNKID_ATTACK_OBJECT,
  2408. CHUNKID_LOOK_OBJECT,
  2409. MICROCHUNKID_PRIORITY = 1,
  2410. MICROCHUNKID_ACTION_ID,
  2411. MICROCHUNKID_OBSERVER_ID,
  2412. MICROCHUNKID_MOVE_LOCATION,
  2413. MICROCHUNKID_MOVE_OBJECT_OFFSET,
  2414. MICROCHUNKID_MOVE_SPEED,
  2415. MICROCHUNKID_MOVE_ARRIVED_DISTANCE,
  2416. MICROCHUNKID_MOVE_BACKUP,
  2417. MICROCHUNKID_MOVE_FOLLOW,
  2418. MICROCHUNKID_MOVE_CROUCHED,
  2419. MICROCHUNKID_MOVE_PATHFIND,
  2420. MICROCHUNKID_ATTACK_RANGE,
  2421. MICROCHUNKID_ATTACK_ERROR,
  2422. MICROCHUNKID_ATTACK_PRIMARY_FIRE,
  2423. MICROCHUNKID_ATTACK_CROUCHED,
  2424. MICROCHUNKID_ATTACK_LOCATION,
  2425. MICROCHUNKID_WAYPATH_ID,
  2426. MICROCHUNKID_WAYPOINT_START_ID,
  2427. MICROCHUNKID_WAYPOINT_END_ID,
  2428. MICROCHUNKID_WAYPATH_SPLINED,
  2429. MICROCHUNKID_ANIMATION_NAME,
  2430. MICROCHUNKID_ANIMATION_LOOPING,
  2431. MICROCHUNKID_FACE_LOCATION,
  2432. MICROCHUNKID_FACE_DURATION,
  2433. MICROCHUNKID_CONVERSATION_NAME,
  2434. MICROCHUNKID_ACTIVE_CONVERSATION_ID,
  2435. MICROCHUNKID_LOOK_LOCATION,
  2436. MICROCHUNKID_LOOK_DURATION,
  2437. MICROCHUNKID_DISABLE_HIBERNATION,
  2438. MICROCHUNKID_ATTACK_CHECK_BLOCKED,
  2439. MICROCHUNKID_ATTACK_ACTIVE,
  2440. MICROCHUNKID_ATTACK_WANDER_ALLOWED,
  2441. MICROCHUNKID_FORCE_FACING,
  2442. MICROCHUNKID_ATTACK_ERROR_OVERRIDE,
  2443. MICROCHUNKID_ATTACK_FACE_TARGET,
  2444. MICROCHUNKID_IGNORE_FACING,
  2445. MICROCHUNKID_ATTACK_FORCE_FIRE,
  2446. MICROCHUNKID_SHUTDOWN_ENGINE_ON_ARRIVAL,
  2447. };
  2448. bool SafeActionParamsStruct::Save( ChunkSaveClass & csave )
  2449. {
  2450. csave.Begin_Chunk( CHUNKID_VARIABLES );
  2451. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_PRIORITY, Priority );
  2452. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ACTION_ID, ActionID );
  2453. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_OBSERVER_ID, ObserverID );
  2454. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_LOOK_LOCATION, LookLocation );
  2455. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_LOOK_DURATION, LookDuration );
  2456. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_LOCATION, MoveLocation );
  2457. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_OBJECT_OFFSET, MoveObjectOffset );
  2458. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_SPEED, MoveSpeed );
  2459. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_ARRIVED_DISTANCE, MoveArrivedDistance );
  2460. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_BACKUP, MoveBackup );
  2461. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_FOLLOW, MoveFollow );
  2462. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_CROUCHED, MoveCrouched );
  2463. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_SHUTDOWN_ENGINE_ON_ARRIVAL, ShutdownEngineOnArrival );
  2464. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_MOVE_PATHFIND, MovePathfind );
  2465. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_RANGE, AttackRange );
  2466. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_ERROR, AttackError );
  2467. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_ERROR_OVERRIDE, AttackErrorOverride );
  2468. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_PRIMARY_FIRE, AttackPrimaryFire );
  2469. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_CROUCHED, AttackCrouched );
  2470. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_LOCATION, AttackLocation );
  2471. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_ACTIVE, AttackActive );
  2472. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_CHECK_BLOCKED, AttackCheckBlocked );
  2473. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_WANDER_ALLOWED, AttackWanderAllowed );
  2474. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_FACE_TARGET, AttackFaceTarget );
  2475. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ATTACK_FORCE_FIRE, AttackForceFire );
  2476. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_WAYPATH_ID, WaypathID );
  2477. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_WAYPOINT_START_ID, WaypointStartID );
  2478. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_WAYPOINT_END_ID, WaypointEndID );
  2479. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_WAYPATH_SPLINED, WaypathSplined );
  2480. WRITE_MICRO_CHUNK_WWSTRING( csave, MICROCHUNKID_ANIMATION_NAME,SafeAnimationName );
  2481. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ANIMATION_LOOPING, AnimationLooping );
  2482. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_FORCE_FACING, ForceFacing );
  2483. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IGNORE_FACING, IgnoreFacing );
  2484. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_FACE_LOCATION, FaceLocation );
  2485. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_FACE_DURATION, FaceDuration );
  2486. WRITE_MICRO_CHUNK_WWSTRING( csave, MICROCHUNKID_CONVERSATION_NAME, SafeConversationName );
  2487. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_ACTIVE_CONVERSATION_ID, ActiveConversationID );
  2488. csave.End_Chunk();
  2489. if ( MoveObjectRef != NULL ) {
  2490. csave.Begin_Chunk( CHUNKID_MOVE_OBJECT );
  2491. MoveObjectRef.Save( csave );
  2492. csave.End_Chunk();
  2493. }
  2494. if ( AttackObjectRef != NULL ) {
  2495. csave.Begin_Chunk( CHUNKID_ATTACK_OBJECT );
  2496. AttackObjectRef.Save( csave );
  2497. csave.End_Chunk();
  2498. }
  2499. if ( LookObjectRef != NULL ) {
  2500. csave.Begin_Chunk( CHUNKID_LOOK_OBJECT );
  2501. LookObjectRef.Save( csave );
  2502. csave.End_Chunk();
  2503. }
  2504. return true;
  2505. }
  2506. bool SafeActionParamsStruct::Load( ChunkLoadClass &cload )
  2507. {
  2508. while (cload.Open_Chunk()) {
  2509. switch(cload.Cur_Chunk_ID()) {
  2510. case CHUNKID_VARIABLES:
  2511. while (cload.Open_Micro_Chunk()) {
  2512. switch(cload.Cur_Micro_Chunk_ID()) {
  2513. READ_MICRO_CHUNK( cload, MICROCHUNKID_PRIORITY, Priority );
  2514. READ_MICRO_CHUNK( cload, MICROCHUNKID_ACTION_ID, ActionID );
  2515. READ_MICRO_CHUNK( cload, MICROCHUNKID_OBSERVER_ID, ObserverID );
  2516. READ_MICRO_CHUNK( cload, MICROCHUNKID_LOOK_LOCATION, LookLocation );
  2517. READ_MICRO_CHUNK( cload, MICROCHUNKID_LOOK_DURATION, LookDuration );
  2518. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_LOCATION, MoveLocation );
  2519. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_OBJECT_OFFSET, MoveObjectOffset );
  2520. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_SPEED, MoveSpeed );
  2521. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_ARRIVED_DISTANCE,MoveArrivedDistance );
  2522. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_BACKUP, MoveBackup );
  2523. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_FOLLOW, MoveFollow );
  2524. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_CROUCHED, MoveCrouched );
  2525. READ_MICRO_CHUNK( cload, MICROCHUNKID_SHUTDOWN_ENGINE_ON_ARRIVAL, ShutdownEngineOnArrival );
  2526. READ_MICRO_CHUNK( cload, MICROCHUNKID_MOVE_PATHFIND, MovePathfind );
  2527. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_RANGE, AttackRange );
  2528. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_ERROR, AttackError );
  2529. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_ERROR_OVERRIDE,AttackErrorOverride );
  2530. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_PRIMARY_FIRE, AttackPrimaryFire );
  2531. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_CROUCHED, AttackCrouched );
  2532. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_LOCATION, AttackLocation );
  2533. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_ACTIVE, AttackActive );
  2534. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_CHECK_BLOCKED, AttackCheckBlocked );
  2535. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_WANDER_ALLOWED, AttackWanderAllowed );
  2536. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_FACE_TARGET, AttackFaceTarget );
  2537. READ_MICRO_CHUNK( cload, MICROCHUNKID_ATTACK_FORCE_FIRE, AttackForceFire );
  2538. READ_MICRO_CHUNK( cload, MICROCHUNKID_WAYPATH_ID, WaypathID );
  2539. READ_MICRO_CHUNK( cload, MICROCHUNKID_WAYPOINT_START_ID, WaypointStartID );
  2540. READ_MICRO_CHUNK( cload, MICROCHUNKID_WAYPOINT_END_ID, WaypointEndID );
  2541. READ_MICRO_CHUNK( cload, MICROCHUNKID_WAYPATH_SPLINED, WaypathSplined );
  2542. READ_MICRO_CHUNK_WWSTRING( cload, MICROCHUNKID_ANIMATION_NAME,SafeAnimationName );
  2543. READ_MICRO_CHUNK( cload, MICROCHUNKID_ANIMATION_LOOPING, AnimationLooping );
  2544. READ_MICRO_CHUNK( cload, MICROCHUNKID_FORCE_FACING, ForceFacing );
  2545. READ_MICRO_CHUNK( cload, MICROCHUNKID_IGNORE_FACING, IgnoreFacing );
  2546. READ_MICRO_CHUNK( cload, MICROCHUNKID_FACE_LOCATION, FaceLocation );
  2547. READ_MICRO_CHUNK( cload, MICROCHUNKID_FACE_DURATION, FaceDuration );
  2548. READ_MICRO_CHUNK_WWSTRING( cload, MICROCHUNKID_CONVERSATION_NAME, SafeConversationName );
  2549. READ_MICRO_CHUNK( cload, MICROCHUNKID_ACTIVE_CONVERSATION_ID, ActiveConversationID );
  2550. default:
  2551. Debug_Say(("Unhandled Micro Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  2552. break;
  2553. }
  2554. cload.Close_Micro_Chunk();
  2555. }
  2556. break;
  2557. case CHUNKID_MOVE_OBJECT:
  2558. MoveObjectRef.Load( cload );
  2559. break;
  2560. case CHUNKID_ATTACK_OBJECT:
  2561. AttackObjectRef.Load( cload );
  2562. break;
  2563. case CHUNKID_LOOK_OBJECT:
  2564. LookObjectRef.Load( cload );
  2565. break;
  2566. default:
  2567. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  2568. break;
  2569. }
  2570. cload.Close_Chunk();
  2571. }
  2572. return true;
  2573. }
  2574. /*
  2575. **
  2576. */
  2577. ActionClass::ActionClass( SmartGameObj *obj ) :
  2578. ActionCode( NULL ),
  2579. ActCount( 0 ),
  2580. IsPaused( false )
  2581. {
  2582. WWASSERT( obj != NULL );
  2583. ActionObj = obj;
  2584. }
  2585. ActionClass::~ActionClass( void )
  2586. {
  2587. Set_Action_Code( NULL );
  2588. Delete_Action_Code();
  2589. }
  2590. void ActionClass::Set_Action_Code( ActionCodeClass * code )
  2591. {
  2592. if ( ActionCode != NULL ) {
  2593. ActionCode->Shutdown();
  2594. Register_Action_Code_Deletion( ActionCode );
  2595. }
  2596. ActionCode = code;
  2597. if ( ActionCode ) {
  2598. _ActionCodeChanges++;
  2599. ActionCode->Init( this );
  2600. }
  2601. }
  2602. void ActionClass::Done( int reason )
  2603. {
  2604. Parameters.Priority = 0; // reset
  2605. if ( ActionCode != NULL ) {
  2606. Set_Action_Code( NULL );
  2607. Notify_Completed( Parameters.ObserverID, Parameters.ActionID, reason ); // Notify the observer
  2608. }
  2609. }
  2610. bool ActionClass::Is_Acting( void )
  2611. {
  2612. return ( ActionCode != NULL );
  2613. }
  2614. bool ActionClass::Is_Animating( void )
  2615. {
  2616. if ( ActionCode != NULL ) {
  2617. return ActionCode->Is_Animating();
  2618. }
  2619. return false;
  2620. }
  2621. bool ActionClass::Modify( const ActionParamsStruct & parameters, bool modify_move, bool modify_attack )
  2622. {
  2623. bool retval = false;
  2624. if ( ActionCode != NULL ) {
  2625. SafeActionParamsStruct safe_params;
  2626. safe_params = parameters;
  2627. ActionCode->Modify_Parameters( safe_params, modify_move, modify_attack );
  2628. retval = true;
  2629. }
  2630. return retval;
  2631. }
  2632. bool ActionClass::Reset( float priority )
  2633. {
  2634. if ( priority >= Parameters.Priority ) {
  2635. Done( ACTION_COMPLETE_LOW_PRIORITY );
  2636. return true;
  2637. }
  2638. return false;
  2639. }
  2640. void ActionClass::Notify_Completed( int observer_id, int action_id, int reason )
  2641. {
  2642. if ( observer_id != 0 ) {
  2643. // Find the observer and notify
  2644. SmartGameObj * obj = Get_Action_Obj();
  2645. const GameObjObserverList & observer_list = obj->Get_Observers();
  2646. for( int index = 0; index < observer_list.Count(); index++ ) {
  2647. if ( observer_list[ index ]->Get_ID() == observer_id ) {
  2648. observer_list[ index ]->Action_Complete( obj, action_id, (ActionCompleteReason)reason );
  2649. }
  2650. }
  2651. }
  2652. }
  2653. bool ActionClass::Request_Action( ActionCodeClass * action, const ActionParamsStruct & parameters )
  2654. {
  2655. if ( parameters.Priority >= Parameters.Priority ) {
  2656. bool old_action_completed = false;
  2657. int old_observer_id = 0;
  2658. int old_action_id = 0;
  2659. if ( ActionCode != NULL ) {
  2660. old_action_completed = true;
  2661. old_observer_id = Parameters.ObserverID;
  2662. old_action_id = Parameters.ActionID;
  2663. }
  2664. Parameters = parameters;
  2665. Set_Action_Code( action );
  2666. if ( old_action_completed ) {
  2667. Notify_Completed( old_observer_id, old_action_id, ACTION_COMPLETE_LOW_PRIORITY );
  2668. }
  2669. return true;
  2670. } else {
  2671. Notify_Completed( parameters.ObserverID, parameters.ActionID, ACTION_COMPLETE_LOW_PRIORITY );
  2672. if ( action != NULL ) {
  2673. delete action;
  2674. }
  2675. return false;
  2676. }
  2677. }
  2678. bool ActionClass::Follow_Input( const ActionParamsStruct & parameters )
  2679. {
  2680. return Request_Action( new FollowInputActionCodeClass(), parameters );
  2681. }
  2682. bool ActionClass::Stand( const ActionParamsStruct & parameters )
  2683. {
  2684. return Request_Action( new StandActionCodeClass(), parameters );
  2685. }
  2686. bool ActionClass::Play_Animation( const ActionParamsStruct & parameters )
  2687. {
  2688. #if 0 // This is difficult, because huamns play default anims
  2689. // Try to confirm we are not playing a non-action anim
  2690. if ( !Is_Animating() && ActionObj->Get_Anim_Control()->Get_Animation_Name()[0] != 0 ) {
  2691. Debug_Say(( "Can't Action_Play_Animation when playing a non-action anim\n" ));
  2692. WWASSERT( 0 );
  2693. }
  2694. #endif
  2695. return Request_Action( new PlayAnimationActionCodeClass(), parameters );
  2696. }
  2697. bool ActionClass::Goto( const ActionParamsStruct & parameters )
  2698. {
  2699. return Request_Action( new GotoActionCodeClass(), parameters );
  2700. }
  2701. bool ActionClass::Attack( const ActionParamsStruct & parameters )
  2702. {
  2703. return Request_Action( new AttackActionCodeClass(), parameters );
  2704. }
  2705. bool ActionClass::Face_Location( const ActionParamsStruct & parameters )
  2706. {
  2707. return Request_Action( new FaceLocationActionCodeClass(), parameters );
  2708. }
  2709. bool ActionClass::Have_Conversation( const ActionParamsStruct & parameters )
  2710. {
  2711. return Request_Action( new ConversationActionCodeClass(), parameters );
  2712. }
  2713. bool ActionClass::Dock_Vehicle( const ActionParamsStruct & parameters )
  2714. {
  2715. return Request_Action( new DockActionCodeClass(), parameters );
  2716. }
  2717. bool ActionClass::Enter_Exit( const ActionParamsStruct & parameters )
  2718. {
  2719. return Request_Action( new EnterExitActionCodeClass(), parameters );
  2720. }
  2721. bool ActionClass::Dive( const ActionParamsStruct & parameters )
  2722. {
  2723. return Request_Action( new DiveActionCodeClass(), parameters );
  2724. }
  2725. void ActionClass::Act( void )
  2726. {
  2727. if ( IsPaused == false ) {
  2728. ActCount++;
  2729. ActionObj->Clear_Control();
  2730. if ( ActionCode != NULL ) {
  2731. WWPROFILE( "Action Code" );
  2732. _ActionActCalls++;
  2733. ActionCode->Act();
  2734. }
  2735. Delete_Action_Code();
  2736. }
  2737. }
  2738. bool ActionClass::Is_Active( void )
  2739. {
  2740. return ( ActionCode != NULL );
  2741. }
  2742. bool ActionClass::Is_Busy( void )
  2743. {
  2744. bool retval = false;
  2745. if ( ActionCode != NULL ) {
  2746. retval = ActionCode->Is_Busy();
  2747. }
  2748. return retval;
  2749. }
  2750. /*
  2751. ** ActionClass Save and Load
  2752. */
  2753. enum {
  2754. CHUNKID_ACTION_CODE = 712001900,
  2755. CHUNKID_PARAMETERS,
  2756. };
  2757. bool ActionClass::Save( ChunkSaveClass & csave )
  2758. {
  2759. if ( ActionCode != NULL ) {
  2760. csave.Begin_Chunk( CHUNKID_ACTION_CODE );
  2761. csave.Begin_Chunk( ActionCode->Get_Factory().Chunk_ID() );
  2762. ActionCode->Get_Factory().Save( csave, ActionCode );
  2763. csave.End_Chunk();
  2764. csave.End_Chunk();
  2765. }
  2766. csave.Begin_Chunk( CHUNKID_PARAMETERS );
  2767. Parameters.Save( csave );
  2768. csave.End_Chunk();
  2769. // Don't need to save ActionObj because it is set on ActionClass Constructor
  2770. // Don't need to save ActCount because it is diagnostic
  2771. return true;
  2772. }
  2773. bool ActionClass::Load( ChunkLoadClass &cload )
  2774. {
  2775. while (cload.Open_Chunk()) {
  2776. switch(cload.Cur_Chunk_ID()) {
  2777. case CHUNKID_ACTION_CODE:
  2778. while (cload.Open_Chunk()) {
  2779. PersistFactoryClass * factory = SaveLoadSystemClass::Find_Persist_Factory( cload.Cur_Chunk_ID() );
  2780. if ( factory ) {
  2781. ActionCode = (ActionCodeClass *)factory->Load( cload );
  2782. WWASSERT( ActionCode != NULL );
  2783. ActionCode->Set_Action( this );
  2784. }
  2785. cload.Close_Chunk();
  2786. }
  2787. break;
  2788. case CHUNKID_PARAMETERS:
  2789. Parameters.Load( cload );
  2790. break;
  2791. default:
  2792. Debug_Say(( "Unrecognized Action chunkID\n" ));
  2793. break;
  2794. }
  2795. cload.Close_Chunk();
  2796. }
  2797. return true;
  2798. }
  2799. void ActionClass::Begin_Hibernation( void )
  2800. {
  2801. //
  2802. // Let the action code know we are entering hibernation
  2803. //
  2804. if ( ActionCode != NULL ) {
  2805. ActionCode->Begin_Hibernation();
  2806. }
  2807. return ;
  2808. }
  2809. void ActionClass::End_Hibernation( void )
  2810. {
  2811. //
  2812. // Let the action code know we are coming out of hibernation
  2813. //
  2814. if ( ActionCode != NULL ) {
  2815. ActionCode->End_Hibernation();
  2816. }
  2817. return ;
  2818. }