summaryrefslogtreecommitdiff
path: root/src/char/int_homun.c
blob: a07378c2a1d4d3cbb1600c53bb84cc4d38f50dc1 (plain) (blame)
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
// 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/mmo.h"
#include "../common/socket.h"
#include "../common/db.h"
#include "../common/lock.h"
#include "../common/showmsg.h"
#include "char.h"
#include "inter.h"
#include "int_homun.h"

char homun_txt[1024]="save/homun.txt";

static struct dbt *homun_db;
static int homun_newid = 100;

int inter_homun_tostr(char *str,struct s_homunculus *p)
{
	int i;

	str+=sprintf(str,"%d,%d\t%s\t%d,%d,%d,%d,%d,"
		"%u,%d,%d,%d,"
		"%u,%d,%d,"
		"%d,%d,%d,%d,%d,%d\t",
		p->hom_id, p->class_, p->name,
		p->char_id, p->hp, p->max_hp, p->sp, p->max_sp,
	  	p->intimacy, p->hunger, p->skillpts, p->level,
		p->exp, p->rename_flag, p->vaporize,
		p->str, p->agi, p->vit, p->int_, p->dex, p->luk);

	for (i = 0; i < MAX_HOMUNSKILL; i++)
	{
		if (p->hskill[i].id && !p->hskill[i].flag)
			str+=sprintf(str,"%d,%d,", p->hskill[i].id, p->hskill[i].lv);
	}

	return 0;
}

int inter_homun_fromstr(char *str,struct s_homunculus *p)
{
	int i, next, len;
	int tmp_int[25];
	unsigned int tmp_uint[5];
	char tmp_str[256];

	memset(p,0,sizeof(struct s_homunculus));

	i=sscanf(str,"%d,%d\t%127[^\t]\t%d,%d,%d,%d,%d,"
		"%u,%d,%d,%d,"
		"%u,%d,%d,"
		"%d,%d,%d,%d,%d,%d\t%n",
		&tmp_int[0],&tmp_int[1],tmp_str,
		&tmp_int[2],&tmp_int[3],&tmp_int[4],&tmp_int[5],&tmp_int[6],
		&tmp_uint[0],&tmp_int[7],&tmp_int[8],&tmp_int[9],
		&tmp_uint[1],&tmp_int[10],&tmp_int[11],
		&tmp_int[12],&tmp_int[13],&tmp_int[14],&tmp_int[15],&tmp_int[16],&tmp_int[17],
		&next);

	if(i!=21)
		return 1;

	p->hom_id = tmp_int[0];
	p->class_ = tmp_int[1];
	memcpy(p->name, tmp_str, NAME_LENGTH-1);

	p->char_id = tmp_int[2];
  	p->hp = tmp_int[3];
	p->max_hp = tmp_int[4];
	p->sp = tmp_int[5];
	p->max_sp = tmp_int[6];

	p->intimacy = tmp_uint[0];
	p->hunger = tmp_int[7];
	p->skillpts = tmp_int[8];
	p->level = tmp_int[9];

	p->exp = tmp_uint[1];
	p->rename_flag = tmp_int[10];
	p->vaporize = tmp_int[11];

	p->str = tmp_int[12];
	p->agi = tmp_int[13];
	p->vit = tmp_int[14];
	p->int_= tmp_int[15];
	p->dex = tmp_int[16];
	p->luk = tmp_int[17];

	//Read skills.
	while(str[next] && str[next] != '\n' && str[next] != '\r') {
		if (sscanf(str+next, "%d,%d,%n", &tmp_int[0], &tmp_int[1], &len) != 2)
			return 2;

		if (tmp_int[0] > HM_SKILLBASE && tmp_int[0] <= HM_SKILLBASE+MAX_HOMUNSKILL)
		{
			i = tmp_int[0] - HM_SKILLBASE -1;
			p->hskill[i].id = tmp_int[0];
			p->hskill[i].lv = tmp_int[1];
		} else
			ShowError("Read Homun: Unsupported Skill ID %d for homunculus (Homun ID=%d\n", tmp_int[0], p->hom_id);
		next += len;
		if (str[next] == ' ')
			next++;
	}
	return 0;
}

