Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Item System (https://forums.graalonline.com/forums/showthread.php?t=81961)

xXziroXx 09-22-2008 09:17 PM

Item System
 
3 Attachment(s)
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!

Chompy 09-22-2008 09:42 PM

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 ;)

Tigairius 09-22-2008 11:44 PM

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.

cbk1994 09-22-2008 11:57 PM

Very nice, good job!

warmaster70229 09-23-2008 12:12 AM

Very nice my friend, although I'd say I enjoy the RPG system I'm planning more, this is very awesome!

xXziroXx 09-23-2008 12:10 PM

Thanks guys. :)

Quote:

Originally Posted by warmaster70229 (Post 1425591)
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? :oo:

Chompy 09-23-2008 02:57 PM

Quote:

Originally Posted by xXziroXx (Post 1425692)
Am I the only one not understand what that's got to do with an item system? :oo:

No idea.. he's living in his own world.

Inverness 09-23-2008 03:10 PM

Nice script. Open source is win.
Quote:

Originally Posted by xXziroXx (Post 1425530)
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]

xXziroXx 09-23-2008 03:26 PM

Quote:

Originally Posted by Inverness (Post 1425710)
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 (Post 1425710)
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.

Inverness 09-23-2008 03:29 PM

Quote:

Originally Posted by xXziroXx (Post 1425715)
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.

xXziroXx 09-23-2008 03:30 PM

Quote:

Originally Posted by Inverness (Post 1425717)
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.

Inverness 09-23-2008 03:50 PM

Quote:

Originally Posted by xXziroXx (Post 1425718)
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.

DrakilorP2P 09-23-2008 07:16 PM

posting something I wrote without asking first? :o theft!

lol j/k jsut kiddin. ^^

Quote:

Originally Posted by xXziroXx (Post 1425692)
Am I the only one not understanding what that's got to do with an item system? :oo:

probly egomania


Quote:

Originally Posted by Inverness (Post 1425721)
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 ;)

Skyld 09-24-2008 01:48 PM

Quote:

Originally Posted by Inverness (Post 1425710)
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.

Inverness 09-24-2008 06:16 PM

Quote:

Originally Posted by DrakilorP2P (Post 1425744)
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 (Post 1425902)
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.


All times are GMT +2. The time now is 08:11 AM.

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