From 5d7c8d9857f096abfc4f205c8771b0b4c91fab2c Mon Sep 17 00:00:00 2001 From: Ibrahim Zidan Date: Tue, 7 May 2019 06:37:11 +0200 Subject: Implement new script fields for items triggered on rentral status changes - OnRentalStartScript triggered when a rental item is added to inventory - OnRentalEndScript triggered when a rental period end/expire of the item Related #140 Signed-off-by: Ibrahim Zidan --- src/map/itemdb.c | 37 ++++++++++++++++++++++++++++++++++++- src/map/itemdb.h | 2 ++ src/map/pc.c | 19 +++++++++++++++---- src/map/script.c | 32 ++++++++++++++++++++++++++++++++ src/map/script.h | 2 ++ 5 files changed, 87 insertions(+), 5 deletions(-) (limited to 'src/map') diff --git a/src/map/itemdb.c b/src/map/itemdb.c index bb2732b17..204d0ead5 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -1726,6 +1726,14 @@ static int itemdb_validate_entry(struct item_data *entry, int n, const char *sou script->free_code(entry->unequip_script); entry->unequip_script = NULL; } + if (entry->rental_start_script != NULL) { + script->free_code(entry->rental_start_script); + entry->rental_start_script = NULL; + } + if (entry->rental_end_script != NULL) { + script->free_code(entry->rental_end_script); + entry->rental_end_script = NULL; + } return 0; #if PACKETVER_MAIN_NUM >= 20181121 || PACKETVER_RE_NUM >= 20180704 || PACKETVER_ZERO_NUM >= 20181114 } @@ -1756,6 +1764,14 @@ static int itemdb_validate_entry(struct item_data *entry, int n, const char *sou script->free_code(entry->unequip_script); entry->unequip_script = NULL; } + if (entry->rental_start_script != NULL) { + script->free_code(entry->rental_start_script); + entry->rental_start_script = NULL; + } + if (entry->rental_end_script != NULL) { + script->free_code(entry->rental_end_script); + entry->rental_end_script = NULL; + } return 0; } } @@ -1883,7 +1899,14 @@ static int itemdb_validate_entry(struct item_data *entry, int n, const char *sou script->free_code(item->unequip_script); item->unequip_script = NULL; } - + if (item->rental_start_script != NULL && item->rental_start_script != entry->rental_start_script) { // Don't free if it's inheriting the same script + script->free_code(item->rental_start_script); + item->rental_start_script = NULL; + } + if (item->rental_end_script != NULL && item->rental_end_script != entry->rental_end_script) { // Don't free if it's inheriting the same script + script->free_code(item->rental_end_script); + item->rental_end_script = NULL; + } *item = *entry; return item->nameid; } @@ -1999,6 +2022,8 @@ static int itemdb_readdb_libconfig_sub(struct config_setting_t *it, int n, const * "> * OnEquipScript: <" OnEquip Script "> * OnUnequipScript: <" OnUnequip Script "> + * OnRentalStartScript: <" on renting script "> + * OnRentalEndScript: <" on renting end script "> * Inherit: inherit or override */ if( !itemdb->lookup_const(it, "Id", &i32) ) { @@ -2276,6 +2301,12 @@ static int itemdb_readdb_libconfig_sub(struct config_setting_t *it, int n, const if( libconfig->setting_lookup_string(it, "OnUnequipScript", &str) ) id.unequip_script = *str ? script->parse(str, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; + if (libconfig->setting_lookup_string(it, "OnRentalStartScript", &str) != CONFIG_FALSE) + id.rental_start_script = (*str != '\0') ? script->parse(str, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; + + if (libconfig->setting_lookup_string(it, "OnRentalEndScript", &str) != CONFIG_FALSE) + id.rental_end_script = (*str != '\0') ? script->parse(str, source, -id.nameid, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL; + return itemdb->validate_entry(&id, n, source); } @@ -2519,6 +2550,10 @@ static void destroy_item_data(struct item_data *self, int free_self) script->free_code(self->equip_script); if( self->unequip_script ) script->free_code(self->unequip_script); + if (self->rental_start_script != NULL) + script->free_code(self->rental_start_script); + if (self->rental_end_script != NULL) + script->free_code(self->rental_end_script); if( self->combos ) aFree(self->combos); HPM->data_store_destroy(&self->hdata); diff --git a/src/map/itemdb.h b/src/map/itemdb.h index 7da7609f1..d1e5973e8 100644 --- a/src/map/itemdb.h +++ b/src/map/itemdb.h @@ -516,6 +516,8 @@ struct item_data { struct script_code *script; ///< Default script for everything. struct script_code *equip_script; ///< Script executed once when equipping. struct script_code *unequip_script; ///< Script executed once when unequipping. + struct script_code *rental_start_script; ///< Script executed once this item get rented + struct script_code *rental_end_script; ///< Script executed once this item rent ends struct { unsigned available : 1; unsigned no_refine : 1; // [celest] diff --git a/src/map/pc.c b/src/map/pc.c index 8ac820d79..b82eb5dae 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -4789,13 +4789,15 @@ static int pc_additem(struct map_session_data *sd, const struct item *item_data, pc->equipitem(sd, i, data->equip); /* rental item check */ - if( item_data->expire_time ) { - if( time(NULL) > item_data->expire_time ) { - pc->rental_expire(sd,i); + if (item_data->expire_time > 0) { + if (time(NULL) > item_data->expire_time) { + pc->rental_expire(sd, i); } else { - int seconds = (int)( item_data->expire_time - time(NULL) ); + int seconds = (int)(item_data->expire_time - time(NULL)); clif->rental_time(sd->fd, sd->status.inventory[i].nameid, seconds); pc->inventory_rental_add(sd, seconds); + if (data->rental_start_script != NULL) + script->run_item_rental_start_script(sd, data, 0); } } quest->questinfo_refresh(sd); @@ -4826,12 +4828,21 @@ static int pc_delitem(struct map_session_data *sd, int n, int amount, int type, sd->status.inventory[n].amount -= amount; sd->weight -= sd->inventory_data[n]->weight*amount ; + + // It's here because the data would most likely get zeroed in following if [Hemagx] + struct item_data *itd = sd->inventory_data[n]; + bool is_rental = (sd->status.inventory[n].expire_time > 0) ? true : false; + if( sd->status.inventory[n].amount <= 0 ){ if(sd->status.inventory[n].equip) pc->unequipitem(sd, n, PCUNEQUIPITEM_RECALC|PCUNEQUIPITEM_FORCE); memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0])); sd->inventory_data[n] = NULL; } + + if (is_rental && itd->rental_end_script != NULL) + script->run_item_rental_end_script(sd, itd, 0); + if(!(type&1)) clif->delitem(sd,n,amount,reason); if(!(type&2)) diff --git a/src/map/script.c b/src/map/script.c index de00f66be..8658b7ab5 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -25891,6 +25891,36 @@ static void script_run_item_unequip_script(struct map_session_data *sd, struct i script->current_item_id = 0; } +static void script_run_item_rental_start_script(struct map_session_data *sd, struct item_data *data, int oid) __attribute__((nonnull(1, 2))); + +/** + * Run item rental start script + * @param sd player session data. Must be correct and checked before. + * @param data rental 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_rental_start_script(struct map_session_data *sd, struct item_data *data, int oid) +{ + script->current_item_id = data->nameid; + script->run(data->rental_start_script, 0, sd->bl.id, oid); + script->current_item_id = 0; +} + +static void script_run_item_rental_end_script(struct map_session_data *sd, struct item_data *data, int oid) __attribute__((nonnull(1, 2))); + +/** +* Run item rental end script +* @param sd player session data. Must be correct and checked before. +* @param data rental 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_rental_end_script(struct map_session_data *sd, struct item_data *data, int oid) +{ + script->current_item_id = data->nameid; + script->run(data->rental_end_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 } @@ -27443,4 +27473,6 @@ void script_defaults(void) script->run_use_script = script_run_use_script; script->run_item_equip_script = script_run_item_equip_script; script->run_item_unequip_script = script_run_item_unequip_script; + script->run_item_rental_start_script = script_run_item_rental_start_script; + script->run_item_rental_end_script = script_run_item_rental_end_script; } diff --git a/src/map/script.h b/src/map/script.h index 64e709a27..a75b948ab 100644 --- a/src/map/script.h +++ b/src/map/script.h @@ -1049,6 +1049,8 @@ struct script_interface { void (*run_use_script) (struct map_session_data *sd, struct item_data *data, int oid); void (*run_item_equip_script) (struct map_session_data *sd, struct item_data *data, int oid); void (*run_item_unequip_script) (struct map_session_data *sd, struct item_data *data, int oid); + 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); }; #ifdef HERCULES_CORE -- cgit v1.2.3-70-g09d2 From 3d595e664563bf6ac3e434152baadd7dbc295d06 Mon Sep 17 00:00:00 2001 From: Ibrahim Zidan Date: Tue, 7 May 2019 06:57:09 +0200 Subject: Implement getfont script command which returns player's current chat font Signed-off-by: Ibrahim Zidan --- doc/script_commands.txt | 6 ++++++ src/map/script.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'src/map') diff --git a/doc/script_commands.txt b/doc/script_commands.txt index d14393a8a..72f86024e 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -9172,6 +9172,12 @@ currently used font is used, default interface font is used again. --------------------------------------- +*getfont() + +This command return the player's current font. +if no player is attached it would always return a 0, which is also the default font. + +---------------------------------------' *showdigit({, }) Displays given numeric 'value' in large digital clock font on top of the diff --git a/src/map/script.c b/src/map/script.c index 8658b7ab5..64b794ba0 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -22744,6 +22744,19 @@ static BUILDIN(setfont) return true; } +static BUILDIN(getfont) +{ + struct map_session_data *sd = script->rid2sd(st); + + if (sd == NULL) { + script_pushint(st, 0); + return true; + } + + script_pushint(st, sd->status.font); + return true; +} + static int buildin_mobuseskill_sub(struct block_list *bl, va_list ap) { struct mob_data *md = NULL; @@ -26356,6 +26369,7 @@ static void script_parse_builtin(void) BUILDIN_DEF(mercenary_set_faith,"ii"), BUILDIN_DEF(readbook,"ii"), BUILDIN_DEF(setfont,"i"), + BUILDIN_DEF(getfont, ""), BUILDIN_DEF(areamobuseskill,"siiiiviiiii"), BUILDIN_DEF(progressbar,"si"), BUILDIN_DEF(progressbar_unit,"si?"), -- cgit v1.2.3-70-g09d2 From 5d8e006fb85df2b3bbc32f2348bbcaad950546ba Mon Sep 17 00:00:00 2001 From: Ibrahim Zidan Date: Tue, 7 May 2019 08:05:39 +0200 Subject: Remove the hardcoded checks in pc_rental_expire and use OnRentalEndScript field in item database for those items instead Fixes #140 Signed-off-by: Ibrahim Zidan --- db/pre-re/item_db.conf | 55 ++++++++++++++++++++++++++++++++++++------- db/re/item_db.conf | 55 ++++++++++++++++++++++++++++++++++++------- src/map/pc.c | 64 -------------------------------------------------- 3 files changed, 92 insertions(+), 82 deletions(-) (limited to 'src/map') diff --git a/db/pre-re/item_db.conf b/db/pre-re/item_db.conf index 3fe2fdacd..ba9de0dd4 100644 --- a/db/pre-re/item_db.conf +++ b/db/pre-re/item_db.conf @@ -69572,7 +69572,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 1; "> + Script: <" setfont(1); "> + OnRentalEndScript: <" + if (getfont() == 1) + setfont(0); + "> }, { Id: 12288 @@ -69589,7 +69593,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 2; "> + Script: <" setfont(2); "> + OnRentalEndScript: <" + if (getfont() == 2) + setfont(0); + "> }, { Id: 12289 @@ -69606,7 +69614,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 3; "> + Script: <" setfont(3); "> + OnRentalEndScript: <" + if (getfont() == 3) + setfont(0); + "> }, { Id: 12290 @@ -69858,7 +69870,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 4; "> + Script: <" setfont(4); "> + OnRentalEndScript: <" + if (getfont() == 4) + setfont(0); + "> }, { Id: 12305 @@ -69875,7 +69891,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 5; "> + Script: <" setfont(5); "> + OnRentalEndScript: <" + if (getfont() == 5) + setfont(0); + "> }, { Id: 12306 @@ -69892,7 +69912,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 6; "> + Script: <" setfont(6); "> + OnRentalEndScript: <" + if (getfont() == 6) + setfont(0); + "> }, { Id: 12307 @@ -69909,7 +69933,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 7; "> + Script: <" setfont(7); "> + OnRentalEndScript: <" + if (getfont() == 7) + setfont(0); + "> }, { Id: 12308 @@ -69926,7 +69954,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 8; "> + Script: <" setfont(8); "> + OnRentalEndScript: <" + if (getfont() == 8) + setfont(0); + "> }, { Id: 12309 @@ -69943,7 +69975,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 9; "> + Script: <" setfont(9); "> + OnRentalEndScript: <" + if (getfont() == 9) + setfont(0); + "> }, { Id: 12310 @@ -73053,6 +73089,7 @@ item_db: ( noauction: true } Script: <" setcashmount(); "> + OnRentalEndScript: <" sc_end(SC_ALL_RIDING); "> }, { Id: 12701 diff --git a/db/re/item_db.conf b/db/re/item_db.conf index 9f49c07ef..f9997cb53 100644 --- a/db/re/item_db.conf +++ b/db/re/item_db.conf @@ -89228,7 +89228,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 1; "> + Script: <" setfont(1); "> + OnRentalEndScript: <" + if (getfont() == 1) + setfont(0); + "> }, { Id: 12288 @@ -89245,7 +89249,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 2; "> + Script: <" setfont(2); "> + OnRentalEndScript: <" + if (getfont() == 2) + setfont(0); + "> }, { Id: 12289 @@ -89262,7 +89270,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 3; "> + Script: <" setfont(3); "> + OnRentalEndScript: <" + if (getfont() == 3) + setfont(0); + "> }, { Id: 12290 @@ -89514,7 +89526,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 4; "> + Script: <" setfont(4); "> + OnRentalEndScript: <" + if (getfont() == 4) + setfont(0); + "> }, { Id: 12305 @@ -89531,7 +89547,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 5; "> + Script: <" setfont(5); "> + OnRentalEndScript: <" + if (getfont() == 5) + setfont(0); + "> }, { Id: 12306 @@ -89548,7 +89568,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 6; "> + Script: <" setfont(6); "> + OnRentalEndScript: <" + if (getfont() == 6) + setfont(0); + "> }, { Id: 12307 @@ -89565,7 +89589,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 7; "> + Script: <" setfont(7); "> + OnRentalEndScript: <" + if (getfont() == 7) + setfont(0); + "> }, { Id: 12308 @@ -89582,7 +89610,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 8; "> + Script: <" setfont(8); "> + OnRentalEndScript: <" + if (getfont() == 8) + setfont(0); + "> }, { Id: 12309 @@ -89599,7 +89631,11 @@ item_db: ( nomail: true noauction: true } - Script: <" setfont 9; "> + Script: <" setfont(9); "> + OnRentalEndScript: <" + if (getfont() == 9) + setfont(0); + "> }, { Id: 12310 @@ -93351,6 +93387,7 @@ item_db: ( noauction: true } Script: <" setcashmount(); "> + OnRentalEndScript: <" sc_end(SC_ALL_RIDING); "> }, { Id: 12623 diff --git a/src/map/pc.c b/src/map/pc.c index b82eb5dae..2cefa7674 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -565,72 +565,8 @@ static int pc_inventory_rental_clear(struct map_session_data *sd) /* assumes i is valid (from default areas where it is called, it is) */ static void pc_rental_expire(struct map_session_data *sd, int i) { - int nameid; - nullpo_retv(sd); Assert_retv(i >= 0 && i < sd->status.inventorySize); - nameid = sd->status.inventory[i].nameid; - - /* Soon to be dropped, we got plans to integrate it with item db */ - switch( nameid ) { - case ITEMID_BOARDING_HALTER: - status_change_end(&sd->bl,SC_ALL_RIDING,INVALID_TIMER); - break; - case ITEMID_LOVE_ANGEL: - if( sd->status.font == 1 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_SQUIRREL: - if( sd->status.font == 2 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_GOGO: - if( sd->status.font == 3 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_PICTURE_DIARY: - if( sd->status.font == 4 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_MINI_HEART: - if( sd->status.font == 5 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_NEWCOMER: - if( sd->status.font == 6 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_KID: - if( sd->status.font == 7 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_MAGIC_CASTLE: - if( sd->status.font == 8 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - case ITEMID_BULGING_HEAD: - if( sd->status.font == 9 ) { - sd->status.font = 0; - clif->font(sd); - } - break; - } clif->rental_expired(sd->fd, i, sd->status.inventory[i].nameid); pc->delitem(sd, i, sd->status.inventory[i].amount, 0, DELITEM_NORMAL, LOG_TYPE_RENTAL); -- cgit v1.2.3-70-g09d2