int_homun.c 8.9 KB

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