summaryrefslogblamecommitdiff
path: root/src/map/charsave.c
blob: c335064c5c910d0444a14018fa38eebbc04c662c (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488







































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                           
 
                                                                                                                                    


                                    
                                                  
                                  
                                                           


                                                                                                            
                                                                                              
                                                                                                                                                     




                         
                                                          








                                                                                                              
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../common/core.h"
#include "../common/socket.h"
#include "../common/timer.h"
#include "../common/nullpo.h"
#include "../common/mmo.h"
#include "../common/strlib.h"
#include "../common/showmsg.h"
#include "../common/malloc.h"

#include "charsave.h"
#include "map.h"

#ifndef TXT_ONLY

struct mmo_charstatus *charsave_loadchar(int charid){
	int i,j, friends;
	struct mmo_charstatus *c;
	char *str_p;
	friends = 0;

         c = (struct mmo_charstatus *)aMalloc(sizeof(struct mmo_charstatus));

         if(charid <= 0){
         	ShowError("charsave_loadchar() charid <= 0! (%d)", charid);
				aFree(c);
         	return NULL;
         }

	//Tested, Mysql 4.1.9+ has no problems with the long query, the buf is 65k big and the sql server needs for it 0.00009 secs on an athlon xp 2400+ WinXP (1GB Mem) ..  [Sirius]
         sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, `str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, `option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`hair`,`hair_color`, `clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, `last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`, `partner_id`, `father`, `mother`, `child`, `fame` FROM `char` WHERE `char_id` = '%d'", charid);
    	if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				aFree(c);
         	return NULL;
         }

         charsql_res = mysql_store_result(&charsql_handle);
         if(mysql_num_rows(charsql_res) <= 0){
         	ShowWarning("charsave_loadchar() -> CHARACTER NOT FOUND! (id: %d)\n", charid);
         	mysql_free_result(charsql_res);
				aFree(c);
         	return NULL;
         }

         //fetch data
         charsql_row = mysql_fetch_row(charsql_res);

         //fill with data
         c->char_id = charid;
         c->account_id = atoi(charsql_row[1]);
         c->char_num = atoi(charsql_row[2]);
         strcpy(c->name, charsql_row[3]);
         c->class_ = atoi(charsql_row[4]);
         c->base_level = atoi(charsql_row[5]);
         c->job_level = atoi(charsql_row[6]);
         c->base_exp = atoi(charsql_row[7]);
         c->job_exp = atoi(charsql_row[8]);
         c->zeny = atoi(charsql_row[9]);
         c->str = atoi(charsql_row[10]);
         c->agi = atoi(charsql_row[11]);
         c->vit = atoi(charsql_row[12]);
         c->int_ = atoi(charsql_row[13]);
         c->dex = atoi(charsql_row[14]);
         c->luk = atoi(charsql_row[15]);
         c->max_hp = atoi(charsql_row[16]);
         c->hp = atoi(charsql_row[17]);
         c->max_sp = atoi(charsql_row[18]);
         c->sp = atoi(charsql_row[19]);
         c->status_point = atoi(charsql_row[20]);
         c->skill_point = atoi(charsql_row[21]);
         c->option = atoi(charsql_row[22]);
         c->karma = atoi(charsql_row[23]);
         c->manner = atoi(charsql_row[24]);
         c->party_id = atoi(charsql_row[25]);
         c->guild_id = atoi(charsql_row[26]);
         c->pet_id = atoi(charsql_row[27]);
         c->hair = atoi(charsql_row[28]);
         c->hair_color = atoi(charsql_row[29]);
         c->clothes_color = atoi(charsql_row[30]);
         c->weapon = atoi(charsql_row[31]);
         c->shield = atoi(charsql_row[32]);
         c->head_top = atoi(charsql_row[33]);
         c->head_mid = atoi(charsql_row[34]);
         c->head_bottom = atoi(charsql_row[35]);
			c->last_point.map = mapindex_name2id(charsql_row[36]);
         c->last_point.x = atoi(charsql_row[37]);
         c->last_point.y = atoi(charsql_row[38]);
    		c->save_point.map = mapindex_name2id(charsql_row[39]);
         c->save_point.x = atoi(charsql_row[40]);
         c->save_point.y = atoi(charsql_row[41]);
         c->partner_id = atoi(charsql_row[42]);
         c->father = atoi(charsql_row[43]);
         c->mother = atoi(charsql_row[44]);
         c->child = atoi(charsql_row[45]);
         c->fame = atoi(charsql_row[46]);

        	mysql_free_result(charsql_res);

	//Check for '0' Savepoint / LastPoint
	if (c->last_point.x == 0 || c->last_point.y == 0 || c->last_point.map == 0){
		c->last_point.map = mapindex_name2id(MAP_PRONTERA);
		c->last_point.x = 100;
		c->last_point.y = 100;
	}

	if (c->save_point.x == 0 || c->save_point.y == 0 || c->save_point.map == 0){
		c->save_point.map = mapindex_name2id(MAP_PRONTERA);
		c->save_point.x = 100;
		c->save_point.y = 100;
	}


	//read the memo points
         sprintf(tmp_sql, "SELECT `memo_id`, `char_id`, `map`, `x`, `y` FROM `memo` WHERE `char_id` = '%d' ORDER BY `memo_id`", charid);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				aFree(c);
				return NULL;
         }

         charsql_res = mysql_store_result(&charsql_handle);
         if(charsql_res){
	         for(i = 0; (charsql_row = mysql_fetch_row(charsql_res)); i++){
	                 c->memo_point[i].map = mapindex_name2id(charsql_row[2]);
	                 c->memo_point[i].x = atoi(charsql_row[3]);
	                 c->memo_point[i].y = atoi(charsql_row[4]);
	         }
		mysql_free_result(charsql_res);
         }

         //read inventory...
			str_p = tmp_sql;
         str_p += sprintf(str_p, "SELECT `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
			for (i = 0; i < MAX_SLOTS; i++)
				str_p += sprintf(str_p, ", `card%d`", i);
			str_p += sprintf(str_p, " FROM `inventory` WHERE `char_id` = '%d'", charid);
			if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				aFree(c);
				return NULL;
         }

         charsql_res = mysql_store_result(&charsql_handle);
	if(charsql_res){
	         for(i = 0; (charsql_row = mysql_fetch_row(charsql_res)); i++){
	                 //c->inventory[i].id = atoi(charsql_row[0]);
	                 c->inventory[i].nameid = atoi(charsql_row[0]);
	                 c->inventory[i].amount = atoi(charsql_row[1]);
	                 c->inventory[i].equip = atoi(charsql_row[2]);
	                 c->inventory[i].identify = atoi(charsql_row[3]);
	                 c->inventory[i].refine = atoi(charsql_row[4]);
	                 c->inventory[i].attribute = atoi(charsql_row[5]);
						  for (j = 0; j < MAX_SLOTS; j++)
							c->inventory[i].card[j] = atoi(charsql_row[6+j]);
	         }
	         mysql_free_result(charsql_res);
         }


         //cart inventory ..
			str_p = tmp_sql;
         str_p += sprintf(str_p, "SELECT `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
			for (i = 0; i < MAX_SLOTS; i++)
				str_p += sprintf(str_p, ", `card%d`", i);
			str_p += sprintf(str_p, " FROM `cart_inventory` WHERE `char_id` = '%d'", charid);

         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				aFree(c);
				return NULL;
         }

         charsql_res = mysql_store_result(&charsql_handle);
	if(charsql_res){
	         for(i = 0; (charsql_row = mysql_fetch_row(charsql_res)); i++){
	                 //c->cart[i].id = atoi(charsql_row[0]);
	                 c->cart[i].nameid = atoi(charsql_row[0]);
	                 c->cart[i].amount = atoi(charsql_row[1]);
	                 c->cart[i].equip = atoi(charsql_row[2]);
	                 c->cart[i].identify = atoi(charsql_row[3]);
	                 c->cart[i].refine = atoi(charsql_row[4]);
	                 c->cart[i].attribute = atoi(charsql_row[5]);
						  for (j = 0; j < MAX_SLOTS; j++)
							c->cart[i].card[j] = atoi(charsql_row[6+j]);
	         }
	         mysql_free_result(charsql_res);
         }


         //Skills...
         sprintf(tmp_sql, "SELECT `char_id`, `id`, `lv` FROM `skill` WHERE `char_id` = '%d'", charid);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				aFree(c);
				return NULL;
         }

         charsql_res = mysql_store_result(&charsql_handle);
         if(charsql_res){
         	while((charsql_row = mysql_fetch_row(charsql_res))){
                         i = atoi(charsql_row[1]);
                         c->skill[i].id = i;
                         c->skill[i].lv = atoi(charsql_row[2]);
                 }
                 mysql_free_result(charsql_res);
         }
