From 6902fd25a47e613295382bc6ef86bb5a04848819 Mon Sep 17 00:00:00 2001 From: Jesusaves Date: Tue, 9 Mar 2021 18:28:27 -0300 Subject: [TMW2] Report back to script NST_CUSTOM purchases. This allows to impose restrictions on amounts, for example. --- src/map/npc.c | 22 +++++++++++++++++++--- src/map/npc.h | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'src/map') diff --git a/src/map/npc.c b/src/map/npc.c index d6b18ec9e..6b510b4b3 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -1511,7 +1511,7 @@ static int npc_cashshop_buylist(struct map_session_data *sd, int points, struct // Payment Process ---------------------------------------------------- if( nd->subtype == SCRIPT && nd->u.scr.shop->type == NST_CUSTOM ) { - if( !npc->trader_pay(nd,sd,vt,points) ) + if( !npc->trader_pay(nd,sd,vt,points,item_list) ) return ERROR_TYPE_MONEY; } else { if( sd->kafraPoints < points || sd->cashPoints < (vt - points) ) @@ -1866,7 +1866,7 @@ static void npc_trader_count_funds(struct npc_data *nd, struct map_session_data * * @return bool whether it was successful (if the script does not respond it will fail) **/ -static bool npc_trader_pay(struct npc_data *nd, struct map_session_data *sd, int price, int points) +static bool npc_trader_pay(struct npc_data *nd, struct map_session_data *sd, int price, int points, struct itemlist *item_list) { char evname[EVENT_NAME_LENGTH]; struct event_data *ev = NULL; @@ -1879,6 +1879,22 @@ static bool npc_trader_pay(struct npc_data *nd, struct map_session_data *sd, int if ( (ev = strdb_get(npc->ev_db, evname)) ) { pc->setreg(sd,script->add_variable("@price"),price); pc->setreg(sd,script->add_variable("@points"),points); + if (item_list != NULL) { + // discard old contents + script->cleararray_pc(sd, "@bought_nameid", (void*)0); + script->cleararray_pc(sd, "@bought_quantity", (void*)0); + int key_nameid = 0; + int key_amount = 0; + + // save list of bought items + for (int i = 0; i < VECTOR_LENGTH(*item_list); i++) { + struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i); + intptr_t nameid = entry->id; + intptr_t amount = entry->amount; + script->setarray_pc(sd, "@bought_nameid", i, (void *)nameid, &key_nameid); + script->setarray_pc(sd, "@bought_quantity", i, (void *)amount, &key_amount); + } + } script->run_npc(ev->nd->u.scr.script, ev->pos, sd->bl.id, ev->nd->bl.id); } else ShowError("npc_trader_pay: '%s' event '%s' not found, operation failed\n",nd->exname,evname); @@ -1967,7 +1983,7 @@ static int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, points = price; if( nd->subtype == SCRIPT && nd->u.scr.shop->type == NST_CUSTOM ) { - if( !npc->trader_pay(nd,sd,price,points) ) + if( !npc->trader_pay(nd,sd,price,points,NULL) ) return ERROR_TYPE_MONEY; } else { if( (sd->kafraPoints < points) || (sd->cashPoints < price - points) ) diff --git a/src/map/npc.h b/src/map/npc.h index 5ff63532d..4e70c1f72 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -310,7 +310,7 @@ struct npc_interface { void (*debug_warps) (void); /* */ void (*trader_count_funds) (struct npc_data *nd, struct map_session_data *sd); - bool (*trader_pay) (struct npc_data *nd, struct map_session_data *sd, int price, int points); + bool (*trader_pay) (struct npc_data *nd, struct map_session_data *sd, int price, int points, struct itemlist *item_list); void (*trader_update) (int master); enum market_buy_result (*market_buylist) (struct map_session_data *sd, struct itemlist *item_list); int (*barter_buylist) (struct map_session_data *sd, struct barteritemlist *item_list); -- cgit v1.2.3-70-g09d2