Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Lapine Upgrade interface #3068

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,217 changes: 5,217 additions & 0 deletions db/pre-re/item_lapineupgrade.conf

Large diffs are not rendered by default.

5,217 changes: 5,217 additions & 0 deletions db/re/item_lapineupgrade.conf

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions doc/script_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10929,3 +10929,9 @@ Opens lapine ddukddak user interface for the player
returns true on success and false on failure

---------------------------------------
*openlapineupgradeui(<item_id>)

Opens lapine ddukddak upgrade user interface for the player
returns true on success and false on failure

---------------------------------------
102 changes: 97 additions & 5 deletions src/map/clif.c
Original file line number Diff line number Diff line change
Expand Up @@ -24227,7 +24227,10 @@ static bool clif_lapineDdukDdak_open(struct map_session_data *sd, int item_id)
{
#if PACKETVER_MAIN_NUM >= 20160601 || PACKETVER_RE_NUM >= 20160525 || defined(PACKETVER_ZERO)
nullpo_retr(false, sd);
nullpo_retr(false, itemdb->exists(item_id));

if (itemdb->exists(item_id) == NULL)
return false;

struct PACKET_ZC_LAPINEDDUKDDAK_OPEN p;

p.packetType = HEADER_ZC_LAPINEDDUKDDAK_OPEN;
Expand Down Expand Up @@ -24333,13 +24336,17 @@ static bool clif_lapineUpgrade_open(struct map_session_data *sd, int item_id)
{
#if PACKETVER_MAIN_NUM >= 20170726 || PACKETVER_RE_NUM >= 20170621 || defined(PACKETVER_ZERO)
nullpo_retr(false, sd);
nullpo_retr(false, itemdb->exists(item_id));

if (itemdb->exists(item_id) == NULL)
return false;

struct PACKET_ZC_LAPINEUPGRADE_OPEN p;

p.packetType = HEADER_ZC_LAPINEUPGRADE_OPEN;
p.itemId = item_id;
clif->send(&p, sizeof(p), &sd->bl, SELF);

sd->state.lapine_ui = 1;
return true;
#else
return false;
Expand All @@ -24350,16 +24357,101 @@ static void clif_parse_lapineUpgrade_close(int fd, struct map_session_data *sd)
static void clif_parse_lapineUpgrade_close(int fd, struct map_session_data *sd)
{
#if PACKETVER_MAIN_NUM >= 20170111 || PACKETVER_RE_NUM >= 20170111 || defined(PACKETVER_ZERO)
sd->state.lapine_ui = 0;
#endif // PACKETVER_MAIN_NUM >= 20170111 || PACKETVER_RE_NUM >= 20170111 || defined(PACKETVER_ZERO)
}

static void clif_parse_lapineUpgrade_makeItem(int fd, struct map_session_data *sd) __attribute__((nonnull (2)));
static void clif_parse_lapineUpgrade_makeItem(int fd, struct map_session_data *sd)
{
#if PACKETVER_MAIN_NUM >= 20170111 || PACKETVER_RE_NUM >= 20170111 || defined(PACKETVER_ZERO)
ShowError("Lapin upgrade not implemented yet.");
clif->lapineUpgrade_result(sd, LAPINE_UPGRADE_FAILED);
#endif // PACKETVER_MAIN_NUM >= 20170111 || PACKETVER_RE_NUM >= 20170111 || defined(PACKETVER_ZERO)
const struct PACKET_CZ_LAPINEUPGRADE_MAKE_ITEM *p = RP2PTR(fd);
struct item_data *it = itemdb->exists(p->itemId);

if (it == NULL || it->lapineupgrade == NULL)
return;

if (sd->state.lapine_ui != 1)
return;

if (pc_cant_act_except_lapine(sd))
return;

if (pc->search_inventory(sd, it->nameid) == INDEX_NOT_FOUND)
return;

const int index = p->index - 2;
if (index < 0 || index >= sd->status.inventorySize)
return;

int i;

for (i = 0; i < VECTOR_LENGTH(it->lapineupgrade->TargetItems); ++i) {
struct itemlist_entry *entry = &VECTOR_INDEX(it->lapineupgrade->TargetItems, i);

if (entry->id != sd->status.inventory[index].nameid)
continue;

if (it->lapineupgrade->NeedRefineMin > sd->status.inventory[index].refine
|| it->lapineupgrade->NeedRefineMax < sd->status.inventory[index].refine) {
clif->lapineUpgrade_result(sd, LAPINE_UPGRADE_FAILED);
return;
}

if (it->lapineupgrade->NoEnchant) {
int j;

ARR_FIND(0, MAX_SLOTS, j, sd->status.inventory[index].card[j] != 0);

if (j != MAX_SLOTS) {
clif->lapineUpgrade_result(sd, LAPINE_UPGRADE_FAILED);
return;
}
}

if (it->lapineupgrade->NeedOptionMin > 0) {
int options = 0;
for (int j = 0; j < MAX_ITEM_OPTIONS; ++j) {
if (sd->status.inventory[index].option[j].index != 0)
options++;
}

if (it->lapineupgrade->NeedOptionMin > options) {
clif->lapineUpgrade_result(sd, LAPINE_UPGRADE_FAILED);
return;
}
}

break;
}

if (i == VECTOR_LENGTH(it->lapineupgrade->TargetItems)) {
clif->lapineUpgrade_result(sd, LAPINE_UPGRADE_FAILED);
return;
}

if (it->lapineupgrade->script != NULL) {
pc->setreg(sd, script->add_variable("@lapine_itemid"), sd->status.inventory[index].nameid);
pc->setreg(sd, script->add_variable("@lapine_idx"), index);
pc->setreg(sd, script->add_variable("@lapine_refine"), sd->status.inventory[index].refine);
pc->setreg(sd, script->add_variable("@lapine_attribute"), sd->status.inventory[index].attribute);
pc->setreg(sd, script->add_variable("@lapine_bound"), sd->status.inventory[index].bound);

for (int j = 0; j < MAX_SLOTS; ++j)
pc->setreg(sd, reference_uid(script->add_variable("@lapine_card"), j), sd->status.inventory[index].card[j]);

for (int j = 0; j < MAX_ITEM_OPTIONS; ++j) {
pc->setreg(sd, reference_uid(script->add_variable("@lapine_option_idx"), j), sd->status.inventory[index].option[j].index);
pc->setreg(sd, reference_uid(script->add_variable("@lapine_option_value"), j), sd->status.inventory[index].option[j].value);
pc->setreg(sd, reference_uid(script->add_variable("@lapine_option_param"), j), sd->status.inventory[index].option[j].param);
}

script->run_item_lapineupgrade_script(sd, it, npc->fake_nd->bl.id);
}

clif->lapineUpgrade_result(sd, LAPINE_UPGRADE_SUCCESS);
return;
#endif // PACKETVER_MAIN_NUM >= 20170111 || PACKETVER_RE_NUM >= 20170111 || defined(PACKETVER_ZERO)
}

static bool clif_lapineUpgrade_result(struct map_session_data *sd, enum lapineUpgrade_result result)
Expand Down
107 changes: 107 additions & 0 deletions src/map/itemdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,103 @@ static bool itemdb_read_libconfig_lapineddukddak_sub_sources(struct config_setti
return true;
}

static bool itemdb_read_libconfig_lapineupgrade(void)
{
struct config_t item_lapineupgrade;
struct config_setting_t *it = NULL;
char filepath[256];

int i = 0;
int count = 0;

safesnprintf(filepath, sizeof(filepath), "%s/%s", map->db_path, DBPATH"item_lapineupgrade.conf");
if (libconfig->load_file(&item_lapineupgrade, filepath) == CONFIG_FALSE)
return false;

while ((it = libconfig->setting_get_elem(item_lapineupgrade.root, i++)) != NULL) {
if (itemdb->read_libconfig_lapineupgrade_sub(it, filepath))
++count;
}

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

static bool itemdb_read_libconfig_lapineupgrade_sub(struct config_setting_t *it, const char *source)
{
nullpo_retr(false, it);
nullpo_retr(false, source);

struct item_data *data = NULL;
const char *name = config_setting_name(it);
const char *str = NULL;
int i32 = 0;
bool real_bool = false;

if ((data = itemdb->name2id(name)) == NULL) {
ShowWarning("itemdb_read_libconfig_lapineupgrade_sub: unknown item '%s', skipping..\n", name);
return false;
}

data->lapineupgrade = aCalloc(1, sizeof(struct item_lapineupgrade));

if (libconfig->setting_lookup_int(it, "NeedRefineMin", &i32) == CONFIG_TRUE)
data->lapineupgrade->NeedRefineMin = (int8)i32;

if (libconfig->setting_lookup_int(it, "NeedRefineMax", &i32) == CONFIG_TRUE)
data->lapineupgrade->NeedRefineMax = (int8)i32;

if (libconfig->setting_lookup_int(it, "NeedOptionMin", &i32) == CONFIG_TRUE)
data->lapineupgrade->NeedOptionMin = (int8)i32;

if (libconfig->setting_lookup_bool_real(it, "NoEnchants", &real_bool) == CONFIG_TRUE)
data->lapineupgrade->NoEnchant = real_bool;

struct config_setting_t *targets = libconfig->setting_get_member(it, "TargetItems");
itemdb->read_libconfig_lapineupgrade_sub_targets(targets, data);

if (libconfig->setting_lookup_string(it, "Script", &str) == CONFIG_TRUE)
data->lapineupgrade->script = *str ? script->parse(str, source, -data->nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL;
return true;
}

static bool itemdb_read_libconfig_lapineupgrade_sub_targets(struct config_setting_t *targets, struct item_data *data)
{
nullpo_retr(false, data);
nullpo_retr(false, data->lapineupgrade);

int i = 0;
struct config_setting_t *entry = NULL;

if (targets == NULL || !config_setting_is_group(targets))
return false;

VECTOR_INIT(data->lapineupgrade->TargetItems);
while ((entry = libconfig->setting_get_elem(targets, i++)) != NULL) {
struct item_data *edata = NULL;
struct itemlist_entry item = {0};
const char *name = config_setting_name(entry);
int i32 = 0;

if ((edata = itemdb->name2id(name)) == NULL) {
ShowWarning("itemdb_read_libconfig_lapineupgrade_sub_targets: unknown item '%s', skipping..\n", name);
continue;
}
item.id = edata->nameid;

if ((i32 = libconfig->setting_get_int(entry)) == CONFIG_TRUE && (i32 <= 0 || i32 > MAX_AMOUNT)) {
ShowWarning("itemdb_read_libconfig_lapineupgrade_sub_targets: invalid amount (%d) for target item '%s', skipping..\n", i32, name);
continue;
}
item.amount = i32;

VECTOR_ENSURE(data->lapineupgrade->TargetItems, 1, 1);
VECTOR_PUSH(data->lapineupgrade->TargetItems, item);
}
return true;
}

/**
* Reads all item-related databases.
*/
Expand Down Expand Up @@ -2631,6 +2728,7 @@ static void itemdb_read(bool minimal)
itemdb->read_chains();
itemdb->read_packages();
itemdb->read_libconfig_lapineddukddak();
itemdb->read_libconfig_lapineupgrade();
}

/**
Expand Down Expand Up @@ -2697,6 +2795,12 @@ static void destroy_item_data(struct item_data *self, int free_self)
VECTOR_CLEAR(self->lapineddukddak->SourceItems);
aFree(self->lapineddukddak);
}
if (self->lapineupgrade != NULL) {
if (self->lapineupgrade->script != NULL)
script->free_code(self->lapineupgrade->script);
VECTOR_CLEAR(self->lapineupgrade->TargetItems);
aFree(self->lapineupgrade);
}
HPM->data_store_destroy(&self->hdata);
#if defined(DEBUG)
// trash item
Expand Down Expand Up @@ -3004,4 +3108,7 @@ void itemdb_defaults(void)
itemdb->read_libconfig_lapineddukddak = itemdb_read_libconfig_lapineddukddak;
itemdb->read_libconfig_lapineddukddak_sub = itemdb_read_libconfig_lapineddukddak_sub;
itemdb->read_libconfig_lapineddukddak_sub_sources = itemdb_read_libconfig_lapineddukddak_sub_sources;
itemdb->read_libconfig_lapineupgrade = itemdb_read_libconfig_lapineupgrade;
itemdb->read_libconfig_lapineupgrade_sub = itemdb_read_libconfig_lapineupgrade_sub;
itemdb->read_libconfig_lapineupgrade_sub_targets = itemdb_read_libconfig_lapineupgrade_sub_targets;
}
14 changes: 14 additions & 0 deletions src/map/itemdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ struct item_lapineddukddak {
struct script_code *script;
};

struct item_lapineupgrade {
int8 NeedRefineMin;
int8 NeedRefineMax;
int8 NeedOptionMin;
bool NoEnchant;
VECTOR_DECL(struct itemlist_entry) TargetItems;
struct script_code *script;
};

struct item_data {
int nameid;
char name[ITEM_NAME_LENGTH],jname[ITEM_NAME_LENGTH];
Expand Down Expand Up @@ -578,6 +587,7 @@ struct item_data {
struct item_group *group;
struct item_package *package;
struct item_lapineddukddak *lapineddukddak;
struct item_lapineupgrade *lapineupgrade;
struct hplugin_data_store *hdata; ///< HPM Plugin Data Store
};

Expand Down Expand Up @@ -717,6 +727,10 @@ struct itemdb_interface {
bool (*read_libconfig_lapineddukddak) (void);
bool (*read_libconfig_lapineddukddak_sub) (struct config_setting_t *it, const char *source);
bool (*read_libconfig_lapineddukddak_sub_sources) (struct config_setting_t *sources, struct item_data *data);

bool (*read_libconfig_lapineupgrade) (void);
bool (*read_libconfig_lapineupgrade_sub) (struct config_setting_t *it, const char *source);
bool (*read_libconfig_lapineupgrade_sub_targets) (struct config_setting_t *sources, struct item_data *data);
};

#ifdef HERCULES_CORE
Expand Down
40 changes: 40 additions & 0 deletions src/map/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -27373,6 +27373,28 @@ static BUILDIN(openlapineddukddakboxui)
return true;
}

static BUILDIN(openlapineupgradeui)
{
struct map_session_data *sd = script->rid2sd(st);

if (sd == NULL)
return false;

const int item_id = script_getnum(st, 2);
struct item_data *it = itemdb->exists(item_id);
if (it == NULL || it->lapineupgrade == NULL) {
ShowError("buildin_openlapineupgradeui: Item %d is not valid\n", item_id);
script->reportfunc(st);
script->reportsrc(st);
script_pushint(st, false);
return true;
}

clif->lapineUpgrade_open(sd, item_id);
script_pushint(st, true);
return true;
}

// Reset 'Feeling' maps.
BUILDIN(resetfeel)
{
Expand Down Expand Up @@ -27600,6 +27622,22 @@ static void script_run_item_lapineddukddak_script(struct map_session_data *sd, s
script->current_item_id = 0;
}

static void script_run_item_lapineupgrade_script(struct map_session_data *sd, struct item_data *data, int oid) __attribute__((nonnull(1, 2)));

/**
* Run item lapineddukddak script for item.
*
* @param sd player session data. Must be correct and checked before.
* @param data unequipped item data. Must be correct and checked before.
* @param oid npc id. Can be also 0 or fake npc id.
*/
static void script_run_item_lapineupgrade_script(struct map_session_data *sd, struct item_data *data, int oid)
{
script->current_item_id = data->nameid;
script->run(data->lapineupgrade->script, 0, sd->bl.id, oid);
script->current_item_id = 0;
}

#define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, false }
#define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, false }
#define BUILDIN_DEF_DEPRECATED(x,args) { buildin_ ## x , #x , args, true }
Expand Down Expand Up @@ -28230,6 +28268,7 @@ static void script_parse_builtin(void)
BUILDIN_DEF(identify, "i"),
BUILDIN_DEF(identifyidx, "i"),
BUILDIN_DEF(openlapineddukddakboxui, "i"),
BUILDIN_DEF(openlapineupgradeui, "i"),

BUILDIN_DEF(callfunctionofnpc, "vs*"),
};
Expand Down Expand Up @@ -29481,6 +29520,7 @@ void script_defaults(void)
script->run_item_rental_start_script = script_run_item_rental_start_script;
script->run_item_rental_end_script = script_run_item_rental_end_script;
script->run_item_lapineddukddak_script = script_run_item_lapineddukddak_script;
script->run_item_lapineupgrade_script = script_run_item_lapineupgrade_script;

script->sellitemcurrency_add = script_sellitemcurrency_add;
}
1 change: 1 addition & 0 deletions src/map/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ struct script_interface {
void (*run_item_rental_end_script) (struct map_session_data *sd, struct item_data *data, int oid);
void (*run_item_rental_start_script) (struct map_session_data *sd, struct item_data *data, int oid);
void (*run_item_lapineddukddak_script) (struct map_session_data *sd, struct item_data *data, int oid);
void (*run_item_lapineupgrade_script) (struct map_session_data *sd, struct item_data *data, int oid);
bool (*sellitemcurrency_add) (struct npc_data *nd, struct script_state* st, int argIndex);
};

Expand Down