int_homun.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "int_homun.hpp"
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <common/mmo.hpp>
  7. #include <common/showmsg.hpp>
  8. #include <common/socket.hpp>
  9. #include <common/sql.hpp>
  10. #include <common/strlib.hpp>
  11. #include <common/utils.hpp>
  12. #include "char.hpp"
  13. #include "inter.hpp"
  14. int32 inter_homunculus_sql_init(void)
  15. {
  16. return 0;
  17. }
  18. void inter_homunculus_sql_final(void)
  19. {
  20. return;
  21. }
  22. void mapif_homunculus_created(int32 fd, uint32 account_id, struct s_homunculus *sh, unsigned char flag)
  23. {
  24. WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
  25. WFIFOW(fd,0) = 0x3890;
  26. WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
  27. WFIFOL(fd,4) = account_id;
  28. WFIFOB(fd,8)= flag;
  29. memcpy(WFIFOP(fd,9),sh,sizeof(struct s_homunculus));
  30. WFIFOSET(fd, WFIFOW(fd,2));
  31. }
  32. void mapif_homunculus_deleted(int32 fd, int32 flag)
  33. {
  34. WFIFOHEAD(fd, 3);
  35. WFIFOW(fd, 0) = 0x3893;
  36. WFIFOB(fd,2) = flag; //Flag 1 = success
  37. WFIFOSET(fd, 3);
  38. }
  39. void mapif_homunculus_loaded(int32 fd, uint32 account_id, struct s_homunculus *hd)
  40. {
  41. WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
  42. WFIFOW(fd,0) = 0x3891;
  43. WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
  44. WFIFOL(fd,4) = account_id;
  45. if( hd != nullptr )
  46. {
  47. WFIFOB(fd,8) = 1; // success
  48. memcpy(WFIFOP(fd,9), hd, sizeof(struct s_homunculus));
  49. }
  50. else
  51. {
  52. WFIFOB(fd,8) = 0; // not found.
  53. memset(WFIFOP(fd,9), 0, sizeof(struct s_homunculus));
  54. }
  55. WFIFOSET(fd, sizeof(struct s_homunculus)+9);
  56. }
  57. void mapif_homunculus_saved(int32 fd, uint32 account_id, bool flag)
  58. {
  59. WFIFOHEAD(fd, 7);
  60. WFIFOW(fd,0) = 0x3892;
  61. WFIFOL(fd,2) = account_id;
  62. WFIFOB(fd,6) = flag; // 1:success, 0:failure
  63. WFIFOSET(fd, 7);
  64. }
  65. void mapif_homunculus_renamed(int32 fd, uint32 account_id, uint32 char_id, unsigned char flag, char* name)
  66. {
  67. WFIFOHEAD(fd, NAME_LENGTH+12);
  68. WFIFOW(fd, 0) = 0x3894;
  69. WFIFOL(fd, 2) = account_id;
  70. WFIFOL(fd, 6) = char_id;
  71. WFIFOB(fd,10) = flag;
  72. safestrncpy(WFIFOCP(fd,11), name, NAME_LENGTH);
  73. WFIFOSET(fd, NAME_LENGTH+12);
  74. }
  75. bool mapif_homunculus_save(struct s_homunculus* hd)
  76. {
  77. bool flag = true;
  78. char esc_name[NAME_LENGTH*2+1];
  79. Sql_EscapeStringLen(sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH));
  80. if( hd->hom_id == 0 )
  81. {// new homunculus
  82. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` "
  83. "(`char_id`, `class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`, `autofeed`) "
  84. "VALUES ('%d', '%d', '%d', '%s', '%d', '%" PRIu64 "', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%u', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
  85. schema_config.homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
  86. hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->autofeed) )
  87. {
  88. Sql_ShowDebug(sql_handle);
  89. flag = false;
  90. }
  91. else
  92. {
  93. hd->hom_id = (int)Sql_LastInsertId(sql_handle);
  94. }
  95. }
  96. else
  97. {
  98. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%" PRIu64 "',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%u',`max_hp`='%u',`sp`='%u',`max_sp`='%u',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d', `autofeed`='%d' WHERE `homun_id`='%d'",
  99. schema_config.homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
  100. hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->autofeed, hd->hom_id) )
  101. {
  102. Sql_ShowDebug(sql_handle);
  103. flag = false;
  104. }
  105. else
  106. {
  107. SqlStmt stmt{ *sql_handle };
  108. int32 i;
  109. if( SQL_ERROR == stmt.Prepare("REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", schema_config.skill_homunculus_db, hd->hom_id) )
  110. SqlStmt_ShowDebug(stmt);
  111. for( i = 0; i < MAX_HOMUNSKILL; ++i )
  112. {
  113. if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 )
  114. {
  115. stmt.BindParam(0, SQLDT_USHORT, &hd->hskill[i].id, 0);
  116. stmt.BindParam(1, SQLDT_USHORT, &hd->hskill[i].lv, 0);
  117. if( SQL_ERROR == stmt.Execute() ){
  118. SqlStmt_ShowDebug(stmt);
  119. flag = false;
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. return flag;
  127. }
  128. // Load an homunculus
  129. bool mapif_homunculus_load(int32 homun_id, struct s_homunculus* hd)
  130. {
  131. char* data;
  132. size_t len;
  133. memset(hd, 0, sizeof(*hd));
  134. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`rename_flag`, `vaporize`, `autofeed` FROM `%s` WHERE `homun_id`='%u'",
  135. schema_config.homunculus_db, homun_id) )
  136. {
  137. Sql_ShowDebug(sql_handle);
  138. return false;
  139. }
  140. if( !Sql_NumRows(sql_handle) )
  141. { //No homunculus found.
  142. Sql_FreeResult(sql_handle);
  143. return false;
  144. }
  145. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  146. {
  147. Sql_ShowDebug(sql_handle);
  148. Sql_FreeResult(sql_handle);
  149. return false;
  150. }
  151. hd->hom_id = homun_id;
  152. Sql_GetData(sql_handle, 1, &data, nullptr); hd->char_id = atoi(data);
  153. Sql_GetData(sql_handle, 2, &data, nullptr); hd->class_ = atoi(data);
  154. Sql_GetData(sql_handle, 3, &data, nullptr); hd->prev_class = atoi(data);
  155. Sql_GetData(sql_handle, 4, &data, &len); safestrncpy(hd->name, data, sizeof(hd->name));
  156. Sql_GetData(sql_handle, 5, &data, nullptr); hd->level = atoi(data);
  157. Sql_GetData(sql_handle, 6, &data, nullptr); hd->exp = strtoull( data, nullptr, 10 );
  158. Sql_GetData(sql_handle, 7, &data, nullptr); hd->intimacy = (uint32)strtoul(data, nullptr, 10);
  159. Sql_GetData(sql_handle, 8, &data, nullptr); hd->hunger = atoi(data);
  160. Sql_GetData(sql_handle, 9, &data, nullptr); hd->str = atoi(data);
  161. Sql_GetData(sql_handle, 10, &data, nullptr); hd->agi = atoi(data);
  162. Sql_GetData(sql_handle, 11, &data, nullptr); hd->vit = atoi(data);
  163. Sql_GetData(sql_handle, 12, &data, nullptr); hd->int_ = atoi(data);
  164. Sql_GetData(sql_handle, 13, &data, nullptr); hd->dex = atoi(data);
  165. Sql_GetData(sql_handle, 14, &data, nullptr); hd->luk = atoi(data);
  166. Sql_GetData(sql_handle, 15, &data, nullptr); hd->hp = strtoul(data, nullptr, 10);
  167. Sql_GetData(sql_handle, 16, &data, nullptr); hd->max_hp = strtoul(data, nullptr, 10);
  168. Sql_GetData(sql_handle, 17, &data, nullptr); hd->sp = strtoul(data, nullptr, 10);
  169. Sql_GetData(sql_handle, 18, &data, nullptr); hd->max_sp = strtoul(data, nullptr, 10);
  170. Sql_GetData(sql_handle, 19, &data, nullptr); hd->skillpts = atoi(data);
  171. Sql_GetData(sql_handle, 20, &data, nullptr); hd->rename_flag = atoi(data);
  172. Sql_GetData(sql_handle, 21, &data, nullptr); hd->vaporize = atoi(data);
  173. Sql_GetData(sql_handle, 22, &data, nullptr); hd->autofeed = atoi(data) != 0;
  174. Sql_FreeResult(sql_handle);
  175. hd->intimacy = umin(hd->intimacy,100000);
  176. hd->hunger = cap_value(hd->hunger, 0, 100);
  177. // Load Homunculus Skill
  178. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`lv` FROM `%s` WHERE `homun_id`=%d", schema_config.skill_homunculus_db, homun_id) )
  179. {
  180. Sql_ShowDebug(sql_handle);
  181. return false;
  182. }
  183. while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  184. {
  185. int32 i;
  186. // id
  187. Sql_GetData(sql_handle, 0, &data, nullptr);
  188. i = atoi(data);
  189. if( i < HM_SKILLBASE || i >= HM_SKILLBASE + MAX_HOMUNSKILL )
  190. continue;// invalid skill id
  191. i = i - HM_SKILLBASE;
  192. hd->hskill[i].id = (unsigned short)atoi(data);
  193. // lv
  194. Sql_GetData(sql_handle, 1, &data, nullptr);
  195. hd->hskill[i].lv = (unsigned char)atoi(data);
  196. }
  197. Sql_FreeResult(sql_handle);
  198. if( charserv_config.save_log )
  199. ShowInfo("Homunculus loaded (ID: %d - %s / Class: %d / CID: %d).\n", hd->hom_id, hd->name, hd->class_, hd->char_id);
  200. return true;
  201. }
  202. bool mapif_homunculus_delete(int32 homun_id)
  203. {
  204. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `homun_id` = '%u'", schema_config.homunculus_db, homun_id)
  205. || SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `homun_id` = '%u'", schema_config.skill_homunculus_db, homun_id)
  206. ) {
  207. Sql_ShowDebug(sql_handle);
  208. return false;
  209. }
  210. ShowInfo("Homunculus '%d' has been deleted.\n", homun_id);
  211. return true;
  212. }
  213. bool mapif_homunculus_rename(char *name)
  214. {
  215. int32 i;
  216. // Check Authorised letters/symbols in the name of the homun
  217. if( charserv_config.char_config.char_name_option == 1 )
  218. {// only letters/symbols in char_name_letters are authorised
  219. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  220. if( strchr(charserv_config.char_config.char_name_letters, name[i]) == nullptr )
  221. return false;
  222. } else
  223. if( charserv_config.char_config.char_name_option == 2 )
  224. {// letters/symbols in char_name_letters are forbidden
  225. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  226. if( strchr(charserv_config.char_config.char_name_letters, name[i]) != nullptr )
  227. return false;
  228. }
  229. return true;
  230. }
  231. void mapif_parse_homunculus_create(int32 fd, int32 len, uint32 account_id, struct s_homunculus* phd)
  232. {
  233. bool result = mapif_homunculus_save(phd);
  234. mapif_homunculus_created(fd, account_id, phd, result);
  235. }
  236. void mapif_parse_homunculus_delete(int32 fd, int32 homun_id)
  237. {
  238. bool result = mapif_homunculus_delete(homun_id);
  239. mapif_homunculus_deleted(fd, result);
  240. }
  241. void mapif_parse_homunculus_load(int32 fd, uint32 account_id, int32 homun_id)
  242. {
  243. struct s_homunculus hd;
  244. bool result = mapif_homunculus_load(homun_id, &hd);
  245. mapif_homunculus_loaded(fd, account_id, ( result ? &hd : nullptr ));
  246. }
  247. void mapif_parse_homunculus_save(int32 fd, int32 len, uint32 account_id, struct s_homunculus* phd)
  248. {
  249. bool result = mapif_homunculus_save(phd);
  250. mapif_homunculus_saved(fd, account_id, result);
  251. }
  252. void mapif_parse_homunculus_rename(int32 fd, uint32 account_id, uint32 char_id, char* name)
  253. {
  254. bool result = mapif_homunculus_rename(name);
  255. mapif_homunculus_renamed(fd, account_id, char_id, result, name);
  256. }
  257. /*==========================================
  258. * Inter Packets
  259. *------------------------------------------*/
  260. int32 inter_homunculus_parse_frommap(int32 fd)
  261. {
  262. unsigned short cmd = RFIFOW(fd,0);
  263. switch( cmd )
  264. {
  265. case 0x3090: mapif_parse_homunculus_create(fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
  266. case 0x3091: mapif_parse_homunculus_load (fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6)); break;
  267. case 0x3092: mapif_parse_homunculus_save (fd, (int)RFIFOW(fd,2), (int)RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,8)); break;
  268. case 0x3093: mapif_parse_homunculus_delete(fd, (int)RFIFOL(fd,2)); break;
  269. case 0x3094: mapif_parse_homunculus_rename(fd, (int)RFIFOL(fd,2), (int)RFIFOL(fd,6), RFIFOCP(fd,10)); break;
  270. default:
  271. return 0;
  272. }
  273. return 1;
  274. }