9
3

BotDB.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "../WorldDatabase.h"
  2. #include "../../common/Log.h"
  3. #include "Bot.h"
  4. #include "../classes.h"
  5. #include "../races.h"
  6. extern Classes classes;
  7. extern Races races;
  8. int32 WorldDatabase::CreateNewBot(int32 char_id, string name, int8 race, int8 advClass, int8 gender, int16 model_id, int32& index) {
  9. DatabaseResult result;
  10. index = 0;
  11. if (!database_new.Select(&result, "SELECT MAX(`bot_id`) FROM `bots` WHERE `char_id` = %u", char_id)) {
  12. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  13. return 0;
  14. }
  15. if (result.Next()) {
  16. if (result.IsNull(0))
  17. index = 1;
  18. else
  19. index = result.GetInt32(0) + 1;
  20. }
  21. if (!database_new.Query("INSERT INTO `bots` (`char_id`, `bot_id`, `name`, `race`, `class`, `gender`, `model_type`) VALUES (%u, %u, \"%s\", %u, %u, %u, %u)", char_id, index, name.c_str(), race, advClass, gender, model_id)) {
  22. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  23. return 0;
  24. }
  25. int32 ret = database_new.LastInsertID();
  26. LogWrite(PLAYER__DEBUG, 0, "Player", "New bot (%s) created for player (%u)", name.c_str(), char_id);
  27. return ret;
  28. }
  29. void WorldDatabase::SaveBotAppearance(Bot* bot) {
  30. SaveBotColors(bot->BotID, "skin_color", bot->features.skin_color);
  31. SaveBotColors(bot->BotID, "model_color", bot->features.model_color);
  32. SaveBotColors(bot->BotID, "eye_color", bot->features.eye_color);
  33. SaveBotColors(bot->BotID, "hair_color1", bot->features.hair_color1);
  34. SaveBotColors(bot->BotID, "hair_color2", bot->features.hair_color2);
  35. SaveBotColors(bot->BotID, "hair_highlight", bot->features.hair_highlight_color);
  36. SaveBotColors(bot->BotID, "hair_type_color", bot->features.hair_type_color);
  37. SaveBotColors(bot->BotID, "hair_type_highlight_color", bot->features.hair_type_highlight_color);
  38. SaveBotColors(bot->BotID, "hair_face_color", bot->features.hair_face_color);
  39. SaveBotColors(bot->BotID, "hair_face_highlight_color", bot->features.hair_face_highlight_color);
  40. SaveBotColors(bot->BotID, "wing_color1", bot->features.wing_color1);
  41. SaveBotColors(bot->BotID, "wing_color2", bot->features.wing_color2);
  42. SaveBotColors(bot->BotID, "shirt_color", bot->features.shirt_color);
  43. //SaveBotColors(bot->BotID, "unknown_chest_color", );
  44. SaveBotColors(bot->BotID, "pants_color", bot->features.pants_color);
  45. //SaveBotColors(bot->BotID, "unknown_legs_color", );
  46. //SaveBotColors(bot->BotID, "unknown9", );
  47. SaveBotFloats(bot->BotID, "eye_type", bot->features.eye_type[0], bot->features.eye_type[1], bot->features.eye_type[2]);
  48. SaveBotFloats(bot->BotID, "ear_type", bot->features.ear_type[0], bot->features.ear_type[1], bot->features.ear_type[2]);
  49. SaveBotFloats(bot->BotID, "eye_brow_type", bot->features.eye_brow_type[0], bot->features.eye_brow_type[1], bot->features.eye_brow_type[2]);
  50. SaveBotFloats(bot->BotID, "cheek_type", bot->features.cheek_type[0], bot->features.cheek_type[1], bot->features.cheek_type[2]);
  51. SaveBotFloats(bot->BotID, "lip_type", bot->features.lip_type[0], bot->features.lip_type[1], bot->features.lip_type[2]);
  52. SaveBotFloats(bot->BotID, "chin_type", bot->features.chin_type[0], bot->features.chin_type[1], bot->features.chin_type[2]);
  53. SaveBotFloats(bot->BotID, "nose_type", bot->features.nose_type[0], bot->features.nose_type[1], bot->features.nose_type[2]);
  54. SaveBotFloats(bot->BotID, "body_size", bot->features.body_size, 0, 0);
  55. SaveBotFloats(bot->BotID, "body_age", bot->features.body_age, 0, 0);
  56. SaveBotColors(bot->BotID, "soga_skin_color", bot->features.soga_skin_color);
  57. SaveBotColors(bot->BotID, "soga_model_color", bot->features.soga_model_color);
  58. SaveBotColors(bot->BotID, "soga_eye_color", bot->features.soga_eye_color);
  59. SaveBotColors(bot->BotID, "soga_hair_color1", bot->features.soga_hair_color1);
  60. SaveBotColors(bot->BotID, "soga_hair_color2", bot->features.soga_hair_color2);
  61. SaveBotColors(bot->BotID, "soga_hair_highlight", bot->features.soga_hair_highlight_color);
  62. SaveBotColors(bot->BotID, "soga_hair_type_color", bot->features.soga_hair_type_color);
  63. SaveBotColors(bot->BotID, "soga_hair_type_highlight_color", bot->features.soga_hair_type_highlight_color);
  64. SaveBotColors(bot->BotID, "soga_hair_face_color", bot->features.soga_hair_face_color);
  65. SaveBotColors(bot->BotID, "soga_hair_face_highlight_color", bot->features.soga_hair_face_highlight_color);
  66. SaveBotColors(bot->BotID, "soga_wing_color1", bot->features.wing_color1);
  67. SaveBotColors(bot->BotID, "soga_wing_color2", bot->features.wing_color2);
  68. SaveBotColors(bot->BotID, "soga_shirt_color", bot->features.shirt_color);
  69. //SaveBotColors(bot->BotID, "soga_unknown_chest_color", );
  70. SaveBotColors(bot->BotID, "soga_pants_color", bot->features.pants_color);
  71. //SaveBotColors(bot->BotID, "soga_unknown_legs_color", );
  72. //SaveBotColors(bot->BotID, "soga_unknown13", );
  73. SaveBotFloats(bot->BotID, "soga_eye_type", bot->features.soga_eye_type[0], bot->features.soga_eye_type[1], bot->features.soga_eye_type[2]);
  74. SaveBotFloats(bot->BotID, "soga_ear_type", bot->features.soga_ear_type[0], bot->features.soga_ear_type[1], bot->features.soga_ear_type[2]);
  75. SaveBotFloats(bot->BotID, "soga_eye_brow_type", bot->features.soga_eye_brow_type[0], bot->features.soga_eye_brow_type[1], bot->features.soga_eye_brow_type[2]);
  76. SaveBotFloats(bot->BotID, "soga_cheek_type", bot->features.soga_cheek_type[0], bot->features.soga_cheek_type[1], bot->features.soga_cheek_type[2]);
  77. SaveBotFloats(bot->BotID, "soga_lip_type", bot->features.soga_lip_type[0], bot->features.soga_lip_type[1], bot->features.soga_lip_type[2]);
  78. SaveBotFloats(bot->BotID, "soga_chin_type", bot->features.soga_chin_type[0], bot->features.soga_chin_type[1], bot->features.soga_chin_type[2]);
  79. SaveBotFloats(bot->BotID, "soga_nose_type", bot->features.soga_nose_type[0], bot->features.soga_nose_type[1], bot->features.soga_nose_type[2]);
  80. if (!database_new.Query("UPDATE `bots` SET `model_type` = %u, `hair_type` = %u, `face_type` = %u, `wing_type` = %u, `chest_type` = %u, `legs_type` = %u, `soga_model_type` = %u, `soga_hair_type` = %u, `soga_face_type` = %u WHERE `id` = %u",
  81. bot->GetModelType(), bot->GetHairType(), bot->GetFacialHairType(), bot->GetWingType(), bot->GetChestType(), bot->GetLegsType(), bot->GetSogaModelType(), bot->GetSogaHairType(), bot->GetSogaFacialHairType(), bot->BotID)) {
  82. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  83. return;
  84. }
  85. }
  86. void WorldDatabase::SaveBotColors(int32 bot_id, const char* type, EQ2_Color color) {
  87. if (!database_new.Query("INSERT INTO `bot_appearance` (`bot_id`, `type`, `red`, `green`, `blue`) VALUES (%i, '%s', %i, %i, %i) ON DUPLICATE KEY UPDATE `red` = %i, `blue` = %i, `green` = %i", bot_id, type, color.red, color.green, color.blue, color.red, color.blue, color.green)) {
  88. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  89. return;
  90. }
  91. }
  92. void WorldDatabase::SaveBotFloats(int32 bot_id, const char* type, float float1, float float2, float float3) {
  93. if (!database_new.Query("INSERT INTO `bot_appearance` (`bot_id`, `type`, `red`, `green`, `blue`, `signed_value`) VALUES (%i, '%s', %i, %i, %i, 1) ON DUPLICATE KEY UPDATE `red` = %i, `blue` = %i, `green` = %i", bot_id, type, float1, float2, float3, float1, float2, float3)) {
  94. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  95. return;
  96. }
  97. }
  98. bool WorldDatabase::LoadBot(int32 char_id, int32 bot_index, Bot* bot) {
  99. DatabaseResult result;
  100. if (!database_new.Select(&result, "SELECT * FROM bots WHERE `char_id` = %u AND `bot_id` = %u", char_id, bot_index)) {
  101. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  102. return false;
  103. }
  104. if (result.Next()) {
  105. bot->BotID = result.GetInt32(0);
  106. bot->BotIndex = result.GetInt32(2);
  107. bot->SetName(result.GetString(3));
  108. bot->SetRace(result.GetInt8(4));
  109. bot->SetAdventureClass(result.GetInt8(5));
  110. bot->SetGender(result.GetInt8(6));
  111. bot->SetModelType(result.GetInt16(7));
  112. bot->SetHairType(result.GetInt16(8));
  113. bot->SetFacialHairType(result.GetInt16(9));
  114. bot->SetWingType(result.GetInt16(10));
  115. bot->SetChestType(result.GetInt16(11));
  116. bot->SetLegsType(result.GetInt16(12));
  117. bot->SetSogaModelType(result.GetInt16(13));
  118. bot->SetSogaHairType(result.GetInt16(14));
  119. bot->SetSogaFacialHairType(result.GetInt16(15));
  120. }
  121. else
  122. return false;
  123. LoadBotAppearance(bot);
  124. LoadBotEquipment(bot);
  125. return true;
  126. }
  127. void WorldDatabase::LoadBotAppearance(Bot* bot) {
  128. DatabaseResult result;
  129. string type;
  130. map<string, int8> appearance_types;
  131. EQ2_Color color;
  132. color.red = 0;
  133. color.green = 0;
  134. color.blue = 0;
  135. if (!database_new.Select(&result, "SELECT distinct `type` FROM bot_appearance WHERE length(`type`) > 0 AND `bot_id` = %u", bot->BotID)) {
  136. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  137. return;
  138. }
  139. while (result.Next()) {
  140. type = result.GetString(0);
  141. appearance_types[type] = GetAppearanceType(type);
  142. if (appearance_types[type] == 255)
  143. LogWrite(WORLD__ERROR, 0, "Appearance", "Unknown appearance type '%s' in LoadBotAppearances.", type.c_str());
  144. }
  145. if (!database_new.Select(&result, "SELECT `type`, `signed_value`, `red`, `green`, `blue` FROM bot_appearance WHERE length(`type`) > 0 AND bot_id = %u", bot->BotID)) {
  146. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  147. return;
  148. }
  149. while (result.Next()) {
  150. type = result.GetString(0);
  151. if (appearance_types[type] < APPEARANCE_SOGA_EBT) {
  152. color.red = result.GetInt8(2);
  153. color.green = result.GetInt8(3);
  154. color.blue = result.GetInt8(4);
  155. }
  156. switch (appearance_types[type]) {
  157. case APPEARANCE_SOGA_HFHC: {
  158. bot->features.soga_hair_face_highlight_color = color;
  159. break;
  160. }
  161. case APPEARANCE_SOGA_HTHC: {
  162. bot->features.soga_hair_type_highlight_color = color;
  163. break;
  164. }
  165. case APPEARANCE_SOGA_HFC: {
  166. bot->features.soga_hair_face_color = color;
  167. break;
  168. }
  169. case APPEARANCE_SOGA_HTC: {
  170. bot->features.soga_hair_type_color = color;
  171. break;
  172. }
  173. case APPEARANCE_SOGA_HH: {
  174. bot->features.soga_hair_highlight_color = color;
  175. break;
  176. }
  177. case APPEARANCE_SOGA_HC1: {
  178. bot->features.soga_hair_color1 = color;
  179. break;
  180. }
  181. case APPEARANCE_SOGA_HC2: {
  182. bot->features.soga_hair_color2 = color;
  183. break;
  184. }
  185. case APPEARANCE_SOGA_SC: {
  186. bot->features.soga_skin_color = color;
  187. break;
  188. }
  189. case APPEARANCE_SOGA_EC: {
  190. bot->features.soga_eye_color = color;
  191. break;
  192. }
  193. case APPEARANCE_HTHC: {
  194. bot->features.hair_type_highlight_color = color;
  195. break;
  196. }
  197. case APPEARANCE_HFHC: {
  198. bot->features.hair_face_highlight_color = color;
  199. break;
  200. }
  201. case APPEARANCE_HTC: {
  202. bot->features.hair_type_color = color;
  203. break;
  204. }
  205. case APPEARANCE_HFC: {
  206. bot->features.hair_face_color = color;
  207. break;
  208. }
  209. case APPEARANCE_HH: {
  210. bot->features.hair_highlight_color = color;
  211. break;
  212. }
  213. case APPEARANCE_HC1: {
  214. bot->features.hair_color1 = color;
  215. break;
  216. }
  217. case APPEARANCE_HC2: {
  218. bot->features.hair_color2 = color;
  219. break;
  220. }
  221. case APPEARANCE_WC1: {
  222. bot->features.wing_color1 = color;
  223. break;
  224. }
  225. case APPEARANCE_WC2: {
  226. bot->features.wing_color2 = color;
  227. break;
  228. }
  229. case APPEARANCE_SC: {
  230. bot->features.skin_color = color;
  231. break;
  232. }
  233. case APPEARANCE_EC: {
  234. bot->features.eye_color = color;
  235. break;
  236. }
  237. case APPEARANCE_SOGA_EBT: {
  238. for (int i = 0; i < 3; i++)
  239. bot->features.soga_eye_brow_type[i] = result.GetSInt8(2 + i);
  240. break;
  241. }
  242. case APPEARANCE_SOGA_CHEEKT: {
  243. for (int i = 0; i < 3; i++)
  244. bot->features.soga_cheek_type[i] = result.GetSInt8(2 + i);
  245. break;
  246. }
  247. case APPEARANCE_SOGA_NT: {
  248. for (int i = 0; i < 3; i++)
  249. bot->features.soga_nose_type[i] = result.GetSInt8(2 + i);
  250. break;
  251. }
  252. case APPEARANCE_SOGA_CHINT: {
  253. for (int i = 0; i < 3; i++)
  254. bot->features.soga_chin_type[i] = result.GetSInt8(2 + i);
  255. break;
  256. }
  257. case APPEARANCE_SOGA_LT: {
  258. for (int i = 0; i < 3; i++)
  259. bot->features.soga_lip_type[i] = result.GetSInt8(2 + i);
  260. break;
  261. }
  262. case APPEARANCE_SOGA_EART: {
  263. for (int i = 0; i < 3; i++)
  264. bot->features.soga_ear_type[i] = result.GetSInt8(2 + i);
  265. break;
  266. }
  267. case APPEARANCE_SOGA_EYET: {
  268. for (int i = 0; i < 3; i++)
  269. bot->features.soga_eye_type[i] = result.GetSInt8(2 + i);
  270. break;
  271. }
  272. case APPEARANCE_EBT: {
  273. for (int i = 0; i < 3; i++)
  274. bot->features.eye_brow_type[i] = result.GetSInt8(2 + i);
  275. break;
  276. }
  277. case APPEARANCE_CHEEKT: {
  278. for (int i = 0; i < 3; i++)
  279. bot->features.cheek_type[i] = result.GetSInt8(2 + i);
  280. break;
  281. }
  282. case APPEARANCE_NT: {
  283. for (int i = 0; i < 3; i++)
  284. bot->features.nose_type[i] = result.GetSInt8(2 + i);
  285. break;
  286. }
  287. case APPEARANCE_CHINT: {
  288. for (int i = 0; i < 3; i++)
  289. bot->features.chin_type[i] = result.GetSInt8(2 + i);
  290. break;
  291. }
  292. case APPEARANCE_EART: {
  293. for (int i = 0; i < 3; i++)
  294. bot->features.ear_type[i] = result.GetSInt8(2 + i);
  295. break;
  296. }
  297. case APPEARANCE_EYET: {
  298. for (int i = 0; i < 3; i++)
  299. bot->features.eye_type[i] = result.GetSInt8(2 + i);
  300. break;
  301. }
  302. case APPEARANCE_LT: {
  303. for (int i = 0; i < 3; i++)
  304. bot->features.lip_type[i] = result.GetSInt8(2 + i);
  305. break;
  306. }
  307. case APPEARANCE_SHIRT: {
  308. bot->features.shirt_color = color;
  309. break;
  310. }
  311. case APPEARANCE_UCC: {
  312. break;
  313. }
  314. case APPEARANCE_PANTS: {
  315. bot->features.pants_color = color;
  316. break;
  317. }
  318. case APPEARANCE_ULC: {
  319. break;
  320. }
  321. case APPEARANCE_U9: {
  322. break;
  323. }
  324. case APPEARANCE_BODY_SIZE: {
  325. bot->features.body_size = color.red;
  326. break;
  327. }
  328. case APPEARANCE_SOGA_WC1: {
  329. break;
  330. }
  331. case APPEARANCE_SOGA_WC2: {
  332. break;
  333. }
  334. case APPEARANCE_SOGA_SHIRT: {
  335. break;
  336. }
  337. case APPEARANCE_SOGA_UCC: {
  338. break;
  339. }
  340. case APPEARANCE_SOGA_PANTS: {
  341. break;
  342. }
  343. case APPEARANCE_SOGA_ULC: {
  344. break;
  345. }
  346. case APPEARANCE_SOGA_U13: {
  347. break;
  348. }
  349. case APPEARANCE_BODY_AGE: {
  350. bot->features.body_age = color.red;
  351. break;
  352. }
  353. case APPEARANCE_MC:{
  354. bot->features.model_color = color;
  355. break;
  356. }
  357. case APPEARANCE_SMC:{
  358. bot->features.soga_model_color = color;
  359. break;
  360. }
  361. }
  362. }
  363. }
  364. void WorldDatabase::SaveBotItem(int32 bot_id, int32 item_id, int8 slot) {
  365. if (!database_new.Query("INSERT INTO `bot_equipment` (`bot_id`, `slot`, `item_id`) VALUES (%u, %u, %u) ON DUPLICATE KEY UPDATE `item_id` = %u", bot_id, slot, item_id, item_id)) {
  366. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  367. return;
  368. }
  369. }
  370. void WorldDatabase::LoadBotEquipment(Bot* bot) {
  371. DatabaseResult result;
  372. if (!database_new.Select(&result, "SELECT `slot`, `item_id` FROM `bot_equipment` WHERE `bot_id` = %u", bot->BotID)) {
  373. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  374. return;
  375. }
  376. Item* master_item = 0;
  377. Item* item = 0;
  378. while (result.Next()) {
  379. int8 slot = result.GetInt8(0);
  380. int32 item_id = result.GetInt32(1);
  381. master_item = master_item_list.GetItem(item_id);
  382. if (master_item) {
  383. item = new Item(master_item);
  384. if (item) {
  385. bot->GetEquipmentList()->AddItem(slot, item);
  386. bot->SetEquipment(item, slot);
  387. }
  388. }
  389. }
  390. }
  391. string WorldDatabase::GetBotList(int32 char_id) {
  392. DatabaseResult result;
  393. string ret;
  394. if (!database_new.Select(&result, "SELECT `bot_id`, `name`, `race`, `class` FROM `bots` WHERE `char_id` = %u", char_id)) {
  395. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  396. return ret;
  397. }
  398. while (result.Next()) {
  399. ret += to_string(result.GetInt32(0)) + ": ";
  400. ret += result.GetString(1);
  401. ret += " the ";
  402. ret += races.GetRaceNameCase(result.GetInt8(2));
  403. ret += " ";
  404. ret += classes.GetClassNameCase(result.GetInt8(3)) + "\n";
  405. }
  406. return ret;
  407. }
  408. void WorldDatabase::DeleteBot(int32 char_id, int32 bot_index) {
  409. if (!database_new.Query("DELETE FROM `bots` WHERE `char_id` = %u AND `bot_id` = %u", char_id, bot_index)) {
  410. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  411. }
  412. }
  413. void WorldDatabase::SetBotStartingItems(Bot* bot, int8 class_id, int8 race_id) {
  414. int32 bot_id = bot->BotID;
  415. LogWrite(PLAYER__DEBUG, 0, "Bot", "Adding default items for race: %u, class: %u for bot_id: %u", race_id, class_id, bot_id);
  416. DatabaseResult result;
  417. if (!database_new.Select(&result, "SELECT item_id FROM starting_items WHERE class_id IN (%i, %i, %i, 255) AND race_id IN (%i, 255) ORDER BY id", classes.GetBaseClass(class_id), classes.GetSecondaryBaseClass(class_id), class_id, race_id)) {
  418. LogWrite(DATABASE__ERROR, 0, "DBNew", "MySQL Error %u: %s", database_new.GetError(), database_new.GetErrorMsg());
  419. return;
  420. }
  421. while (result.Next()) {
  422. bot->GiveItem(result.GetInt32(0));
  423. }
  424. }