summaryrefslogtreecommitdiff
path: root/src/map/quest.c
blob: f146ca325ea8ff928ded31f16ce05a25295ab01c (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
372
373
374
375
376
377
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder

#include "../common/cbasetypes.h"
#include "../common/socket.h"
#include "../common/timer.h"
#include "../common/malloc.h"
#include "../common/version.h"
#include "../common/nullpo.h"
#include "../common/showmsg.h"
#include "../common/strlib.h"
#include "../common/utils.h"

#include "map.h"
#include "chrif.h"
#include "pc.h"
#include "npc.h"
#include "itemdb.h"
#include "script.h"
#include "intif.h"
#include "battle.h"
#include "mob.h"
#include "party.h"
#include "unit.h"
#include "log.h"
#include "clif.h"
#include "quest.h"
#include "intif.h"

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

struct s_quest_db {
	int id;
	unsigned int time;
	int mob[MAX_QUEST_OBJECTIVES];
	int count[MAX_QUEST_OBJECTIVES];
	//char name[NAME_LENGTH];
};
struct s_quest_db quest_db[MAX_QUEST_DB];

//Send quest info on login
int quest_pc_login(TBL_PC * sd)
{
	if(sd->num_quests == 0)
		return 1;

	clif_send_questlog(sd);
	clif_send_questlog_info(sd);

	return 0;
}

int quest_add(TBL_PC * sd, int quest_id)
{

	int i, j, count;

	if( quest_check_quest(sd, quest_id, HAVEQUEST) >= 0 )
	{
		ShowError("quest_add: you already have quest %d.\n",quest_id);
		return -1;
	}

	if( (j = quest_search_db(quest_id)) < 0 )
	{
		ShowError("quest_add: quest %d not found in DB.\n",quest_id);
		return -1;
	}

	if( sd->num_quests >= MAX_QUEST_DB )
	{
		ShowError("quest_add: your quest log is full.(max quests: %d)\n", MAX_QUEST_DB);
		return 1;
	}

	i = sd->avail_quests;
	memmove(&sd->quest_log[i+1], &sd->quest_log[i], sizeof(struct quest)*(sd->num_quests-sd->avail_quests));
	memset(&sd->quest_log[i], 0, sizeof(struct quest));
	sd->quest_log[i].quest_id = quest_db[j].id;
	if( quest_db[j].time )
		sd->quest_log[i].time = (unsigned int)(time(NULL) + quest_db[j].time);
	sd->quest_log[i].state = Q_ACTIVE;
	for( count = 0; count < MAX_QUEST_OBJECTIVES && quest_db[j].mob[count]; count++ )
		sd->quest_log[i].mob[count] = quest_db[j].mob[count];
	sd->quest_log[i].num_objectives = count;

	sd->num_quests++;
	sd->avail_quests++;

	//Notify inter server
	intif_quest_add(sd->status.char_id, &sd->quest_log[i]);

	return 0;
}

int quest_add_ack(int char_id, int quest_id, int success)
{
	int i;
	TBL_PC * sd = map_charid2sd(char_id);

	///Player no longer on map
	if(!sd)
		return -1;
	
	ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
	if( i == sd->avail_quests )
		return -1; //Shouldn't happen!

	if( success )
		clif_send_quest_info(sd, &sd->quest_log[i]); //Notify client
	else
	{
		sd->avail_quests--;
		if( sd->num_quests < MAX_QUEST_DB && sd->quest_log[i+1].quest_id )
			memmove(&sd->quest_log[i], &sd->quest_log[i+1], sizeof(struct quest)*(sd->num_quests-i));
		memset(&sd->quest_log[--sd->num_quests], 0, sizeof(struct quest));
		ShowError("Quest %d for character %d could not be added!\n", quest_id, char_id);
		return 1;
	}

	return 0;
}

int quest_delete(TBL_PC * sd, int quest_id)
{
	//Search for quest
	if( quest_check_quest(sd, quest_id, HAVEQUEST) < 0 ) //Quest not found
	{
		ShowError("quest_delete: quest %d not found in your quest log.\n",quest_id);
		return -1;
	}

	intif_quest_delete(sd->status.char_id, quest_id);

	return 0;
}

int quest_delete_ack(int char_id, int quest_id, int success)
{
	int i;
	TBL_PC * sd = map_charid2sd(char_id);

	///Player no longer on map
	if(!sd)
		return -1;

	//Search for quest
	ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
	if(i == sd->num_quests) //Shouldn't happen!
		return -1;

	if(success)
	{
		if( sd->quest_log[i].state != Q_COMPLETE )
			sd->avail_quests--;
		if( sd->num_quests < MAX_QUEST_DB && sd->quest_log[i+1].quest_id )
			memmove(&sd->quest_log[i], &sd->quest_log[i+1], sizeof(struct quest)*(sd->num_quests-i));
		memset(&sd->quest_log[--sd->num_quests], 0, sizeof(struct quest));
		clif_send_quest_delete(sd, quest_id);
	}
	else
	{
		ShowError("Quest %d for character %d could not be deleted!\n", quest_id, char_id);
		return 1;
	}

	return 0;
}

void quest_update_objective(TBL_PC * sd, int mob)
{
	int i,j;

	for( i = 0; i < sd->avail_quests; i++ )
	{
		if( sd->quest_log[i].state != Q_ACTIVE )
			continue;

		for( j = 0; j < MAX_QUEST_OBJECTIVES; j++ )
			if( sd->quest_log[i].mob[j] == mob )
			{
				sd->quest_log[i].count[j]++;
				sd->save_quest[i] = true;
				//clif_send_quest_info(sd, &sd->quest_log[i]); //TODO: Figure out the real packet [Inkfish]
				//break;
			}
	}
}

int quest_update_status(TBL_PC * sd, int quest_id, int status)
{
	int i;

	//Search for quest
	ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id); // Only status of active and inactive quests can't be updated and completed quests can't [Inkfish]
	if(i == sd->avail_quests) //Quest not found
	{
		ShowError("Quest %d not found in your quest log!\n", quest_id);
		return -1;
	}

	sd->quest_log[i].state = (quest_state)status;
	if( status != Q_COMPLETE )
	{
		clif_send_quest_status(sd, quest_id, (bool)status);
		sd->save_quest[i] = true;
	}
	else
	{
		struct quest tmp_quest;

		clif_send_quest_delete(sd, quest_id);
		sd->avail_quests--;

		memcpy(&tmp_quest, &sd->quest_log[i],sizeof(struct quest));
		memcpy(&sd->quest_log[i], &sd->quest_log[sd->avail_quests],sizeof(struct quest));
		memcpy(&sd->quest_log[sd->avail_quests], &tmp_quest,sizeof(struct quest));

		sd->save_quest[sd->avail_quests] = true;
	}

	return 0;
}

