summaryrefslogtreecommitdiff
path: root/src/map/stylist.c
blob: 438302214bd48f47873ea4514b09d9a122f61b07 (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
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2018-2019  Hercules Dev Team
*
* Hercules is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#define HERCULES_CORE

#include "map/stylist.h"

#include "common/conf.h"
#include "common/db.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/showmsg.h"

#include "map/clif.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/pc.h"
#include "map/script.h"

static struct stylist_interface stylist_s;
struct stylist_interface *stylist;

static bool stylist_read_db_libconfig(void)
{
	struct config_t stylist_conf;
	struct config_setting_t *stylist_db = NULL, *it = NULL;
	char config_filename[256];
	libconfig->format_db_path("stylist_db.conf", config_filename, sizeof(config_filename));
	int i = 0;

	if (!libconfig->load_file(&stylist_conf, config_filename))
		return false;

	if ((stylist_db = libconfig->setting_get_member(stylist_conf.root, "stylist_db")) == NULL) {
		ShowError("can't read %s\n", config_filename);
		return false;
	}

	stylist->vector_clear();

	while ((it = libconfig->setting_get_elem(stylist_db, i++))) {
		stylist->read_db_libconfig_sub(it, i - 1, config_filename);
	}

	libconfig->destroy(&stylist_conf);
	ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", i, config_filename);
	return true;
}

static bool stylist_read_db_libconfig_sub(struct config_setting_t *it, int idx, const char *source)
{
	struct stylist_data_entry entry = { 0 };
	int i32 = 0, type = 0;
	int64 i64 = 0;

	nullpo_ret(it);
	nullpo_ret(source);

	if (!itemdb->lookup_const(it, "Type", &type) || type >= MAX_STYLIST_TYPE || type < 0) {
		ShowWarning("stylist_read_db_libconfig_sub: Invalid or missing Type (%d) in \"%s\", entry #%d, skipping.\n", type, source, idx);
		return false;
	}
	if (!itemdb->lookup_const(it, "Id", &i32) || i32 < 0) {
		ShowWarning("stylist_read_db_libconfig_sub: Invalid or missing Id (%d) in \"%s\", entry #%d, skipping.\n", i32, source, idx);
		return false;
	}
	entry.id = i32;

	if (libconfig->setting_lookup_int64(it, "Zeny", &i64)) {
		if (i64 > MAX_ZENY) {
			ShowWarning("stylist_read_db_libconfig_sub: zeny is too big in \"%s\", entry #%d, capping to MAX_ZENY.\n", source, idx);
			entry.zeny = MAX_ZENY;
		} else {
			entry.zeny = (int)i64;
		}
	}

	if (itemdb->lookup_const(it, "ItemID", &i32))
		entry.itemid = i32;

	if (itemdb->lookup_const(it, "BoxItemID", &i32))
		entry.boxid = i32;

	if (libconfig->setting_lookup_bool(it, "AllowDoram", &i32))
		entry.allow_doram = (i32 == 0) ? false : true;

	VECTOR_ENSURE(stylist->data[type], 1, 1);
	VECTOR_PUSH(stylist->data[type], entry);
	return true;
}

static bool stylist_validate_requirements(struct map_session_data *sd, int type, int16 idx)
{
	struct item it;
	struct stylist_data_entry *entry;

	nullpo_retr(false, sd);
	Assert_retr(false, type >= 0 && type < MAX_STYLIST_TYPE);
	Assert_retr(false, idx >= 0 && idx < VECTOR_LENGTH(stylist->data[type]));

	entry = &VECTOR_INDEX(stylist->data[type], idx);

	if (sd->status.class == JOB_SUMMONER && (entry->allow_doram == false))
		return false;

	if (entry->id >= 0) {
		if (entry->zeny != 0) {
			if (sd->status.zeny < entry->zeny)
				return false;

			sd->status.zeny -= entry->zeny;
			clif->updatestatus(sd, SP_ZENY);
		} else if (entry->itemid != 0) {
			it.nameid = entry->itemid;
			it.amount = 1;
			return script->buildin_delitem_search(sd, &it, false);
		} else if (entry->boxid != 0) {
			it.nameid = entry->boxid;
			it.amount = 1;
			return script->buildin_delitem_search(sd, &it, false);
		}
		return true;
	}
	return false;
}

static void stylist_send_rodexitem(struct map_session_data *sd, int itemid)
{
	struct rodex_message msg = { 0 };

	nullpo_retv(sd);

	msg.receiver_id = sd->status.char_id;
	msg.items[0].item.nameid = itemid;
	msg.items[0].item.amount = 1;
	msg.items[0].item.identify = 1;
	msg.type = MAIL_TYPE_NPC | MAIL_TYPE_ITEM;

	safestrncpy(msg.sender_name, msg_txt(366), NAME_LENGTH);
	safestrncpy(msg.title, msg_txt(367), RODEX_TITLE_LENGTH);
	safestrncpy(msg.body, msg_txt(368), MAIL_BODY_LENGTH);
	msg.send_date = (int)time(NULL);
	msg.expire_date = (int)time(NULL) + RODEX_EXPIRE;

	intif->rodex_sendmail(&msg);
}

static void stylist_request_style_change(struct map_session_data *sd, int type, int16 idx, bool isitem)
{
	struct stylist_data_entry *entry;

	nullpo_retv(sd);
	Assert_retv(idx > 0);
	Assert_retv(type >= 0 && type < MAX_STYLIST_TYPE);

	if ((idx - 1) < VECTOR_LENGTH(stylist->data[type])) {
		entry = &VECTOR_INDEX(stylist->data[type], idx - 1);
		if (stylist->validate_requirements(sd, type, idx - 1)) {
			if (isitem == false)
				pc->changelook(sd, type, entry->id);
			else
				stylist->send_rodexitem(sd, entry->id);
		}
	}
}

static void stylist_vector_init(void)
{
	for (int i = 0; i < MAX_STYLIST_TYPE; i++)
		VECTOR_INIT(stylist->data[i]);
}
static void stylist_vector_clear(void)
{
	for (int i = 0; i < MAX_STYLIST_TYPE; i++)
		VECTOR_CLEAR(stylist->data[i]);
}

static void do_init_stylist(bool minimal)
{
	if (minimal)
		return;

	// Initialize the db
	stylist->vector_init();

	// Load the db
	stylist->read_db_libconfig();
}

static void do_final_stylist(void)
{
	// Clear the db
	stylist->vector_clear();
}

void stylist_defaults(void)
{
	stylist = &stylist_s;

	/* core */
	stylist->init = do_init_stylist;
	stylist->final = do_final_stylist;
	/* */
	stylist->vector_init = stylist_vector_init;
	stylist->vector_clear = stylist_vector_clear;
	/* database */
	stylist->read_db_libconfig = stylist_read_db_libconfig;
	stylist->read_db_libconfig_sub = stylist_read_db_libconfig_sub;
	/* */
	stylist->request_style_change = stylist_request_style_change;
	stylist->validate_requirements = stylist_validate_requirements;
	stylist->send_rodexitem = stylist_send_rodexitem;
}