/* Reg values are handled by the char server.
         //Global REG
         sprintf(tmp_sql, "SELECT `char_id`, `str`, `value` FROM `global_reg_value` WHERE `type` = '3' AND `char_id` = '%d'", charid);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				aFree(c);
				return NULL;
         }

         charsql_res = mysql_store_result(&charsql_handle);
         if(charsql_res){
         	for(i = 0; (charsql_row = mysql_fetch_row(charsql_res)); i++){
                 	strcpy(c->global_reg[i].str, charsql_row[1]);
                 	strcpy(c->global_reg[i].value, charsql_row[2]);
                 }
                 mysql_free_result(charsql_res);
				c->global_reg_num = i;
         }
*/
	//Shamelessly stolen from its_sparky (ie: thanks) and then assimilated by [Skotlex]
	//Friend list 
	sprintf(tmp_sql, "SELECT f.friend_account, f.friend_id, c.name FROM friends f LEFT JOIN `char` c ON f.friend_account=c.account_id AND f.friend_id=c.char_id WHERE f.char_id='%d'", charid);

	if(mysql_query(&charsql_handle, tmp_sql)){
		ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
		ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		sql_res = NULL; //To avoid trying to read data.
	}
	else
		sql_res = mysql_store_result(&charsql_handle);
	
	if(sql_res)
	{
		for(i = 0; (sql_row = mysql_fetch_row(sql_res)) && i<MAX_FRIENDS; i++)
		{
			if (sql_row[2] != NULL)
			{
				c->friends[i].account_id = atoi(sql_row[0]);
				c->friends[i].char_id = atoi(sql_row[1]);
				strncpy(c->friends[i].name, sql_row[2], NAME_LENGTH-1); //The -1 is to avoid losing the ending \0 [Skotlex]
			}
		}
		mysql_free_result(sql_res);
	}

	ShowInfo("charsql_loadchar(): loading of '%d' (%s) complete.\n", charid, c->name);
	return c;
}

