Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-21-2006, 11:11 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Script: Shop system

Once again in the interest of education, here's my shop system. It's broken into several parts, so make sure to read the instructions carefully!

Now, of course you can modify the script to make it match your own setup, however, here's how it works in my configuration. It also works using gralats by default; you'll have to change it in order to use custom money.

You'll need to update the shop weapon so that it adds the item to the player matching your own system. Also, you'll want to make sure that the seteffect fade doesn't interfere with your daynight, and finally, note that the weapon has a "Debit" option. This was for my setup on Rudora so that you could pay straight from your bank account. Either match it to your own bank system or just rip it out.

The system will depend upon three database NPCs, two classes and one weapon. Start by making the Database NPCs;
  • DB_Items; this is where we will store information about the items which you will sell
  • DB_Shops; this is where we will store the names of the shops, funds received from shop sales, etc
  • DB_Prices; this is where we will store the prices of items

You'll need to populate the NPCs with some data. Let's start with DB_Items; put the following script in DB_Items:
PHP Code:
function onCreated()
{
  
/*
    Format:
    this.item_id = {Name, Internalname, Image, ActionWeapon, Canbedropped};
   */
 
  
this.item_100 = {"Block""block""block.png""-No Item"1};

Next, in DB_Prices, put the following:
PHP Code:
function onCreated()
{
  
// Format:
  // this.item_id = price
  
  
this.item_100 3// 3 gralats in this case

Next, you'll need to set up the levels with the classes. It shouldn't really matter what you name the classes, but for this example, we'll use 'shopcontrol' and 'shopitem'.

Each shop level requires one shopcontrol class. This puts the level's base name into a level variable, and displays the shop's funds and name. Put this somewhere nice-looking. Just do something like this:
PHP Code:
this.join("shopcontrol"); 
Now, for each item that you want to sell in the shop, place a shopitem. You must specify additional data for shopitem classes, such as the item you want to sell. Here's an example:
PHP Code:
this.item 100// this is item ID 100 from DB_Items
this.customimage false// use the image set in DB_Items
this.join("shopitem"); 
Finally, add the shop interface weapon into a weapon called "-Shop", and make sure that it is added to any players using the shop. Hell, you could just add it to all players on logon.

That's about it, really. Once you upload the shop, it should work. You should also be able to specify a name for the shop inside DB_Shops' flag list once it's uploaded.

Enjoy.

Shop item class:
PHP Code:
function onCreated()
{
  
onTimeout();
}

function 
onTimeout()
{
  
this.okay 0;
  
  if (
this.item == NULL)
  {
    
temp.data "-unavailable-";
  }
  
  if (
DB_Items.("item_" this.item) == NULL)
  {
    
temp.data "-unavailable-";
  }
  
  if (
DB_Prices.("item_" this.item) == NULL)
  {
    
temp.data "-unavailable-";
  }
  
  
this.itemdata DB_Items.("item_" this.item);
  
this.price DB_Prices.("item_" this.item);

  
this.setShape(13232);

  if (
temp.data == "-unavailable-")
  {
    
this.setimg(""); // <- image for unavailable item here
  
}
    else
  {
    if (
this.customimage != TRUE ||
        
this.customimage == 0)
    {
      
this.setimg(this.itemdata[2]);
    }
  }

  if (
temp.data == NULL)
  {
    
temp.data this.itemdata[0];
    
this.okay 1;
  }
  
  
showimg(1"@Tahoma@bc@" temp.datathis.1this.0.5);
  
changeimgzoom(10.5);
  
  
setTimer(2);
}

function 
onActionleftmouse()
{
  if (
this.okay == 1)
  {
    
player.triggerclient("-Shop""purchaseItem"this.itemthis.itemdatathis.price);
  }

Shop control class:
PHP Code:
function onCreated()
{
  
level.creditor extractfilebase(this.level.name);
  
  
onTimeout();
}

function 
onTimeout()
{
  
level.shopname = (!(DB_Shops.(level.creditor "_name")) ? 
                    
"Unnamed Shop" DB_Shops.(level.creditor "_name"));
  
  
showimg(1format("@Tahoma@bc@%s"level.shopname), 1.51);
  
changeimgvis(12);
  
changeimgzoom(10.8);
  
  
showimg(2format("@Tahoma@bc@Shop Funds: %ig"DB_Shops.(level.creditor "_funds")), 1.5y);
  
changeimgvis(22);
  
changeimgzoom(20.5);

  
setTimer(2);

-Shop weapon:
PHP Code:
function onActionserverside()
{
  if (
params[0] == "purchaseItem")
  {
    if (
params[1] == NULL)
    {
      
player.say2("An error occured. 1" params[1]);
      return;
    }
    
    if (
DB_Prices.("item_" params[1]) == NULL)
    {
      
player.say2("An error occured. 2");
      return;
    }
    
    
temp.price DB_Prices.("item_" params[1]);
    
temp.item params[1];
    
temp.creditor = (!level.creditor "unknown" level.creditor);
    
    if (
params[2] == false)
    {
      if (
player.rupees >= temp.price)
      {
        
player.rupees -= temp.price
        
// ADD THE ITEM TO THE PLAYER HERE
        
DB_Shops.(temp.creditor "_funds") += temp.price;
      
        
player.say2("Thank you.");
      }
        else
      {
        
player.say2("You do not have enough money!");
      }
    }
      else
    {
      if (
player.bankBalance() >= temp.price)
      {     
        
temp.res player.bankDebit(temp.pricetemp.creditor);   
        if (
temp.res == 0)
        { 
          
// ADD THE ITEM TO THE PLAYER HERE
          
DB_Shops.(temp.creditor "_funds") += temp.price;
      
          
player.say2(format("Thank you.\nDebited: %dg.\nNew bank balance: %dg."temp.priceplayer.bankBalance()));
        }
          else
        {
          
player.say2(format("Bank debit failed!\n%s."temp.res));
        }
      }
        else
      {
        
player.say2("You do not have enough money!");
      }
    }
  }
}

//#CLIENTSIDE

function onPlayerEnters()
{
  
client.menus.remove("shop");
  
hideimgs(200201);
  
  
Shop_Purchase.visible false;
  
Shop_Cancel.visible false;
  
Shop_Debit.visible false;
}

function 
onActionclientside(temp.actionnametemp.itemidtemp.itemdatatemp.itemprice)
{
  switch (
temp.actionname)
  {
    case 
"purchaseItem":
    {
      if (
"shop" in client.menus)
      {
        return;
      }
    
      
client.menus.add("shop");
      
this.sellitem temp.itemid;
      
      for (
temp.0temp.<= 0.7temp.+= 0.1)
      {
        
seteffect(000temp.i);
        
sleep(0.05);
      }
      
      
showimg(200"@Tahoma@cb@Purchase " temp.itemdata[0], screenwidth 2, (screenheight 2) - 150);
      
changeimgvis(2005);
      
      
showimg(201"@Tahoma@cb@Price: " temp.itemprice "g"screenwidth 2, (screenheight 2) - 120);
      
changeimgzoom(2010.6);
      
changeimgvis(2015);
      
      new 
GuiCheckBoxCtrl(Shop_Debit)
      {
        
profile "GuiBlueCheckBoxProfile";
        
        
position = {(screenwidth 2) - 64, (screenheight 2) - 110};
        
extent = {13616};
        
        
visible true;
        
        
text "Debit from bank account";
      }
      
      new 
GuiButtonCtrl(Shop_Purchase)
      {
        
profile "GuiGreenButtonProfile";
      
        
position = {(screenwidth 2) - 72, (screenheight 2) - 90};
        
extent = {6424};
        
        
visible true;
      
        
text "Purchase";
        
        
thiso.catchEvent(this"onAction""onPurchase");
      }
      
      new 
GuiButtonCtrl(Shop_Cancel)
      {
        
profile "GuiBlueButtonProfile";
      
        
position = {(screenwidth 2) + 12, (screenheight 2) - 90};
        
extent = {6424};
        
        
visible true;
      
        
text "Cancel";
        
        
thiso.catchEvent(this"onAction""onCancelPurchase");
      }
      
      break;
    }
  
    default:
    {
      echo(
"Invalid shop data received!");
    }
  }
}

function 
onPurchase()
{
  
triggeraction(00"serverside""-Rudora-Shop""purchaseItem"this.sellitemShop_Debit.checked);
  
onCancelPurchase();
}

function 
onCancelPurchase()
{
  if (!(
"shop" in client.menus))
  {
    return;
  }
  
  
hideimgs(200201);
  
  
Shop_Purchase.destroy();
  
Shop_Cancel.destroy();
  
Shop_Debit.destroy();
  
  for (
temp.0.7temp.>= 0temp.-= 0.1)
  {
    
seteffect(000temp.i);
    
sleep(0.05);
  }
 
  
client.menus.remove("shop");

Reply With Quote
  #2  
Old 08-24-2006, 01:25 AM
Angel_Light Angel_Light is offline
Varia Developer
Angel_Light's Avatar
Join Date: Nov 2005
Location: Knoxville, TN
Posts: 1,684
Angel_Light is on a distinguished road
Send a message via AIM to Angel_Light Send a message via MSN to Angel_Light
Sexy ;o
__________________
Deep into the Darkness peering...
Reply With Quote
  #3  
Old 08-24-2006, 02:11 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
such a simple script to come from you, skyld!

helpful though, way to go ;o
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #4  
Old 08-24-2006, 02:16 AM
KuJi KuJi is offline
Banned
Join Date: Apr 2004
Location: Staten Island, New York
Posts: 2,202
KuJi will become famous soon enough
Send a message via ICQ to KuJi Send a message via AIM to KuJi Send a message via MSN to KuJi Send a message via Yahoo to KuJi
Nifty..
Reply With Quote
  #5  
Old 10-09-2006, 02:51 AM
Angel_Light Angel_Light is offline
Varia Developer
Angel_Light's Avatar
Join Date: Nov 2005
Location: Knoxville, TN
Posts: 1,684
Angel_Light is on a distinguished road
Send a message via AIM to Angel_Light Send a message via MSN to Angel_Light
What do you mean by ActionWeapon? in DB_Items? And what exactly do I put in DB_Shops?
__________________
Deep into the Darkness peering...
Reply With Quote
  #6  
Old 10-12-2006, 12:20 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
ActionWeapon is most likely the actual Weapon the item uses to execute stuff.
I myself prefer to make certain public functions that are always executed in the weapon by the Mudlib.
(mud_onPickUp(), mud_onEquip(), mud_onActivate(), mud_onUnEquip(), mud_onDrop())

It works nicely and Oblivion is a good source of ideas.
__________________
Reply With Quote
  #7  
Old 10-17-2006, 02:27 AM
Angel_Light Angel_Light is offline
Varia Developer
Angel_Light's Avatar
Join Date: Nov 2005
Location: Knoxville, TN
Posts: 1,684
Angel_Light is on a distinguished road
Send a message via AIM to Angel_Light Send a message via MSN to Angel_Light
I'm entirely sure but okay, still having problems with it though.
__________________
Deep into the Darkness peering...
Reply With Quote
  #8  
Old 11-07-2009, 11:23 PM
sssssssssss sssssssssss is offline
Cyril Rain
sssssssssss's Avatar
Join Date: May 2003
Location: Texas, USA
Posts: 1,134
sssssssssss will become famous soon enough
Resurrecting an old post. This is a good resource for some of us still, but Angel_Light's old question is mine.

So ActionWeapon is the -Shop weapon?
and how do you set up DB_Shops?
__________________
Cyril Rain
Creator and leader of SLX
Admin of Elysium
Elysium's Facebook Page: http://facebook.com/GraalOnlineElysium
Graal Forum Thread: http://forums.graalonline.com...
Graalians Thread: http://www.graalians.com...


Reply With Quote
  #9  
Old 11-07-2009, 11:50 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by sssssssssss View Post
Resurrecting an old post. This is a good resource for some of us still, but Angel_Light's old question is mine.

So ActionWeapon is the -Shop weapon?
The "ActionWeapon" looks like it's the weapon that goes along with the item, e.g. "Swords/Super Sword" might be the weapon name for a "supersword" item.
Quote:
and how do you set up DB_Shops?
It looks like it's all there, do you mean how do you add the database NPC itself? Just click Scripts > NPCs (or the NPC button if using the graphical RC), then Add (name it DB_Shops, and put it in an existing level).
__________________
Reply With Quote
  #10  
Old 11-08-2009, 02:35 AM
sssssssssss sssssssssss is offline
Cyril Rain
sssssssssss's Avatar
Join Date: May 2003
Location: Texas, USA
Posts: 1,134
sssssssssss will become famous soon enough
Well on here it just says make a DB called DB_Shops, and add a shop name in the flags.
doesnt tell what format, var to use if any, how to name the shops, nothing that i saw.

the block.png sample item isnt even showing up for me either, and i followed all the directions.
__________________
Cyril Rain
Creator and leader of SLX
Admin of Elysium
Elysium's Facebook Page: http://facebook.com/GraalOnlineElysium
Graal Forum Thread: http://forums.graalonline.com...
Graalians Thread: http://www.graalians.com...


Reply With Quote
  #11  
Old 11-08-2009, 03:18 AM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
Quote:
Originally Posted by sssssssssss View Post
Well on here it just says make a DB called DB_Shops, and add a shop name in the flags.
doesnt tell what format, var to use if any, how to name the shops, nothing that i saw.
It does, but you have to actually read the code.

PHP Code:
  level.shopname = (!(DB_Shops.(level.creditor "_name")) ?  
                    
"Unnamed Shop" DB_Shops.(level.creditor "_name")); 
In DB_Shops, the format for naming shops is this.mylevel_name = "My Shop Name".
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #12  
Old 11-08-2009, 05:16 AM
sssssssssss sssssssssss is offline
Cyril Rain
sssssssssss's Avatar
Join Date: May 2003
Location: Texas, USA
Posts: 1,134
sssssssssss will become famous soon enough
in the flags i have

this.armageddon-shop1.nw_name = "SHOP 1"
also tried w/o .nw in the level name, w/o _name although its obviously required.

its not showing up.

can't buy anything either. Was also wondering, how would you add it to add player.bombs instead of a weapon? is typing that in where the ActionWeapon is sufficient?
__________________
Cyril Rain
Creator and leader of SLX
Admin of Elysium
Elysium's Facebook Page: http://facebook.com/GraalOnlineElysium
Graal Forum Thread: http://forums.graalonline.com...
Graalians Thread: http://www.graalians.com...



Last edited by sssssssssss; 11-08-2009 at 05:32 AM..
Reply With Quote
  #13  
Old 11-08-2009, 07:04 PM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
Quote:
Originally Posted by sssssssssss View Post
in the flags i have

this.armageddon-shop1.nw_name = "SHOP 1"
also tried w/o .nw in the level name, w/o _name although its obviously required.

its not showing up.
Then you're doing something else wrong.

Quote:
Originally Posted by sssssssssss View Post
can't buy anything either. Was also wondering, how would you add it to add player.bombs instead of a weapon? is typing that in where the ActionWeapon is sufficient?
You have to script adding the item yourself.
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #14  
Old 11-09-2009, 07:12 AM
sssssssssss sssssssssss is offline
Cyril Rain
sssssssssss's Avatar
Join Date: May 2003
Location: Texas, USA
Posts: 1,134
sssssssssss will become famous soon enough
Quote:
Originally Posted by LoneAngelIbesu View Post
Then you're doing something else wrong.
Did everything that it said to do.
__________________
Cyril Rain
Creator and leader of SLX
Admin of Elysium
Elysium's Facebook Page: http://facebook.com/GraalOnlineElysium
Graal Forum Thread: http://forums.graalonline.com...
Graalians Thread: http://www.graalians.com...


Reply With Quote
  #15  
Old 11-13-2009, 02:38 AM
sssssssssss sssssssssss is offline
Cyril Rain
sssssssssss's Avatar
Join Date: May 2003
Location: Texas, USA
Posts: 1,134
sssssssssss will become famous soon enough
Quote:
Originally Posted by LoneAngelIbesu View Post
In DB_Shops, the format for naming shops is this.mylevel_name = "My Shop Name".
Just to clarify, its actually just
PHP Code:
mylevel_name "My Shop Name" 
no this.var at all, and no .nw either.

So for all to come, add DB_Shops, and put that in it for each shop.

Ex:
PHP Code:
armageddon-shop1_name="Lystra Shop" 
for the level armageddon-shop1.nw
__________________
Cyril Rain
Creator and leader of SLX
Admin of Elysium
Elysium's Facebook Page: http://facebook.com/GraalOnlineElysium
Graal Forum Thread: http://forums.graalonline.com...
Graalians Thread: http://www.graalians.com...


Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:07 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.