OSDN Git Service

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