summaryrefslogtreecommitdiff
path: root/src/map/script.c
diff options
context:
space:
mode:
authorultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2007-11-18 10:36:09 +0000
committerultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2007-11-18 10:36:09 +0000
commitad3c28295537a5d942809ca07cae49bc350c45ab (patch)
tree36ca2554405b41fcb8d279d85e1efe7ad76923ab /src/map/script.c
parent16ef82dd78c2facf754e911fbaab1e5f279c50d4 (diff)
downloadhercules-ad3c28295537a5d942809ca07cae49bc350c45ab.tar.gz
hercules-ad3c28295537a5d942809ca07cae49bc350c45ab.tar.bz2
hercules-ad3c28295537a5d942809ca07cae49bc350c45ab.tar.xz
hercules-ad3c28295537a5d942809ca07cae49bc350c45ab.zip
* Replaced jA's way of allocating npc shop data with a simple dynamic array that gets allocated during loading and freed on unload
- automatically fixes bugreport:404, which would otherwise require manipulating the npcname_db (the original author didn't, hence the bug) - now a supporting variable 'count' is used for tracking the length instead of an extra dummy entry at the end of the shop list - partially removed the MAX_SHOPITEM restriction (if this was written properly, the system could support an unlimited amount of entries) git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11753 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/map/script.c')
-rw-r--r--src/map/script.c138
1 files changed, 50 insertions, 88 deletions
diff --git a/src/map/script.c b/src/map/script.c
index ac8667841..7d0a4b780 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -11713,86 +11713,58 @@ BUILDIN_FUNC(callshop)
return 0;
}
-#ifndef MAX_SHOPITEM
- #define MAX_SHOPITEM 100
-#endif
BUILDIN_FUNC(npcshopitem)
{
- struct npc_data *nd= NULL;
- int n = 0;
- int i = 3;
- int amount;
-
const char* npcname = script_getstr(st, 2);
- nd = npc_name2id(npcname);
+ struct npc_data* nd = npc_name2id(npcname);
+ int n, i;
+ int amount;
- if(!nd || nd->bl.subtype!=SHOP)
+ if( !nd || nd->bl.subtype != SHOP )
{ //Not found.
script_pushint(st,0);
return 0;
}
- // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
- amount = ((st->end-2)/2)+1;
-
- //Reinsert as realloc could change the pointer address.
- map_deliddb(&nd->bl);
- nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
- sizeof(nd->u.shop_item[0]) * amount);
- map_addiddb(&nd->bl);
-
- // Reset sell list.
- memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
-
- n = 0;
+ // st->end - 2 = nameid + value # ... / 2 = number of items
+ amount = (st->end-2)/2;
- while (script_hasdata(st,i)) {
- nd->u.shop_item[n].nameid = script_getnum(st,i);
- i++;
- nd->u.shop_item[n].value = script_getnum(st,i);
- i++;
- n++;
+ // generate new shop item list
+ RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
+ for( n = 0, i = 3; n < amount; n++, i+=2 )
+ {
+ nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
+ nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
}
+ nd->u.shop.count = n;
+
+ script_pushint(st,1);
return 0;
}
BUILDIN_FUNC(npcshopadditem)
{
- struct npc_data *nd=NULL;
- int n = 0;
- int i = 3;
+ const char* npcname = script_getstr(st,2);
+ struct npc_data* nd = npc_name2id(npcname);
+ int n, i;
int amount;
- const char* npcname = script_getstr(st, 2);
- nd = npc_name2id(npcname);
-
- if (!nd || nd->bl.subtype!=SHOP)
+ if( !nd || nd->bl.subtype != SHOP )
{ //Not found.
script_pushint(st,0);
return 0;
}
- amount = ((st->end-2)/2)+1;
- while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
- n++;
+ amount = (st->end-2)/2;
- //Reinsert as realloc could change the pointer address.
- map_deliddb(&nd->bl);
- nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
- sizeof(nd->u.shop_item[0]) * (amount+n));
- map_addiddb(&nd->bl);
-
- while(script_hasdata(st,i)){
- nd->u.shop_item[n].nameid = script_getnum(st,i);
- i++;
- nd->u.shop_item[n].value = script_getnum(st,i);
- i++;
- n++;
+ // append new items to existing shop item list
+ RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
+ for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
+ {
+ nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
+ nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
}
-
- // Marks the last of our stuff..
- nd->u.shop_item[n].value = 0;
- nd->u.shop_item[n].nameid = 0;
+ nd->u.shop.count = n;
script_pushint(st,1);
return 0;
@@ -11800,61 +11772,50 @@ BUILDIN_FUNC(npcshopadditem)
BUILDIN_FUNC(npcshopdelitem)
{
- struct npc_data *nd=NULL;
- int n=0;
- int i=3;
- int size = 0;
-
- const char* npcname = script_getstr(st, 2);
- nd = npc_name2id(npcname);
+ const char* npcname = script_getstr(st,2);
+ struct npc_data* nd = npc_name2id(npcname);
+ int n, i;
+ int amount;
+ int size;
- if (!nd || nd->bl.subtype!=SHOP)
+ if( !nd || nd->bl.subtype != SHOP )
{ //Not found.
script_pushint(st,0);
return 0;
}
- while (nd->u.shop_item[size].nameid)
- size++;
+ amount = (st->end-2)/2;
+ size = nd->u.shop.count;
- while (script_hasdata(st,i)) {
- for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
- if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
- // We're moving 1 extra empty block. Junk data is eliminated later.
- memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
- }
+ // remove specified items from the shop item list
+ for( i = 3; i < 3 + amount; i++ )
+ {
+ ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
+ if( n < size )
+ {
+ memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
+ size--;
}
- i++;
}
- size = 0;
-
- while (nd->u.shop_item[size].nameid)
- size++;
-
- //Reinsert as realloc could change the pointer address.
- map_deliddb(&nd->bl);
- nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
- sizeof(nd->u.shop_item[0]) * (size+1));
- map_addiddb(&nd->bl);
+ RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
+ nd->u.shop.count = size;
script_pushint(st,1);
return 0;
}
-//Sets a script to attach to an npc.
+//Sets a script to attach to a shop npc.
BUILDIN_FUNC(npcshopattach)
{
- struct npc_data *nd=NULL;
- const char* npcname = script_getstr(st, 2);
+ const char* npcname = script_getstr(st,2);
+ struct npc_data* nd = npc_name2id(npcname);
int flag = 1;
if( script_hasdata(st,3) )
flag = script_getnum(st,3);
- nd = npc_name2id(npcname);
-
- if (!nd || nd->bl.subtype!=SHOP)
+ if( !nd || nd->bl.subtype != SHOP )
{ //Not found.
script_pushint(st,0);
return 0;
@@ -11864,6 +11825,7 @@ BUILDIN_FUNC(npcshopattach)
nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
else
nd->master_nd = NULL;
+
script_pushint(st,1);
return 0;
}