Переглянути джерело

Fixed the dynamic shop system memory allocation and id_db problem. Added sample script.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@5842 54d463be-8e91-2dee-dedb-b68131a5f0ec
Lance 19 роки тому
батько
коміт
008b473603
5 змінених файлів з 94 додано та 8 видалено
  1. 84 0
      npc/sample/npc_dynamic_shop.txt
  2. 1 0
      npc/scripts_custom.conf
  3. 4 5
      src/map/clif.c
  4. 2 2
      src/map/npc.c
  5. 3 1
      src/map/script.c

+ 84 - 0
npc/sample/npc_dynamic_shop.txt

@@ -0,0 +1,84 @@
+-	shop	dyn_shop1	-1,501:50
+
+prontera.gat,181,200,4	script	Dynamic Shop	123,{
+callshop "dyn_shop1",0;
+end;
+
+OnSellItem:
+for(set @i, 0; @i < getarraysize(@sold_nameid); set @i, @i + 1){
+	if(countitem(@sold_nameid[@i]) < @sold_quantity[@i] || @sold_quantity[@i] <= 0){
+		mes "omgh4x!";
+		end;
+	}
+	if(@sold_nameid[@i] == 501){
+		set $@rpotsleft, $@rpotsleft + @sold_quantity[@i];
+		set Zeny, Zeny + @sold_quantity[@i]*20;
+		delitem 501, @sold_quantity[@i];
+	} else {
+		if(@sold_nameid[@i] == 502){
+			set $@opotsleft, $@opotsleft + @sold_quantity[@i];
+			set Zeny, Zeny + @sold_quantity[@i]*100;
+			delitem 502, @sold_quantity[@i];
+		} else {
+			mes "Sorry, I don't need your items.";
+		}
+	}
+}
+deletearray @sold_quantity, getarraysize(@sold_quantity);
+deletearray @sold_nameid, getarraysize(@sold_nameid);
+mes "Deal completed.";
+close;
+
+OnBuyItem:
+for(set @i, 0; @i < getarraysize(@bought_nameid); set @i, @i + 1){
+	if(@bought_quantity[@i] <= 0){
+		mes "omgh4x!";
+		end;
+	}
+	if(@bought_nameid[@i] == 501){
+		if(@bought_quantity[@i] > $@rpotsleft){
+			if($@rpotsleft > 0){
+				set @bought_quantity[@i], $@rpotsleft;
+			} else {
+				mes "We are out of red potions!";
+				close;
+			}
+		}
+		if(Zeny >= 40*@bought_quantity[@i]){
+			set Zeny, Zeny - 40*@bought_quantity[@i];
+			getitem 501, @bought_quantity[@i];
+			set $@rpotsleft, $@rpotsleft - @bought_quantity[@i];
+		} else {
+			mes "You have insufficient cash.";
+			close;
+		}
+	} else {
+		if(@bought_quantity[@i] > $@opotsleft){
+			if($@opotsleft > 0){
+				set @bought_quantity[@i], $@opotsleft;
+			} else {
+				mes "We are out of orange potions!";
+				close;
+			}
+		}
+		if(Zeny >= 200*@bought_quantity[@i]){
+			set Zeny, Zeny - 200*@bought_quantity[@i];
+			getitem 502, @bought_quantity[@i];
+			set $@opotsleft, $@opotsleft - @bought_quantity[@i];
+		} else {
+			mes "You have insufficient cash.";
+			close;
+		}
+	}
+}
+deletearray @bought_quantity, getarraysize(@bought_quantity);
+deletearray @bought_nameid, getarraysize(@bought_nameid);
+mes "Trade done.";
+close;
+
+OnInit:
+npcshopitem "dyn_shop1", 501,40,502,200;
+set $@rpotsleft, 10;
+set $@opotsleft, 10;
+end;
+}

+ 1 - 0
npc/scripts_custom.conf

@@ -89,6 +89,7 @@ npc: npc/custom/adoption.txt
 //npc: npc/custom/Lance/FR_HallOfFame.c
 //npc: npc/custom/Lance/FR_WeatherController.c
 //npc: npc/custom/Lance/FR_MailSystem.c
+//npc: npc/custom/sample/npc_dynamic_shop.txt
 // --------------------------------------------------------------
 
 // --------------------------------------------------------------

+ 4 - 5
src/map/clif.c

@@ -8974,7 +8974,6 @@ void clif_parse_NpcBuyListSend(int fd,struct map_session_data *sd)
 	n = (RFIFOW(fd,2)-4) /4;
 	item_list = (unsigned short*)RFIFOP(fd,4);
 
-
 	if (sd->trade_partner || !sd->npc_shopid){
 		fail = 1;
 	}else{
@@ -8983,8 +8982,8 @@ void clif_parse_NpcBuyListSend(int fd,struct map_session_data *sd)
 			for(i=0;i<n;i++){
 				setd_sub(sd, "@bought_nameid", i, (void *)item_list[i*2+1]);
 				setd_sub(sd, "@bought_quantity", i, (void *)item_list[i*2]);
-				npc_event(sd, npc_ev, 0);
 			}
+			npc_event(sd, npc_ev, 0);
 			fail = 0;
 		}else{
 			fail = npc_buylist(sd,n,item_list);
@@ -9019,10 +9018,10 @@ void clif_parse_NpcSellListSend(int fd,struct map_session_data *sd)
 		if((nd = ((struct npc_data *)map_id2bl(sd->npc_shopid))->master_nd)){
 			sprintf(npc_ev, "%s::OnSellItem", nd->exname);
 			for(i=0;i<n;i++){
-				setd_sub(sd, "@sold_nameid", i, (void *)item_list[i*2+1]);
-				setd_sub(sd, "@sold_quantity", i, (void *)item_list[i*2]);
-				npc_event(sd, npc_ev, 0);
+				setd_sub(sd, "@sold_nameid", i, (void *)sd->status.inventory[item_list[i*2]-2].nameid);
+				setd_sub(sd, "@sold_quantity", i, (void *)item_list[i*2+1]);
 			}
+			npc_event(sd, npc_ev, 0);
 			fail = 0;
 		}else{
 			fail = npc_selllist(sd,n,item_list);

+ 2 - 2
src/map/npc.c

@@ -2766,14 +2766,14 @@ int do_init_npc(void)
 	return 0;
 }
 // [Lance]
-	int npc_changename(const char *name, const char *newname, short look){
+int npc_changename(const char *name, const char *newname, short look){
 	struct npc_data *nd= (struct npc_data *) strdb_remove(npcname_db,(unsigned char*)name);
 	if (nd==NULL)
 		return 0;
 	npc_enable(name,0);
 	strcpy(nd->name,newname);
 	nd->class_ = look;
-	strdb_put(npcname_db,nd->name,nd);
 	npc_enable(newname,1);
 	return 0;
 }
+

+ 3 - 1
src/map/script.c

@@ -9871,7 +9871,9 @@ int buildin_npcshopitem(struct script_state *st)
 		}
 
 		nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
-			sizeof(nd->u.shop_item[0]) * n);
+			sizeof(nd->u.shop_item[0]) * (n+1));
+
+		map_addiddb(&nd->bl);
 
 		nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
 	} else {