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 09-22-2008, 09:17 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Item System

Included Classes: functions_items (credits to me), functions_cached_database (credits to DrakilorP2P)
Included Database NPC's: ItemCache (credits to me & DrakilorP2P)
Disclaimer: While the addItem() function might mention quantity's, I never got around actually implementing them, so they currently don't exist.
Notes: Join functions_items to players in onActionPlayerOnline().



Purpose of system? To provide servers with an easy to use, flexible, powerful and most importantly a secure way of handling items.


How does it work? All items are stored in .arc files (yes, laugh away Chris and Inverness) which are just normal .txt files with a fancy extension. Doesn't matter which you use, I use it just because it's what I've been used to for years.

The system loads the players .arc data into player.items which is a serversided TStaticVar placeholder for the items, so that you don't have to read their data directly from the .arc files everytime you need it. It's automatically updated whenever a players data changes. If you're implementing an inventory with this system, you'll have to make it grab the data from serverside to clientside ONCE and store it in a this. variable clientsided (see the function triggerToClient() in functions_items class to see how I did it). The moveItem() function is currently calling it on both sender and retriever to refresh the clientsided data every time an item is moved to/from a TServerPlayer object.


But what about item creation and 'trading'? You never answer that in How does it work! You're right, I didn't, since it was cluttered enough as it is, but at least now I got your attention right?

addItem(id, quantity, obj) - Adds the item with the item id of 'id' from 'obj' to the current object. Simply put anyways, however it's not that easy. First it makes sure that 'obj' isn't nothing at all, and if it is, it sets it to ItemCache which is where ALL items appear when created. That's right, an item must physically exist on 'obj' before they can be added. After that it checks to ensure that 'obj' isn't itself, and if so, aborts. If the item exists, it'll be moved - more on that soon.

itemExists(id, obj) - Checks if the item physically exists on 'obj' and returns true/false according to those results.

moveItem(id, from, to) - Moves the actual item from 'from' to 'to' - which is set to the current object if NULL. This function also updates the .arc data, and triggers triggerToClient() to refresh clientsided data.


But dude, I wanna know how to create items! Yeah yeah, I get it. It's rather simple, just trigger the createItem(arc, [itemname]) function! If 'arc' is a valid arc name, the item will be created and put on the ItemCache database NPC for safekeeping until it's grabbed. When that's done, it triggers ItemCache's saveItem(obj) function (obj equals the actual temp.item TStaticVar). Item's have a constantly increasing variable as id if the .arc have uniqueID set to true, otherwise it uses it's item name as id.

Common ways of doing it:

PHP Code:
temp.item createItem(arc);
addItem(temp.item); 
PHP Code:
addItem(createItem(arc)); 

That's really all you have to know to be able to use/alter the functions. Now to the final part - arc creation. For that, you'll have to manually edit the database NPC ItemCache whenever you want to make a new one. I'll go over a few of it's basic vars first though.

this.directory - Folder name of saved .arc files. Defaults to levels/MUDLIB/archtypes/
this.subfolders - Optional. Turn it into an array of subfolders if you'd prefer using that.
this.newEntryFunction - Name of the function handling new entries.

Check the debugArcCreation() function to see how you actually create the .arc files and set their data.


What rights do I need to give my NPC-Server?

PHP Code:
rw  levels/MUDLIB/archtypes/*.arc
rw  levels/MUDLIB/archtypes/*/
*.arc
rw  levels
/MUDLIB/itemdb/*.arc
rw  levels/MUDLIB/itemdb/*/
*.arc 

I more then likely missed a couple of things, but just ask if there's anything you're curious about. I don't mind anyone using or altering it, but if anyone asks you about it, please redirect them to this thread. If you do use it, please, do tell , and if you alter it don't be shy to show your modifications or additions!
Attached Files
File Type: txt functions_items.txt (5.5 KB, 456 views)
File Type: txt functions_cached_database.txt (4.5 KB, 442 views)
File Type: txt npcItemCache.txt (3.8 KB, 420 views)
__________________
Follow my work on social media post-Graal:Updated august 2025.

Last edited by xXziroXx; 09-22-2008 at 09:30 PM..
Reply With Quote
  #2  
Old 09-22-2008, 09:42 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Very interesting system you got there, I must say.

It's very well done and I like the way you're synchronizing data between client and server. Requires a bit of experience with objects to handle the system though, but that isn't a bad thing. I also like the cache system.

Well done
__________________
Reply With Quote
  #3  
Old 09-22-2008, 11:44 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
I was impressed the first time you showed it to me. It's a very very nice system, I wish I had time to script something to replicate it because it looks so fun to script.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #4  
Old 09-22-2008, 11:57 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
Very nice, good job!
__________________
Reply With Quote
  #5  