int charsave_savechar(int charid, struct mmo_charstatus *c){
	int i,j;
	char *str_p;
//	char tmp_str[64];
//	char tmp_str2[512];
         //First save the 'char'
	sprintf(tmp_sql ,"UPDATE `char` SET `class`='%d', `base_level`='%d', `job_level`='%d',"
		"`base_exp`='%d', `job_exp`='%d', `zeny`='%d',"
		"`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
		"`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
		"`option`='%d',`karma`='%d',`manner`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',"
		"`hair`='%d',`hair_color`='%d',`clothes_color`='%d',`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
		"`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d',"
		"`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d', `fame`='%d'"
		"WHERE  `account_id`='%d' AND `char_id` = '%d'",
		c->class_, c->base_level, c->job_level,
		c->base_exp, c->job_exp, c->zeny,
		c->max_hp, c->hp, c->max_sp, c->sp, c->status_point, c->skill_point,
		c->str, c->agi, c->vit, c->int_, c->dex, c->luk,
		c->option, c->karma, c->manner, c->party_id, c->guild_id, c->pet_id,
		c->hair, c->hair_color, c->clothes_color,
		c->weapon, c->shield, c->head_top, c->head_mid, c->head_bottom,
		mapindex_id2name(c->last_point.map), c->last_point.x, c->last_point.y,
		mapindex_id2name(c->save_point.map), c->save_point.x, c->save_point.y, c->partner_id, c->father, c->mother,
		c->child, c->fame, c->account_id, c->char_id
	);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
         }


		//Save the inventory
		sprintf(tmp_sql, "DELETE FROM `inventory` WHERE `char_id` = '%d'", charid);
		if(mysql_query(&charsql_handle, tmp_sql)){
			ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
			ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		}
		for(i = 0; i < MAX_INVENTORY; i++){
			if(c->inventory[i].nameid > 0){
				str_p = tmp_sql;
				str_p += sprintf(str_p, "INSERT INTO `inventory` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
			for (j = 0; j < MAX_SLOTS; j++)
				str_p += sprintf(str_p, ", `card%d`", j);
				
			str_p += sprintf(str_p, ") VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d'",
				charid, c->inventory[i].nameid, c->inventory[i].amount, c->inventory[i].equip,
				c->inventory[i].identify, c->inventory[i].refine, c->inventory[i].attribute);

			for (j = 0; j < MAX_SLOTS; j++)
				str_p += sprintf(str_p, ", '%d'", c->inventory[i].card[j]);
			
			strcat(tmp_sql,")");
			
			if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
			}
		}
	}

		//Save the cart
		sprintf(tmp_sql, "DELETE FROM `cart_inventory` WHERE `char_id` = '%d'", charid);
		if(mysql_query(&charsql_handle, tmp_sql)){
			ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
			ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		}
		for(i = 0; i < MAX_CART; i++){
			if(c->cart[i].nameid > 0){
				str_p = tmp_sql;
				str_p += sprintf(str_p, "INSERT INTO `cart_inventory` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
				for (j = 0; j < MAX_SLOTS; j++)
					str_p += sprintf(str_p, ", `card%d`", j);
				
				str_p += sprintf(str_p, ") VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d'",
					charid, c->cart[i].nameid, c->cart[i].amount, c->cart[i].equip,
					c->cart[i].identify, c->cart[i].refine, c->cart[i].attribute);

				for (j = 0; j < MAX_SLOTS; j++)
					str_p += sprintf(str_p, ", '%d'", c->cart[i].card[j]);
			
				strcat(tmp_sql,")");
				
				if(mysql_query(&charsql_handle, tmp_sql)){
					ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
					ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
				}
			}
		}


         //Save memo points
         sprintf(tmp_sql, "DELETE FROM `memo` WHERE `char_id` = '%d'", charid);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
         }
         for(i = 0; i < MAX_MEMOPOINTS; i++){
         	if(c->memo_point[i].map && c->memo_point[i].x > 0 && c->memo_point[i].y > 0){
                 	sprintf(tmp_sql, "INSERT INTO `memo` ( `char_id`, `map`, `x`, `y` ) VALUES ('%d', '%s', '%d', '%d')", charid, mapindex_id2name(c->memo_point[i].map), c->memo_point[i].x, c->memo_point[i].y);
	                if(mysql_query(&charsql_handle, tmp_sql)){
							ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
							ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
	                }
                 }
         }


         //Save skills
         sprintf(tmp_sql, "DELETE FROM `skill` WHERE `char_id` = '%d'", charid);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
         }
         for(i = 0; i < MAX_SKILL; i++){
         	if(c->skill[i].id > 0){
                 	sprintf(tmp_sql, "INSERT INTO `skill` (`char_id`, `id`, `lv`) VALUES ('%d', '%d', '%d')", charid, c->skill[i].id, c->skill[i].lv);
	                if(mysql_query(&charsql_handle, tmp_sql)){
							ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
							ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
	                }
                 }
         }


