int_homun.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Homunculus saving by Albator and Orn for eAthena.
  2. // GNU/GPL rulez !
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "char.h"
  7. #include "../common/strlib.h"
  8. #include "../common/showmsg.h"
  9. struct s_homunculus *homun_pt;
  10. #ifndef SQL_DEBUG
  11. #define mysql_query(_x, _y) mysql_real_query(_x, _y, strlen(_y)) //supports ' in names and runs faster [Kevin]
  12. #else
  13. #define mysql_query(_x, _y) debug_mysql_query(__FILE__, __LINE__, _x, _y)
  14. #endif
  15. int inter_homunculus_sql_init(void){
  16. //memory alloc
  17. homun_pt = (struct s_homunculus*)aCalloc(sizeof(struct s_homunculus), 1);
  18. return 0;
  19. }
  20. void inter_homunculus_sql_final(void){
  21. if (homun_pt) aFree(homun_pt);
  22. return;
  23. }
  24. int mapif_saved_homunculus(int fd, int account_id, short flag)
  25. {
  26. WFIFOW(fd,0) = 0x3892;
  27. WFIFOL(fd,2)=account_id;
  28. WFIFOB(fd,6) = flag;
  29. WFIFOSET(fd, 7);
  30. return 0;
  31. }
  32. int mapif_info_homunculus(int fd, int account_id, struct s_homunculus *hd)
  33. {
  34. WFIFOW(fd,0) = 0x3891;
  35. WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
  36. WFIFOL(fd,4) = account_id;
  37. WFIFOB(fd,8) = 1; // account loaded with success
  38. memcpy(WFIFOP(fd,9), hd, sizeof(struct s_homunculus));
  39. WFIFOSET(fd, sizeof(struct s_homunculus)+9);
  40. return 0;
  41. }
  42. int mapif_homunculus_deleted(int fd, int flag)
  43. {
  44. WFIFOW(fd, 0) = 0x3893;
  45. WFIFOB(fd,2) = flag; //Flag 1 = success
  46. WFIFOSET(fd, 3);
  47. return 0;
  48. }
  49. int mapif_homunculus_created(int fd, int account_id, struct s_homunculus *sh, short flag)
  50. {
  51. WFIFOW(fd, 0) =0x3890;
  52. WFIFOL(fd,2) = account_id;
  53. WFIFOL(fd,6) = sh->char_id;
  54. if(flag==1){
  55. WFIFOW(fd, 10)=1;
  56. WFIFOL(fd,12) = sh->hom_id;
  57. }
  58. else{
  59. WFIFOW(fd, 10)=0;
  60. WFIFOL(fd,12) = 0;
  61. }
  62. WFIFOSET(fd, 16);
  63. return 0;
  64. }
  65. void init_homun_skills(struct s_homunculus *hd)
  66. {
  67. int i;
  68. for(i=0;i<MAX_HOMUNSKILL;i++)
  69. hd->hskill[i].id = hd->hskill[i].lv = hd->hskill[i].flag = 0;
  70. }
  71. // Save/Update Homunculus Skills
  72. int mapif_save_homunculus_skills(struct s_homunculus *hd)
  73. {
  74. int i;
  75. for(i=0; i<MAX_HOMUNSKILL; i++)
  76. {
  77. if(hd->hskill[i].id != 0 && hd->hskill[i].lv != 0 )
  78. {
  79. sprintf(tmp_sql,"REPLACE INTO `skill_homunculus` (`homun_id`, `id`, `lv`) VALUES (%d, %d, %d)",
  80. hd->hom_id, hd->hskill[i].id, hd->hskill[i].lv);
  81. if(mysql_query(&mysql_handle, tmp_sql)){
  82. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  83. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  84. return 0;
  85. }
  86. }
  87. }
  88. return 1;
  89. }
  90. int mapif_save_homunculus(int fd, int account_id, struct s_homunculus *hd)
  91. {
  92. int flag =1;
  93. char t_name[NAME_LENGTH*2];
  94. jstrescapecpy(t_name, hd->name);
  95. if(hd->hom_id==0) // new homunculus
  96. {
  97. ShowInfo("New homunculus name : %s\n",hd->name);
  98. sprintf(tmp_sql, "INSERT INTO `homunculus` "
  99. "(`char_id`, `class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) "
  100. "VALUES ('%d', '%d', '%s', '%d', '%lu', '%lu', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  101. hd->char_id, hd->class_,t_name,hd->level,hd->exp,hd->intimacy,hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
  102. hd->hp,hd->max_hp,hd->sp,hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize);
  103. }
  104. else
  105. {
  106. sprintf(tmp_sql, "UPDATE `homunculus` SET `char_id`='%d', `class`='%d',`name`='%s',`level`='%d',`exp`='%lu',`intimacy`='%lu',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'",
  107. hd->char_id, hd->class_,t_name,hd->level,hd->exp,hd->intimacy,hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk,
  108. hd->hp,hd->max_hp,hd->sp,hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id);
  109. }
  110. if(mysql_query(&mysql_handle, tmp_sql)){
  111. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  112. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  113. flag = 0;
  114. }
  115. if(hd->hom_id==0 && flag!=0)
  116. hd->hom_id = (int)mysql_insert_id(&mysql_handle); // new homunculus
  117. else
  118. {
  119. flag = mapif_save_homunculus_skills(hd);
  120. mapif_saved_homunculus(fd, flag);
  121. }
  122. return flag;
  123. }
  124. // Load an homunculus
  125. int mapif_load_homunculus(int fd){
  126. int i;
  127. memset(homun_pt, 0, sizeof(struct s_homunculus));
  128. sprintf(tmp_sql,"SELECT `homun_id`,`char_id`,`class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`rename_flag`, `vaporize` FROM `homunculus` WHERE `homun_id`='%lu'", RFIFOL(fd,6));
  129. if(mysql_query(&mysql_handle, tmp_sql) ) {
  130. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  131. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  132. return 0;
  133. }
  134. sql_res = mysql_store_result(&mysql_handle) ;
  135. if (sql_res!=NULL && mysql_num_rows(sql_res)>0) {
  136. sql_row = mysql_fetch_row(sql_res);
  137. homun_pt->hom_id = RFIFOL(fd,6) ; //RFIFOL(fd,2);
  138. homun_pt->class_ = atoi(sql_row[2]);
  139. memcpy(homun_pt->name, sql_row[3],NAME_LENGTH-1);
  140. homun_pt->char_id = atoi(sql_row[1]);
  141. homun_pt->level = atoi(sql_row[4]);
  142. homun_pt->exp = atoi(sql_row[5]);
  143. homun_pt->intimacy = atoi(sql_row[6]);
  144. homun_pt->hunger = atoi(sql_row[7]);
  145. homun_pt->str = atoi(sql_row[8]);
  146. homun_pt->agi = atoi(sql_row[9]);
  147. homun_pt->vit = atoi(sql_row[10]);
  148. homun_pt->int_ = atoi(sql_row[11]);
  149. homun_pt->dex = atoi(sql_row[12]);
  150. homun_pt->luk = atoi(sql_row[13]);
  151. homun_pt->hp = atoi(sql_row[14]);
  152. homun_pt->max_hp = atoi(sql_row[15]);
  153. homun_pt->sp = atoi(sql_row[16]);
  154. homun_pt->max_sp = atoi(sql_row[17]);
  155. homun_pt->skillpts = atoi(sql_row[18]);
  156. homun_pt->rename_flag = atoi(sql_row[19]);
  157. homun_pt->vaporize = atoi(sql_row[20]);
  158. }
  159. if(homun_pt->hunger < 0)
  160. homun_pt->hunger = 0;
  161. else if(homun_pt->hunger > 100)
  162. homun_pt->hunger = 100;
  163. if(homun_pt->intimacy < 0)
  164. homun_pt->intimacy = 0;
  165. else if(homun_pt->intimacy > 100000)
  166. homun_pt->intimacy = 100000;
  167. mysql_free_result(sql_res);
  168. // Load Homunculus Skill
  169. init_homun_skills(homun_pt); //bousille homun_pt !!!
  170. sprintf(tmp_sql,"SELECT `id`,`lv` FROM `skill_homunculus` WHERE `homun_id`=%d",homun_pt->hom_id);
  171. if(mysql_query(&mysql_handle, tmp_sql) ) {
  172. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  173. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  174. return 0;
  175. }
  176. sql_res = mysql_store_result(&mysql_handle);
  177. if(sql_res){
  178. while((sql_row = mysql_fetch_row(sql_res))){
  179. i = (atoi(sql_row[0])-HM_SKILLBASE-1);
  180. homun_pt->hskill[i].id = atoi(sql_row[0]);
  181. homun_pt->hskill[i].lv = atoi(sql_row[1]);
  182. }
  183. }
  184. mysql_free_result(sql_res);
  185. ShowInfo("Homunculus loaded (%d - %s).\n", homun_pt->hom_id, homun_pt->name);
  186. return mapif_info_homunculus(fd, RFIFOL(fd,2), homun_pt);
  187. }
  188. int mapif_delete_homunculus(int fd)
  189. {
  190. sprintf(tmp_sql, "DELETE FROM `homunculus` WHERE `homun_id` = '%lu'", RFIFOL(fd,2));
  191. if(mysql_query(&mysql_handle, tmp_sql)){
  192. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  193. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  194. return mapif_homunculus_deleted(fd, 0);
  195. }
  196. return mapif_homunculus_deleted(fd, 1);
  197. }
  198. int mapif_rename_homun_ack(int fd, int account_id, int char_id, int flag, char *name){
  199. WFIFOW(fd, 0) =0x3894;
  200. WFIFOL(fd, 2) =account_id;
  201. WFIFOL(fd, 6) =char_id;
  202. WFIFOB(fd, 10) =flag;
  203. memcpy(WFIFOP(fd, 11), name, NAME_LENGTH);
  204. WFIFOSET(fd, NAME_LENGTH+12);
  205. return 0;
  206. }
  207. int mapif_rename_homun(int fd, int account_id, int char_id, char *name){
  208. int i;
  209. // Check Authorised letters/symbols in the name of the homun
  210. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  211. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  212. if (strchr(char_name_letters, name[i]) == NULL) {
  213. mapif_rename_homun_ack(fd, account_id, char_id, 0, name);
  214. return 0;
  215. }
  216. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  217. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  218. if (strchr(char_name_letters, name[i]) != NULL) {
  219. mapif_rename_homun_ack(fd, account_id, char_id, 0, name);
  220. return 0;
  221. }
  222. }
  223. mapif_rename_homun_ack(fd, account_id, char_id, 1, name);
  224. return 0;
  225. }
  226. int mapif_parse_CreateHomunculus(int fd)
  227. {
  228. memset(homun_pt, 0, sizeof(struct s_homunculus));
  229. /* Data from packet */
  230. homun_pt->char_id = RFIFOL(fd,6);
  231. homun_pt->class_ = RFIFOW(fd,10);
  232. homun_pt->max_hp = RFIFOL(fd,12);
  233. homun_pt->max_sp = RFIFOL(fd,16);
  234. memcpy(homun_pt->name, (char*)RFIFOP(fd, 20), NAME_LENGTH-1);
  235. homun_pt->str = RFIFOL(fd,44);
  236. homun_pt->agi = RFIFOL(fd,48);
  237. homun_pt->vit = RFIFOL(fd,52);
  238. homun_pt->int_ = RFIFOL(fd,56);
  239. homun_pt->dex = RFIFOL(fd,60);
  240. homun_pt->luk = RFIFOL(fd,64);
  241. /* Const data for each creation*/
  242. homun_pt->hom_id = 0;
  243. homun_pt->exp =0;
  244. homun_pt->hp = 10 ;
  245. homun_pt->sp = 0 ;
  246. homun_pt->rename_flag = 0;
  247. homun_pt->skillpts =0;
  248. homun_pt->hunger = 32;
  249. homun_pt->level=1;
  250. homun_pt->intimacy = 21;
  251. // Save in sql db
  252. if(mapif_save_homunculus(fd,RFIFOL(fd,2), homun_pt))
  253. return mapif_homunculus_created(fd, RFIFOL(fd,2), homun_pt, 1); // send homun_id
  254. else
  255. return mapif_homunculus_created(fd, RFIFOL(fd,2), homun_pt, 0); // fail
  256. }
  257. int inter_homunculus_parse_frommap(int fd){
  258. switch(RFIFOW(fd, 0)){
  259. case 0x3090: mapif_parse_CreateHomunculus(fd); break;
  260. case 0x3091: mapif_load_homunculus(fd); break;
  261. case 0x3092: mapif_save_homunculus(fd, RFIFOL(fd,6), (struct s_homunculus*) RFIFOP(fd, 10)); break;
  262. case 0x3093: mapif_delete_homunculus(fd); break; // doesn't need to be parse, very simple packet...
  263. case 0x3094: mapif_rename_homun(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOP(fd, 10)); break;
  264. default:
  265. return 0;
  266. }
  267. return 1;
  268. }