Old 09-23-2008, 12:12 AM
warmaster70229 warmaster70229 is offline
Banned
Join Date: Jun 2007
Location: Texas ;D
Posts: 111
warmaster70229 is on a distinguished road
Send a message via AIM to warmaster70229 Send a message via MSN to warmaster70229 Send a message via Yahoo to warmaster70229
Very nice my friend, although I'd say I enjoy the RPG system I'm planning more, this is very awesome!
Reply With Quote
  #6  
Old 09-23-2008, 12:10 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Thanks guys.

Quote:
Originally Posted by warmaster70229 View Post
I enjoy the RPG system I'm planning more
Am I the only one not understanding what that's got to do with an item system?
__________________
Follow my work on social media post-Graal:Updated august 2025.

Last edited by xXziroXx; 09-23-2008 at 03:51 PM..
Reply With Quote
  #7  
Old 09-23-2008, 02:57 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by xXziroXx View Post
Am I the only one not understand what that's got to do with an item system?
No idea.. he's living in his own world.
__________________
Reply With Quote
  #8  
Old 09-23-2008, 03:10 PM
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
Nice script. Open source is win.
Quote:
Originally Posted by xXziroXx View Post
All items are stored in .arc files (yes, laugh away Chris and Inverness)
When I read that I knew what I was gonna say in my reply but you had to kill it.

By the way, I have a few notes after looking through the script.
First, if the npc name is ItemCache then you can simply use ItemCache.varname rather than findnpc("ItemCache").varname.
Second, would be a bit more efficient to check if an object is existing by using varname.type() == 2 rather than varname.objecttype() == "TStaticVar".
Third, Stefan made it so you can use var = {} rather than var = new[0]
__________________
Reply With Quote
  #9  
Old 09-23-2008, 03:26 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Inverness View Post
First, if the npc name is ItemCache then you can simply use ItemCache.varname rather than findnpc("ItemCache").varname.
I prefer declaring findNPC() manually for code readability. Doesn't really matter.


Quote:
Originally Posted by Inverness View Post
Second, would be a bit more efficient to check if an object is existing by using varname.type() == 2 rather than varname.objecttype() == "TStaticVar".
I honestly don't think it makes any difference.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #10  
Old 09-23-2008, 03:29 PM
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
Quote:
Originally Posted by xXziroXx View Post
I honestly don't think it makes any difference.
If you don't think there is a difference between comparing two integers and comparing two strings then I have nothing else to say.
__________________
Reply With Quote
  #11  
Old 09-23-2008, 03:30 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Quote:
Originally Posted by Inverness View Post
If you don't think there is a difference between comparing two integers and comparing two strings then I have nothing else to say.
Let me rephrase that, I don't think there's a notable difference.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #12  
Old 09-23-2008, 03:50 PM
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
Quote:
Originally Posted by xXziroXx View Post
Let me rephrase that, I don't think there's a notable difference.
Of course not. I'm just saying it's a cleaner way to do it.
__________________
Reply With Quote
  #13  
Old 09-23-2008, 07:16 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
posting something I wrote without asking first? :o theft!

lol j/k jsut kiddin.

Quote:
Originally Posted by xXziroXx View Post
Am I the only one not understanding what that's got to do with an item system?
probly egomania


Quote:
Originally Posted by Inverness View Post
Of course not. I'm just saying it's a cleaner way to do it.
nitpicking is substitute for useful criticism for people who arent good at high level and-or abstract scripting
__________________
Testbed user: I figured since I can never find any scripters it was time to take desperate measures...and...TEACH MYSELF 0.0
Reply With Quote
  #14  
Old 09-24-2008, 01:48 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
Quote:
Originally Posted by Inverness View Post
Third, Stefan made it so you can use var = {} rather than var = new[0]
var = new [0]; is better style, especially for those who come from a variety of other high level languages. It's quite possible to also do things like var = new [3]; and so on to already allocate the space you need/make it clearer just how big the array should be at first glance.

I personally believe Stefan shouldn't have added var = {}; - it's quite bad style.
Reply With Quote
  #15  
Old 09-24-2008, 06:16 PM
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
Quote:
Originally Posted by DrakilorP2P View Post
nitpicking is substitute for useful criticism for people who arent good at high level and-or abstract scripting
I really don't care what you think of my programming ability, I'm quite confident in it.

Quote:
Originally Posted by Skyld View Post
var = new [0]; is better style, especially for those who come from a variety of other high level languages. It's quite possible to also do things like var = new [3]; and so on to already allocate the space you need/make it clearer just how big the array should be at first glance.

I personally believe Stefan shouldn't have added var = {}; - it's quite bad style.
I suppose it is a personal preference thing. I believe I will consider using new[0] instead since it does fit in better with new[n] as you said.
__________________
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 08:57 PM.


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