Advertisement
Guest User

Shopaholic Page 1

a guest
Jul 29th, 2012
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.52 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # page 1 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. module Vocab
  10. BankAccounts = "Accounts"
  11. BankVault = "Vault"
  12. BankCancel = "Cancel"
  13. BankDeposit = "Deposit"
  14. BankWithdraw = "Withdraw"
  15. BankTransfer = "Transfer"
  16. #-----------------------------------------------------------------------------
  17. # The variables below probably belong in a banking module, I'll update it
  18. # when I'm not feeling so lazy!
  19. #-----------------------------------------------------------------------------
  20. # Icon indexes for each account type.
  21. #-----------------------------------------------------------------------------
  22. BankAccountIcons = [ #Savings Icon
  23. 1631,
  24.  
  25. #Checking Icon
  26. 1666,
  27.  
  28. #Loan Icon
  29. 1630
  30. ]
  31. #-----------------------------------------------------------------------------
  32. # Help window text for accounts.
  33. #-----------------------------------------------------------------------------
  34. BankDescriptions = [ #Savings Description
  35. "A high yield savings account.",
  36.  
  37. #Checking Description
  38. "Provides credit for shopkeepers.",
  39.  
  40. #Loan Description
  41. ""
  42. ]
  43. #-----------------------------------------------------------------------------
  44. # Account names.
  45. #-----------------------------------------------------------------------------
  46. BankAccountNames = [ #Savings
  47. "Savings",
  48.  
  49. #Checking
  50. "Checking",
  51.  
  52. #Loan
  53. "Loan"
  54. ]
  55. end
  56. #-------------------------------------------------------------------------------
  57. # Updates the Scene_title class to add in the new game objects
  58. #-------------------------------------------------------------------------------
  59. class Scene_Title < Scene_Base
  60.  
  61. alias oldCreateGameObjectsShopoholic create_game_objects
  62. def create_game_objects
  63. oldCreateGameObjectsShopoholic
  64. $game_shopping = Game_Shopping.new
  65. $game_banking = Game_Banking.new
  66. end
  67. end
  68. #-------------------------------------------------------------------------------
  69. # updates scene file to allow persistance of the game objects accross sessions
  70. #-------------------------------------------------------------------------------
  71. class Scene_File < Scene_Base
  72.  
  73. alias oldWriteSaveShopoholic write_save_data
  74. def write_save_data(file)
  75. oldWriteSaveShopoholic(file)
  76. Marshal.dump($game_shopping, file)
  77. Marshal.dump($game_banking, file)
  78. end
  79.  
  80. alias oldReadSaveShopoholic read_save_data
  81. def read_save_data(file)
  82. oldReadSaveShopoholic(file)
  83. $game_shopping = Marshal.load(file)
  84. $game_banking = Marshal.load(file)
  85. end
  86. end
  87. #-------------------------------------------------------------------------------
  88. # The main redefinition, changes the shoping scene to include the new features
  89. #-------------------------------------------------------------------------------
  90. class Scene_Shop < Scene_Base
  91.  
  92. def initialize(*args)
  93. if args.length < 1
  94. initBasic
  95. else
  96. @shopID = args[0]
  97. @discountRate = args[1] #for selling used items back to the player
  98. @sellRate = args[2] #For buying items from the player
  99. @markupRate = args[3] #For greed or generosity
  100. @taxRate = args[4] #tax rate
  101. @inflationRate = args[5] #price increase on high-demand items
  102. @restockRate = args[6] #minutes between restockings
  103. @continuousRestock = args[7] #controlls restock timer behaviour
  104. @rates = [@discountRate, @sellRate, @markupRate,
  105. @taxRate, @inflationRate]
  106. end
  107. if (timeElapsed? and @restockRate > 0) or
  108. $game_shopping.shopRestockTimers[@shopID] == 0
  109. restock
  110. $game_shopping.resetTimer(@shopID, @restockRate, @continuousRestock)
  111. end
  112. setMoney
  113. end
  114.  
  115. def setMoney
  116. if $game_shopping.shopsAcceptDebit and $game_party.hasDebitItem
  117. @money = $game_party.netLiquidity
  118. else
  119. @money = $game_party.gold
  120. end
  121. end
  122.  
  123. def initBasic
  124. @shopID = $game_shopping.currentShopID
  125. @discountRate = $game_shopping.currentDiscountRate
  126. @sellRate = $game_shopping.currentSellRate
  127. @markupRate = $game_shopping.currentMarkupRate
  128. @taxRate = $game_shopping.currentTaxRate
  129. @inflationRate = $game_shopping.currentInflationRate
  130. @restockRate = 0
  131. @rates = [@discountRate, @sellRate, @markupRate,
  132. @taxRate, @inflationRate]
  133. end
  134.  
  135. def terminate
  136. super
  137. dispose_menu_background
  138. dispose_command_window
  139. @help_window.dispose
  140. @gold_window.dispose
  141. @dummy_window.dispose
  142. @buy_window.dispose
  143. @sell_window.dispose
  144. @number_window.dispose
  145. @details_window.dispose
  146. @message_window.dispose
  147. end
  148.  
  149. def start
  150. super
  151. create_menu_background
  152. create_command_window
  153. @help_window = Window_Help.new
  154. @gold_window = Window_Shop_Gold.new(384, 56)
  155. @dummy_window = Window_Base.new(0, 112, 544, 304)
  156. @buy_window = Window_ShopBuy.new(0, 112, @shopID, @rates)
  157. @buy_window.active = false
  158. @buy_window.visible = false
  159. @buy_window.help_window = @help_window
  160. @sell_window = Window_ShopSell.new(0, 112, 544, 304)
  161. @sell_window.active = false
  162. @sell_window.visible = false
  163. @sell_window.help_window = @help_window
  164. @number_window = Window_ShopNumber.new(0, 112)
  165. @number_window.active = false
  166. @number_window.visible = false
  167. @details_window = Window_Shop_Details.new(304, 112, 240, 304,
  168. @rates, @shopID)
  169. @details_window.visible = false
  170. @message_window = Window_Shop_Message.new(100, 200, 400, 72)
  171. @message_window.active = false
  172. @message_window.visible = false
  173. end
  174.  
  175. def update
  176. super
  177. update_menu_background
  178. @help_window.update
  179. @command_window.update
  180. @gold_window.update
  181. @dummy_window.update
  182. @buy_window.update
  183. @sell_window.update
  184. @number_window.update
  185. @details_window.update
  186. if @message_window.visible
  187. if Input.trigger?(Input::C) or Input.trigger?(Input::B)
  188. @message_window.visible = false
  189. end
  190. else
  191. if @command_window.active
  192. update_command_selection
  193. elsif @buy_window.active
  194. update_buy_selection
  195. elsif @sell_window.active
  196. update_sell_selection
  197. elsif @number_window.active
  198. update_number_input
  199. end
  200. end
  201. #check for restock if allowed
  202. if $game_shopping.allowRestockWhileShopping
  203. if timeElapsed? and @restockRate > 0
  204. restock
  205. $game_shopping.resetTimer(@shopID, @restockRate, @continuousRestock)
  206. @message_window.showRestockMessage
  207. @message_window.visible = true
  208. @buy_window.refreshNeeded = true
  209. end
  210. end
  211. end
  212.  
  213. def update_command_selection
  214. if Input.trigger?(Input::B)
  215. Sound.play_cancel
  216. $scene = Scene_Map.new
  217. elsif Input.trigger?(Input::C)
  218. case @command_window.index
  219. when 0 # buy
  220. Sound.play_decision
  221. @command_window.active = false
  222. @dummy_window.visible = false
  223. @buy_window.active = true
  224. @buy_window.visible = true
  225. @buy_window.refresh
  226. @details_window.visible = true
  227. @details_window.buyState = true
  228. when 1 # sell
  229. if $game_temp.shop_purchase_only
  230. Sound.play_buzzer
  231. else
  232. Sound.play_decision
  233. @command_window.active = false
  234. @dummy_window.visible = false
  235. @sell_window.active = true
  236. @sell_window.visible = true
  237. @sell_window.refresh
  238. @details_window.buyState = false
  239. end
  240. when 2 # Quit
  241. Sound.play_decision
  242. $scene = Scene_Map.new
  243. end
  244. end
  245. end
  246.  
  247. def update_buy_selection
  248. @details_window.item = @buy_window.item
  249. if Input.trigger?(Input::B)
  250. Sound.play_cancel
  251. @command_window.active = true
  252. @dummy_window.visible = true
  253. @buy_window.active = false
  254. @buy_window.visible = false
  255. @details_window.visible = false
  256. @details_window.item = nil
  257. @help_window.set_text("")
  258. return
  259. end
  260. if Input.trigger?(Input::C)
  261. @item = @buy_window.item
  262. if @item == nil
  263. Sound.play_buzzer
  264. return
  265. end
  266. if @item.is_a?(RPG::Skill) or @item.is_a?(RPG::Class) or
  267. @item.is_a?(RPG::Actor)
  268. number = 0
  269. resellNumber = $game_temp.shop_goods[@buy_window.index - 1][2]
  270. @price = $game_shopping.shopPrice(@item, @rates, true)
  271. else
  272. number = $game_party.item_number(@item)
  273. if itemIsUsed?
  274. resellNumber = $game_shopping.shopUsedItems[@shopID][@item]
  275. @price = $game_shopping.shopPrice(@item, @rates, true, true)
  276. else
  277. isInflated = false
  278. itemMax = $game_shopping.shopLimitedItems[@shopID][@item]
  279. if itemMax == nil or itemMax == false or
  280. itemMax == true or itemMax == -1
  281. resellNumber = 99
  282. else
  283. resellNumber = itemMax
  284. isInflated = itemMax <= $game_shopping.demandAmount
  285. end
  286. @price = $game_shopping.shopPrice(@item, @rates, true, false,
  287. isInflated)
  288. end
  289. end
  290. if @item == nil or @price > @money or number == 99
  291. Sound.play_buzzer
  292. else
  293. max = @price == 0 ? 99 : @money / @price
  294. max = [max, resellNumber, 99 - number].min
  295. if max == 0
  296. Sound.play_buzzer
  297. @message_window.showOutOfStockMessage(@item)
  298. @message_window.visible = true
  299. else
  300. Sound.play_decision
  301. if @item.is_a?(RPG::Skill)
  302. if $game_shopping.exhaustableSkills
  303. callNumberWindow(max)
  304. elsif not @item.discovered
  305. buyItem(1)
  306. else
  307. @message_window.showOutOfStockMessage(@item)
  308. @message_window.visible = true
  309. end
  310. elsif @item.is_a?(RPG::Class) or @item.is_a?(RPG::Actor)
  311. if not @item.discovered
  312. buyItem(1)
  313. else
  314. @message_window.showOutOfStockMessage(@item)
  315. @message_window.visible = true
  316. end
  317. else
  318. callNumberWindow(max)
  319. end
  320. end
  321. end
  322. end
  323. end
  324.  
  325. def callNumberWindow(max)
  326. @buy_window.active = false
  327. @buy_window.visible = false
  328. @number_window.set(@item, max, @price)
  329. @number_window.active = true
  330. @number_window.visible = true
  331. end
  332.  
  333. def update_sell_selection
  334. if Input.trigger?(Input::B)
  335. Sound.play_cancel
  336. @command_window.active = true
  337. @dummy_window.visible = true
  338. @sell_window.active = false
  339. @sell_window.visible = false
  340. @details_window.item = nil
  341. @help_window.set_text("")
  342. elsif Input.trigger?(Input::C)
  343. @item = @sell_window.item
  344. @details_window.item = @item
  345. if @item == nil or @item.price == 0
  346. Sound.play_buzzer
  347. else
  348. Sound.play_decision
  349. max = $game_party.item_number(@item)
  350. @price = $game_shopping.sellItemPrice(@item, @rates)
  351. @sell_window.active = false
  352. @sell_window.visible = false
  353. @number_window.set(@item, max, @price)
  354. @number_window.active = true
  355. @number_window.visible = true
  356. @details_window.visible = true
  357. end
  358. end
  359. end
  360.  
  361. def cancel_number_input
  362. Sound.play_cancel
  363. @number_window.active = false
  364. @number_window.visible = false
  365. case @command_window.index
  366. when 0 # Buy
  367. @buy_window.active = true
  368. @buy_window.visible = true
  369. when 1 # Sell
  370. @sell_window.active = true
  371. @sell_window.visible = true
  372. @details_window.visible = false
  373. end
  374. end
  375.  
  376. def decide_number_input
  377. Sound.play_shop
  378. @number_window.active = false
  379. @number_window.visible = false
  380. case @command_window.index
  381. when 0 # Buy
  382. buyItem(@number_window.number)
  383. when 1 # sell
  384. $game_party.gain_gold(@number_window.number * @price)
  385. $game_party.lose_item(@item, @number_window.number)
  386. itemHash = {@item => @number_window.number}
  387. $game_shopping.shopUsedItems[@shopID].update(itemHash)
  388. setMoney
  389. @gold_window.refresh
  390. @sell_window.refresh
  391. @sell_window.active = true
  392. @sell_window.visible = true
  393. @details_window.visible = false
  394. end
  395. end
  396.  
  397. def buyItem(amount)
  398. #take gold!
  399. cost = amount * @price
  400. if cost > $game_party.gold
  401. cost -= $game_party.gold
  402. $game_party.lose_gold($game_party.gold)
  403. for bank in $game_banking.bankAccounts
  404. for account in bank
  405. if account.type == 1 #checking
  406. if cost > account.balance
  407. cost -= account.balance
  408. account.balance = 0
  409. else
  410. account.withdraw(cost)
  411. cost = 0
  412. end
  413. end
  414. end
  415. break if cost == 0
  416. end
  417. else
  418. $game_party.lose_gold(cost)
  419. end
  420.  
  421. #Give item, or discover if needed
  422. $game_party.gain_item(@item, amount) if @item.is_a?(RPG::Item) or
  423. @item.is_a?(RPG::Weapon) or
  424. @item.is_a?(RPG::Armor)
  425. $data_skills [@item.id].discover if @item.is_a?(RPG::Skill)
  426. $data_classes [@item.id].discover if @item.is_a?(RPG::Class)
  427. $data_actors [@item.id].discover if @item.is_a?(RPG::Actor)
  428.  
  429. #update actors if skill discovery occured
  430. if @item.is_a?(RPG::Skill)
  431. for x in 1..$data_actors.length - 1
  432. $game_actors[x].refreshSkills
  433. end
  434. end
  435.  
  436. #update party if actor discovery occured
  437. if @item.is_a?(RPG::Actor)
  438. $game_party.add_actor(@item.id)
  439. end
  440.  
  441. if @item.is_a?(RPG::Skill) and $game_shopping.exhaustableSkills
  442. #-------------------------------------------------------------------------
  443. #If you are building a game with exhaustable skills, add code here
  444. #to increase the specific skill by the purchased amount.
  445. # Note: @item is a skill object in this context!
  446. #
  447. # The next version of shopoholic may implement this if there is
  448. # enough desire for the feature.
  449. #-------------------------------------------------------------------------
  450. end
  451.  
  452. itemMax = $game_shopping.shopLimitedItems[@shopID][@item]
  453. if itemMax != nil and itemMax != false and itemMax != true and itemMax > 0
  454. $game_shopping.shopLimitedItems[@shopID][@item] -= amount
  455. elsif itemIsUsed?
  456. $game_shopping.shopUsedItems[@shopID][@item] -= amount
  457. end
  458. setMoney
  459. @gold_window.refresh
  460. @buy_window.refresh
  461. @details_window.refreshNeeded = true
  462. @buy_window.active = true
  463. @buy_window.visible = true
  464. end
  465.  
  466. def itemIsUsed?
  467. return true if @buy_window.index > $game_temp.shop_goods.length
  468. return false
  469. end
  470.  
  471. def timeElapsed?
  472. now = Graphics.frame_count / Graphics.frame_rate
  473. lastRestockTime = $game_shopping.shopRestockTimers[@shopID]
  474. timeDif = now - lastRestockTime
  475. return (timeDif / 60).floor >= @restockRate
  476. end
  477.  
  478. def restock
  479. for goods_item in $game_temp.shop_goods
  480. case goods_item[0]
  481. when 0
  482. item = $data_items [goods_item[1]]
  483. when 1
  484. item = $data_weapons[goods_item[1]]
  485. when 2
  486. item = $data_armors [goods_item[1]]
  487. when 3
  488. item = $data_skills [goods_item[1]]
  489. when 4
  490. item = $data_classes[goods_item[1]]
  491. when 5
  492. item = $data_actors [goods_item[1]]
  493. end
  494. if item != nil
  495. $game_shopping.shopLimitedItems[@shopID][item] = goods_item[2]
  496. end
  497. end
  498. end
  499. end
  500. #-------------------------------------------------------------------------------
  501. # Updates the shop window to support the new features
  502. #-------------------------------------------------------------------------------
  503. class Window_ShopBuy < Window_Selectable
  504.  
  505. attr_accessor :refreshNeeded
  506.  
  507. def initialize(x, y, shopID, rates)
  508. super(x, y, 304, 304)
  509. @shop_goods = $game_temp.shop_goods
  510. @shopID = shopID
  511. @rates = rates
  512. @refreshNeeded = false
  513.  
  514. #---------------------------------------------------------------------------
  515. # Change these if you want to label the item pools differently
  516. #---------------------------------------------------------------------------
  517. @headings = ["New Items", "Used Items"]
  518. refresh
  519. self.index = 0
  520. end
  521.  
  522. def setMoney
  523. if $game_shopping.shopsAcceptDebit and $game_party.hasDebitItem
  524. @money = $game_party.netLiquidity
  525. else
  526. @money = $game_party.gold
  527. end
  528. end
  529.  
  530. def update
  531. super
  532. refresh if @refreshNeeded
  533. end
  534.  
  535. def refresh
  536. setMoney
  537. @data = []
  538. @data.push(nil)
  539. for goods_item in @shop_goods
  540. case goods_item[0]
  541. when 0
  542. item = $data_items[goods_item[1]]
  543. when 1
  544. item = $data_weapons[goods_item[1]]
  545. when 2
  546. item = $data_armors[goods_item[1]]
  547. when 3
  548. item = $data_skills[goods_item[1]]
  549. when 4
  550. item = $data_classes[goods_item[1]]
  551. when 5
  552. item = $data_actors[goods_item[1]]
  553. end
  554. if item != nil
  555. @data.push(item)
  556. end
  557. end
  558. @labelIndexes = [0, @data.length]
  559. @data.push(nil)
  560. soldItems = $game_shopping.shopUsedItems[@shopID]
  561. for soldItem in soldItems.keys
  562. if soldItems[soldItem] > 0
  563. @data.push(soldItem)
  564. end
  565. end
  566. if @data[@data.length-1] == nil
  567. @item_max = @data.size - 1
  568. else
  569. @item_max = @data.size
  570. end
  571. create_contents
  572. for i in 0...@item_max
  573. if @labelIndexes.index(i) == nil
  574. draw_item(i)
  575. else
  576. draw_label(i) unless i > 0 and i == @item_max
  577. end
  578. end
  579. if self.index > @item_max - 1
  580. self.index = @item_max - 1
  581. end
  582. @refreshNeeded = false
  583. end
  584.  
  585. def draw_item(index)
  586. item = @data[index]
  587. if index > @labelIndexes[1]
  588. price = $game_shopping.shopPrice(item, @rates, true, true)
  589. forSale = 1
  590. else
  591. itemMax = $game_shopping.shopLimitedItems[@shopID][item]
  592. if itemMax == nil or itemMax == false or itemMax == true
  593. itemMax = -1
  594. isInflated = false
  595. else
  596. isInflated = (itemMax > 0 and itemMax <= $game_shopping.demandAmount)
  597. end
  598. forSale = itemMax == -1 ? 100 : itemMax
  599. price = $game_shopping.shopPrice(item, @rates, true, false, isInflated)
  600. end
  601. if item.is_a?(RPG::Item) or item.is_a?(RPG::Weapon) or item.is_a?(RPG::Armor)
  602. number = $game_party.item_number(item)
  603. elsif item.is_a?(RPG::Skill) and $game_shopping.exhaustableSkills
  604. #number = the current ammount of that skill possesed.
  605. else
  606. number = 1
  607. end
  608. enabled = (price <= @money and number < 99 and forSale > 0)
  609. rect = item_rect(index)
  610. self.contents.clear_rect(rect)
  611. draw_item_name(item, rect.x + 24, rect.y, enabled)
  612. rect.width -= 4
  613. self.contents.draw_text(rect, price, 2)
  614. end
  615.  
  616. def draw_label(index)
  617. label = @headings[@labelIndexes.index(index)]
  618. rect = item_rect(index)
  619. self.contents.clear_rect(rect)
  620. rect.width -= 4
  621. self.contents.draw_text(rect, label)
  622. end
  623. end
  624. #-------------------------------------------------------------------------------
  625. # Allows the Window_Item class to be used in the bank vault
  626. #-------------------------------------------------------------------------------
  627. class Window_Item < Window_Selectable
  628. attr_accessor :column_max
  629. end
  630. #-------------------------------------------------------------------------------
  631. # Updates game_actor to check for discovery of skills
  632. #-------------------------------------------------------------------------------
  633. class Game_Actor < Game_Battler
  634. def setup(actor_id)
  635. actor = $data_actors[actor_id]
  636. @actor_id = actor_id
  637. @name = actor.name
  638. @character_name = actor.character_name
  639. @character_index = actor.character_index
  640. @face_name = actor.face_name
  641. @face_index = actor.face_index
  642. @class_id = actor.class_id
  643. @weapon_id = actor.weapon_id
  644. @armor1_id = actor.armor1_id
  645. @armor2_id = actor.armor2_id
  646. @armor3_id = actor.armor3_id
  647. @armor4_id = actor.armor4_id
  648. @level = actor.initial_level
  649. @exp_list = Array.new(101)
  650. make_exp_list
  651. @exp = @exp_list[@level]
  652. @skills = []
  653. for i in self.class.learnings
  654. learn_skill(i.skill_id) if i.level <= @level and
  655. $data_skills[i.skill_id].discovered
  656. end
  657. clear_extra_values
  658. recover_all
  659. end
  660.  
  661. def refreshSkills
  662. @skills = []
  663. for i in self.class.learnings
  664. learn_skill(i.skill_id) if i.level <= @level and
  665. $data_skills[i.skill_id].discovered
  666. end
  667. end
  668. end
  669. #-------------------------------------------------------------------------------
  670. # Updates game_party to allow for debit purchasing
  671. #-------------------------------------------------------------------------------
  672. class Game_Party < Game_Unit
  673. def netLiquidity
  674. checkingTotal = 0
  675. for bank in $game_banking.bankAccounts
  676. for account in bank
  677. if account.type == 1 #checking
  678. checkingTotal += account.balance
  679. end
  680. end
  681. end
  682. return @gold + checkingTotal
  683. end
  684.  
  685. def hasDebitItem
  686. return has_item?($game_shopping.debitItem, true)
  687. end
  688. end
  689. #-------------------------------------------------------------------------------
  690. # Module for discovery of skills, classes and actors. (or anything, in general)
  691. #-------------------------------------------------------------------------------
  692. module Discovery
  693. attr_reader :discovered
  694. def discover
  695. @discovered = true
  696. end
  697.  
  698. def hide
  699. @discovered = false
  700. end
  701. end
  702. #-------------------------------------------------------------------------------
  703. # Price methods, discovery and icons for skills, classes and actors.
  704. #-------------------------------------------------------------------------------
  705. module RPG
  706. class Skill < UsableItem
  707. include Discovery
  708.  
  709. def price
  710. return $game_shopping.skillPrices[@id]
  711. end
  712. end
  713.  
  714. class Class
  715. include Discovery
  716.  
  717. attr_accessor :icon_index
  718. attr_accessor :description
  719.  
  720. def price
  721. return $game_shopping.classPrices[@id]
  722. end
  723. end
  724.  
  725. class Actor
  726. include Discovery
  727.  
  728. attr_accessor :icon_index
  729. attr_accessor :description
  730.  
  731. def price
  732. return $game_shopping.actorPrices[@id]
  733. end
  734. end
  735. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement