/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2020 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* 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 .
*/
#define HERCULES_CORE
#include "config/core.h" // NPC_SECURE_TIMEOUT_INPUT, NPC_SECURE_TIMEOUT_MENU, NPC_SECURE_TIMEOUT_NEXT, SECURE_NPCTIMEOUT, SECURE_NPCTIMEOUT_INTERVAL
#include "npc.h"
#include "map/battle.h"
#include "map/chat.h"
#include "map/clan.h"
#include "map/clif.h"
#include "map/guild.h"
#include "map/instance.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/map.h"
#include "map/mob.h"
#include "map/pc.h"
#include "map/pet.h"
#include "map/quest.h"
#include "map/script.h"
#include "map/skill.h"
#include "map/status.h"
#include "map/unit.h"
#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/db.h"
#include "common/ers.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/sql.h"
#include "common/strlib.h"
#include "common/timer.h"
#include "common/utils.h"
#include
#include
#include
#include
#include
#include
static struct npc_interface npc_s;
struct npc_interface *npc;
//For holding the view data of npc classes. [Skotlex]
static struct view_data npc_viewdb[MAX_NPC_CLASS];
static struct view_data npc_viewdb2[MAX_NPC_CLASS2_END-MAX_NPC_CLASS2_START];
/* for speedup */
static unsigned int npc_market_qty[MAX_INVENTORY];
static struct script_event_s {
//Holds pointers to the commonly executed scripts for speedup. [Skotlex]
struct event_data *event[UCHAR_MAX];
const char *event_name[UCHAR_MAX];
uint8 event_count;
} script_event[NPCE_MAX];
/**
* Returns the viewdata for normal npc classes.
* @param class_ The NPC class ID.
* @return The viewdata, or NULL if the ID is invalid.
*/
static struct view_data *npc_get_viewdata(int class_)
{
if (class_ == INVISIBLE_CLASS)
return &npc_viewdb[0];
if (npc->db_checkid(class_)) {
if (class_ < MAX_NPC_CLASS) {
return &npc_viewdb[class_];
} else if (class_ >= MAX_NPC_CLASS2_START && class_ < MAX_NPC_CLASS2_END) {
return &npc_viewdb2[class_-MAX_NPC_CLASS2_START];
}
}
return NULL;
}
/**
* Checks if a given id is a valid npc id.
*
* Since new npcs are added all the time, the max valid value is the one before the first mob (Scorpion = 1001)
*
* @param id The NPC ID to validate.
* @return Whether the value is a valid ID.
*/
static bool npc_db_checkid(int id)
{
if (id >= WARP_CLASS && id <= 125) // First subrange
return true;
if (id == HIDDEN_WARP_CLASS || id == INVISIBLE_CLASS) // Special IDs not included in the valid ranges
return true;
if (id > 400 && id < MAX_NPC_CLASS) // Second subrange
return true;
if (id >= MAX_NPC_CLASS2_START && id < MAX_NPC_CLASS2_END) // Second range
return true;
if (pc->db_checkid(id))
return true;
// Anything else is invalid
return false;
}
/// Returns a new npc id that isn't being used in id_db.
/// Fatal error if nothing is available.
static int npc_get_new_npc_id(void)
{
if (npc->npc_id >= START_NPC_NUM && !map->blid_exists(npc->npc_id))
return npc->npc_id++;// available
else {// find next id
int base_id = npc->npc_id;
while (base_id != ++npc->npc_id) {
if (npc->npc_id < START_NPC_NUM)
npc->npc_id = START_NPC_NUM;
if (!map->blid_exists(npc->npc_id))
return npc->npc_id++;// available
}
// full loop, nothing available
ShowFatalError("npc_get_new_npc_id: All ids are taken. Exiting...");
exit(1);
}
}
static int npc_isnear_sub(struct block_list *bl, va_list args)
{
const struct npc_data *nd = NULL;
nullpo_ret(bl);
Assert_ret(bl->type == BL_NPC);
nd = BL_UCCAST(BL_NPC, bl);
if( nd->option & (OPTION_HIDE|OPTION_INVISIBLE) )
return 0;
if( battle_config.vendchat_near_hiddennpc && ( nd->class_ == FAKE_NPC || nd->class_ == HIDDEN_WARP_CLASS ) )
return 0;
return 1;
}
static bool npc_isnear(struct block_list *bl)
{
if( battle_config.min_npc_vendchat_distance > 0
&& map->foreachinrange(npc->isnear_sub,bl, battle_config.min_npc_vendchat_distance, BL_NPC) )
return true;
return false;
}
static int npc_ontouch_event(struct map_session_data *sd, struct npc_data *nd)
{
char name[EVENT_NAME_LENGTH];
nullpo_retr(1, nd);
if( nd->touching_id )
return 0; // Attached a player already. Can't trigger on anyone else.
if( pc_ishiding(sd) )
return 1; // Can't trigger 'OnTouch_'. try 'OnTouch' later.
snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.ontouch_name);
return npc->event(sd,name,1);
}
static int npc_ontouch2_event(struct map_session_data *sd, struct npc_data *nd)
{
char name[EVENT_NAME_LENGTH];
nullpo_retr(1, sd);
nullpo_retr(1, nd);
if (sd->areanpc_id == nd->bl.id)
return 0;
snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.ontouch2_name);
return npc->event(sd, name, 2);
}
static int npc_onuntouch_event(struct map_session_data *sd, struct npc_data *nd)
{
char name[EVENT_NAME_LENGTH];
nullpo_ret(sd);
nullpo_ret(nd);
if (sd->areanpc_id != nd->bl.id)
return 0;
snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.onuntouch_name);
return npc->event(sd, name, 2);
}
/*==========================================
* Sub-function of npc_enable, runs OnTouch event when enabled
*------------------------------------------*/
static int npc_enable_sub(struct block_list *bl, va_list ap)
{
struct npc_data *nd;
nullpo_ret(bl);
nullpo_ret(nd=va_arg(ap,struct npc_data *));
if (bl->type == BL_PC) {
struct map_session_data *sd = BL_UCAST(BL_PC, bl);
if (nd->option&OPTION_INVISIBLE)
return 1;
if( npc->ontouch_event(sd,nd) > 0 && npc->ontouch2_event(sd,nd) > 0 )
{ // failed to run OnTouch event, so just click the npc
if (sd->npc_id != 0)
return 0;
pc_stop_walking(sd, STOPWALKING_FLAG_FIXPOS);
npc->click(sd,nd);
}
}
return 0;
}
/*==========================================
* Disable / Enable NPC
*------------------------------------------*/
static int npc_enable(const char *name, int flag)
{
struct npc_data* nd = npc->name2id(name);
if ( nd == NULL ) {
ShowError("npc_enable: Attempted to %s a non-existing NPC '%s' (flag=%d).\n", (flag&3) ? "show" : "hide", name, flag);
return 0;
}
if (flag&1) {
nd->option&=~OPTION_INVISIBLE;
clif->spawn(&nd->bl);
} else if (flag&2)
nd->option&=~OPTION_HIDE;
else if (flag&4)
nd->option|= OPTION_HIDE;
else { //Can't change the view_data to invisible class because the view_data for all npcs is shared! [Skotlex]
nd->option|= OPTION_INVISIBLE;
clif->clearunit_area(&nd->bl,CLR_OUTSIGHT); // Hack to trick maya purple card [Xazax]
}
if (nd->class_ == WARP_CLASS || nd->class_ == FLAG_CLASS) { //Client won't display option changes for these classes [Toms]
if (nd->option&(OPTION_HIDE|OPTION_INVISIBLE))
clif->clearunit_area(&nd->bl, CLR_OUTSIGHT);
else
clif->spawn(&nd->bl);
} else
clif->changeoption(&nd->bl);
if( flag&3 && (nd->u.scr.xs >= 0 || nd->u.scr.ys >= 0) ) //check if player standing on a OnTouchArea
map->foreachinarea( npc->enable_sub, nd->bl.m, nd->bl.x-nd->u.scr.xs, nd->bl.y-nd->u.scr.ys, nd->bl.x+nd->u.scr.xs, nd->bl.y+nd->u.scr.ys, BL_PC, nd );
return 0;
}
/*==========================================
* NPC lookup (get npc_data through npcname)
*------------------------------------------*/
static struct npc_data *npc_name2id(const char *name)
{
return strdb_get(npc->name_db, name);
}
/**
* For the Secure NPC Timeout option (check config/Secure.h) [RR]
**/
/**
* Timer to check for idle time and timeout the dialog if necessary
**/
static int npc_rr_secure_timeout_timer(int tid, int64 tick, int id, intptr_t data)
{
#ifdef SECURE_NPCTIMEOUT
struct map_session_data* sd = NULL;
unsigned int timeout = NPC_SECURE_TIMEOUT_NEXT;
if( (sd = map->id2sd(id)) == NULL || !sd->npc_id ) {
if( sd ) sd->npc_idle_timer = INVALID_TIMER;
return 0;//Not logged in anymore OR no longer attached to a npc
}
switch( sd->npc_idle_type ) {
case NPCT_INPUT:
timeout = NPC_SECURE_TIMEOUT_INPUT;
break;
case NPCT_MENU:
timeout = NPC_SECURE_TIMEOUT_MENU;
break;
//case NPCT_WAIT: var starts with this value
}
if( DIFF_TICK(tick,sd->npc_idle_tick) > (timeout*1000) ) {
/**
* If we still have the NPC script attached, tell it to stop.
**/
if( sd->st )
sd->st->state = END;
sd->state.menu_or_input = 0;
sd->npc_menu = 0;
/**
* This guy's been idle for longer than allowed, close him.
**/
clif->scriptclose(sd,sd->npc_id);
sd->npc_idle_timer = INVALID_TIMER;
/**
* We will end the script ourselves, client will request to end it again if it have dialog,
* however it will be ignored, workaround for client stuck if NPC have no dialog. [hemagx]
**/
sd->state.dialog = 0;
npc->scriptcont(sd, sd->npc_id, true);
} else //Create a new instance of ourselves to continue
sd->npc_idle_timer = timer->add(timer->gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc->secure_timeout_timer,sd->bl.id,0);
#endif
return 0;
}
/*==========================================
* Dequeue event and add timer for execution (100ms)
*------------------------------------------*/
static int npc_event_dequeue(struct map_session_data *sd)
{
nullpo_ret(sd);
if(sd->npc_id) { //Current script is aborted.
if(sd->state.using_fake_npc){
clif->clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
sd->state.using_fake_npc = 0;
}
if (sd->st) {
script->free_state(sd->st);
sd->st = NULL;
}
sd->npc_id = 0;
}
if (!sd->eventqueue[0][0])
return 0; //Nothing to dequeue
if (!pc->addeventtimer(sd,100,sd->eventqueue[0])) { //Failed to dequeue, couldn't set a timer.
ShowWarning("npc_event_dequeue: event timer is full !\n");
return 0;
}
//Event dequeued successfully, shift other elements.
memmove(sd->eventqueue[0], sd->eventqueue[1], (MAX_EVENTQUEUE-1)*sizeof(sd->eventqueue[0]));
sd->eventqueue[MAX_EVENTQUEUE-1][0]=0;
return 1;
}
/**
* @see DBCreateData
*/
static struct DBData npc_event_export_create(union DBKey key, va_list args)
{
struct linkdb_node** head_ptr;
CREATE(head_ptr, struct linkdb_node*, 1);
*head_ptr = NULL;
return DB->ptr2data(head_ptr);
}
/*==========================================
* exports a npc event label
* called from npc_parse_script
*------------------------------------------*/
static int npc_event_export(struct npc_data *nd, int i)
{
char* lname;
int pos;
nullpo_ret(nd);
Assert_ret(i >= 0 && i < nd->u.scr.label_list_num);
lname = nd->u.scr.label_list[i].name;
pos = nd->u.scr.label_list[i].pos;
if ((nd->u.scr.label_list[i].flags & LABEL_IS_EXTERN) != 0
&& ((nd->u.scr.label_list[i].flags & LABEL_IS_USERFUNC) == 0
|| script->config.functions_as_events)) {
struct event_data *ev;
struct linkdb_node **label_linkdb = NULL;
char buf[EVENT_NAME_LENGTH];
snprintf(buf, ARRAYLENGTH(buf), "%s::%s", nd->exname, lname);
if (strdb_exists(npc->ev_db, buf)) // There was already another event of the same name?
return 1;
// generate the data and insert it
CREATE(ev, struct event_data, 1);
ev->nd = nd;
ev->pos = pos;
strdb_put(npc->ev_db, buf, ev);
label_linkdb = strdb_ensure(npc->ev_label_db, lname, npc->event_export_create);
linkdb_insert(label_linkdb, nd, ev);
}
return 0;
}
static int npc_event_sub(struct map_session_data *sd, struct event_data *ev, const char *eventname); //[Lance]
/**
* Exec name (NPC events) on player or global
* Do on all NPC when called with foreach
*/
static void npc_event_doall_sub(void *key, void *data, va_list ap)
{
struct event_data* ev = data;
int* c;
const char* name;
int rid;
nullpo_retv(c = va_arg(ap, int*));
nullpo_retv(name = va_arg(ap, const char*));
rid = va_arg(ap, int);
if (ev /* && !ev->nd->src_id */) // Do not run on duplicates. [Paradox924X]
{
if(rid) { // a player may only have 1 script running at the same time
char buf[EVENT_NAME_LENGTH];
snprintf(buf, ARRAYLENGTH(buf), "%s::%s", ev->nd->exname, name);
npc->event_sub(map->id2sd(rid), ev, buf);
}
else {
script->run_npc(ev->nd->u.scr.script, ev->pos, rid, ev->nd->bl.id);
}
(*c)++;
}
}
// runs the specified event (supports both single-npc and global events)
static int npc_event_do(const char *name)
{
nullpo_ret(name);
if( name[0] == ':' && name[1] == ':' ) {
return npc->event_doall(name+2); // skip leading "::"
}
else {
struct event_data *ev = strdb_get(npc->ev_db, name);
if (ev) {
script->run_npc(ev->nd->u.scr.script, ev->pos, 0, ev->nd->bl.id);
return 1;
}
}
return 0;
}
// runs the specified event, with a RID attached (global only)
static int npc_event_doall_id(const char *name, int rid)
{
int c = 0;
struct linkdb_node **label_linkdb = strdb_get(npc->ev_label_db, name);
if (label_linkdb == NULL)
return 0;
linkdb_foreach(label_linkdb, npc->event_doall_sub, &c, name, rid);
return c;
}
// runs the specified event (global only)
static int npc_event_doall(const char *name)
{
return npc->event_doall_id(name, 0);
}
/*==========================================
* Clock event execution
* OnMinute/OnClock/OnHour/OnDay/OnDDHHMM
*------------------------------------------*/
static int npc_event_do_clock(int tid, int64 tick, int id, intptr_t data)
{
static struct tm ev_tm_b; // tracks previous execution time
time_t clock;
struct tm* t;
char buf[64];
int c = 0;
clock = time(NULL);
t = localtime(&clock);
if (t->tm_min != ev_tm_b.tm_min ) {
char* day;
switch (t->tm_wday) {
case 0: day = "Sun"; break;
case 1: day = "Mon"; break;
case 2: day = "Tue"; break;
case 3: day = "Wed"; break;
case 4: day = "Thu"; break;
case 5: day = "Fri"; break;
case 6: day = "Sat"; break;
default:day = ""; break;
}
sprintf(buf,"OnMinute%02d",t->tm_min);
c += npc->event_doall(buf);
sprintf(buf,"OnClock%02d%02d",t->tm_hour,t->tm_min);
c += npc->event_doall(buf);
sprintf(buf,"On%s%02d%02d",day,t->tm_hour,t->tm_min);
c += npc->event_doall(buf);
}
if (t->tm_hour != ev_tm_b.tm_hour) {
sprintf(buf,"OnHour%02d",t->tm_hour);
c += npc->event_doall(buf);
}
if (t->tm_mday != ev_tm_b.tm_mday) {
sprintf(buf,"OnDay%02d%02d",t->tm_mon+1,t->tm_mday);
c += npc->event_doall(buf);
}
memcpy(&ev_tm_b,t,sizeof(ev_tm_b));
return c;
}
/**
* OnInit event execution (the start of the event and watch)
* @param reload Is the server reloading?
**/
static void npc_event_do_oninit(bool reload)
{
ShowStatus("Event '"CL_WHITE"OnInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs."CL_CLL"\n", npc->event_doall("OnInit"));
// This interval has already been added on startup
if( !reload )
timer->add_interval(timer->gettick()+100,npc->event_do_clock,0,0,1000);
}
/*==========================================
* Incorporation of the label for the timer event
* called from npc_parse_script
*------------------------------------------*/
static int npc_timerevent_export(struct npc_data *nd, int i)
{
int t = 0, len = 0;
char *lname;
int pos;
nullpo_ret(nd);
lname = nd->u.scr.label_list[i].name;
pos = nd->u.scr.label_list[i].pos;
if (sscanf(lname, "OnTimer%d%n", &t, &len) == 1 && len < NAME_LENGTH && lname[len] == '\0') {
// Timer event
struct npc_timerevent_list *te = nd->u.scr.timer_event;
int j, k = nd->u.scr.timeramount;
if (te == NULL)
te = (struct npc_timerevent_list *)aMalloc(sizeof(struct npc_timerevent_list));
else
te = (struct npc_timerevent_list *)aRealloc( te, sizeof(struct npc_timerevent_list) * (k+1) );
for (j = 0; j < k; j++) {
if (te[j].timer > t) {
memmove(te+j+1, te+j, sizeof(struct npc_timerevent_list)*(k-j));
break;
}
}
te[j].timer = t;
te[j].pos = pos;
nd->u.scr.timer_event = te;
nd->u.scr.timeramount++;
}
return 0;
}
struct timer_event_data {
int rid; //Attached player for this timer.
int next; //timer index (starts with 0, then goes up to nd->u.scr.timeramount)
int time; //holds total time elapsed for the script from when timer was started to when last time the event triggered.
};
/*==========================================
* triger 'OnTimerXXXX' events
*------------------------------------------*/
static int npc_timerevent(int tid, int64 tick, int id, intptr_t data)
{
int old_rid, old_timer;
int64 old_tick;
struct npc_data *nd = map->id2nd(id);
struct npc_timerevent_list *te;
struct timer_event_data *ted = (struct timer_event_data*)data;
struct map_session_data *sd=NULL;
nullpo_ret(ted);
if( nd == NULL ) {
ShowError("npc_timerevent: NPC not found??\n");
return 0;
}
if (ted->rid && (sd = map->id2sd(ted->rid)) == NULL) {
ShowError("npc_timerevent: Attached player not found.\n");
ers_free(npc->timer_event_ers, ted);
return 0;
}
// These stuffs might need to be restored.
old_rid = nd->u.scr.rid;
old_tick = nd->u.scr.timertick;
old_timer = nd->u.scr.timer;
// Set the values of the timer
nd->u.scr.rid = sd?sd->bl.id:0; //attached rid
nd->u.scr.timertick = tick; //current time tick
nd->u.scr.timer = ted->time; //total time from beginning to now
// Locate the event
te = nd->u.scr.timer_event + ted->next;
// Arrange for the next event
ted->next++;
if( nd->u.scr.timeramount > ted->next )
{
int next = nd->u.scr.timer_event[ ted->next ].timer - nd->u.scr.timer_event[ ted->next - 1 ].timer;
ted->time += next;
if( sd )
sd->npc_timer_id = timer->add(tick+next,npc->timerevent,id,(intptr_t)ted);
else
nd->u.scr.timerid = timer->add(tick+next,npc->timerevent,id,(intptr_t)ted);
}
else
{
if( sd )
sd->npc_timer_id = INVALID_TIMER;
else
nd->u.scr.timerid = INVALID_TIMER;
ers_free(npc->timer_event_ers, ted);
}
// Run the script
script->run_npc(nd->u.scr.script,te->pos,nd->u.scr.rid,nd->bl.id);
nd->u.scr.rid = old_rid; // Attached-rid should be restored anyway.
if( sd )
{ // Restore previous data, only if this timer is a player-attached one.
nd->u.scr.timer = old_timer;
nd->u.scr.timertick = old_tick;
}
return 0;
}
/*==========================================
* Start/Resume NPC timer
*------------------------------------------*/
static int npc_timerevent_start(struct npc_data *nd, int rid)
{
int j;
int64 tick = timer->gettick();
struct map_session_data *sd = NULL; //Player to whom script is attached.
nullpo_ret(nd);
// Check if there is an OnTimer Event
ARR_FIND( 0, nd->u.scr.timeramount, j, nd->u.scr.timer_event[j].timer > nd->u.scr.timer );
if (nd->u.scr.rid > 0 && (sd = map->id2sd(nd->u.scr.rid)) == NULL) {
// Failed to attach timer to this player.
ShowError("npc_timerevent_start: Attached player not found!\n");
return 1;
}
// Check if timer is already started.
if( sd ) {
if( sd->npc_timer_id != INVALID_TIMER )
return 0;
} else if( nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick )
return 0;
if (j < nd->u.scr.timeramount) {
int next;
struct timer_event_data *ted;
// Arrange for the next event
ted = ers_alloc(npc->timer_event_ers, struct timer_event_data);
ted->next = j; // Set event index
ted->time = nd->u.scr.timer_event[j].timer;
next = nd->u.scr.timer_event[j].timer - nd->u.scr.timer;
if( sd )
{
ted->rid = sd->bl.id; // Attach only the player if attachplayerrid was used.
sd->npc_timer_id = timer->add(tick+next,npc->timerevent,nd->bl.id,(intptr_t)ted);
}
else
{
ted->rid = 0;
nd->u.scr.timertick = tick; // Set when timer is started
nd->u.scr.timerid = timer->add(tick+next,npc->timerevent,nd->bl.id,(intptr_t)ted);
}
} else if (!sd) {
nd->u.scr.timertick = tick;
}
return 0;
}
/*==========================================
* Stop NPC timer
*------------------------------------------*/
static int npc_timerevent_stop(struct npc_data *nd)
{
struct map_session_data *sd = NULL;
int *tid;
nullpo_ret(nd);
if (nd->u.scr.rid && (sd = map->id2sd(nd->u.scr.rid)) == NULL) {
ShowError("npc_timerevent_stop: Attached player not found!\n");
return 1;
}
tid = sd?&sd->npc_timer_id:&nd->u.scr.timerid;
if( *tid == INVALID_TIMER && (sd || !nd->u.scr.timertick) ) // Nothing to stop
return 0;
// Delete timer
if (*tid != INVALID_TIMER) {
const struct TimerData *td = timer->get(*tid);
if (td && td->data)
ers_free(npc->timer_event_ers, (void*)td->data);
timer->delete(*tid,npc->timerevent);
*tid = INVALID_TIMER;
}
if (!sd && nd->u.scr.timertick) {
nd->u.scr.timer += DIFF_TICK32(timer->gettick(),nd->u.scr.timertick); // Set 'timer' to the time that has passed since the beginning of the timers
nd->u.scr.timertick = 0; // Set 'tick' to zero so that we know it's off.
}
return 0;
}
/*==========================================
* Aborts a running NPC timer that is attached to a player.
*------------------------------------------*/
static void npc_timerevent_quit(struct map_session_data *sd)
{
const struct TimerData *td;
struct npc_data* nd;
struct timer_event_data *ted;
nullpo_retv(sd);
// Check timer existence
if( sd->npc_timer_id == INVALID_TIMER )
return;
if( !(td = timer->get(sd->npc_timer_id)) )
{
sd->npc_timer_id = INVALID_TIMER;
return;
}
// Delete timer
nd = map->id2nd(td->id);
ted = (struct timer_event_data*)td->data;
timer->delete(sd->npc_timer_id, npc->timerevent);
sd->npc_timer_id = INVALID_TIMER;
// Execute OnTimerQuit
if (nd != NULL) {
char buf[EVENT_NAME_LENGTH];
struct event_data *ev;
snprintf(buf, ARRAYLENGTH(buf), "%s::OnTimerQuit", nd->exname);
ev = (struct event_data*)strdb_get(npc->ev_db, buf);
if( ev && ev->nd != nd )
{
ShowWarning("npc_timerevent_quit: Unable to execute \"OnTimerQuit\", two NPCs have the same event name [%s]!\n",buf);
ev = NULL;
}
if( ev )
{
int old_rid,old_timer;
int64 old_tick;
nullpo_retv(ted);
//Set timer related info.
old_rid = (nd->u.scr.rid == sd->bl.id ? 0 : nd->u.scr.rid); // Detach rid if the last attached player logged off.
old_tick = nd->u.scr.timertick;
old_timer = nd->u.scr.timer;
nd->u.scr.rid = sd->bl.id;
nd->u.scr.timertick = timer->gettick();
nd->u.scr.timer = ted->time;
//Execute label
script->run_npc(nd->u.scr.script,ev->pos,sd->bl.id,nd->bl.id);
//Restore previous data.
nd->u.scr.rid = old_rid;
nd->u.scr.timer = old_timer;
nd->u.scr.timertick = old_tick;
}
}
ers_free(npc->timer_event_ers, ted);
}
/*==========================================
* Get the tick value of an NPC timer
* If it's stopped, return stopped time
*------------------------------------------*/
static int64 npc_gettimerevent_tick(struct npc_data *nd)
{
int64 tick;
nullpo_ret(nd);
// TODO: Get player attached timer's tick. Now we can just get it by using 'getnpctimer' inside OnTimer event.
tick = nd->u.scr.timer; // The last time it's active(start, stop or event trigger)
if( nd->u.scr.timertick ) // It's a running timer
tick += DIFF_TICK(timer->gettick(), nd->u.scr.timertick);
return tick;
}
/*==========================================
* Set tick for running and stopped timer
*------------------------------------------*/
static int npc_settimerevent_tick(struct npc_data *nd, int newtimer)
{
bool flag;
int old_rid;
//struct map_session_data *sd = NULL;
nullpo_ret(nd);
// TODO: Set player attached timer's tick.
old_rid = nd->u.scr.rid;
nd->u.scr.rid = 0;
// Check if timer is started
flag = (nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick);
if( flag ) npc->timerevent_stop(nd);
nd->u.scr.timer = newtimer;
if( flag ) npc->timerevent_start(nd, -1);
nd->u.scr.rid = old_rid;
return 0;
}
static int npc_event_sub(struct map_session_data *sd, struct event_data *ev, const char *eventname)
{
nullpo_retr(2, sd);
nullpo_retr(2, eventname);
if ( sd->npc_id != 0 )
{
//Enqueue the event trigger.
int i;
ARR_FIND( 0, MAX_EVENTQUEUE, i, sd->eventqueue[i][0] == '\0' );
if( i < MAX_EVENTQUEUE )
{
safestrncpy(sd->eventqueue[i],eventname,EVENT_NAME_LENGTH); //Event enqueued.
return 0;
}
ShowWarning("npc_event: player's event queue is full, can't add event '%s' !\n", eventname);
return 1;
}
if( ev->nd->option&OPTION_INVISIBLE )
{
//Disabled npc, shouldn't trigger event.
npc->event_dequeue(sd);
return 2;
}
script->run_npc(ev->nd->u.scr.script,ev->pos,sd->bl.id,ev->nd->bl.id);
return 0;
}
/*==========================================
* NPC processing event type
*------------------------------------------*/
static int npc_event(struct map_session_data *sd, const char *eventname, int ontouch)
{
struct event_data* ev = (struct event_data*)strdb_get(npc->ev_db, eventname);
struct npc_data *nd;
nullpo_ret(sd);
if( ev == NULL || (nd = ev->nd) == NULL ) {
if( !ontouch )
ShowError("npc_event: event not found [%s]\n", eventname);
return ontouch;
}
switch(ontouch) {
case 1:
nd->touching_id = sd->bl.id;
sd->touching_id = nd->bl.id;
break;
case 2:
sd->areanpc_id = nd->bl.id;
break;
}
return npc->event_sub(sd,ev,eventname);
}
/*==========================================
* Sub chk then execute area event type
*------------------------------------------*/
static int npc_touch_areanpc_sub(struct block_list *bl, va_list ap)
{
struct map_session_data *sd;
int pc_id;
char *name;
nullpo_ret(bl);
nullpo_ret((sd = map->id2sd(bl->id)));
pc_id = va_arg(ap,int);
name = va_arg(ap,char*);
if( sd->state.warping )
return 0;
if( pc_ishiding(sd) )
return 0;
if( pc_id == sd->bl.id )
return 0;
npc->event(sd,name,1);
return 1;
}
/*==========================================
* Chk if sd is still touching his assigned npc.
* If not, it unsets it and searches for another player in range.
*------------------------------------------*/
static int npc_touchnext_areanpc(struct map_session_data *sd, bool leavemap)
{
struct npc_data *nd;
short xs, ys;
nullpo_retr(1, sd);
nd = map->id2nd(sd->touching_id);
if( !nd || nd->touching_id != sd->bl.id )
return 1;
xs = nd->u.scr.xs;
ys = nd->u.scr.ys;
if( sd->bl.m != nd->bl.m ||
sd->bl.x < nd->bl.x - xs || sd->bl.x > nd->bl.x + xs ||
sd->bl.y < nd->bl.y - ys || sd->bl.y > nd->bl.y + ys ||
pc_ishiding(sd) || leavemap )
{
char name[EVENT_NAME_LENGTH];
nd->touching_id = sd->touching_id = 0;
snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.ontouch_name);
map->forcountinarea(npc->touch_areanpc_sub,nd->bl.m,nd->bl.x - xs,nd->bl.y - ys,nd->bl.x + xs,nd->bl.y + ys,1,BL_PC,sd->bl.id,name);
}
return 0;
}
/*==========================================
* Exec OnTouch for player if in range of area event
*------------------------------------------*/
static int npc_touch_areanpc(struct map_session_data *sd, int16 m, int16 x, int16 y)
{
int xs,ys;
int f = 1;
int i;
int j, found_warp = 0;
nullpo_retr(1, sd);
Assert_retr(1, m >= 0 && m < map->count);
#if 0 // Why not enqueue it? [Inkfish]
if(sd->npc_id)
return 1;
#endif // 0
for(i=0;ilist[m].npc_num;i++) {
if (map->list[m].npc[i]->option&OPTION_INVISIBLE) {
f=0; // a npc was found, but it is disabled; don't print warning
continue;
}
switch(map->list[m].npc[i]->subtype) {
case WARP:
xs=map->list[m].npc[i]->u.warp.xs;
ys=map->list[m].npc[i]->u.warp.ys;
break;
case SCRIPT:
xs=map->list[m].npc[i]->u.scr.xs;
ys=map->list[m].npc[i]->u.scr.ys;
break;
default:
continue;
}
if( x >= map->list[m].npc[i]->bl.x-xs && x <= map->list[m].npc[i]->bl.x+xs
&& y >= map->list[m].npc[i]->bl.y-ys && y <= map->list[m].npc[i]->bl.y+ys )
break;
}
if( i == map->list[m].npc_num ) {
if( f == 1 ) // no npc found
ShowError("npc_touch_areanpc : stray NPC cell/NPC not found in the block on coordinates '%s',%d,%d\n", map->list[m].name, x, y);
return 1;
}
switch(map->list[m].npc[i]->subtype) {
case WARP:
if( pc_ishiding(sd) || (sd->sc.count && sd->sc.data[SC_CAMOUFLAGE]) )
break; // hidden chars cannot use warps
pc->setpos(sd,map->list[m].npc[i]->u.warp.mapindex,map->list[m].npc[i]->u.warp.x,map->list[m].npc[i]->u.warp.y,CLR_OUTSIGHT);
break;
case SCRIPT:
for (j = i; j < map->list[m].npc_num; j++) {
if (map->list[m].npc[j]->subtype != WARP) {
continue;
}
if ((sd->bl.x >= (map->list[m].npc[j]->bl.x - map->list[m].npc[j]->u.warp.xs)
&& sd->bl.x <= (map->list[m].npc[j]->bl.x + map->list[m].npc[j]->u.warp.xs))
&& (sd->bl.y >= (map->list[m].npc[j]->bl.y - map->list[m].npc[j]->u.warp.ys)
&& sd->bl.y <= (map->list[m].npc[j]->bl.y + map->list[m].npc[j]->u.warp.ys))
) {
if( pc_ishiding(sd) || (sd->sc.count && sd->sc.data[SC_CAMOUFLAGE]) )
break; // hidden chars cannot use warps
pc->setpos(sd,map->list[m].npc[j]->u.warp.mapindex,map->list[m].npc[j]->u.warp.x,map->list[m].npc[j]->u.warp.y,CLR_OUTSIGHT);
found_warp = 1;
break;
}
}
if (found_warp > 0) {
break;
}
if( npc->ontouch_event(sd,map->list[m].npc[i]) > 0 && npc->ontouch2_event(sd,map->list[m].npc[i]) > 0 )
{ // failed to run OnTouch event, so just click the npc
struct unit_data *ud = unit->bl2ud(&sd->bl);
if( ud && ud->walkpath.path_pos < ud->walkpath.path_len )
{ // Since walktimer always == INVALID_TIMER at this time, we stop walking manually. [Inkfish]
clif->fixpos(&sd->bl);
ud->walkpath.path_pos = ud->walkpath.path_len;
}
sd->areanpc_id = map->list[m].npc[i]->bl.id;
npc->click(sd,map->list[m].npc[i]);
}
break;
}
return 0;
}
/*==========================================
* Exec OnUnTouch for player if out range of area event
*------------------------------------------*/
static int npc_untouch_areanpc(struct map_session_data *sd, int16 m, int16 x, int16 y)
{
struct npc_data *nd = NULL;
nullpo_retr(1, sd);
Assert_retr(1, m >= 0 && m < map->count);
if (!sd->areanpc_id)
return 0;
nd = map->id2nd(sd->areanpc_id);
if (nd == NULL) {
sd->areanpc_id = 0;
return 1;
}
npc->onuntouch_event(sd, nd);
sd->areanpc_id = 0;
return 0;
}
// OnTouch NPC or Warp for Mobs
// Return 1 if Warped
static int npc_touch_areanpc2(struct mob_data *md)
{
int i, m, x, y, id;
char eventname[EVENT_NAME_LENGTH];
struct event_data* ev;
int xs, ys;
nullpo_ret(md);
m = md->bl.m;
x = md->bl.x;
y = md->bl.y;
for( i = 0; i < map->list[m].npc_num; i++ ) {
if( map->list[m].npc[i]->option&OPTION_INVISIBLE )
continue;
switch( map->list[m].npc[i]->subtype ) {
case WARP:
if( !( battle_config.mob_warp&1 ) )
continue;
xs = map->list[m].npc[i]->u.warp.xs;
ys = map->list[m].npc[i]->u.warp.ys;
break;
case SCRIPT:
xs = map->list[m].npc[i]->u.scr.xs;
ys = map->list[m].npc[i]->u.scr.ys;
break;
default:
continue; // Keep Searching
}
if( x >= map->list[m].npc[i]->bl.x-xs && x <= map->list[m].npc[i]->bl.x+xs && y >= map->list[m].npc[i]->bl.y-ys && y <= map->list[m].npc[i]->bl.y+ys ) {
// In the npc touch area
switch( map->list[m].npc[i]->subtype ) {
case WARP:
xs = map->mapindex2mapid(map->list[m].npc[i]->u.warp.mapindex);
if( m < 0 )
break; // Cannot Warp between map servers
if( unit->warp(&md->bl, xs, map->list[m].npc[i]->u.warp.x, map->list[m].npc[i]->u.warp.y, CLR_OUTSIGHT) == 0 )
return 1; // Warped
break;
case SCRIPT:
if( map->list[m].npc[i]->bl.id == md->areanpc_id )
break; // Already touch this NPC
snprintf(eventname, ARRAYLENGTH(eventname), "%s::OnTouchNPC", map->list[m].npc[i]->exname);
if( (ev = (struct event_data*)strdb_get(npc->ev_db, eventname)) == NULL || ev->nd == NULL )
break; // No OnTouchNPC Event
md->areanpc_id = map->list[m].npc[i]->bl.id;
id = md->bl.id; // Stores Unique ID
script->run_npc(ev->nd->u.scr.script, ev->pos, md->bl.id, ev->nd->bl.id);
if( map->id2md(id) == NULL ) return 1; // Not Warped, but killed
break;
}
return 0;
}
}
return 0;
}
//Checks if there are any NPC on-touch objects on the given range.
//Flag determines the type of object to check for:
//&1: NPC Warps
//&2: NPCs with on-touch events.
static int npc_check_areanpc(int flag, int16 m, int16 x, int16 y, int16 range)
{
int i;
int x0,y0,x1,y1;
int xs,ys;
Assert_retr(1, m >= 0 && m < map->count);
if (range < 0) return 0;
x0 = max(x-range, 0);
y0 = max(y-range, 0);
x1 = min(x+range, map->list[m].xs-1);
y1 = min(y+range, map->list[m].ys-1);
//First check for npc_cells on the range given
i = 0;
for (ys = y0; ys <= y1 && !i; ys++) {
for(xs = x0; xs <= x1 && !i; xs++) {
if (map->getcell(m, NULL, xs, ys, CELL_CHKNPC))
i = 1;
}
}
if (!i) return 0; //No NPC_CELLs.
//Now check for the actual NPC on said range.
for(i=0;ilist[m].npc_num;i++) {
if (map->list[m].npc[i]->option&OPTION_INVISIBLE)
continue;
switch(map->list[m].npc[i]->subtype) {
case WARP:
if (!(flag&1))
continue;
xs=map->list[m].npc[i]->u.warp.xs;
ys=map->list[m].npc[i]->u.warp.ys;
break;
case SCRIPT:
if (!(flag&2))
continue;
xs=map->list[m].npc[i]->u.scr.xs;
ys=map->list[m].npc[i]->u.scr.ys;
break;
default:
continue;
}
if( x1 >= map->list[m].npc[i]->bl.x-xs && x0 <= map->list[m].npc[i]->bl.x+xs
&& y1 >= map->list[m].npc[i]->bl.y-ys && y0 <= map->list[m].npc[i]->bl.y+ys )
break; // found a npc
}
if (i==map->list[m].npc_num)
return 0;
return (map->list[m].npc[i]->bl.id);
}
/*==========================================
* Chk if player not too far to access the npc.
* Returns npc_data (success) or NULL (fail).
*------------------------------------------*/
static struct npc_data *npc_checknear(struct map_session_data *sd, struct block_list *bl)
{
struct npc_data *nd = BL_CAST(BL_NPC, bl);
int distance = AREA_SIZE + 1;
nullpo_retr(NULL, sd);
if (nd == NULL)
return NULL;
if (sd->npc_id == bl->id)
return nd;
if (nd->class_<0) //Class-less npc, enable click from anywhere.
return nd;
if (distance > nd->area_size)
distance = nd->area_size;
nullpo_retr(NULL, bl);
if (bl->m != sd->bl.m ||
bl->x < sd->bl.x - distance || bl->x > sd->bl.x + distance ||
bl->y < sd->bl.y - distance || bl->y > sd->bl.y + distance)
{
return NULL;
}
return nd;
}
/*==========================================
* Make NPC talk in global chat (like npctalk)
*------------------------------------------*/
static int npc_globalmessage(const char *name, const char *mes)
{
struct npc_data* nd = npc->name2id(name);
char temp[100];
if (!nd)
return 0;
nullpo_ret(name);
nullpo_ret(mes);
snprintf(temp, sizeof(temp), "%s : %s", name, mes);
clif->GlobalMessage(&nd->bl,temp);
return 0;
}
// MvP tomb [GreenBox]
static void run_tomb(struct map_session_data *sd, struct npc_data *nd)
{
char buffer[200];
char time[10];
nullpo_retv(nd);
sd->npc_id = nd->bl.id;
strftime(time, sizeof(time), "%H:%M", localtime(&nd->u.tomb.kill_time));
// TODO: Find exact color?
snprintf(buffer, sizeof(buffer), msg_sd(sd,857), nd->u.tomb.md->db->name); // "[ ^EE0000%s^000000 ]"
clif->scriptmes(sd, nd->bl.id, buffer);
clif->scriptmes(sd, nd->bl.id, msg_sd(sd,858)); // "Has met its demise"
snprintf(buffer, sizeof(buffer), msg_sd(sd,859), time); // "Time of death : ^EE0000%s^000000"
clif->scriptmes(sd, nd->bl.id, buffer);
clif->scriptmes(sd, nd->bl.id, msg_sd(sd,860)); // "Defeated by"
snprintf(buffer, sizeof(buffer), msg_sd(sd,861), nd->u.tomb.killer_name[0] ? nd->u.tomb.killer_name : msg_sd(sd,15)); // "[^EE0000%s^000000]" / "Unknown"
clif->scriptmes(sd, nd->bl.id, buffer);
clif->scriptclose(sd, nd->bl.id);
}
/*==========================================
* NPC 1st call when clicking on npc
* Do specific action for NPC type (openshop, run scripts...)
*------------------------------------------*/
static int npc_click(struct map_session_data *sd, struct npc_data *nd)
{
nullpo_retr(1, sd);
// This usually happens when the player clicked on a NPC that has the view id
// of a mob, to activate this kind of npc it's needed to be in a 2,2 range
// from it. If the OnTouch area of a npc, coincides with the 2,2 range of
// another it's expected that the OnTouch event be put first in stack, because
// unit_walktoxy_timer is executed before any other function in this case.
// So it's best practice to put an 'end;' before OnTouch events in npcs that
// have view ids of mobs to avoid this "issue" [Panikon]
if (sd->npc_id != 0) {
// The player clicked a npc after entering an OnTouch area
if( sd->areanpc_id != sd->npc_id )
ShowError("npc_click: npc_id != 0\n");
return 1;
}
if( !nd )
return 1;
if ((nd = npc->checknear(sd,&nd->bl)) == NULL)
return 1;
//Hidden/Disabled npc.
if (nd->class_ < 0 || nd->option&(OPTION_INVISIBLE|OPTION_HIDE))
return 1;
switch(nd->subtype) {
case SHOP:
clif->npcbuysell(sd,nd->bl.id);
break;
case CASHSHOP:
clif->cashshop_show(sd,nd);
break;
case SCRIPT:
if( nd->u.scr.shop && nd->u.scr.shop->items && nd->u.scr.trader ) {
if( !npc->trader_open(sd,nd) )
return 1;
} else
script->run_npc(nd->u.scr.script,0,sd->bl.id,nd->bl.id);
break;
case TOMB:
npc->run_tomb(sd,nd);
break;
}
return 0;
}
/**
* Validates a character's script related data and (re-)runs the script if validation was successful.
*
* Is called when:
* - The Next/Close button was clicked.
* - A menu option was selected.
* - A value was entered by input() script command.
* - A progress bar has reached 100%.
* - The character timed out because of idling.
*
* @param sd The character's session data.
* @param id The NPC ID.
* @param closing Whether the script is closing, or not.
* @return 0 on success, otherwise 1.
*
**/
static int npc_scriptcont(struct map_session_data *sd, int id, bool closing)
{
nullpo_retr(1, sd);
struct block_list *target = map->id2bl(id);
#ifdef SECURE_NPCTIMEOUT
if (sd->npc_idle_timer != INVALID_TIMER) { /// Not yet timed out.
#endif
if (id != sd->npc_id) {
struct npc_data *nd_sd = map->id2nd(sd->npc_id);
struct npc_data *nd = BL_CAST(BL_NPC, target);
ShowDebug("npc_scriptcont: %s (sd->npc_id=%d) is not %s (id=%d).\n",
(nd_sd != NULL) ? nd_sd->name : "'Unknown NPC'", sd->npc_id,
(nd != NULL) ? nd->name : "'Unknown NPC'", id);
return 1;
}
#ifdef SECURE_NPCTIMEOUT
}
#endif
if (id != npc->fake_nd->bl.id) { /// Not an item script.
if (sd->state.npc_unloaded != 0) {
sd->state.npc_unloaded = 0;
} else if (npc->checknear(sd, target) == NULL) {
ShowWarning("npc_scriptcont: Failed npc->checknear test.\n");
return 1;
}
}
#ifdef SECURE_NPCTIMEOUT
sd->npc_idle_tick = timer->gettick(); /// Update the last NPC iteration.
#endif
/// WPE can get to this point with a progressbar; we deny it.
if (sd->progressbar.npc_id != 0 && DIFF_TICK(sd->progressbar.timeout, timer->gettick()) > 0)
return 1;
if (sd->st == NULL) {
sd->npc_id = 0;
return 1;
}
if (closing && sd->st->state == CLOSE)
sd->st->state = END;
script->run_main(sd->st);
return 0;
}
/*==========================================
* Chk if valid call then open buy or selling list
*------------------------------------------*/
static int npc_buysellsel(struct map_session_data *sd, int id, int type)
{
struct npc_data *nd;
nullpo_retr(1, sd);
if ((nd = npc->checknear(sd,map->id2bl(id))) == NULL)
return 1;
if ( nd->subtype != SHOP && !(nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->items) ) {
if( nd->subtype == SCRIPT )
ShowError("npc_buysellsel: trader '%s' has no shop list!\n",nd->exname);
else
ShowError("npc_buysellsel: no such shop npc %d (%s)\n",id,nd->exname);
if (sd->npc_id == id)
sd->npc_id = 0;
return 1;
}
if (nd->option & OPTION_INVISIBLE) // can't buy if npc is not visible (hack?)
return 1;
if( nd->class_ < 0 && !sd->state.callshop ) {// not called through a script and is not a visible NPC so an invalid call
return 1;
}
// reset the callshop state for future calls
sd->state.callshop = 0;
sd->npc_shopid = id;
if (type==0) {
clif->buylist(sd,nd);
} else {
clif->selllist(sd);
}
return 0;
}
/*==========================================
* Cash Shop Buy List
*------------------------------------------*/
static int npc_cashshop_buylist(struct map_session_data *sd, int points, struct itemlist *item_list)
{
int i, j, new_, w, vt;
struct npc_data *nd = NULL;
struct npc_item_list *shop = NULL;
unsigned short shop_size = 0;
nullpo_retr(ERROR_TYPE_SYSTEM, sd);
nullpo_retr(ERROR_TYPE_SYSTEM, item_list);
if( sd->state.trading )
return ERROR_TYPE_EXCHANGE;
if (VECTOR_LENGTH(*item_list) <= 0)
return ERROR_TYPE_ITEM_ID;
if( points < 0 )
return ERROR_TYPE_MONEY;
nd = map->id2nd(sd->npc_shopid);
if (nd == NULL)
return ERROR_TYPE_NPC;
if( nd->subtype != CASHSHOP ) {
if (nd->subtype == SCRIPT && nd->u.scr.shop &&
nd->u.scr.shop->type != NST_ZENY &&
nd->u.scr.shop->type != NST_MARKET &&
nd->u.scr.shop->type != NST_BARTER &&
nd->u.scr.shop->type != NST_EXPANDED_BARTER) {
shop = nd->u.scr.shop->item;
shop_size = nd->u.scr.shop->items;
} else {
return ERROR_TYPE_NPC;
}
} else {
shop = nd->u.shop.shop_item;
shop_size = nd->u.shop.count;
}
new_ = 0;
w = 0;
vt = 0; // Global Value
// Validating Process ----------------------------------------------------
for (i = 0; i < VECTOR_LENGTH(*item_list); i++) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
if (!itemdb->exists(entry->id) || entry->amount <= 0)
return ERROR_TYPE_ITEM_ID;
ARR_FIND(0,shop_size,j,shop[j].nameid == entry->id);
if (j == shop_size || shop[j].value <= 0)
return ERROR_TYPE_ITEM_ID;
if (!itemdb->isstackable(entry->id) && entry->amount > 1) {
ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, entry->amount, entry->id);
entry->amount = 1;
}
switch (pc->checkadditem(sd, entry->id, entry->amount)) {
case ADDITEM_NEW:
new_++;
break;
case ADDITEM_OVERAMOUNT:
return ERROR_TYPE_INVENTORY_WEIGHT;
}
vt += shop[j].value * entry->amount;
w += itemdb_weight(entry->id) * entry->amount;
}
if( w + sd->weight > sd->max_weight )
return ERROR_TYPE_INVENTORY_WEIGHT;
if( pc->inventoryblank(sd) < new_ )
return ERROR_TYPE_INVENTORY_WEIGHT;
if( points > vt ) points = vt;
// Payment Process ----------------------------------------------------
if( nd->subtype == SCRIPT && nd->u.scr.shop->type == NST_CUSTOM ) {
if( !npc->trader_pay(nd,sd,vt,points) )
return ERROR_TYPE_MONEY;
} else {
if( sd->kafraPoints < points || sd->cashPoints < (vt - points) )
return ERROR_TYPE_MONEY;
pc->paycash(sd,vt,points);
}
// Delivery Process ----------------------------------------------------
for (i = 0; i < VECTOR_LENGTH(*item_list); i++) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
struct item item_tmp;
memset(&item_tmp,0,sizeof(item_tmp));
if (!pet->create_egg(sd, entry->id)) {
item_tmp.nameid = entry->id;
item_tmp.identify = 1;
pc->additem(sd, &item_tmp, entry->amount, LOG_TYPE_NPC);
}
}
return ERROR_TYPE_NONE;
}
//npc_buylist for script-controlled shops.
static int npc_buylist_sub(struct map_session_data *sd, struct itemlist *item_list, struct npc_data *nd)
{
char npc_ev[EVENT_NAME_LENGTH];
int i;
int key_nameid = 0;
int key_amount = 0;
nullpo_ret(item_list);
nullpo_ret(nd);
// discard old contents
script->cleararray_pc(sd, "@bought_nameid", (void*)0);
script->cleararray_pc(sd, "@bought_quantity", (void*)0);
// save list of bought items
for (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);
}
// invoke event
snprintf(npc_ev, ARRAYLENGTH(npc_ev), "%s::OnBuyItem", nd->exname);
npc->event(sd, npc_ev, 0);
return 0;
}
/**
* Loads persistent NPC Market Data from SQL
**/
static void npc_market_fromsql(void)
{
struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);
char name[NAME_LENGTH+1];
int itemid;
int amount;
if ( SQL_ERROR == SQL->StmtPrepare(stmt, "SELECT `name`, `itemid`, `amount` FROM `%s`", map->npc_market_data_db)
|| SQL_ERROR == SQL->StmtExecute(stmt)
) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
return;
}
SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof name, NULL, NULL);
SQL->StmtBindColumn(stmt, 1, SQLDT_INT, &itemid, sizeof itemid, NULL, NULL);
SQL->StmtBindColumn(stmt, 2, SQLDT_INT, &amount, sizeof amount, NULL, NULL);
while ( SQL_SUCCESS == SQL->StmtNextRow(stmt) ) {
struct npc_data *nd = NULL;
unsigned short i;
if( !(nd = npc->name2id(name)) ) {
ShowError("npc_market_fromsql: NPC '%s' not found! skipping...\n",name);
npc->market_delfromsql_sub(name, INT_MAX);
continue;
} else if (nd->subtype != SCRIPT || !nd->u.scr.shop || !nd->u.scr.shop->items || nd->u.scr.shop->type != NST_MARKET) {
ShowError("npc_market_fromsql: NPC '%s' is not proper for market, skipping...\n",name);
npc->market_delfromsql_sub(name, INT_MAX);
continue;
}
for(i = 0; i < nd->u.scr.shop->items; i++) {
if( nd->u.scr.shop->item[i].nameid == itemid ) {
nd->u.scr.shop->item[i].qty = amount;
break;
}
}
if( i == nd->u.scr.shop->items ) {
ShowError("npc_market_fromsql: NPC '%s' does not sell item %d (qty %d), deleting...\n",name,itemid,amount);
npc->market_delfromsql_sub(name, itemid);
continue;
}
}
SQL->StmtFree(stmt);
}
/**
* Saves persistent NPC Market Data into SQL
**/
static void npc_market_tosql(struct npc_data *nd, int index)
{
nullpo_retv(nd);
Assert_retv(index >= 0 && index < nd->u.scr.shop->items);
if (SQL_ERROR == SQL->Query(map->mysql_handle, "REPLACE INTO `%s` VALUES ('%s','%d','%d')",
map->npc_market_data_db, nd->exname, nd->u.scr.shop->item[index].nameid, nd->u.scr.shop->item[index].qty))
Sql_ShowDebug(map->mysql_handle);
}
/**
* Removes persistent NPC Market Data from SQL
*/
static void npc_market_delfromsql_sub(const char *npcname, int index)
{
if (index == INT_MAX ) {
if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s'", map->npc_market_data_db, npcname) )
Sql_ShowDebug(map->mysql_handle);
} else {
if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s' AND `itemid`='%d' LIMIT 1",
map->npc_market_data_db, npcname, index) )
Sql_ShowDebug(map->mysql_handle);
}
}
/**
* Removes persistent NPC Market Data from SQL
**/
static void npc_market_delfromsql(struct npc_data *nd, int index)
{
nullpo_retv(nd);
Assert_retv(index == INT_MAX || (index >= 0 && index < nd->u.scr.shop->items));
npc->market_delfromsql_sub(nd->exname, index == INT_MAX ? index : nd->u.scr.shop->item[index].nameid);
}
/**
* Loads persistent NPC Barter Data from SQL
**/
static void npc_barter_fromsql(void)
{
struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);
char name[NAME_LENGTH + 1];
int itemid;
int amount;
int removeId;
int removeAmount;
if (SQL_ERROR == SQL->StmtPrepare(stmt, "SELECT `name`, `itemId`, `amount`, `priceId`, `priceAmount` FROM `%s`", map->npc_barter_data_db)
|| SQL_ERROR == SQL->StmtExecute(stmt)
) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
return;
}
SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof name, NULL, NULL);
SQL->StmtBindColumn(stmt, 1, SQLDT_INT, &itemid, sizeof itemid, NULL, NULL);
SQL->StmtBindColumn(stmt, 2, SQLDT_UINT32, &amount, sizeof amount, NULL, NULL);
SQL->StmtBindColumn(stmt, 3, SQLDT_INT, &removeId, sizeof removeId, NULL, NULL);
SQL->StmtBindColumn(stmt, 4, SQLDT_INT, &removeAmount, sizeof removeAmount, NULL, NULL);
while (SQL_SUCCESS == SQL->StmtNextRow(stmt)) {
struct npc_data *nd = NULL;
unsigned short i;
if (!(nd = npc->name2id(name))) {
ShowError("npc_barter_fromsql: NPC '%s' not found! skipping...\n",name);
npc->barter_delfromsql_sub(name, INT_MAX, 0, 0);
continue;
} else if (nd->subtype != SCRIPT || !nd->u.scr.shop || !nd->u.scr.shop->items || nd->u.scr.shop->type != NST_BARTER) {
ShowError("npc_barter_fromsql: NPC '%s' is not proper for barter, skipping...\n",name);
npc->barter_delfromsql_sub(name, INT_MAX, 0, 0);
continue;
}
for (i = 0; i < nd->u.scr.shop->items; i++) {
struct npc_item_list *const item = &nd->u.scr.shop->item[i];
if (item->nameid == itemid && item->value == removeId && item->value2 == removeAmount) {
item->qty = amount;
break;
}
}
if (i == nd->u.scr.shop->items) {
ShowError("npc_barter_fromsql: NPC '%s' does not sell item %d (qty %d), deleting...\n", name, itemid, amount);
npc->barter_delfromsql_sub(name, itemid, removeId, removeAmount);
continue;
}
}
SQL->StmtFree(stmt);
}
/**
* Saves persistent NPC Barter Data into SQL
**/
static void npc_barter_tosql(struct npc_data *nd, int index)
{
nullpo_retv(nd);
Assert_retv(index >= 0 && index < nd->u.scr.shop->items);
const struct npc_item_list *const item = &nd->u.scr.shop->item[index];
if (item->qty == -1)
return;
if (SQL_ERROR == SQL->Query(map->mysql_handle, "REPLACE INTO `%s` VALUES ('%s', '%d', '%d', '%u', '%d')",
map->npc_barter_data_db, nd->exname, item->nameid, item->qty, item->value, item->value2)) {
Sql_ShowDebug(map->mysql_handle);
}
}
/**
* Removes persistent NPC Barter Data from SQL
*/
static void npc_barter_delfromsql_sub(const char *npcname, int itemId, int itemId2, int amount2)
{
if (itemId == INT_MAX) {
if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s'", map->npc_barter_data_db, npcname))
Sql_ShowDebug(map->mysql_handle);
} else {
if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s' AND `itemId`='%d' AND `priceId`='%d' AND `priceAmount`='%d' LIMIT 1",
map->npc_barter_data_db, npcname, itemId, itemId2, amount2)) {
Sql_ShowDebug(map->mysql_handle);
}
}
}
/**
* Removes persistent NPC Barter Data from SQL
**/
static void npc_barter_delfromsql(struct npc_data *nd, int index)
{
nullpo_retv(nd);
if (index == INT_MAX) {
npc->barter_delfromsql_sub(nd->exname, INT_MAX, 0, 0);
} else {
Assert_retv(index >= 0 && index < nd->u.scr.shop->items);
const struct npc_item_list *const item = &nd->u.scr.shop->item[index];
npc->barter_delfromsql_sub(nd->exname, item->nameid, item->value, item->value2);
}
}
/**
* Loads persistent NPC Expanded Barter Data from SQL
**/
static void npc_expanded_barter_fromsql(void)
{
struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle);
char name[NAME_LENGTH + 1];
int itemid;
int amount;
int zeny;
StringBuf buf;
StrBuf->Init(&buf);
StrBuf->AppendStr(&buf, "SELECT `name`, `itemId`, `amount`, `zeny`");
for (int k = 1; k < 11; k ++) {
StrBuf->Printf(&buf, ", `currencyId%d`, `currencyAmount%d`, `currencyRefine%d`", k, k, k);
}
StrBuf->Printf(&buf, " FROM `%s`", map->npc_expanded_barter_data_db);
if (SQL_ERROR == SQL->StmtPrepareStr(stmt, StrBuf->Value(&buf))
|| SQL_ERROR == SQL->StmtExecute(stmt)
) {
SqlStmt_ShowDebug(stmt);
SQL->StmtFree(stmt);
StrBuf->Destroy(&buf);
return;
}
struct npc_barter_currency tempCurrency[10];
SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof name, NULL, NULL);
SQL->StmtBindColumn(stmt, 1, SQLDT_INT, &itemid, sizeof itemid, NULL, NULL);
SQL->StmtBindColumn(stmt, 2, SQLDT_UINT32, &amount, sizeof amount, NULL, NULL);
SQL->StmtBindColumn(stmt, 3, SQLDT_UINT32, &zeny, sizeof zeny, NULL, NULL);
for (int k = 0; k < 10; k ++) {
SQL->StmtBindColumn(stmt, k * 3 + 4, SQLDT_INT, &tempCurrency[k].nameid, sizeof tempCurrency[k].nameid, NULL, NULL);
SQL->StmtBindColumn(stmt, k * 3 + 5, SQLDT_INT, &tempCurrency[k].amount, sizeof tempCurrency[k].amount, NULL, NULL);
SQL->StmtBindColumn(stmt, k * 3 + 6, SQLDT_INT, &tempCurrency[k].refine, sizeof tempCurrency[k].refine, NULL, NULL);
}
while (SQL_SUCCESS == SQL->StmtNextRow(stmt)) {
struct npc_data *nd = NULL;
unsigned short i;
if ((nd = npc->name2id(name)) == NULL) {
ShowError("npc_expanded_barter_fromsql: NPC '%s' not found! skipping...\n",name);
npc->expanded_barter_delfromsql_sub(name, INT_MAX, 0, 0, NULL);
continue;
} else if (nd->subtype != SCRIPT || nd->u.scr.shop == NULL || nd->u.scr.shop->items == 0 || nd->u.scr.shop->type != NST_EXPANDED_BARTER) {
ShowError("npc_expanded_barter_fromsql: NPC '%s' is not proper for barter, skipping...\n",name);
npc->expanded_barter_delfromsql_sub(name, INT_MAX, 0, 0, NULL);
continue;
}
for (i = 0; i < nd->u.scr.shop->items; i++) {
struct npc_item_list *const item = &nd->u.scr.shop->item[i];
if (item->nameid == itemid && item->value == zeny) {
int count = nd->u.scr.shop->item[i].value2;
if (count > 10)
count = 10;
int curIndex;
for (curIndex = 0; curIndex < count; curIndex ++) {
struct npc_barter_currency *currency = &nd->u.scr.shop->item[i].currency[curIndex];
struct npc_barter_currency *currency2 = &tempCurrency[curIndex];
if (currency->nameid != currency2->nameid ||
currency->amount != currency2->amount ||
currency->refine != currency2->refine) {
break;
}
}
if (curIndex == count) {
item->qty = amount;
break;
}
}
}
if (i == nd->u.scr.shop->items) {
ShowError("npc_expanded_barter_fromsql: NPC '%s' does not sell item %d (qty %d), deleting...\n", name, itemid, amount);
npc->expanded_barter_delfromsql_sub(name, itemid, zeny, 10, &tempCurrency[0]);
continue;
}
}
SQL->StmtFree(stmt);
StrBuf->Destroy(&buf);
}
/**
* Saves persistent NPC Expanded Barter Data into SQL
**/
static void npc_expanded_barter_tosql(struct npc_data *nd, int index)
{
nullpo_retv(nd);
Assert_retv(index >= 0 && index < nd->u.scr.shop->items);
const struct npc_item_list *const item = &nd->u.scr.shop->item[index];
if (item->qty == -1)
return;
npc->expanded_barter_delfromsql(nd, index);
StringBuf buf;
StrBuf->Init(&buf);
StrBuf->Printf(&buf, "INSERT INTO `%s` VALUES ('%s', '%d', '%d', '%u'", map->npc_expanded_barter_data_db, nd->exname, item->nameid, item->qty, item->value);
int currencyCount = item->value2;
if (currencyCount > 10)
currencyCount = 10;
int k;
for (k = 0; k < currencyCount; k++) {
struct npc_barter_currency *currency = &item->currency[k];
StrBuf->Printf(&buf, ", '%d', '%d', '%d'", currency->nameid, currency->amount, currency->refine);
}
for (; k < 10; k ++) {
StrBuf->Printf(&buf, ", '0', '0', '0'");
}
StrBuf->AppendStr(&buf, ")");
if (SQL_ERROR == SQL->QueryStr(map->mysql_handle, StrBuf->Value(&buf))) {
Sql_ShowDebug(map->mysql_handle);
}
StrBuf->Destroy(&buf);
}
/**
* Removes persistent NPC Expanded Barter Data from SQL
*/
static void npc_expanded_barter_delfromsql_sub(const char *npcname, int itemId, int zeny, int currencyCount, struct npc_barter_currency* currency)
{
if (itemId == INT_MAX) {
if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s'", map->npc_expanded_barter_data_db, npcname))
Sql_ShowDebug(map->mysql_handle);
} else {
StringBuf buf;
StrBuf->Init(&buf);
StrBuf->Printf(&buf, "DELETE FROM `%s` WHERE `name`='%s' AND `itemId`='%d' AND `zeny`='%d'",
map->npc_expanded_barter_data_db, npcname, itemId, zeny);
int k = 0;
if (currencyCount > 10)
currencyCount = 10;
for (k = 0; k < currencyCount; k++) {
struct npc_barter_currency *currency1 = ¤cy[k];
StrBuf->Printf(&buf, " AND currencyId%d='%d' and currencyAmount%d='%d' and currencyRefine%d='%d'",
k + 1, currency1->nameid, k + 1, currency1->amount, k + 1, currency1->refine);
}
for (; k < 10; k ++) {
StrBuf->Printf(&buf, " AND currencyId%d='0' and currencyAmount%d='0' and currencyRefine%d='0'",
k + 1, k + 1, k + 1);
}
StrBuf->AppendStr(&buf, " LIMIT 1");
if (SQL_ERROR == SQL->QueryStr(map->mysql_handle, StrBuf->Value(&buf))) {
Sql_ShowDebug(map->mysql_handle);
}
StrBuf->Destroy(&buf);
}
}
/**
* Removes persistent NPC Expanded Barter Data from SQL
**/
static void npc_expanded_barter_delfromsql(struct npc_data *nd, int index)
{
nullpo_retv(nd);
if (index == INT_MAX) {
npc->expanded_barter_delfromsql_sub(nd->exname, INT_MAX, 0, 0, NULL);
} else {
Assert_retv(index >= 0 && index < nd->u.scr.shop->items);
const struct npc_item_list *const item = &nd->u.scr.shop->item[index];
npc->expanded_barter_delfromsql_sub(nd->exname, item->nameid, item->value, item->value2, &item->currency[0]);
}
}
/**
* Judges whether to allow and spawn a trader's window.
**/
static bool npc_trader_open(struct map_session_data *sd, struct npc_data *nd)
{
nullpo_retr(false, sd);
nullpo_retr(false, nd);
if( !nd->u.scr.shop || !nd->u.scr.shop->items )
return false;
switch( nd->u.scr.shop->type ) {
case NST_ZENY:
sd->state.callshop = 1;
clif->npcbuysell(sd,nd->bl.id);
return true;/* we skip sd->npc_shopid, npc->buysell will set it then when the player selects */
case NST_MARKET: {
unsigned short i;
for(i = 0; i < nd->u.scr.shop->items; i++) {
if( nd->u.scr.shop->item[i].qty )
break;
}
/* nothing to display, no items available */
if (i == nd->u.scr.shop->items) {
clif->messagecolor_self(sd->fd, COLOR_RED, msg_sd(sd,881));
return false;
}
clif->npc_market_open(sd,nd);
}
break;
case NST_BARTER:
clif->npc_barter_open(sd, nd);
break;
case NST_EXPANDED_BARTER:
clif->npc_expanded_barter_open(sd, nd);
break;
default:
clif->cashshop_show(sd,nd);
break;
}
sd->npc_shopid = nd->bl.id;
return true;
}
/**
* Creates (npc_data)->u.scr.shop and updates all duplicates across the server to match the created pointer
*
* @param master id of the original npc
**/
static void npc_trader_update(int master)
{
struct DBIterator *iter;
struct block_list* bl;
struct npc_data *master_nd = map->id2nd(master);
CREATE(master_nd->u.scr.shop,struct npc_shop_data,1);
iter = db_iterator(map->id_db);
for (bl = dbi_first(iter); dbi_exists(iter); bl = dbi_next(iter)) {
if (bl->type == BL_NPC) {
struct npc_data *nd = BL_UCAST(BL_NPC, bl);
if (nd->src_id == master) {
nd->u.scr.shop = master_nd->u.scr.shop;
}
}
}
dbi_destroy(iter);
}
/**
* Tries to issue a CountFunds event to the shop.
*
* @param nd shop
* @param sd player
**/
static void npc_trader_count_funds(struct npc_data *nd, struct map_session_data *sd)
{
char evname[EVENT_NAME_LENGTH];
struct event_data *ev = NULL;
nullpo_retv(nd);
nullpo_retv(sd);
npc->trader_funds[0] = npc->trader_funds[1] = 0;/* clear */
switch( nd->u.scr.shop->type ) {
case NST_CASH:
npc->trader_funds[0] = sd->cashPoints;
npc->trader_funds[1] = sd->kafraPoints;
return;
case NST_CUSTOM:
break;
default:
ShowError("npc_trader_count_funds: unsupported shop type %d\n",nd->u.scr.shop->type);
return;
}
snprintf(evname, EVENT_NAME_LENGTH, "%s::OnCountFunds",nd->exname);
if ( (ev = strdb_get(npc->ev_db, evname)) )
script->run_npc(ev->nd->u.scr.script, ev->pos, sd->bl.id, ev->nd->bl.id);
else
ShowError("npc_trader_count_funds: '%s' event '%s' not found, operation failed\n",nd->exname,evname);
/* the callee will rely on npc->trader_funds, upon success script->run updates them */
}
/**
* Tries to issue a payment to the NPC Event capable of handling it
*
* @param nd shop
* @param sd player
* @param price total cost
* @param points the amount input in the shop by the user to use from the secondary currency (if any is being employed)
*
* @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)
{
char evname[EVENT_NAME_LENGTH];
struct event_data *ev = NULL;
nullpo_retr(false, nd);
nullpo_retr(false, sd);
npc->trader_ok = false;/* clear */
snprintf(evname, EVENT_NAME_LENGTH, "%s::OnPayFunds",nd->exname);
if ( (ev = strdb_get(npc->ev_db, evname)) ) {
pc->setreg(sd,script->add_variable("@price"),price);
pc->setreg(sd,script->add_variable("@points"),points);
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);
return npc->trader_ok;/* run script will deal with it */
}
/*==========================================
* Cash Shop Buy
*------------------------------------------*/
static int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, int points)
{
struct npc_data *nd = NULL;
struct item_data *item;
struct npc_item_list *shop = NULL;
int i, price, w;
unsigned short shop_size = 0;
nullpo_retr(ERROR_TYPE_SYSTEM, sd);
if( amount <= 0 )
return ERROR_TYPE_ITEM_ID;
if( points < 0 )
return ERROR_TYPE_MONEY;
if( sd->state.trading )
return ERROR_TYPE_EXCHANGE;
nd = map->id2nd(sd->npc_shopid);
if (nd == NULL)
return ERROR_TYPE_NPC;
if( (item = itemdb->exists(nameid)) == NULL )
return ERROR_TYPE_ITEM_ID; // Invalid Item
if( nd->subtype != CASHSHOP ) {
if (nd->subtype == SCRIPT && nd->u.scr.shop &&
nd->u.scr.shop->type != NST_ZENY &&
nd->u.scr.shop->type != NST_MARKET &&
nd->u.scr.shop->type != NST_BARTER &&
nd->u.scr.shop->type != NST_EXPANDED_BARTER) {
shop = nd->u.scr.shop->item;
shop_size = nd->u.scr.shop->items;
} else {
return ERROR_TYPE_NPC;
}
} else {
shop = nd->u.shop.shop_item;
shop_size = nd->u.shop.count;
}
ARR_FIND(0, shop_size, i, shop[i].nameid == nameid);
if( i == shop_size )
return ERROR_TYPE_ITEM_ID;
if( shop[i].value <= 0 )
return ERROR_TYPE_ITEM_ID;
if(!itemdb->isstackable(nameid) && amount > 1) {
ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
amount = 1;
}
switch( pc->checkadditem(sd, nameid, amount) ) {
case ADDITEM_NEW:
if( pc->inventoryblank(sd) == 0 )
return ERROR_TYPE_INVENTORY_WEIGHT;
break;
case ADDITEM_OVERAMOUNT:
return ERROR_TYPE_INVENTORY_WEIGHT;
}
w = item->weight * amount;
if( w + sd->weight > sd->max_weight )
return ERROR_TYPE_INVENTORY_WEIGHT;
if ((int64)shop[i].value * amount > INT_MAX) {
ShowWarning("npc_cashshop_buy: Item '%s' (%d) price overflow attempt!\n", item->name, nameid);
ShowDebug("(NPC:'%s' (%s,%d,%d), player:'%s' (%d/%d), value:%u, amount:%d)\n",
nd->exname, map->list[nd->bl.m].name, nd->bl.x, nd->bl.y,
sd->status.name, sd->status.account_id, sd->status.char_id,
shop[i].value, amount);
return ERROR_TYPE_ITEM_ID;
}
price = shop[i].value * amount;
if( points > price )
points = price;
if( nd->subtype == SCRIPT && nd->u.scr.shop->type == NST_CUSTOM ) {
if( !npc->trader_pay(nd,sd,price,points) )
return ERROR_TYPE_MONEY;
} else {
if( (sd->kafraPoints < points) || (sd->cashPoints < price - points) )
return ERROR_TYPE_MONEY;
pc->paycash(sd, price, points);
}
if( !pet->create_egg(sd, nameid) ) {
struct item item_tmp;
memset(&item_tmp, 0, sizeof(struct item));
item_tmp.nameid = nameid;
item_tmp.identify = 1;
pc->additem(sd,&item_tmp, amount, LOG_TYPE_NPC);
}
return ERROR_TYPE_NONE;
}
/**
* Processes a player item purchase from npc shop.
*
* @param sd Buyer character.
* @param item_list List of items.
* @return result code for clif->parse_NpcBuyListSend.
*/
static int npc_buylist(struct map_session_data *sd, struct itemlist *item_list)
{
struct npc_data* nd;
struct npc_item_list *shop = NULL;
int64 z;
int i,j,w,skill_t,new_, idx = skill->get_index(MC_DISCOUNT);
unsigned short shop_size = 0;
nullpo_retr(3, sd);
nullpo_retr(3, item_list);
nd = npc->checknear(sd,map->id2bl(sd->npc_shopid));
if( nd == NULL )
return 3;
if( nd->subtype != SHOP ) {
if( nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->type == NST_ZENY ) {
shop = nd->u.scr.shop->item;
shop_size = nd->u.scr.shop->items;
} else
return 3;
} else {
shop = nd->u.shop.shop_item;
shop_size = nd->u.shop.count;
}
z = 0;
w = 0;
new_ = 0;
// process entries in buy list, one by one
for (i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
int value;
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
// find this entry in the shop's sell list
ARR_FIND( 0, shop_size, j,
entry->id == shop[j].nameid || //Normal items
entry->id == itemdb_viewid(shop[j].nameid) //item_avail replacement
);
if (j == shop_size)
return 3; // no such item in shop
entry->id = shop[j].nameid; //item_avail replacement
value = shop[j].value;
if (!itemdb->exists(entry->id))
return 3; // item no longer in itemdb
if (!itemdb->isstackable(entry->id) && entry->amount > 1) {
//Exploit? You can't buy more than 1 of equipment types o.O
ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, entry->amount, entry->id);
entry->amount = 1;
}
if( nd->master_nd ) {
// Script-controlled shops decide by themselves, what can be bought and for what price.
continue;
}
switch (pc->checkadditem(sd, entry->id, entry->amount)) {
case ADDITEM_EXIST:
break;
case ADDITEM_NEW:
new_++;
break;
case ADDITEM_OVERAMOUNT:
#if PACKETVER >= 20110705
return 9;
#else
return 2;
#endif
}
value = pc->modifybuyvalue(sd,value);
z += (int64)value * entry->amount;
w += itemdb_weight(entry->id) * entry->amount;
}
if (nd->master_nd != NULL) //Script-based shops.
return npc->buylist_sub(sd, item_list, nd->master_nd);
if (z > sd->status.zeny)
return 1; // Not enough Zeny
if( w + sd->weight > sd->max_weight )
return 2; // Too heavy
if( pc->inventoryblank(sd) < new_ )
return 3; // Not enough space to store items
pc->payzeny(sd, (int)z, LOG_TYPE_NPC, NULL);
for (i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
if (itemdb_type(entry->id) == IT_PETEGG) {
pet->create_egg(sd, entry->id);
} else {
struct item item_tmp;
memset(&item_tmp,0,sizeof(item_tmp));
item_tmp.nameid = entry->id;
item_tmp.identify = 1;
pc->additem(sd, &item_tmp, entry->amount, LOG_TYPE_NPC);
}
}
// custom merchant shop exp bonus
if( battle_config.shop_exp > 0 && z > 0 && (skill_t = pc->checkskill2(sd,idx)) > 0 ) {
if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
skill_t = sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
if( skill_t > 0 ) {
z = apply_percentrate64(z, skill_t * battle_config.shop_exp, 10000);
if (z < 1)
z = 1;
pc->gainexp(sd, NULL, 0, (int)z, false);
}
}
return 0;
}
/**
* Processes incoming npc market purchase list
**/
static enum market_buy_result npc_market_buylist(struct map_session_data *sd, struct itemlist *item_list)
{
struct npc_data* nd;
struct npc_item_list *shop = NULL;
int64 z;
int i,j,w,new_;
unsigned short shop_size = 0;
nullpo_retr(1, sd);
nullpo_retr(1, item_list);
nd = npc->checknear(sd,map->id2bl(sd->npc_shopid));
if (nd == NULL || nd->subtype != SCRIPT || VECTOR_LENGTH(*item_list) == 0 || !nd->u.scr.shop || nd->u.scr.shop->type != NST_MARKET)
return MARKET_BUY_RESULT_ERROR;
shop = nd->u.scr.shop->item;
shop_size = nd->u.scr.shop->items;
z = 0;
w = 0;
new_ = 0;
// process entries in buy list, one by one
for (i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
int value;
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
// find this entry in the shop's sell list
ARR_FIND( 0, shop_size, j,
entry->id == shop[j].nameid || //Normal items
entry->id == itemdb_viewid(shop[j].nameid) //item_avail replacement
);
if (j == shop_size) /* TODO find official response for this */
return MARKET_BUY_RESULT_ERROR; // no such item in shop
entry->id = shop[j].nameid; //item_avail replacement
if (entry->amount > (int)shop[j].qty)
return MARKET_BUY_RESULT_AMOUNT_TOO_BIG;
value = shop[j].value;
npc_market_qty[i] = j;
if (!itemdb->exists(entry->id)) /* TODO find official response for this */
return MARKET_BUY_RESULT_ERROR; // item no longer in itemdb
if (!itemdb->isstackable(entry->id) && entry->amount > 1) {
//Exploit? You can't buy more than 1 of equipment types o.O
ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, entry->amount, entry->id);
entry->amount = 1;
}
switch (pc->checkadditem(sd, entry->id, entry->amount)) {
case ADDITEM_EXIST:
break;
case ADDITEM_NEW:
new_++;
break;
case ADDITEM_OVERAMOUNT: /* TODO find official response for this */
return 1;
}
z += (int64)value * entry->amount;
w += itemdb_weight(entry->id) * entry->amount;
}
if (z > sd->status.zeny) /* TODO find official response for this */
return MARKET_BUY_RESULT_NO_ZENY; // Not enough Zeny
if( w + sd->weight > sd->max_weight ) /* TODO find official response for this */
return MARKET_BUY_RESULT_OVER_WEIGHT; // Too heavy
if( pc->inventoryblank(sd) < new_ ) /* TODO find official response for this */
return MARKET_BUY_RESULT_OUT_OF_SPACE; // Not enough space to store items
pc->payzeny(sd,(int)z,LOG_TYPE_NPC, NULL);
for (i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
j = npc_market_qty[i];
if (entry->amount > (int)shop[j].qty) /* wohoo someone tampered with the packet. */
return MARKET_BUY_RESULT_AMOUNT_TOO_BIG;
shop[j].qty -= entry->amount;
npc->market_tosql(nd,j);
if (itemdb_type(entry->id) == IT_PETEGG) {
pet->create_egg(sd, entry->id);
} else {
struct item item_tmp;
memset(&item_tmp,0,sizeof(item_tmp));
item_tmp.nameid = entry->id;
item_tmp.identify = 1;
pc->additem(sd, &item_tmp, entry->amount, LOG_TYPE_NPC);
}
}
return MARKET_BUY_RESULT_SUCCESS;
}
/**
* Processes incoming npc barter purchase list
**/
static int npc_barter_buylist(struct map_session_data *sd, struct barteritemlist *item_list)
{
struct npc_data* nd;
struct npc_item_list *shop = NULL;
int w, new_;
unsigned short shop_size = 0;
nullpo_retr(1, sd);
nullpo_retr(1, item_list);
nd = npc->checknear(sd, map->id2bl(sd->npc_shopid));
if (nd == NULL || nd->subtype != SCRIPT || VECTOR_LENGTH(*item_list) == 0 || !nd->u.scr.shop || nd->u.scr.shop->type != NST_BARTER)
return 11;
shop = nd->u.scr.shop->item;
shop_size = nd->u.scr.shop->items;
w = 0;
new_ = 0;
int items[MAX_INVENTORY] = { 0 };
// process entries in buy list, one by one
for (int i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
struct barter_itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
const int n = entry->removeIndex;
if (n < 0 || n >= sd->status.inventorySize)
return 11; // wrong inventory index
if (entry->addAmount <= 0)
return 14; // not enough item amount in inventory
int removeId = sd->status.inventory[n].nameid;
const int j = entry->shopIndex;
if (j < 0 || j >= shop_size)
return 13; // no such item in shop
if (entry->addId != shop[j].nameid && entry->addId != itemdb_viewid(shop[j].nameid))
return 13; // no such item in shop
if (removeId != shop[j].value && removeId != itemdb_viewid(shop[j].value))
return 13; // no such item in shop
entry->addId = shop[j].nameid; // item_avail replacement
removeId = shop[j].value; // item_avail replacement
if (!itemdb->exists(entry->addId))
return 13; // item no longer in itemdb
const int removeAmount = shop[j].value2;
if ((int)shop[j].qty != -1 && entry->addAmount > (int)shop[j].qty)
return 14; // not enough item amount in shop
if (removeAmount * entry->addAmount > sd->status.inventory[n].amount)
return 14; // not enough item amount in inventory
items[n] += removeAmount * entry->addAmount;
if (items[n] > sd->status.inventory[n].amount)
return 14; // not enough item amount in inventory
entry->addId = shop[j].nameid; //item_avail replacement
npc_market_qty[i] = j;
if (!itemdb->isstackable(entry->addId) && entry->addAmount > 1) {
//Exploit? You can't buy more than 1 of equipment types o.O
ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, entry->addAmount, entry->addId);
entry->addAmount = 1;
}
switch (pc->checkadditem(sd, entry->addId, entry->addAmount)) {
case ADDITEM_EXIST:
break;
case ADDITEM_NEW:
new_++;
break;
case ADDITEM_OVERAMOUNT: /* TODO find official response for this */
return 1;
}
w += itemdb_weight(entry->addId) * entry->addAmount;
w -= itemdb_weight(removeId) * removeAmount;
}
if (w + sd->weight > sd->max_weight)
return 2; // Too heavy
if (pc->inventoryblank(sd) < new_)
return 3; // Not enough space to store items
for (int i = 0; i < sd->status.inventorySize; ++i) {
const int removeAmountTotal = items[i];
if (removeAmountTotal == 0)
continue;
if (pc->delitem(sd, i, removeAmountTotal, 0, DELITEM_SOLD, LOG_TYPE_NPC) != 0) {
return 11; // unknown exploit
}
}
for (int i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
struct barter_itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
const int shopIdx = npc_market_qty[i];
if ((int)shop[shopIdx].qty != -1) {
if (entry->addAmount > (int)shop[shopIdx].qty) /* wohoo someone tampered with the packet. */
return 14;
shop[shopIdx].qty -= entry->addAmount;
}
npc->barter_tosql(nd, shopIdx);
if (itemdb_type(entry->addId) == IT_PETEGG) {
pet->create_egg(sd, entry->addId);
} else {
struct item item_tmp;
memset(&item_tmp, 0, sizeof(item_tmp));
item_tmp.nameid = entry->addId;
item_tmp.identify = 1;
pc->additem(sd, &item_tmp, entry->addAmount, LOG_TYPE_NPC);
}
}
return 12;
}
/**
* Processes incoming npc expanded barter purchase list
**/
static int npc_expanded_barter_buylist(struct map_session_data *sd, struct barteritemlist *item_list)
{
nullpo_retr(1, sd);
nullpo_retr(1, item_list);
struct npc_data* nd = npc->checknear(sd, map->id2bl(sd->npc_shopid));
if (nd == NULL || nd->subtype != SCRIPT || VECTOR_LENGTH(*item_list) == 0 ||
!nd->u.scr.shop || nd->u.scr.shop->type != NST_EXPANDED_BARTER) {
return 11;
}
struct npc_item_list *shop = nd->u.scr.shop->item;
unsigned short shop_size = nd->u.scr.shop->items;
int w = 0;
int new_ = 0;
int64 z = 0;
int items[MAX_INVENTORY] = { 0 };
// process entries in buy list, one by one
for (int i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
struct barter_itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
if (entry->addAmount <= 0)
return 14; // not enough item amount in inventory
const int j = entry->shopIndex;
if (j < 0 || j >= shop_size)
return 13; // no such item in shop
if (entry->addId != shop[j].nameid && entry->addId != itemdb_viewid(shop[j].nameid))
return 13; // no such item in shop
entry->addId = shop[j].nameid; // item_avail replacement
if (!itemdb->exists(entry->addId))
return 13; // item no longer in itemdb
if ((int)shop[j].qty != -1 && entry->addAmount > (int)shop[j].qty)
return 14; // not enough item amount in shop
int currencyCount = shop[j].value2;
for (int currencyIndex = 0; currencyIndex < currencyCount; currencyIndex ++) {
struct npc_barter_currency *currency = &shop[j].currency[currencyIndex];
const int currencyItemId = currency->nameid;
const int currencyRefine = currency->refine;
int removeAmount = currency->amount * entry->addAmount;
if (removeAmount <= 0)
continue;
for (int n = 0; n < sd->status.inventorySize && removeAmount > 0; ++n) {
// check item id and existing amount
if (sd->status.inventory[n].nameid == currencyItemId && sd->status.inventory[n].amount > 0) {
// check item refine level
if (currencyRefine != -1 && sd->status.inventory[n].refine != currencyRefine)
continue;
if (sd->status.inventory[n].amount >= removeAmount) {
items[n] += removeAmount;
removeAmount = 0;
w -= itemdb_weight(currencyItemId) * removeAmount;
break;
} else {
items[n] += sd->status.inventory[n].amount;
removeAmount -= sd->status.inventory[n].amount;
w -= itemdb_weight(currencyItemId) * sd->status.inventory[n].amount;
}
}
if (items[n] > sd->status.inventory[n].amount)
return 14; // not enough item amount in inventory
}
if (removeAmount != 0) {
return 14; // not enough item amount in inventory
}
}
entry->addId = shop[j].nameid; //item_avail replacement
npc_market_qty[i] = j;
if (!itemdb->isstackable(entry->addId) && entry->addAmount > 1) {
//Exploit? You can't buy more than 1 of equipment types o.O
ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, entry->addAmount, entry->addId);
entry->addAmount = 1;
}
switch (pc->checkadditem(sd, entry->addId, entry->addAmount)) {
case ADDITEM_EXIST:
break;
case ADDITEM_NEW:
new_++;
break;
case ADDITEM_OVERAMOUNT: /* TODO find official response for this */
return 1;
}
z += (int64)shop[j].value * entry->addAmount;
w += itemdb_weight(entry->addId) * entry->addAmount;
}
if (z > sd->status.zeny)
return 3; // Not enough Zeny
if ((int64)w + sd->weight > sd->max_weight)
return 2; // Too heavy
if (pc->inventoryblank(sd) < new_)
return 3; // Not enough space to store items
for (int i = 0; i < sd->status.inventorySize; ++i) {
const int removeAmountTotal = items[i];
if (removeAmountTotal == 0)
continue;
if (pc->delitem(sd, i, removeAmountTotal, 0, DELITEM_SOLD, LOG_TYPE_NPC) != 0) {
return 11; // unknown exploit
}
}
pc->payzeny(sd, (int)z, LOG_TYPE_NPC, NULL);
for (int i = 0; i < VECTOR_LENGTH(*item_list); ++i) {
struct barter_itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
const int shopIdx = npc_market_qty[i];
if ((int)shop[shopIdx].qty != -1) {
if (entry->addAmount > (int)shop[shopIdx].qty) /* wohoo someone tampered with the packet. */
return 14;
shop[shopIdx].qty -= entry->addAmount;
}
npc->expanded_barter_tosql(nd, shopIdx);
if (itemdb_type(entry->addId) == IT_PETEGG) {
pet->create_egg(sd, entry->addId);
} else {
struct item item_tmp;
memset(&item_tmp, 0, sizeof(item_tmp));
item_tmp.nameid = entry->addId;
item_tmp.identify = 1;
pc->additem(sd, &item_tmp, entry->addAmount, LOG_TYPE_NPC);
}
}
return 12;
}
/// npc_selllist for script-controlled shops
static int npc_selllist_sub(struct map_session_data *sd, struct itemlist *item_list, struct npc_data *nd)
{
char npc_ev[EVENT_NAME_LENGTH];
char card_slot[NAME_LENGTH];
char opt_index_str[NAME_LENGTH];
char opt_value_str[NAME_LENGTH];
char opt_param_str[NAME_LENGTH];
int i = 0;
int j = 0;
int key_nameid = 0;
int key_amount = 0;
int key_refine = 0;
int key_attribute = ATTR_NONE;
int key_identify = 0;
int key_card[MAX_SLOTS];
int key_opt_idx[MAX_ITEM_OPTIONS];
int key_opt_value[MAX_ITEM_OPTIONS];
int key_opt_param[MAX_ITEM_OPTIONS];
nullpo_ret(sd);
nullpo_ret(item_list);
nullpo_ret(nd);
// discard old contents
script->cleararray_pc(sd, "@sold_nameid", (void *)0);
script->cleararray_pc(sd, "@sold_quantity", (void *)0);
script->cleararray_pc(sd, "@sold_refine", (void *)0);
script->cleararray_pc(sd, "@sold_attribute", (void *)0);
script->cleararray_pc(sd, "@sold_identify", (void *)0);
for (j = 0; j < MAX_SLOTS; j++) { // clear each of the card slot entries
key_card[j] = 0;
snprintf(card_slot, sizeof(card_slot), "@sold_card%d", j + 1);
script->cleararray_pc(sd, card_slot, (void *)0);
}
for (j = 0; j < MAX_ITEM_OPTIONS; j++) { // Clear Each item option entry
key_opt_idx[j] = 0;
key_opt_value[j] = 0;
key_opt_param[j] = 0;
snprintf(opt_index_str, sizeof(opt_index_str), "@sold_opt_idx%d", j + 1);
script->cleararray_pc(sd, opt_index_str, (void *)0);
snprintf(opt_value_str, sizeof(opt_value_str), "@sold_opt_val%d", j + 1);
script->cleararray_pc(sd, opt_value_str, (void *)0);
snprintf(opt_param_str, sizeof(opt_param_str), "@sold_opt_param%d", j + 1);
script->cleararray_pc(sd, opt_param_str, (void *)0);
}
// save list of to be sold items
for (i = 0; i < VECTOR_LENGTH(*item_list); i++) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
struct item *item = &sd->status.inventory[entry->id];
intptr_t nameid = item->nameid;
intptr_t amount = entry->amount;
intptr_t refine = item->refine;
intptr_t attribute = item->attribute;
intptr_t identify = item->identify;
// process item based information into the arrays
script->setarray_pc(sd, "@sold_nameid", i, (void *)nameid, &key_nameid);
script->setarray_pc(sd, "@sold_quantity", i, (void *)amount, &key_amount);
script->setarray_pc(sd, "@sold_refine", i, (void *)refine, &key_refine);
script->setarray_pc(sd, "@sold_attribute", i, (void *)attribute, &key_attribute);
script->setarray_pc(sd, "@sold_identify", i, (void *)identify, &key_identify);
for (j = 0; j < MAX_SLOTS; j++) {
intptr_t card = item->card[j];
snprintf(card_slot, sizeof(card_slot), "@sold_card%d", j + 1);
script->setarray_pc(sd, card_slot, i, (void *)card, &key_card[j]);
}
for (j = 0; j < MAX_ITEM_OPTIONS; j++) {
intptr_t opt_idx = item->option[j].index;
intptr_t opt_value = item->option[j].value;
intptr_t opt_param = item->option[j].param;
snprintf(opt_index_str, sizeof(opt_index_str), "@sold_opt_idx%d", j + 1);
script->setarray_pc(sd, opt_index_str, i, (void *)opt_idx, &key_opt_idx[j]);
snprintf(opt_value_str, sizeof(opt_value_str), "@sold_opt_val%d", j + 1);
script->setarray_pc(sd, opt_value_str, i, (void *)opt_value, &key_opt_value[j]);
snprintf(opt_param_str, sizeof(opt_param_str), "@sold_opt_param%d", j + 1);
script->setarray_pc(sd, opt_param_str, i, (void *)opt_param, &key_opt_param[j]);
}
}
// invoke event
snprintf(npc_ev, ARRAYLENGTH(npc_ev), "%s::OnSellItem", nd->exname);
npc->event(sd, npc_ev, 0);
return 0;
}
/**
* Processes a character's request to sell items to a NPC shop.
*
* @param sd The character who wants to sell the items.
* @param item_list The list of items and respective amounts which should be sold.
* @return 1 on failure, 0 on success.
*
**/
static int npc_selllist(struct map_session_data *sd, struct itemlist *item_list)
{
nullpo_retr(1, sd);
nullpo_retr(1, item_list);
struct npc_data *nd = npc->checknear(sd, map->id2bl(sd->npc_shopid));
if (nd == NULL)
return 1;
if (nd->subtype != SHOP) {
if (nd->subtype != SCRIPT || nd->u.scr.shop == NULL || (nd->u.scr.shop->type != NST_ZENY && nd->u.scr.shop->type != NST_MARKET))
return 1;
}
if (sd->status.zeny >= MAX_ZENY && nd->master_nd == NULL)
return 1;
bool duplicates[MAX_INVENTORY] = { false };
int64 z = 0;
// Verify the sell list.
for (int i = 0; i < VECTOR_LENGTH(*item_list); i++) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
int idx = entry->id;
if (idx >= sd->status.inventorySize || idx < 0 || entry->amount < 0)
return 1;
if (duplicates[idx]) // Sanity check. The client sends each inventory index at most once. [Haru]
return 1;
duplicates[idx] = true;
int nameid = sd->status.inventory[idx].nameid;
if (nameid == 0 || sd->inventory_data[idx] == NULL || sd->status.inventory[idx].amount < entry->amount)
return 1;
if (nd->master_nd != NULL) // Script-controlled shops decide by themselves, what can be sold and at what price.
continue;
int value = pc->modifysellvalue(sd, sd->inventory_data[idx]->value_sell);
z += (int64)value * entry->amount;
}
if (nd->master_nd != NULL) // Script-controlled shops.
return npc->selllist_sub(sd, item_list, nd->master_nd);
if (z + sd->status.zeny > MAX_ZENY)
return 1;
// Delete items.
for (int i = 0; i < VECTOR_LENGTH(*item_list); i++) {
struct itemlist_entry *entry = &VECTOR_INDEX(*item_list, i);
int idx = entry->id;
if (sd->inventory_data[idx]->type == IT_PETEGG && sd->status.inventory[idx].card[0] == CARD0_PET) {
if (pet->search_petDB_index(sd->status.inventory[idx].nameid, PET_EGG) >= 0)
intif->delete_petdata(MakeDWord(sd->status.inventory[idx].card[1], sd->status.inventory[idx].card[2]));
}
// Achievements. [Smokexyz/Hercules]
achievement->validate_item_sell(sd, sd->status.inventory[idx].nameid, entry->amount);
pc->delitem(sd, idx, entry->amount, 0, DELITEM_SOLD, LOG_TYPE_NPC);
}
if (z > MAX_ZENY)
z = MAX_ZENY;
pc->getzeny(sd, (int)z, LOG_TYPE_NPC, NULL);
int skill_t;
int skill_idx = skill->get_index(MC_OVERCHARGE);
// Custom merchant shop exp bonus.
if (battle_config.shop_exp > 0 && z > 0 && (skill_t = pc->checkskill2(sd, skill_idx)) > 0) {
if (sd->status.skill[skill_idx].flag >= SKILL_FLAG_REPLACED_LV_0)
skill_t = sd->status.skill[skill_idx].flag - SKILL_FLAG_REPLACED_LV_0;
if (skill_t > 0) {
z = apply_percentrate64(z, skill_t * battle_config.shop_exp, 10000);
if (z < 1)
z = 1;
pc->gainexp(sd, NULL, 0, (int)z, false);
}
}
return 0;
}
//Atempt to remove an npc from a map
//This doesn't remove it from map_db
static int npc_remove_map(struct npc_data *nd)
{
int16 m,i;
nullpo_retr(1, nd);
if(nd->bl.prev == NULL || nd->bl.m < 0)
return 1; //Not assigned to a map.
m = nd->bl.m;
clif->clearunit_area(&nd->bl,CLR_RESPAWN);
npc->unsetcells(nd);
map->delblock(&nd->bl);
//Remove npc from map->list[].npc list. [Skotlex]
ARR_FIND( 0, map->list[m].npc_num, i, map->list[m].npc[i] == nd );
if( i == map->list[m].npc_num ) return 2; //failed to find it?
map->list[m].npc_num--;
map->list[m].npc[i] = map->list[m].npc[map->list[m].npc_num];
map->list[m].npc[map->list[m].npc_num] = NULL;
return 0;
}
/**
* @see DBApply
*/
static int npc_unload_ev(union DBKey key, struct DBData *data, va_list ap)
{
struct event_data* ev = DB->data2ptr(data);
char* npcname = va_arg(ap, char *);
if(strcmp(ev->nd->exname,npcname)==0){
db_remove(npc->ev_db, key);
return 1;
}
return 0;
}
/**
* @see DBApply
*/
static int npc_unload_ev_label(union DBKey key, struct DBData *data, va_list ap)
{
struct linkdb_node **label_linkdb = DB->data2ptr(data);
struct npc_data* nd = va_arg(ap, struct npc_data *);
linkdb_erase(label_linkdb, nd);
return 0;
}
/**
* Unloads a NPC if it's a duplicate of the passed one.
*
* @param nd The NPC to check.
* @param args List of arguments.
* @return Always 0.
*
**/
static int npc_unload_dup_sub(struct npc_data *nd, va_list args)
{
nullpo_ret(nd);
const int src_id = va_arg(args, int);
const int unload_mobs = va_arg(args, int);
if (nd->src_id == src_id)
npc->unload(nd, true, (unload_mobs == 1));
return 0;
}
/**
* Unloads all NPCs which are duplicates of the passed one.
*
* @param nd The source NPC.
* @param unload_mobs If true, mobs spawned by duplicates will be removed.
*
* @author Skotlex
*
**/
static void npc_unload_duplicates(struct npc_data *nd, bool unload_mobs)
{
nullpo_retv(nd);
map->foreachnpc(npc->unload_dup_sub, nd->bl.id, unload_mobs);
}
/**
* Removes a mob, which was spawned by a NPC (monster/areamonster/guardian/bg_monster/atcommand("@monster xy")).
*
* @param md The mob to remove.
* @param args List of arguments.
* @return 1 on success, 0 on failure.
*
* @author Kenpachi
*
**/
static int npc_unload_mob(struct mob_data *md, va_list args)
{
nullpo_ret(md);
const int npc_id = va_arg(args, int);
if (md->npc_id == npc_id) {
md->state.npc_killmonster = 1;
status_kill(&md->bl);
return 1;
}
return 0;
}
/**
* Removes a NPC from map and database.
*
* @param nd The NPC which should be removed.
* @param single If true, names are freed. (For duplicates.)
* @param unload_mobs If true, mobs spawned by the NPC will be removed.
* @return Always 0.
*
**/
static int npc_unload(struct npc_data *nd, bool single, bool unload_mobs)
{
nullpo_ret(nd);
if (nd->ud != NULL && nd->ud != &npc->base_ud)
skill->clear_unitgroup(&nd->bl);
npc->remove_map(nd);
map->deliddb(&nd->bl);
if (single)
strdb_remove(npc->name_db, nd->exname);
if (nd->chat_id != 0) /// Remove NPC chatroom object and kick users.
chat->delete_npc_chat(nd);
npc_chat->finalize(nd); /// Deallocate NPC PCRE data structures.
if (single && nd->path != NULL) {
npc->releasepathreference(nd->path);
nd->path = NULL;
}
if (single && nd->bl.m != INDEX_NOT_FOUND)
map->remove_questinfo(nd->bl.m, nd);
npc->questinfo_clear(nd);
if (nd->src_id == 0 && (nd->subtype == SHOP || nd->subtype == CASHSHOP)) {
aFree(nd->u.shop.shop_item); /// src check for duplicate shops. [Orcao]
} else if (nd->subtype == SCRIPT) {
char evname[EVENT_NAME_LENGTH];
snprintf(evname, ARRAYLENGTH(evname), "%s::OnNPCUnload", nd->exname);
struct event_data *ev = strdb_get(npc->ev_db, evname);
if (ev != NULL)
script->run_npc(nd->u.scr.script, ev->pos, 0, nd->bl.id); /// Run OnNPCUnload.
if (single) {
npc->ev_db->foreach(npc->ev_db, npc->unload_ev, nd->exname); /// Clean up all related events.
npc->ev_label_db->foreach(npc->ev_label_db, npc->unload_ev_label, nd);
}
struct s_mapiterator *iter = mapit_geteachpc();
struct map_session_data *sd = BL_UCAST(BL_PC, mapit->first(iter));
for (; mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter))) {
if (sd->npc_timer_id != INVALID_TIMER) {
const struct TimerData *td = timer->get(sd->npc_timer_id);
if (td != NULL && td->id != nd->bl.id)
continue;
if (td != NULL && td->data != 0)
ers_free(npc->timer_event_ers, (void*)td->data);
timer->delete(sd->npc_timer_id, npc->timerevent);
sd->npc_timer_id = INVALID_TIMER;
}
}
mapit->free(iter);
if (nd->u.scr.timerid != INVALID_TIMER) {
const struct TimerData *td = timer->get(nd->u.scr.timerid);
if (td != NULL && td->data != 0)
ers_free(npc->timer_event_ers, (void*)td->data);
timer->delete(nd->u.scr.timerid, npc->timerevent);
}
if (nd->u.scr.timer_event != NULL)
aFree(nd->u.scr.timer_event);
if (nd->src_id == 0) {
if (nd->u.scr.script != NULL) {
script->free_code(nd->u.scr.script);
nd->u.scr.script = NULL;
}
if (nd->u.scr.label_list != NULL) {
aFree(nd->u.scr.label_list);
nd->u.scr.label_list = NULL;
nd->u.scr.label_list_num = 0;
}
if (nd->u.scr.shop != NULL) {
if (nd->u.scr.shop->item != NULL) {
for (int i = 0; i < nd->u.scr.shop->items; i ++) {
if (nd->u.scr.shop->item[i].currency != NULL)
aFree(nd->u.scr.shop->item[i].currency);
}
aFree(nd->u.scr.shop->item);
}
aFree(nd->u.scr.shop);
}
}
if (nd->u.scr.guild_id > 0)
guild->flag_remove(nd);
}
if (nd->ud != NULL && nd->ud != &npc->base_ud) {
aFree(nd->ud);
nd->ud = NULL;
}
if (unload_mobs)
map->foreachmob(npc->unload_mob, nd->bl.id);
HPM->data_store_destroy(&nd->hdata);
aFree(nd);
return 0;
}
//
// NPC Source Files
//
/// Clears the npc source file list
static void npc_clearsrcfile(void)
{
struct npc_src_list* file = npc->src_files;
while (file != NULL) {
struct npc_src_list *file_tofree = file;
file = file->next;
aFree(file_tofree);
}
npc->src_files = NULL;
}
/**
* Adds a npc source file.
*
* @param name The file name to add.
*/
static void npc_addsrcfile(const char *name)
{
struct npc_src_list* file;
struct npc_src_list* file_prev = NULL;
nullpo_retv(name);
// prevent multiple insert of source files
file = npc->src_files;
while (file != NULL) {
if (strcmp(name, file->name) == 0)
return;// found the file, no need to insert it again
file_prev = file;
file = file->next;
}
file = (struct npc_src_list*)aMalloc(sizeof(struct npc_src_list) + strlen(name));
file->next = NULL;
safestrncpy(file->name, name, strlen(name) + 1);
if( file_prev == NULL )
npc->src_files = file;
else
file_prev->next = file;
}
/**
* Removes a npc source file.
*
* @param name The file name to remove.
*/
static void npc_delsrcfile(const char *name)
{
struct npc_src_list* file = npc->src_files;
struct npc_src_list* file_prev = NULL;
nullpo_retv(name);
while (file != NULL) {
if (strcmp(file->name, name) == 0) {
if (npc->src_files == file)
npc->src_files = file->next;
else
file_prev->next = file->next;
aFree(file);
break;
}
file_prev = file;
file = file->next;
}
}
/**
* Retains a reference to filepath, for use in nd->path
*
* @param filepath The file path to retain.
* @return A retained reference to filepath.
*/
static const char *npc_retainpathreference(const char *filepath)
{
struct npc_path_data * npd = NULL;
nullpo_ret(filepath);
if (npc->npc_last_path == filepath) {
if (npc->npc_last_npd != NULL)
npc->npc_last_npd->references++;
return npc->npc_last_ref;
}
if ((npd = strdb_get(npc->path_db,filepath)) == NULL) {
CREATE(npd, struct npc_path_data, 1);
strdb_put(npc->path_db, filepath, npd);
CREATE(npd->path, char, strlen(filepath)+1);
safestrncpy(npd->path, filepath, strlen(filepath)+1);
npd->references = 0;
}
npd->references++;
npc->npc_last_npd = npd;
npc->npc_last_ref = npd->path;
npc->npc_last_path = filepath;
return npd->path;
}
/**
* Releases a previously retained filepath.
*
* @param filepath The file path to release.
*/
static void npc_releasepathreference(const char *filepath)
{
struct npc_path_data* npd = NULL;
nullpo_retv(filepath);
if (filepath != npc->npc_last_ref) {
npd = strdb_get(npc->path_db, filepath);
}
if (npd != NULL && --npd->references == 0) {
char *npcpath = npd->path;
strdb_remove(npc->path_db, filepath);/* remove from db */
aFree(npcpath);
}
}
/// Parses and sets the name and exname of a npc.
/// Assumes that m, x and y are already set in nd.
static void npc_parsename(struct npc_data *nd, const char *name, const char *start, const char *buffer, const char *filepath)
{
const char* p;
struct npc_data* dnd;// duplicate npc
char newname[NAME_LENGTH];
nullpo_retv(nd);
nullpo_retv(name);
// parse name
p = strstr(name,"::");
if( p ) { // ::
size_t len = p-name;
if( len > NAME_LENGTH ) {
ShowWarning("npc_parsename: Display name of '%s' is too long (len=%u) in file '%s', line '%d'. Truncating to %d characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
safestrncpy(nd->name, name, sizeof(nd->name));
} else {
memcpy(nd->name, name, len);
memset(nd->name+len, 0, sizeof(nd->name)-len);
}
len = strlen(p+2);
if( len > NAME_LENGTH )
ShowWarning("npc_parsename: Unique name of '%s' is too long (len=%u) in file '%s', line '%d'. Truncating to %d characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
safestrncpy(nd->exname, p+2, sizeof(nd->exname));
} else {//
size_t len = strlen(name);
if( len > NAME_LENGTH )
ShowWarning("npc_parsename: Name '%s' is too long (len=%u) in file '%s', line '%d'. Truncating to %d characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
safestrncpy(nd->name, name, sizeof(nd->name));
safestrncpy(nd->exname, name, sizeof(nd->exname));
}
if( *nd->exname == '\0' || strstr(nd->exname,"::") != NULL ) {// invalid
snprintf(newname, ARRAYLENGTH(newname), "0_%d_%d_%d", nd->bl.m, nd->bl.x, nd->bl.y);
ShowWarning("npc_parsename: Invalid unique name in file '%s', line '%d'. Renaming '%s' to '%s'.\n", filepath, strline(buffer,start-buffer), nd->exname, newname);
safestrncpy(nd->exname, newname, sizeof(nd->exname));
}
if( (dnd=npc->name2id(nd->exname)) != NULL ) {// duplicate unique name, generate new one
char this_mapname[32];
char other_mapname[32];
int i = 0;
do {
++i;
safesnprintf(newname, ARRAYLENGTH(newname), "%d_%d_%d_%d", i, nd->bl.m, nd->bl.x, nd->bl.y);
} while( npc->name2id(newname) != NULL );
strcpy(this_mapname, (nd->bl.m == -1 ? "(not on a map)" : mapindex_id2name(map_id2index(nd->bl.m))));
strcpy(other_mapname, (dnd->bl.m == -1 ? "(not on a map)" : mapindex_id2name(map_id2index(dnd->bl.m))));
ShowWarning("npc_parsename: Duplicate unique name in file '%s', line '%d'. Renaming '%s' to '%s'.\n", filepath, strline(buffer,start-buffer), nd->exname, newname);
ShowDebug("this npc:\n display name '%s'\n unique name '%s'\n map=%s, x=%d, y=%d\n", nd->name, nd->exname, this_mapname, nd->bl.x, nd->bl.y);
ShowDebug("other npc in '%s' :\n display name '%s'\n unique name '%s'\n map=%s, x=%d, y=%d\n",dnd->path, dnd->name, dnd->exname, other_mapname, dnd->bl.x, dnd->bl.y);
safestrncpy(nd->exname, newname, sizeof(nd->exname));
}
}
// Parse View
// Support for using Constants in place of NPC View IDs.
static int npc_parseview(const char *w4, const char *start, const char *buffer, const char *filepath)
{
int val = FAKE_NPC, i = 0;
char viewid[1024]; // Max size of name from constants.conf, see script->read_constdb.
nullpo_retr(FAKE_NPC, w4);
// Extract view ID / constant
while (w4[i] != '\0') {
if (ISSPACE(w4[i]) || w4[i] == '/' || w4[i] == ',')
break;
i++;
}
safestrncpy(viewid, w4, i+=1);
// Check if view id is not an ID (only numbers).
if(!npc->viewisid(viewid))
{
// Check if constant exists and get its value.
if(!script->get_constant(viewid, &val)) {
ShowWarning("npc_parseview: Invalid NPC constant '%s' specified in file '%s', line '%d'. Defaulting to INVISIBLE_CLASS.\n", viewid, filepath, strline(buffer,start-buffer));
val = INVISIBLE_CLASS;
}
} else {
// NPC has an ID specified for view id.
val = atoi(w4);
ShowWarning("npc_parseview: Use of numeric NPC view IDs is deprecated and may be removed in a future update. Please use NPC view constants instead. ID '%d' specified in file '%s', line '%d'.\n", val, filepath, strline(buffer, start-buffer));
}
return val;
}
// View is ID
// Checks if given view is an ID or constant.
static bool npc_viewisid(const char *viewid)
{
nullpo_retr(false, viewid);
if (atoi(viewid) != FAKE_NPC) {
// Loop through view, looking for non-numeric character.
while (*viewid) {
if (ISDIGIT(*viewid++) == 0) return false;
}
}
return true;
}
/**
* Creates a new NPC.
*
* @param subtype The NPC subtype.
* @param m The map id.
* @param x The x coordinate on map.
* @param y The y coordinate on map.
* @param dir The facing direction.
* @param class_ The NPC view class.
* @return A pointer to the created NPC data (ownership passed to the caller).
*/
static struct npc_data *npc_create_npc(enum npc_subtype subtype, int m, int x, int y, enum unit_dir dir, int class_)
{
struct npc_data *nd;
CREATE(nd, struct npc_data, 1);
nd->subtype = subtype;
nd->bl.type = BL_NPC;
nd->bl.id = npc->get_new_npc_id();
nd->bl.prev = nd->bl.next = NULL;
nd->bl.m = m;
nd->bl.x = x;
nd->bl.y = y;
nd->dir = dir;
nd->area_size = AREA_SIZE + 1;
nd->class_ = class_;
nd->speed = 200;
nd->vd = npc_viewdb[0]; // Copy INVISIBLE_CLASS view data. Actual view data is set by npc->add_to_location() later.
VECTOR_INIT(nd->qi_data);
return nd;
}
//Add then display an npc warp on map
static struct npc_data *npc_add_warp(char *name, short from_mapid, short from_x, short from_y, short xs, short ys, unsigned short to_mapindex, short to_x, short to_y)
{
int i, flag = 0;
struct npc_data *nd;
nullpo_retr(NULL, name);
nd = npc->create_npc(WARP, from_mapid, from_x, from_y, 0, battle_config.warp_point_debug ? WARP_DEBUG_CLASS : WARP_CLASS);
safestrncpy(nd->exname, name, ARRAYLENGTH(nd->exname));
if (npc->name2id(nd->exname) != NULL)
flag = 1;
if (flag == 1)
safesnprintf(nd->exname, ARRAYLENGTH(nd->exname), "warp_%d_%d_%d", from_mapid, from_x, from_y);
for( i = 0; npc->name2id(nd->exname) != NULL; ++i )
safesnprintf(nd->exname, ARRAYLENGTH(nd->exname), "warp%d_%d_%d_%d", i, from_mapid, from_x, from_y);
safestrncpy(nd->name, nd->exname, ARRAYLENGTH(nd->name));
nd->u.warp.mapindex = to_mapindex;
nd->u.warp.x = to_x;
nd->u.warp.y = to_y;
nd->u.warp.xs = xs;
nd->u.warp.ys = xs;
npc->add_to_location(nd);
return nd;
}
/**
* Parses a warp NPC.
*
* @param[in] w1 First tab-delimited part of the string to parse.
* @param[in] w2 Second tab-delimited part of the string to parse.
* @param[in] w3 Third tab-delimited part of the string to parse.
* @param[in] w4 Fourth tab-delimited part of the string to parse.
* @param[in] start Pointer to the beginning of the string inside buffer.
* This must point to the same buffer as `buffer`.
* @param[in] buffer Pointer to the buffer containing the script. For
* single-line mapflags not inside a script, this may be
* an empty (but initialized) single-character buffer.
* @param[in] filepath Source file path.
* @param[out] retval Pointer to return the success (EXIT_SUCCESS) or failure
* (EXIT_FAILURE) status. May be NULL.
* @return A pointer to the advanced buffer position.
*/
static const char *npc_parse_warp(const char *w1, const char *w2, const char *w3, const char *w4, const char *start, const char *buffer, const char *filepath, int *retval)
{
int x, y, xs, ys, to_x, to_y, m;
unsigned short i;
char mapname[32], to_mapname[32];
struct npc_data *nd;
nullpo_retr(strchr(start,'\n'), w1);
nullpo_retr(strchr(start,'\n'), w4);
// w1=,,,
// w4=,,,,
if( sscanf(w1, "%31[^,],%d,%d", mapname, &x, &y) != 3
|| sscanf(w4, "%d,%d,%31[^,],%d,%d", &xs, &ys, to_mapname, &to_x, &to_y) != 5
) {
ShowError("npc_parse_warp: Invalid warp definition in file '%s', line '%d'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
m = map->mapname2mapid(mapname);
i = mapindex->name2id(to_mapname);
if( i == 0 ) {
ShowError("npc_parse_warp: Unknown destination map in file '%s', line '%d' : %s\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), to_mapname, w1, w2, w3, w4);
if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
if( m != -1 && ( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) ) {
ShowError("npc_parse_warp: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map->list[m].name, x, y, map->list[m].xs, map->list[m].ys,filepath,strline(buffer,start-buffer));
if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');;//try next
}
nd = npc->create_npc(WARP, m, x, y, 0, battle_config.warp_point_debug ? WARP_DEBUG_CLASS : WARP_CLASS);
npc->parsename(nd, w3, start, buffer, filepath);
nd->path = npc->retainpathreference(filepath);
nd->u.warp.mapindex = i;
nd->u.warp.x = to_x;
nd->u.warp.y = to_y;
nd->u.warp.xs = xs;
nd->u.warp.ys = ys;
npc->npc_warp++;
npc->add_to_location(nd);
return strchr(start,'\n');// continue
}
/**
* Parses a SHOP/CASHSHOP NPC.
*
* @param[in] w1 First tab-delimited part of the string to parse.
* @param[in] w2 Second tab-delimited part of the string to parse.
* @param[in] w3 Third tab-delimited part of the string to parse.
* @param[in] w4 Fourth tab-delimited part of the string to parse.
* @param[in] start Pointer to the beginning of the string inside buffer.
* This must point to the same buffer as `buffer`.
* @param[in] buffer Pointer to the buffer containing the script. For
* single-line mapflags not inside a script, this may be
* an empty (but initialized) single-character buffer.
* @param[in] filepath Source file path.
* @param[out] retval Pointer to return the success (EXIT_SUCCESS) or failure
* (EXIT_FAILURE) status. May be NULL.
* @return A pointer to the advanced buffer position.
*/
static const char *npc_parse_shop(const char *w1, const char *w2, const char *w3, const char *w4, const char *start, const char *buffer, const char *filepath, int *retval)
{
//TODO: could be rewritten to NOT need this temp array [ultramage]
// We could use nd->u.shop.shop_item to store directly the items, but this could lead
// to unecessary memory usage by the server, using a temp dynamic array is the
// best way to do this without having to do multiple reallocs [Panikon]
struct npc_item_list *items = NULL;
size_t items_count = 40; // Starting items size
const char *p;
int x, y, dir, m, i, class_;
struct npc_data *nd;
enum npc_subtype type;
nullpo_retr(strchr(start,'\n'), w1);
nullpo_retr(strchr(start,'\n'), w4);
if( strcmp(w1,"-") == 0 ) {
// 'floating' shop
x = y = dir = 0;
m = -1;
} else {// w1=