/* Reg values are handled by the char server.
         //global_reg_value saving
         sprintf(tmp_sql, "DELETE FROM `global_reg_value` WHERE `type`=3 AND `char_id` = '%d'", charid);
         if(mysql_query(&charsql_handle, tmp_sql)){
				ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
				ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
         }
         for(i = 0; i < c->global_reg_num; i++){
           if(c->global_reg[i].str){
                 if(c->global_reg[i].value){
                 	//jstrescapecpy(tmp_str, c->global_reg[i].str);
                 	sprintf(tmp_sql, "INSERT INTO `global_reg_value` (`char_id`, `str`, `value`) VALUES ('%d', '%s', '%s')", charid, jstrescapecpy(tmp_str,c->global_reg[i].str), jstrescapecpy(tmp_str2,c->global_reg[i].value));
	                if(mysql_query(&charsql_handle, tmp_sql)){
							ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
							ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
	                }
                 }
           }
         }
*/

         //friendlist saving
         sprintf(tmp_sql, "DELETE FROM `friends` WHERE `char_id` = '%d'", charid);
	if(mysql_query(&charsql_handle, tmp_sql)){
		ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
		ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
	}
         for(i = 0; i < MAX_FRIENDS; i++){
         	if(c->friends[i].char_id > 0){
			sprintf(tmp_sql, "INSERT INTO `friends` (`char_id`, `friend_account`, `friend_id`) VALUES ('%d','%d','%d')", charid, c->friends[i].account_id, c->friends[i].char_id);
	                if(mysql_query(&charsql_handle, tmp_sql)){
							ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
							ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
	                }
                 }
    	}

    ShowInfo("charsql_savechar(): saving of '%d' (%s) complete.\n", charid, c->name);
    return 0;
}