int quest_save(TBL_PC * sd)
{
	int i;

	for( i = 0; i < sd->num_quests; i++ )
		if( sd->save_quest[i] )
			intif_quest_save(sd->status.char_id, &sd->quest_log[i]);

	return 0;
}

int quest_save_ack(int char_id, int quest_id, int success)
{

	int i;
	TBL_PC * sd = map_charid2sd(char_id);

	///Player no longer on map
	if(!sd)
		return -1;

	//Search for quest
	ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
	if(i == sd->num_quests) //Quest not found
		return -1;

	if(success)
		sd->save_quest[i] = false;
	else
	{
		ShowError("Quest %d for character %d could not be saved!\n", quest_id, char_id);
		return 1;
	}

	return 0;

}

int quest_check_quest(TBL_PC * sd, int quest_id, quest_check_type type)
{
	int i;

	ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
	if(i == sd->num_quests)
		return -1;

	switch( type )
	{
	case HAVEQUEST:		
		return sd->quest_log[i].state;
	case PLAYTIME:
		return (sd->quest_log[i].time < (unsigned int)time(NULL) ? 2 : sd->quest_log[i].state == Q_COMPLETE ? 1 : 0);
	case HUNTING:
		{
			int j = quest_search_db(quest_id);

			if( j < 0 )
			{
				ShowError("quest_check_quest: quest not found in DB\n");
				return -1;
			}

			if( sd->quest_log[i].count[0] < quest_db[j].count[0] || sd->quest_log[i].count[1] < quest_db[j].count[1] || sd->quest_log[i].count[2] < quest_db[j].count[2] )
			{
				if( sd->quest_log[i].time < (unsigned int)time(NULL) )
					return 1;

				return 0;
			}
			
			return 2;
		}
	default:
		ShowError("quest_check_quest: Unknown parameter %d",type);
		break;
	}

	return -1;
}

int quest_search_db(int quest_id)
{
	int i;

	ARR_FIND(0, MAX_QUEST_DB,i,quest_id == quest_db[i].id);
	if( i == MAX_QUEST_DB )
		return -1;

	return i;
}

int quest_read_db(void)
{
	FILE *fp;
	char line[1024];
	int j,k = 0;
	char *str[20],*p,*np;

	sprintf(line, "%s/quest_db.txt", db_path);
	if( (fp=fopen(line,"r"))==NULL ){
		ShowError("can't read %s\n", line);
		return -1;
	}
	
	while(fgets(line, sizeof(line), fp))
	{
		if(line[0]=='/' && line[1]=='/')
			continue;
		memset(str,0,sizeof(str));

		for( j = 0, p = line; j < 8;j++ )
		{
			if((np=strchr(p,','))!=NULL)
			{
				str[j] = p;
				*np = 0;
				p = np + 1;
			}
			else
			{
				ShowError("quest_read_db: insufficient columes in line %s\n", line);
				continue;
			}
		}
		if(str[0]==NULL)
			continue;

		memset(&quest_db[k], 0, sizeof(quest_db[0]));

		quest_db[k].id = atoi(str[0]);
		quest_db[k].time = atoi(str[1]);
		quest_db[k].mob[0] = atoi(str[2]);
		quest_db[k].count[0] = atoi(str[3]);
		quest_db[k].mob[1] = atoi(str[4]);
		quest_db[k].count[1] = atoi(str[5]);
		quest_db[k].mob[2] = atoi(str[6]);
		quest_db[k].count[2] = atoi(str[7]);
		//memcpy(quest_db[k].name, str[8], sizeof(str[8]));
		k++;
	}
	fclose(fp);
	ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","quest_db.txt");
	return 0;
}

void do_init_quest(void)
{
	quest_read_db();
}