int inter_homun_init()
{
	char line[8192];
	struct s_homunculus *p;
	FILE *fp;
	int c=0;

	homun_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));

	if( (fp=fopen(homun_txt,"r"))==NULL )
		return 1;
	while(fgets(line,sizeof(line),fp)){
		p = (struct s_homunculus*)aCalloc(sizeof(struct s_homunculus), 1);
		if(p==NULL){
			ShowFatalError("int_homun: out of memory!\n");
			exit(0);
		}
		if(inter_homun_fromstr(line,p)==0 && p->hom_id>0){
			if( p->hom_id >= homun_newid)
				homun_newid=p->hom_id+1;
			idb_put(homun_db,p->hom_id,p);
		}else{
			ShowError("int_homun: broken data [%s] line %d\n",homun_txt,c);
			aFree(p);
		}
		c++;
	}
	fclose(fp);
//	printf("int_homun: %s read done (%d homuns)\n",homun_txt,c);
	return 0;
}

void inter_homun_final()
{
	homun_db->destroy(homun_db, NULL);
	return;
}

int inter_homun_save_sub(DBKey key,void *data,va_list ap)
{
	char line[8192];
	FILE *fp;
	inter_homun_tostr(line,(struct s_homunculus *)data);
	fp=va_arg(ap,FILE *);
	fprintf(fp,"%s" RETCODE,line);
	return 0;
}

int inter_homun_save()
{
	FILE *fp;
	int lock;
	if( (fp=lock_fopen(homun_txt,&lock))==NULL ){
		ShowError("int_homun: cant write [%s] !!! data is lost !!!\n",homun_txt);
		return 1;
	}
	homun_db->foreach(homun_db,inter_homun_save_sub,fp);
	lock_fclose(fp,homun_txt,&lock);
//	printf("int_homun: %s saved.\n",homun_txt);
	return 0;
}

int inter_homun_delete(int hom_id)
{
	struct s_homunculus *p;
	p = idb_get(homun_db,hom_id);
	if( p == NULL)
		return 0;
	idb_remove(homun_db,hom_id);
	ShowInfo("Deleted homun (hom_id: %d)\n",hom_id);
	return 1;
}

int mapif_homun_created(int fd,int account_id, struct s_homunculus *p)
{
	WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
	WFIFOW(fd, 0) =0x3890;
	WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
	WFIFOL(fd,4) = account_id;
	WFIFOB(fd,8)= p->hom_id?1:0;
	memcpy(WFIFOP(fd,9), p, sizeof(struct s_homunculus));
	WFIFOSET(fd, WFIFOW(fd,2));
	return 0;
}

int mapif_homun_info(int fd,int account_id,struct s_homunculus *p)
{
	WFIFOHEAD(fd, sizeof(struct s_homunculus)+9);
	WFIFOW(fd,0) = 0x3891;
	WFIFOW(fd,2) = sizeof(struct s_homunculus)+9;
	WFIFOL(fd,4) = account_id;
	WFIFOB(fd,8) = 1; // account loaded with success

	memcpy(WFIFOP(fd,9), p, sizeof(struct s_homunculus));
	WFIFOSET(fd,WFIFOW(fd,2));
	return 0;
}

int mapif_homun_noinfo(int fd,int account_id)
{
	WFIFOHEAD(fd,sizeof(struct s_homunculus) + 9);
	WFIFOW(fd,0)=0x3891;
	WFIFOW(fd,2)=sizeof(struct s_homunculus) + 9;
	WFIFOL(fd,4)=account_id;
	WFIFOB(fd,8)=0;
	memset(WFIFOP(fd,9),0,sizeof(struct s_homunculus));
	WFIFOSET(fd,WFIFOW(fd,2));

	return 0;
}

int mapif_save_homun_ack(int fd,int account_id,int flag)
{
	WFIFOHEAD(fd, 7);
	WFIFOW(fd,0)=0x3892;
	WFIFOL(fd,2)=account_id;
	WFIFOB(fd,6)=flag;
	WFIFOSET(fd,7);
	return 0;
}

int mapif_delete_homun_ack(int fd,int flag)
{
	WFIFOHEAD(fd, 3);
	WFIFOW(fd,0)=0x3893;
	WFIFOB(fd,2)=flag;
	WFIFOSET(fd,3);
	return 0;
}