int charsave_load_scdata(int account_id, int char_id)
{	//Loads character's sc_data
	struct map_session_data *sd;
	
	sd = map_id2sd(account_id);
	if (!sd)
	{
		ShowError("charsave_load_scdata: Player of AID %d not found!\n", account_id);
		return -1;
	}
	if (sd->status.char_id != char_id)
	{
		ShowError("charsave_load_scdata: Receiving data for account %d, char id does not matches (%d != %d)!\n", account_id, sd->status.char_id, char_id);
		return -1;
	}
	sprintf(tmp_sql, "SELECT `type`, `tick`, `val1`, `val2`, `val3`, `val4` FROM `sc_data`"
		"WHERE `account_id`='%d' AND `char_id`='%d'", account_id, char_id);
	
	if(mysql_query(&charsql_handle, tmp_sql)){
		ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
		ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		return -1;
	}

	sql_res = mysql_store_result(&charsql_handle);
	if(sql_res)
	{
		while ((sql_row = mysql_fetch_row(sql_res)))
		{
			if (atoi(sql_row[1]) < 1)
			{	//Protection against invalid tick values. [Skotlex]
				ShowWarning("charsave_load_scdata: Received invalid duration (%d ms) for status change %d (character %s)\n", atoi(sql_row[1]), sd->status.name);
				continue;
			}

			status_change_start(&sd->bl, atoi(sql_row[0]), atoi(sql_row[2]), atoi(sql_row[3]),
				atoi(sql_row[4]), atoi(sql_row[5]), atoi(sql_row[1]), 7);
		}
	}

	//Once loaded, sc_data must be disposed.
	sprintf(tmp_sql, "DELETE FROM `sc_data` WHERE `account_id`='%d' AND `char_id`='%d'", account_id, char_id);
	if(mysql_query(&charsql_handle, tmp_sql)){
		ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
		ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
	}
	return 0;
}

void charsave_save_scdata(int account_id, int char_id, struct status_change* sc_data, int max_sc)
{	//Saves character's sc_data.
	int i,count =0;
	struct TimerData *timer;
	unsigned int tick = gettick();
	char *p = tmp_sql;

	p += sprintf(p, "INSERT INTO `sc_data` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ");
			
	for(i = 0; i < max_sc; i++)
	{
		if (sc_data->data[i].timer == -1)
			continue;
		timer = get_timer(sc_data->data[i].timer);
		if (timer == NULL || timer->func != status_change_timer || DIFF_TICK(timer->tick,tick) < 0)
			continue;
		
		p += (p, " ('%d','%d','%hu','%d','%d','%d','%d','%d'),", account_id, char_id,
			i, DIFF_TICK(timer->tick,tick), sc_data->data[i].val1, sc_data->data[i].val2, sc_data->data[i].val3, sc_data->data[i].val4);
		
		count++;
	}
	if (count > 0)
	{
		*--p = '\0'; //Remove the trailing comma.
		if(mysql_query(&charsql_handle, tmp_sql)){
			ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
			ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
		}
	}
	ShowInfo("charsql_save_scdata(): saved %d status changes of '%d:%d'.\n", count, account_id, char_id);
	return;
}
#endif