scripts.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/Scripts/scripts.h $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 11/29/01 11:08a $*
  29. * *
  30. * $Revision:: 26 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #ifndef SCRIPTS_H
  36. #define SCRIPTS_H
  37. #include "scriptevents.h"
  38. #include "scriptcommands.h"
  39. #include "scriptregistrant.h"
  40. #include "string_ids.h"
  41. #include <stdlib.h>
  42. class CombatSound;
  43. class ScriptFactory;
  44. // ScriptVariables
  45. class ScriptVariableClass
  46. {
  47. public:
  48. ScriptVariableClass( void * data_ptr, int data_size, int id, ScriptVariableClass * next ) :
  49. DataPtr( data_ptr ),
  50. DataSize( data_size ),
  51. ID( id ),
  52. Next( next ) {}
  53. void * Get_Data_Ptr( void ) { return DataPtr; }
  54. int Get_Data_Size( void ) { return DataSize; }
  55. int Get_ID( void ) { return ID; }
  56. ScriptVariableClass * Get_Next( void ) { return Next; }
  57. private:
  58. void * DataPtr;
  59. int DataSize;
  60. int ID;
  61. ScriptVariableClass * Next;
  62. };
  63. class ScriptImpClass : public ScriptClass
  64. {
  65. public:
  66. static void Set_Request_Destroy_Func(void (*function)(ScriptClass*));
  67. ScriptImpClass();
  68. virtual ~ScriptImpClass();
  69. // Retrieve the name of the script.
  70. const char* Get_Name(void);
  71. // Retrieve owner object of the script.
  72. GameObject* Owner()
  73. {return mOwner;}
  74. GameObject** Get_Owner_Ptr()
  75. {return &mOwner;}
  76. // Set the scripts parameter string.
  77. void Set_Parameters_String(const char* params);
  78. // Retrieve the scripts parameter string.
  79. void Get_Parameters_String(char* buffer, unsigned int size);
  80. // Retrieve the parameter count.
  81. int Get_Parameter_Count(void)
  82. {return mArgC;}
  83. // Retrieve a parameter by name.
  84. const char* Get_Parameter(const char* name);
  85. // Retrieve a parameter by is ordinal position in the parameter list.
  86. const char* Get_Parameter(int index);
  87. // Get a parameter as an integer
  88. int Get_Int_Parameter(int index)
  89. {return atoi(Get_Parameter(index));}
  90. // Get a parameter as an integer
  91. int Get_Int_Parameter(const char* parameterName);
  92. // Get a parameter as a float
  93. float Get_Float_Parameter(int index)
  94. {return (float)atof(Get_Parameter(index));}
  95. // Get a parameter as a float
  96. float Get_Float_Parameter(const char* parameterName);
  97. // Get a parameter as a vector3
  98. Vector3 Get_Vector3_Parameter(int index);
  99. // Get a parameter as a float
  100. Vector3 Get_Vector3_Parameter(const char* parameterName);
  101. // Get a parameter as an float
  102. int Get_Parameter_Index(const char* parameterName);
  103. // Set the script factory used to create this script.
  104. //
  105. // This must only be called by the script factory upon creation of
  106. // the script.
  107. void SetFactory(ScriptFactory* factory)
  108. {mFactory = factory;}
  109. /* Event Functions which will be called as events happen
  110. * Scripts can choose to override any of these functions,
  111. * otherwise the following empty functions will be called
  112. */
  113. virtual void Created( GameObject * obj ) {}
  114. virtual void Destroyed( GameObject * obj ) {}
  115. virtual void Killed( GameObject * obj, GameObject * killer ) {}
  116. virtual void Damaged( GameObject * obj, GameObject * damager, float amount ) {}
  117. virtual void Custom( GameObject * obj, int type, int param, GameObject * sender ) {}
  118. virtual void Sound_Heard( GameObject * obj, const CombatSound & sound ) {}
  119. virtual void Enemy_Seen( GameObject * obj, GameObject * enemy ) {}
  120. virtual void Action_Complete( GameObject * obj, int action_id, ActionCompleteReason complete_reason ) {}
  121. virtual void Timer_Expired( GameObject * obj, int timer_id ) {}
  122. virtual void Animation_Complete( GameObject * obj, const char * animation_name ) {}
  123. virtual void Poked( GameObject * obj, GameObject * poker ) {}
  124. virtual void Entered( GameObject * obj, GameObject * enterer ) {}
  125. virtual void Exited( GameObject * obj, GameObject * exiter ) {}
  126. // Save and Load specific script
  127. virtual void Save_Data(ScriptSaver& saver) {}
  128. virtual void Load_Data(ScriptLoader& loader) {}
  129. // Auto Variable Save and Load
  130. virtual void Register_Auto_Save_Variables( void ) {}
  131. void Auto_Save_Variable( void * data_ptr, int data_size, int id );
  132. protected:
  133. void Destroy_Script(void);
  134. void Attach(GameObject* obj);
  135. void Detach(GameObject* obj);
  136. virtual void Save(ScriptSaver& saver);
  137. virtual void Load(ScriptLoader& loader);
  138. private:
  139. static void (*Request_Destroy_Script)(ScriptClass*);
  140. void Clear_Parameters(void);
  141. void Set_Parameter(int index, const char* str);
  142. GameObject* mOwner;
  143. int mArgC;
  144. char** mArgV;
  145. // The factory reference is provided to the script class so that it
  146. // knows what it is (IE: Name, Parameter description).
  147. //
  148. // The factory reference could also be used for script cloning.
  149. // (The script would use this factory to create a new instance of
  150. // itself then copy its state to the new script instance.)
  151. ScriptFactory* mFactory;
  152. // Auto Variable Save and Load
  153. ScriptVariableClass * AutoVariableList;
  154. };
  155. // Declare script definition
  156. #define DECLARE_SCRIPT(x, d) \
  157. REGISTER_SCRIPT(x, d) \
  158. class x : public ScriptImpClass
  159. // Load / Save Macros
  160. #define SAVE_BEGIN()
  161. #define SAVE_DATA(id, var) Commands->Save_Data(saver, id, sizeof(var), &var)
  162. #define SAVE_STRING(id, string) Commands->Save_Data(saver, id, strlen(string), string)
  163. #define SAVE_END()
  164. #define LOAD_BEGIN() \
  165. { \
  166. int id; \
  167. while (Commands->Load_Begin(loader, &id)) { \
  168. switch (id) {
  169. #define LOAD_DATA(id, var) \
  170. case id: \
  171. Commands->Load_Data(loader, sizeof(var), &var); \
  172. break;
  173. #define LOAD_END() \
  174. default: \
  175. break; \
  176. } \
  177. Commands->Load_End(loader); \
  178. } \
  179. }
  180. #define LOAD_STRING(id, var) LOAD_DATA(id, var)
  181. // Auto Variable Save/Load
  182. #define REGISTER_VARIABLES() public: void Register_Auto_Save_Variables( void )
  183. #define SAVE_VARIABLE( x, id ) Auto_Save_Variable( &x, sizeof( x ), id )
  184. extern ScriptCommands* Commands;
  185. // Array Macros
  186. #define ARRAY_ELEMENT_COUNT( x ) ( sizeof( x ) / sizeof( x[0] ) )
  187. #define RANDOM_ARRAY_ELEMENT( x ) ( x[Commands->Get_Random_Int( 0, ARRAY_ELEMENT_COUNT( x ) )] )
  188. #endif // SCRIPTS_H