int mapif_rename_homun_ack(int fd, int account_id, int char_id, int flag, char *name){
	WFIFOHEAD(fd, NAME_LENGTH+12);
	WFIFOW(fd, 0) =0x3894;
	WFIFOL(fd, 2) =account_id;
	WFIFOL(fd, 6) =char_id;
	WFIFOB(fd, 10) =flag;
	memcpy(WFIFOP(fd, 11), name, NAME_LENGTH);
	WFIFOSET(fd, NAME_LENGTH+12);

	return 0;
}

int mapif_create_homun(int fd)
{
	struct s_homunculus *p;
	RFIFOHEAD(fd);
	p= (struct s_homunculus *) aCalloc(sizeof(struct s_homunculus), 1);
	if(p==NULL){
		ShowFatalError("int_homun: out of memory !\n");
		//Sending the received data will pass hom_id == 0 <- fail.
		mapif_homun_created(fd,RFIFOL(fd,4),(struct s_homunculus*)RFIFOP(fd,8));
		return 0;
	}
	memcpy(p, RFIFOP(fd,8), sizeof(struct s_homunculus));
	p->hom_id = homun_newid++; //New ID
	idb_put(homun_db,p->hom_id,p);
	mapif_homun_created(fd,RFIFOL(fd,4),p);
	return 0;
}

int mapif_load_homun(int fd)
{
	struct s_homunculus *p;
	int account_id;
	RFIFOHEAD(fd);
	account_id = RFIFOL(fd,2);

	p= idb_get(homun_db,RFIFOL(fd,6));
	if(p==NULL) {
		mapif_homun_noinfo(fd,account_id);
		return 0;
	}
	mapif_homun_info(fd,account_id,p);
	return 0;
}

static void* create_homun(DBKey key, va_list args) {
	struct s_homunculus *p;
	p=(struct s_homunculus *)aCalloc(sizeof(struct s_homunculus),1);
	p->hom_id = key.i;
	return p;
}
int mapif_save_homun(int fd,int account_id,struct s_homunculus *data)
{
	struct s_homunculus *p;
	int hom_id;
	
	if (data->hom_id == 0)
		data->hom_id = homun_newid++;
	hom_id = data->hom_id;
	p= idb_ensure(homun_db,hom_id,create_homun);
	memcpy(p,data,sizeof(struct s_homunculus));
	mapif_save_homun_ack(fd,account_id,1);
	return 0;
}

int mapif_delete_homun(int fd,int hom_id)
{
	mapif_delete_homun_ack(fd,inter_homun_delete(hom_id));
	return 0;
}

int mapif_rename_homun(int fd, int account_id, int char_id, char *name){
	int i;

	// Check Authorised letters/symbols in the name of the homun
	if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
		for (i = 0; i < NAME_LENGTH && name[i]; i++)
		if (strchr(char_name_letters, name[i]) == NULL) {
			mapif_rename_homun_ack(fd, account_id, char_id, 0, name);
			return 0;
		}
	} else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
		for (i = 0; i < NAME_LENGTH && name[i]; i++)
		if (strchr(char_name_letters, name[i]) != NULL) {
			mapif_rename_homun_ack(fd, account_id, char_id, 0, name);
			return 0;
		}
	}

	mapif_rename_homun_ack(fd, account_id, char_id, 1, name);
	return 0;
}

int mapif_parse_SaveHomun(int fd)
{
	RFIFOHEAD(fd);
	mapif_save_homun(fd,RFIFOL(fd,4),(struct s_homunculus *)RFIFOP(fd,8));
	return 0;
}

int mapif_parse_DeleteHomun(int fd)
{
	RFIFOHEAD(fd);
	mapif_delete_homun(fd,RFIFOL(fd,2));
	return 0;
}

int mapif_parse_RenameHomun(int fd){
	RFIFOHEAD(fd);
	mapif_rename_homun(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOP(fd, 10));
	return 0;
}

int inter_homun_parse_frommap(int fd)
{
	RFIFOHEAD(fd);
	switch(RFIFOW(fd,0)){
	case 0x3090: mapif_create_homun(fd); break;
	case 0x3091: mapif_load_homun(fd); break;
	case 0x3092: mapif_parse_SaveHomun(fd); break;
	case 0x3093: mapif_parse_DeleteHomun(fd); break;
	case 0x3094: mapif_parse_RenameHomun(fd); break;
	default:
		return 0;
	}
	return 1;
}