Chaos Project

RPG Maker => RPG Maker Scripts => RMXP Script Database => Topic started by: mad.array on October 12, 2011, 10:48:31 pm

Title: [XP] Perishable Items
Post by: mad.array on October 12, 2011, 10:48:31 pm
Perishable Items
Authors: mad.array
Version: 1.0a
Type: Custom Item System
Key Term: Custom Item System



Introduction

This script adds a 'best before date' to any item that you choose. After you have bought/found an item, it will only have a limited shelf life, before it goes off. Now your heroes can experience stomach bugs from drinking Potions that are past their Use By Date and experience the wonderful smell of musty old perfume!



Features




Screenshots
No screenshots, as rotten items are not pleasant to look at!



Script

Insert below default scripts and above Main. Jiggle it about it if it doesn't work!
Spoiler: ShowHide

#==============================================================================
# Perishable Items (Version 1.0a) by mad.array
#------------------------------------------------------------------------------
#  Use this script to give your items a limited shelf life.
#  To use, add a 'rotten' version of your item in the database, then
#  find def calculate_perish_time and add a case for your original item number.
#  e.g. (Item 1 = Potion, Item 33 = Potion [Rotten])
#  ---------------
#  when 1
#     return 400
# ----------------
# This is the amount of time, in frames, that your item will last before going
# off. Now 400 frames is only 20 seconds, so I'd use a larger number.
#
# Next, find def rot_time and again add a case for your item. Only this time
# it will be returning the item that it will 'rot' into.
# e.g. 2
#  ---------------
#  when 1
#     return 33
# ----------------
#
# Now, enjoy. 400 Frames after picking up a Potion, it will be removed from
# the item screen and replaced with it's rotten replacement. You can have your
# rotten equivalent do anything that a normal item would do, so go nuts!
#==============================================================================

class Game_Party
 attr_accessor    :perishablelist
 
 alias perinitialize initialize
 def initialize
   perinitialize
   @perishablelist = []
 end
 
 alias perish_gain_item gain_item
 def gain_item(item_id, n)
   perish_gain_item(item_id, n)
   for i in 0...n
     pt = calculate_perish_time(item_id)
     if pt !=false
       pt += Graphics.frame_count
       @perishablelist[item_id] = [] if @perishablelist[item_id] == nil
       @perishablelist.push[item_id].push(pt)
     end
   end
 end
 
 alias perish_lose_item lose_item
 def lose_item(item_id, n)
   perish_lose_item(item_id, n)
   for i in 0...n
     @perishablelist[item_id].delete_at(0) if @perishablelist[item_id] != nil
   end
 end
 
 def calculate_perish_time(item_id)
   case item_id
   #when 1
     #return 400
   end
   return false
 end
end

class Window_Item < Window_Selectable
 
 alias perish_draw draw_item
 def draw_item(index)
   perish_draw(index)
   item = @data[index]
   if item.is_a?(RPG::Item) && $game_party.perishablelist[item.id] != nil
     number = $game_party.item_number(item.id)
     changeamt = 0
     for i in 0...number
       if $game_party.perishablelist[item.id][i] < Graphics.frame_count
         changeamt +=1
       end
     end
     if changeamt > 0
       $game_party.gain_item(rot_item(item.id), changeamt)
       $game_party.lose_item(item.id, changeamt)
       refresh
     end
   end
 end
 
 def rot_item(item_id)
   case item_id
   #when 1
     #return 33
   end
   return false
 end
end



Instructions

Instructions are included in the script, but I'll put some here as well.

Once the script is inserted, create a 'rotten' version of your perishables items in the Database and adjust the properties as you see fit. For example have a Potion [Rotten] take away 500 HP rather than restoring it. Then, in the script, find the def calculate_rot_time and add a case for your original item id, returning the number of frames it will take to go off. In this example we'll be using the Potion:


when 1
   return 400


Note that by default there are 20 frames per second in RMXP, so the Potion would go off 20 seconds after you pick it up... I'd advise using a larger number.

Now that that's done, find def rot_time and again add a case with your original item id. Only this time we'll be returning the item that will be replacing our stinky Potion. In this example it's Item 33 (which doesn't exist by default in the Database):


when 1
   return 33


Et voila! Each Potion that you pick up will go off after 20 seconds... so I'd use them fast!


Compatibility

Not tested for compatability. May not work with other Custom Item Systems. Contact me if you are having compatability issues and if I can I'll help out.


Credits and Thanks

Please credit mad.array if you use this script.

newolds Configuration program can be found here http://www.megaupload.com/?d=XT8W253I
If you use it, give credit to him for making your life less rotten! (See what I did there?!)



Author's Notes

I'm sure there is a better way of doing this, so if you can think of another method then please contact me.
Title: Re: [XP] Perishable Items
Post by: Xuroth on October 13, 2011, 03:43:46 am
You know, this is a great idea, mad.array! Excellent Script!
Title: Re: [XP] Perishable Items
Post by: AngryPacman on October 13, 2011, 08:14:02 am
Hey. I have limited time here, so I'll have to be quick about this.
I think it'd be easier if you stored the data in a module instead of those case methods. Something like this:
module RottenItems
  FRAMES = {
    1 => 400
    2 => 2000
    #etc.
  }
  ITEMS = {
    1 => 33
    2 => 34
    #etc.
  }
end

