9
3

BotDB.cpp 16 KB

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