Advertisement
Guest User

Shopaholic Page 4

a guest
Jul 29th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.43 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # page 4 of 12
  3. # Shopoholic v 2.0
  4. # code by cmpsr2000
  5. # available exclusively @ rpgrevolution.com/forums
  6. # Released June 25, 2008
  7. #
  8. #-------------------------------------------------------------------------------
  9. #-------------------------------------------------------------------------------
  10. # This class implements shopkeeper inventories and provides access to true price
  11. # calculations. The instance of this class is $game_shopping
  12. #-------------------------------------------------------------------------------
  13. #-------------------------------------------------------------------------------
  14. #
  15. # IMPORTANT CUSOMIZATION INFORMATION:
  16. # There are 2 ways to call the shopping interface. Both are demonstrated
  17. # in the attached demo game.
  18. #
  19. # Method #1: Built-in Shop Proccessing (easiest for non-scripters)
  20. #
  21. # METHOD #1 ONLY WORKS FOR BASIC SHOPS!
  22. #
  23. # In order to use the preexisting shop interface call from an event, you
  24. # must first provide values to the following fields:
  25. #
  26. # $game_shopping.currentShopID:
  27. # Integer representing the ID number of the shop (starts at 0)
  28. #
  29. # $game_shopping.currentDiscountRate
  30. # Float point representing the discount on used items. (e.g.if the shop
  31. # sells used items at 80% of cost, the value is 0.8 )
  32. #
  33. # $game_shopping.currentSellRate
  34. # Float point representing the percentage of the value the shopkeeper pays
  35. # the player for items. (e.g. If the shopkeeper pays 50% of the item's
  36. # worth, the value is 0.5)
  37. #
  38. # $game_shopping.currentMarkupRate
  39. # Float point representing the shopkeeper's markup (or markdown) of the
  40. # selling prices. For example, If the shopkeeper is greedy and wants to
  41. # markup his items 20% (120% of the item's price in the DB) then the value
  42. # is 1.2. If he is generous and sells his items 10% under cost (90% of the
  43. # item's price in the DB) then the value is 0.9
  44. #
  45. # $game_shopping.currentTaxRate
  46. # Float point representing the tax rate of the shop. (e.g. if the shop has
  47. # a 3% tax rate, the value is 0.03)
  48. #
  49. # $game_shopping.currentInflationRate
  50. # Float point representing the price increase given to items in high demand.
  51. # (e.g. if the shop keeper marks up items 20% when the stock runs low, the
  52. # value is 0.2)
  53. #
  54. # After you have set these variables, you can then use the shop processing
  55. # button from the event wizard.
  56. #
  57. # Method #2: Calling the shopping scene manually (easiest for scripters)
  58. #
  59. # METHOD #2 IS REQUIRED FOR ADVANCED SHOPS!
  60. #
  61. # You can call the shopping scene manually by passing all the above values
  62. # in the constructor. You MUST first define the values of the items in the
  63. # shop before making the shop call!
  64. #
  65. # The format for the shop_goods are:
  66. # $game_temp.shop_goods = [ [ITEM_TYPE1, ITEM_ID1, ITEM_LIMIT1],
  67. # [ITEM_TYPE2, ITEM_ID2, ITEM_LIMIT2],
  68. # etc...]
  69. # ITEM_TYPE: 0 for RPG::Item
  70. # 1 for RPG::Weapon
  71. # 2 for RPG::Armor
  72. # 3 for RPG::Skill <----advanced shops only
  73. # 4 for RPG::Class <----advanced shops only
  74. # 5 for RPG::Actor <----advanced shops only
  75. # ITEM_ID: The ID number of the item in the DB
  76. # ITEM_LIMIT: The max the shop can sell to the player. Set to -1 for no
  77. # limit.
  78. #
  79. # The format for the scene call is:
  80. # $scene = Scene_Shop.new(shopID, discountRate, sellRate,
  81. # markupRate, taxRate, InflationRate,
  82. # restockRate, continuousRestock)
  83. #
  84. # (all the type rules from method #1 apply to this constuctor as well!)
  85. #
  86. # restockRate: How long, in minutes, to wait before the store
  87. # "restocks" its items. (e.g. a value of 90 is 1 hour
  88. # and 30 minutes of playtime) Set to 0 to disable
  89. # restocking completely!
  90. #
  91. # continuousRestock: Boolean (true/false) that determines whether the shop
  92. # restocks continuously. Setting this to true causes a
  93. # shop to restock at the restockRate regardless of the
  94. # player's actions. Setting this to false causes the
  95. # shop to "call" for a restock after the player visits
  96. # a fully stocked store.
  97. #
  98. # If your restockRate is 30 minutes and
  99. # continuousRestock is true:
  100. # When the player visits the shop for the first time
  101. # the timer will start. If the player visits again in
  102. # 45 minutes, The store will be restocked. If the
  103. # player comes back 15 minutes after that, the store
  104. # will be restocked again.
  105. #
  106. # But, if your restockRate is 30 minutes and
  107. # continuousRestock is false:
  108. # When the player visits the shop for the first time
  109. # the timer will start. If the player visits again in
  110. # 45 minutes, the store will be restocked, but the
  111. # timer will reset to 30 minutes! If the player comes
  112. # back 15 minutes later, there will still be 15
  113. # minutes left on the restock timer! This is akin to
  114. # the shopkeeper calling in an order for more stock
  115. # when the player shows up to shop.
  116. #
  117. #-------------------------------------------------------------------------------
  118. class Game_Shopping
  119.  
  120. attr_accessor :shopUsedItems
  121. attr_accessor :shopLimitedItems
  122. attr_accessor :shopRestockTimers
  123. attr_accessor :currentShopID
  124. attr_accessor :currentDiscountRate
  125. attr_accessor :currentSellRate
  126. attr_accessor :currentMarkupRate
  127. attr_accessor :currentTaxRate
  128. attr_accessor :currentInflationRate
  129. attr_reader :allowRestockWhileShopping
  130. attr_reader :shopsAcceptDebit
  131. attr_reader :allowTax
  132. attr_reader :exhaustableSkills
  133. attr_reader :skillPrices
  134. attr_reader :classPrices
  135. attr_reader :actorPrices
  136. attr_reader :demandAmount
  137. #-----------------------------------------------------------------------------
  138. # Creates the $game_shopping object
  139. #-----------------------------------------------------------------------------
  140. def initialize
  141. #---------------------------------------------------------------------------
  142. # Set this to true if you would like the shops to restock themselves while
  143. # the player is currently shopping (this will throw up a message to the
  144. # player to notify him of the restock)
  145. #---------------------------------------------------------------------------
  146. @allowRestockWhileShopping = true
  147. #---------------------------------------------------------------------------
  148. # If you want to be able to restock skills, set this to true. For example,
  149. # if you are building a game where a skill can only be used a certain number
  150. # of times before being replenished(e.g. Final Fantasy VIII), set this to
  151. # true.
  152. #---------------------------------------------------------------------------
  153. @exhaustableSkills = false
  154. #---------------------------------------------------------------------------
  155. # Set this to false if you do not wish to use tax. Make sure to set your
  156. # tax rates to 0!
  157. #---------------------------------------------------------------------------
  158. @allowTax = true
  159. #---------------------------------------------------------------------------
  160. # Set this to false if you do not want shops to accept credit (IE, checks,
  161. # Debit Card or whatever item is linked to checking accounts)
  162. #---------------------------------------------------------------------------
  163. @shopsAcceptDebit = true
  164. #---------------------------------------------------------------------------
  165. # Set this to the item ID of the item required to enable debit purchases.
  166. #---------------------------------------------------------------------------
  167. @debitItemID = 21
  168. #---------------------------------------------------------------------------
  169. # If you want a single icon for all classes or actors, you can set that
  170. # here. If you want to assign icons on a per-class or per-actor basis, set
  171. # the universal to nil, and define the individual ones below.
  172. #---------------------------------------------------------------------------
  173. @universalClassIcon = nil
  174. @universalActorIcon = 1903
  175. #---------------------------------------------------------------------------
  176. # Shopkeepers will start raising their prices on new items when they have
  177. # this number or LESS in their inventory (for limited items)
  178. #---------------------------------------------------------------------------
  179. @demandAmount = 10
  180.  
  181. initShopkeepers
  182. initPrices
  183. initDiscovery
  184. initIcons
  185. initDescriptions
  186. end
  187. #-----------------------------------------------------------------------------
  188. # Sets price data for advanced shop objects.
  189. #
  190. # PLEASE NOTE: Skills, classes and actors cannot be sold, only bought!
  191. #-----------------------------------------------------------------------------
  192. def initPrices
  193. #---------------------------------------------------------------------------
  194. # Set prices for skills here. Start at position 1. Each number corresponds
  195. # to the ID number of the skill in the database. If you don't wish to sell
  196. # a skill, use 'nil' in its place. These skills will be discovered the
  197. # usual way instead of being unlocked from purchasing.
  198. #---------------------------------------------------------------------------
  199. @skillPrices = [ #DON'T TOUCH THIS ONE!
  200. nil,
  201.  
  202. #Skills 1-10
  203. 100, 200, 300, nil, nil, nil, nil, nil, nil, nil,
  204.  
  205. #Skills 11-20
  206. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  207.  
  208. #Skills 21-30
  209. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  210.  
  211. #Skills 31-40
  212. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  213.  
  214. #Skills 41-50
  215. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  216.  
  217. #Skills 51-60
  218. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  219.  
  220. #Skills 61-70
  221. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  222.  
  223. #Skills 71-80
  224. nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
  225.  
  226. #Skills 81-82
  227. nil, nil
  228. ]
  229. #---------------------------------------------------------------------------
  230. # Set prices for classes here. Start at position 1. Each number corresponds
  231. # to the ID number of the class in the database. If you don't wish to sell
  232. # a class, use 'nil' in its place. These classes will be discovered the
  233. # usual way instead of being unlocked from purchasing.
  234. #---------------------------------------------------------------------------
  235. @classPrices = [ #DON'T TOUCH THIS ONE!
  236. nil,
  237.  
  238. #Classes 1-8
  239. nil, 2000, 3000, 4000, 5000, 6000, 7000, 8000
  240. ]
  241. #---------------------------------------------------------------------------
  242. # Set prices for actors here. Start at position 1. Each number corresponds
  243. # to the ID number of the actor in the database. If you don't wish to sell
  244. # an actor, use 'nil' in its place. These actors will be discovered the
  245. # usual way instead of being unlocked from purchasing.
  246. #---------------------------------------------------------------------------
  247. @actorPrices = [ #DON'T TOUCH THIS ONE!
  248. nil,
  249.  
  250. #Actors 1-8
  251. nil, 2000, 3000, 4000, 5000, 6000, 7000, 8000
  252. ]
  253. end
  254. #-----------------------------------------------------------------------------
  255. # Initializes the icons being used for classes and actors.
  256. #-----------------------------------------------------------------------------
  257. def initIcons
  258. if @universalClassIcon != nil
  259. classIcons = []
  260. for x in @classPrices
  261. classIcons.push(@universalClassIcon)
  262. end
  263. else
  264. #-------------------------------------------------------------------------
  265. # If you have individual icons for each class, define them here!
  266. #-------------------------------------------------------------------------
  267. classIcons = [ #DON'T TOUCH THIS ONE!
  268. nil,
  269.  
  270. #Classes 1-8
  271. 1154, 1897, 1682, 1899, 549, 547, 1898, 1630
  272. ]
  273. end
  274.  
  275. if @universalActorIcon != nil
  276. actorIcons = []
  277. for x in @actorPrices
  278. actorIcons.push(@universalActorIcon)
  279. end
  280. else
  281. #-------------------------------------------------------------------------
  282. # If you have individual icons for each actor, define them here!
  283. #-------------------------------------------------------------------------
  284. actorIcons = [ #DON'T TOUCH THIS ONE!
  285. nil,
  286.  
  287. #Actors 1-8
  288. 1, 2, 3, 4, 5, 6, 7, 8
  289. ]
  290. end
  291. for x in 1..$data_classes.length - 1
  292. $data_classes[x].icon_index = classIcons[x]
  293. end
  294. for x in 1..$data_actors.length - 1
  295. $data_actors[x].icon_index = actorIcons[x]
  296. end
  297. end
  298. #-----------------------------------------------------------------------------
  299. # Initializes help text for classes and actors
  300. #-----------------------------------------------------------------------------
  301. def initDescriptions
  302. #---------------------------------------------------------------------------
  303. # Define the text that will appear in the shop help window for classes here:
  304. #---------------------------------------------------------------------------
  305. classDescriptions = [ #DON'T TOUCH THIS ONE!
  306. nil,
  307.  
  308. #Class 1 (Paladin)
  309. "Stout defender and holy warrior.",
  310.  
  311. #Class 2 (Warrior)
  312. "Me hit things hard!",
  313.  
  314. #Class 3 (Priest)
  315. "Has anyone seen my alterboy?",
  316.  
  317. #Class 4 (Magician)
  318. "Poof! You're a spider!",
  319.  
  320. #Class 5 (Knight)
  321. "Yea, I smiteth mine enemies.",
  322.  
  323. #Class 6 (Dark Knight)
  324. "None shall pass!",
  325.  
  326. #Class 7 (Grappler)
  327. "My chi is bigger than yours.",
  328.  
  329. #Class 8 (Thief)
  330. "I can haz ur gold?"
  331. ]
  332. #---------------------------------------------------------------------------
  333. # Define the text that will appear in the shop help window for actors here:
  334. #---------------------------------------------------------------------------
  335. actorDescriptions = [ #DON'T TOUCH THIS ONE!
  336. nil,
  337.  
  338. #Actor 1
  339. "Actor info goes here!",
  340.  
  341. #Actor 2
  342. "Actor info goes here!",
  343.  
  344. #Actor 3
  345. "Actor info goes here!",
  346.  
  347. #Actor 4
  348. "Actor info goes here!",
  349.  
  350. #Actor 5
  351. "Actor info goes here!",
  352.  
  353. #Actor 6
  354. "Actor info goes here!",
  355.  
  356. #Actor 7
  357. "Actor info goes here!",
  358.  
  359. #Actor 8
  360. "Actor info goes here!",
  361. ]
  362. for x in 1..$data_classes.length - 1
  363. $data_classes[x].description = classDescriptions[x]
  364. end
  365. for x in 1..$data_actors.length - 1
  366. $data_actors[x].description = actorDescriptions[x]
  367. end
  368. end
  369. #-----------------------------------------------------------------------------
  370. # Creates the shopkeeper inventories and sets up the timers for restocking
  371. #-----------------------------------------------------------------------------
  372. def initShopkeepers
  373. #---------------------------------------------------------------------------
  374. # IMPORTANT:
  375. # You will get an error when buying or selling used items if you try to
  376. # pass a shopID that is higher than the number of @totalShopkeepers - 1.
  377. # Set this to the number of shops in your game, or HIGHER! Setting higher
  378. # will waste memory, but you can always count up the number of shops you
  379. # have before shipping your finished game and change it back to the
  380. # correct number!
  381. #---------------------------------------------------------------------------
  382. @totalShopkeepers = 5
  383.  
  384. @shopUsedItems = []
  385. @shopLimitedItems = []
  386. @shopRestockTimers = []
  387. for x in 0..@totalShopkeepers - 1
  388. @shopUsedItems [x] = {}
  389. @shopLimitedItems [x] = {}
  390. @shopRestockTimers[x] = 0
  391. end
  392. end
  393. #-----------------------------------------------------------------------------
  394. # Initilizes discovery for available skills, classes and actors
  395. #-----------------------------------------------------------------------------
  396. def initDiscovery
  397. for x in 1..$data_skills.length - 1
  398. if @skillPrices[x] == nil
  399. $data_skills[x].discover
  400. else
  401. $data_skills[x].hide
  402. end
  403. end
  404. for x in 1..$data_classes.length - 1
  405. if @classPrices[x] == nil
  406. $data_classes[x].discover
  407. else
  408. $data_classes[x].hide
  409. end
  410. end
  411. for x in 1..$data_actors.length - 1
  412. if @actorPrices[x] == nil
  413. $data_actors[x].discover
  414. else
  415. $data_actors[x].hide
  416. end
  417. end
  418. end
  419. #-----------------------------------------------------------------------------
  420. # Calculates the item's selling price based on the current shop's rates.
  421. # item: The item whose price is being requested
  422. # rates: A shop rates array: [discountRate, sellRate, markupRate,
  423. # taxRate, inflationRate]
  424. # RETURNS Integer (Fixnum)
  425. #-----------------------------------------------------------------------------
  426. def sellItemPrice(item, rates)
  427. sellRate = rates[1]
  428. shopMarkup = rates[2]
  429. price = item.price * sellRate * shopMarkup
  430. return price.ceil
  431. end
  432. #-----------------------------------------------------------------------------
  433. # Calculates the item's purchase price based on the current shop's rates.
  434. # item: The item whose price is being requested
  435. # rates: A shop rates array: [discountRate, sellRate, markupRate,
  436. # taxRate, inflationRate]
  437. # includeTax: Boolean indicating whether or not the calculation should
  438. # include tax
  439. # used: Boolean indicating if the item is used
  440. # inflated: Boolean indicating if the shop is running low on the item
  441. # RETURNS Integer (Fixnum)
  442. #-----------------------------------------------------------------------------
  443. def shopPrice(item, rates, includeTax = false, used = false, inflated = false)
  444. discount = rates[0]
  445. shopMarkup = rates[2]
  446. shopTax = rates[3]
  447. shopInflation = rates[4]
  448. if used
  449. price = item.price * shopMarkup * discount
  450. elsif inflated
  451. price = (item.price * shopMarkup) + (item.price * shopInflation)
  452. else
  453. price = item.price * shopMarkup
  454. end
  455. price = includeTax ? price.ceil + (price * shopTax).ceil : price.ceil
  456. return price
  457. end
  458. #-----------------------------------------------------------------------------
  459. # resets the restock timer for a particular shop
  460. # shopID: The ID of the shop
  461. # restockRate: The shop's rate of restocking, in minutes
  462. # continuousRestock: Whether the shop restocks continuously
  463. #-----------------------------------------------------------------------------
  464. def resetTimer(shopID, restockRate, continuousRestock)
  465. if restockRate == 0
  466. @shopRestockTimers[shopID] = 1
  467. else
  468. now = Graphics.frame_count / Graphics.frame_rate
  469. lastRestockTime = @shopRestockTimers[shopID]
  470. while lastRestockTime < now - (restockRate * 60)
  471. lastRestockTime += restockRate * 60
  472. end
  473. @shopRestockTimers[shopID] = continuousRestock ? lastRestockTime : now
  474. end
  475. end
  476. #-----------------------------------------------------------------------------
  477. # Returns the item needed to allow purchases from checking Accounts.
  478. # RETURNS: RPG::UsableItem
  479. #-----------------------------------------------------------------------------
  480. def debitItem
  481. return $data_items[@debitItemID]
  482. end
  483. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement