introscenecharacter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. class IntroSceneCharacter extends Managed
  2. {
  3. protected int m_CharacterId;
  4. protected string m_CharacterType;
  5. protected MenuData m_CharacterDta;
  6. protected PlayerBase m_CharacterObj;
  7. protected vector m_CharacterPos;
  8. protected vector m_CharacterRot;
  9. protected ref TStringArray m_CharGenderList = new TStringArray;
  10. protected ref TStringArray m_CharShirtList = new TStringArray; //legacy
  11. protected ref TStringArray m_CharPantsList = new TStringArray; //legacy
  12. protected ref TStringArray m_CharShoesList = new TStringArray; //legacy
  13. protected ref map<ECharGender, ref array<string>> m_Characters = new map<ECharGender, ref array<string>>;
  14. protected ECharGender m_CharGender;
  15. void IntroSceneCharacter()
  16. {
  17. m_CharacterId = GameConstants.DEFAULT_CHARACTER_MENU_ID;
  18. }
  19. void ~IntroSceneCharacter()
  20. {
  21. CharacterUnload();
  22. }
  23. bool IsDefaultCharacter()
  24. {
  25. return m_CharacterId == GameConstants.DEFAULT_CHARACTER_MENU_ID;
  26. }
  27. void SetToDefaultCharacter()
  28. {
  29. m_CharacterId = GameConstants.DEFAULT_CHARACTER_MENU_ID;
  30. }
  31. //==============================================
  32. // SetcharacterID
  33. //==============================================
  34. void SetCharacterID(int char_id)
  35. {
  36. m_CharacterId = char_id;
  37. }
  38. //==============================================
  39. // GetCharacterID
  40. //==============================================
  41. int GetCharacterID()
  42. {
  43. return m_CharacterId;
  44. }
  45. //==============================================
  46. // GetCharacterObj
  47. //==============================================
  48. PlayerBase GetCharacterObj()
  49. {
  50. return m_CharacterObj;
  51. }
  52. //==============================================
  53. // GetCharGenderList
  54. //==============================================
  55. TStringArray GetCharGenderList()
  56. {
  57. return m_CharGenderList;
  58. }
  59. //==============================================
  60. // GetCharGenderList
  61. //==============================================
  62. TStringArray GetCharList(ECharGender gender)
  63. {
  64. return m_Characters[gender];
  65. }
  66. //==============================================
  67. // GetCharShirtsList
  68. //==============================================
  69. TStringArray GetCharShirtsList()
  70. {
  71. return m_CharShirtList;
  72. }
  73. //==============================================
  74. // GetCharPantsList
  75. //==============================================
  76. TStringArray GetCharPantsList()
  77. {
  78. return m_CharPantsList;
  79. }
  80. //==============================================
  81. // GetCharShoesList
  82. //==============================================
  83. TStringArray GetCharShoesList()
  84. {
  85. return m_CharShoesList;
  86. }
  87. //==============================================
  88. // SetCharacterGender
  89. //==============================================
  90. void SetCharacterGender(ECharGender gender)
  91. {
  92. m_CharGender = gender;
  93. }
  94. //==============================================
  95. // IsCharacterFemale
  96. //==============================================
  97. bool IsCharacterFemale()
  98. {
  99. return ( m_CharGender == ECharGender.Female );
  100. }
  101. //==============================================
  102. // SetCharacterGender
  103. //==============================================
  104. ECharGender GetCharacterGender()
  105. {
  106. return m_CharGender;
  107. }
  108. //==============================================
  109. // GetPosition
  110. //==============================================
  111. vector GetPosition()
  112. {
  113. return m_CharacterObj.GetPosition();
  114. }
  115. //==============================================
  116. // GetNextCharacterID
  117. //==============================================
  118. int GetNextCharacterID()
  119. {
  120. int count = m_CharacterDta.GetCharactersCount();
  121. if ( count == 0 )
  122. {
  123. return -1;
  124. }
  125. if ( m_CharacterId + 1 < count )
  126. {
  127. return m_CharacterId + 1;
  128. }
  129. else
  130. {
  131. return -1;
  132. }
  133. }
  134. //==============================================
  135. // GetPrevCharacterID
  136. //==============================================
  137. int GetPrevCharacterID()
  138. {
  139. int count = m_CharacterDta.GetCharactersCount();
  140. if ( count == 0 )
  141. {
  142. return -1;
  143. }
  144. if ( m_CharacterId > -1 )
  145. {
  146. return m_CharacterId - 1;
  147. }
  148. return count - 1;
  149. }
  150. //==============================================
  151. // CreateNewCharacterRandom
  152. //==============================================
  153. void CreateNewCharacterRandom()
  154. {
  155. // Select random gender
  156. SetCharacterGender( Math.RandomInt(0, 2) );
  157. // Select random character skin (class name)
  158. string char_name_random = m_Characters[GetCharacterGender()].GetRandomElement();
  159. // Create new character
  160. CreateNewCharacterByName( char_name_random );
  161. }
  162. //==============================================
  163. // CreateNewCharacterById
  164. //==============================================
  165. void CreateNewCharacterById( int character_id )
  166. {
  167. if ( character_id == GameConstants.DEFAULT_CHARACTER_MENU_ID )
  168. {
  169. CreateDefaultCharacter();
  170. }
  171. else
  172. {
  173. CharacterLoad( character_id, m_CharacterPos, m_CharacterRot );
  174. }
  175. }
  176. //==============================================
  177. // CreateNewCharacterByName
  178. //==============================================
  179. void CreateNewCharacterByName( string character_name, bool randomize_equip = true )
  180. {
  181. m_CharacterType = character_name;
  182. GetGame().GetMenuDefaultCharacterData().SetCharacterType(m_CharacterType);
  183. if (randomize_equip)
  184. GetGame().GetMenuDefaultCharacterData().GenerateRandomEquip();
  185. CreateNewCharacter();
  186. }
  187. void CreateDefaultCharacter()
  188. {
  189. CharacterUnload();
  190. //m_CharacterDta.RequestGetDefaultCharacterData();
  191. m_CharacterType = GetGame().GetMenuDefaultCharacterData().GetCharacterType();
  192. if (m_CharacterType != "")
  193. {
  194. CreateNewCharacter();
  195. }
  196. if(m_CharacterObj)
  197. {
  198. m_CharacterObj.PlaceOnSurface();
  199. m_CharacterObj.SetPosition(m_CharacterPos);
  200. m_CharacterObj.SetOrientation(m_CharacterRot);
  201. }
  202. else
  203. {
  204. string default_name = Widget.TranslateString( GameConstants.DEFAULT_CHARACTER_NAME );
  205. CreateNewCharacterRandom();
  206. //m_CharacterDta.SetCharacterName(GameConstants.DEFAULT_CHARACTER_MENU_ID, default_name);
  207. GetGame().GetMenuDefaultCharacterData().SetCharacterName(GameConstants.DEFAULT_CHARACTER_NAME);
  208. }
  209. }
  210. void GetLastPlayedServer(int characterID, out string address, out string name, out int port)
  211. {
  212. m_CharacterDta.GetLastServerAddress(characterID,address);
  213. port = m_CharacterDta.GetLastServerPort(characterID);
  214. m_CharacterDta.GetLastServerName(characterID,name);
  215. }
  216. void GetLastPlayedServerEx(int characterID, out string address, out string name, out int port, out int steamQueryPort)
  217. {
  218. steamQueryPort = m_CharacterDta.GetLastSteamQueryPort(characterID);
  219. GetLastPlayedServer(characterID, address, name, port);
  220. }
  221. //==============================================
  222. // CreateNewCharacter
  223. //==============================================
  224. void CreateNewCharacter()
  225. {
  226. // Unload old character (if exist)
  227. CharacterUnload();
  228. // Create Character
  229. m_CharacterObj = PlayerBase.Cast(g_Game.CreateObjectEx(m_CharacterType, m_CharacterPos, ECE_PLACE_ON_SURFACE));
  230. if (m_CharacterObj)
  231. {
  232. m_CharacterObj.PlaceOnSurface();
  233. m_CharacterObj.SetOrientation(m_CharacterRot);
  234. GetGame().GetMenuDefaultCharacterData().EquipDefaultCharacter(m_CharacterObj);
  235. }
  236. //Create New Random Character
  237. SetupPlayerName( true );
  238. }
  239. //! Generates random equip for the new IntroSceneCharacter, whatever is defined in 'cfgCharacterCreation'
  240. /*void EquipNewCharacter()
  241. {
  242. int slot_ID;
  243. string attachment_type;
  244. for (int i = 0; i < DefaultCharacterCreationMethods.GetAttachmentSlotsArray().Count(); i++)
  245. {
  246. slot_ID = DefaultCharacterCreationMethods.GetAttachmentSlotsArray().Get(i);
  247. if (DefaultCharacterCreationMethods.GetConfigArrayCountFromSlotID(slot_ID) > 0)
  248. {
  249. attachment_type = DefaultCharacterCreationMethods.GetConfigAttachmentTypes(slot_ID).GetRandomElement();
  250. if (attachment_type != "")
  251. SetAttachment(attachment_type,slot_ID);
  252. }
  253. }
  254. }*/
  255. //==============================================
  256. // LoadCharacterData
  257. //==============================================
  258. void LoadCharacterData(vector char_pos, vector char_rot, bool default_char = false)
  259. {
  260. m_CharacterDta = g_Game.GetMenuData();
  261. m_CharacterPos = char_pos;
  262. m_CharacterRot = char_rot;
  263. if (!default_char && m_CharacterDta.GetLastPlayedCharacter() > -1 )
  264. {
  265. m_CharacterId = m_CharacterDta.GetLastPlayedCharacter();
  266. m_CharacterDta.GetCharacterName(m_CharacterId, g_Game.GetPlayerGameName());
  267. }
  268. // Load all avalible options for character creation; mostly legacy stuff
  269. g_Game.ConfigGetTextArray("cfgCharacterCreation" + " gender", m_CharGenderList);
  270. g_Game.ConfigGetTextArray("cfgCharacterCreation" + " top", m_CharShirtList);
  271. g_Game.ConfigGetTextArray("cfgCharacterCreation" + " bottom", m_CharPantsList);
  272. g_Game.ConfigGetTextArray("cfgCharacterCreation" + " shoe", m_CharShoesList);
  273. // Init character table
  274. m_Characters.Clear();
  275. m_Characters.Insert( ECharGender.Male, new array<string> );
  276. m_Characters.Insert( ECharGender.Female, new array<string> );
  277. // Sort character by Gender
  278. TStringArray characters = GetGame().ListAvailableCharacters();
  279. for (int i = 0; i < characters.Count(); i++)
  280. {
  281. string char_cfg_name = characters.Get(i);
  282. if ( GetGame().IsKindOf(char_cfg_name, "SurvivorMale_Base") )
  283. {
  284. m_Characters[ECharGender.Male].Insert( char_cfg_name );
  285. }
  286. else
  287. {
  288. m_Characters[ECharGender.Female].Insert( char_cfg_name );
  289. }
  290. }
  291. CreateNewCharacterById(m_CharacterId/*, m_CharacterPos, m_CharacterRot*/);
  292. if (GetCharacterObj() )
  293. {
  294. if ( GetCharacterObj().IsMale() )
  295. SetCharacterGender(ECharGender.Male);
  296. else
  297. SetCharacterGender(ECharGender.Female);
  298. }
  299. }
  300. //-------------------------
  301. // CharacterUnload
  302. //-------------------------
  303. protected void CharacterUnload()
  304. {
  305. if ( m_CharacterObj )
  306. {
  307. g_Game.ObjectDelete(m_CharacterObj);
  308. m_CharacterObj = NULL;
  309. }
  310. }
  311. //-------------------------
  312. // CharacterLoad
  313. //-------------------------
  314. protected void CharacterLoad( int character_id, vector char_pos, vector char_rot )
  315. {
  316. if ( character_id == -1 )
  317. {
  318. Error("IntroSceneCharacter->CharacterLoad: character_id = "+ character_id +" Cant Load Character!!!");
  319. return;
  320. }
  321. CharacterUnload();
  322. SetCharacterID( character_id );
  323. m_CharacterObj = PlayerBase.Cast( m_CharacterDta.CreateCharacterPerson( character_id ) );
  324. if ( m_CharacterObj )
  325. {
  326. m_CharacterObj.PlaceOnSurface();
  327. m_CharacterObj.SetPosition(char_pos);
  328. m_CharacterObj.SetOrientation(char_rot);
  329. }
  330. }
  331. //-------------------------
  332. // SetupPlayerName
  333. //-------------------------
  334. protected void SetupPlayerName( bool new_name )
  335. {
  336. string name = GameConstants.DEFAULT_CHARACTER_NAME;
  337. #ifdef PLATFORM_CONSOLE
  338. BiosUserManager user_manager = GetGame().GetUserManager();
  339. if( user_manager )
  340. {
  341. BiosUser user = user_manager.GetSelectedUser();
  342. if( user )
  343. {
  344. name = user.GetName();
  345. }
  346. }
  347. #else
  348. if ( !new_name )
  349. {
  350. m_CharacterDta.GetCharacterName(m_CharacterId, name);
  351. }
  352. #endif
  353. g_Game.SetPlayerGameName(name);
  354. }
  355. //==============================================
  356. // SetAttachment
  357. //==============================================
  358. void SetAttachment(string type, int slot)
  359. {
  360. if ( !m_CharacterObj )
  361. {
  362. return;
  363. }
  364. g_Game.ObjectDelete(m_CharacterObj.GetInventory().FindAttachment(slot));
  365. EntityAI entity = EntityAI.Cast( g_Game.CreateObjectEx(type, m_CharacterObj.GetPosition(), ECE_PLACE_ON_SURFACE) );
  366. if (entity)
  367. {
  368. m_CharacterObj.LocalTakeEntityAsAttachmentEx(entity, slot);
  369. }
  370. }
  371. string GetCharacterNameById(int char_id )
  372. {
  373. string character_name;
  374. if (char_id == GameConstants.DEFAULT_CHARACTER_MENU_ID)
  375. {
  376. character_name = GetGame().GetMenuDefaultCharacterData().GetCharacterName();
  377. }
  378. else
  379. {
  380. m_CharacterDta.GetCharacterName(char_id, character_name);
  381. }
  382. return character_name;
  383. }
  384. string GetCharacterName()
  385. {
  386. string character_name;
  387. if (IsDefaultCharacter())
  388. {
  389. character_name = GetGame().GetMenuDefaultCharacterData().GetCharacterName();
  390. }
  391. else
  392. {
  393. m_CharacterDta.GetCharacterName(m_CharacterId, character_name);
  394. }
  395. return character_name;
  396. }
  397. //==============================================
  398. // SaveCharName
  399. //==============================================
  400. void SaveCharName( string name )
  401. {
  402. if (IsDefaultCharacter())
  403. {
  404. GetGame().GetMenuDefaultCharacterData().SetCharacterName(name);
  405. }
  406. else
  407. {
  408. m_CharacterDta.SetCharacterName(m_CharacterId, name);
  409. }
  410. m_CharacterDta.SaveCharactersLocal();
  411. }
  412. //==============================================
  413. // SaveDefaultCharacter
  414. //==============================================
  415. void SaveDefaultCharacter()
  416. {
  417. //if (m_CharacterObj)
  418. {
  419. m_CharacterDta.RequestSetDefaultCharacterData();
  420. }
  421. }
  422. }