And replace the calculate_perish_time method with this:
def calculate_perish_time(item_id)
  return RottenItems::FRAMES[item_id].nil? false : RottenItems::FRAMES[item_id]
end

And replace the rot_item method with:
def rot_item(item_id)
  return RottenItems::ITEMS[item_id].nil? false : RottenItems::ITEMS[item_id]
end

And that should probably make it more user-friendly ;)

Would it be okay with you if I rewrote this for VX? It would be a pretty cool script.
Title: Re: [XP] Perishable Items
Post by: mad.array on October 13, 2011, 08:25:29 am
@AngryPacman: You know I never looked into using modules, for no reason other than the first time I saw one in RMXP (Way back when I were a lad) they confused me and I didn't know any better. I guess I should do a bit of experimentation. Yeah, you go ahead and rewrite it for VX. Usual credit bleh applies.
Title: Re: [XP] Perishable Items
Post by: ForeverZer0 on October 13, 2011, 12:19:12 pm
Using a module for the config is a good idea, but changing the case statements to a hashes is definitely not going to improve "user-friendliness".

As for the "rot_item" method a simple "return RottenItems::ITEMS[item_id]" will do just fine. "nil" is the same as "false" when Ruby is looking for a boolean value in an "if" statement.
Title: Re: [XP] Perishable Items
Post by: AngryPacman on October 14, 2011, 09:38:58 am
I find it easier to work with hashes than case methods, and I find that people like that too. I guess it's subjective. You are definitely correct about that .nil? thing, though. My syntax was even wrong it that post XD
I don't like fighting with Zer0, because he's persistent and always sees to it that he wins, so I'll just leave it at that. He's also probably right.

On a side-note, I think Zer0's post-count is the same as the year the person in his avatar was born in. Or their age. O.o
Title: Re: [XP] Perishable Items
Post by: Blizzard on October 14, 2011, 09:51:37 am
The difference between using a hash and using case-when statements is that with a hash you have to think like a programmer and with case-when statements, you just have to be able to read. That's why for the average user it's easier to use case-when statements.
Title: Re: [XP] Perishable Items
Post by: AngryPacman on October 14, 2011, 10:40:23 am
That's really only the case if you don't provide any explanation of syntax, which everyone should. But I do so your reasoning, it makes perfect sense, I just don't like that it's a fact. But it's true, people are lazy and just want something simple (when done with this in mind, a case-when method could look exactly like English - return case x when y then z). I get that.
I just prefer hashes. That's it.
Title: Re: [XP] Perishable Items
Post by: Blizzard on October 14, 2011, 11:00:48 am
I prefer hashes for that kind of thing myself. If you take a look at the unofficial configuration part of RMX-OS, you'll seen nothing but hashes and constants.
Title: Re: [XP] Perishable Items
Post by: ForeverZer0 on October 14, 2011, 12:34:06 pm
Yeah, I personally don't care for myself which it is, but most people who use scripts aren't scripters and find case statements easier to overview and configure.

Aside from that, it usually doesn't matter. I agree it is more of a preference.
Title: Re: [XP] Perishable Items
Post by: LiTTleDRAgo on October 17, 2011, 05:41:08 am
Quotedef rot_item(item_id)
    case item_id
    #when 1
      #return 33
    end
    return fasle
  end


typo?
Title: Re: [XP] Perishable Items
Post by: mad.array on October 17, 2011, 02:42:49 pm
Thanks. Typo fixed. Don't adjust your televisions!

EDIT: I've decided to leave the case... return method for the time/new item methods. I know there are a few options open but I think no matter which I choose there will always be a debate. As mentioned, the case method seems the closest to 'English'. Anyone wishing to change to a module, either with hashes or other methods, can use the steps outlined in the earlier posts.

Thanks for giving me something to think about though!
Title: Re: [XP] Perishable Items
Post by: newold on November 02, 2011, 08:41:12 am
Cool script. I made a visual editor to config your script. Now is extremely easy to set for someone who does not know RGSS xD.

Download it from Megaupload:http://www.megaupload.com/?d=XT8W253I (http://www.megaupload.com/?d=XT8W253I)
Download it from Multi-upload:http://www.multiupload.com/3Q0EP50990 (http://www.multiupload.com/3Q0EP50990)

You can see the visual configurator tool in this video:

http://www.youtube.com/watch?v=1uGw27sjqzg


(http://www.youtube.com/watch?v=1uGw27sjqzg (http://www.youtube.com/watch?v=1uGw27sjqzg))

if you want sorce code ask me ;)

INSTRUCTION:

download file, unzip, copy Config Perishable Items.exe to proyect's folder and run. RPG MAKER XP EDITOR must be closed. This editor not use the original script. You only must open the editor and press save button and it will work fine. You dont need any script more to work ;)
Title: Re: [XP] Perishable Items
Post by: mad.array on January 12, 2012, 11:09:11 am
@newold: That. Is. Awesome.

I did consider a configuration program but it's not my speciality (If I could be said to have one). I'll add a request for credit to the main post, as well as a link, if you want.
Title: Re: [XP] Perishable Items
Post by: newold on January 12, 2012, 12:23:28 pm
Quote from: mad.array on January 12, 2012, 11:09:11 am
@newold: That. Is. Awesome.

I did consider a configuration program but it's not my speciality (If I could be said to have one). I'll add a request for credit to the main post, as well as a link, if you want.


ok, perfect!  :D