Advertisement
Szyu

Questlog

Aug 29th, 2013
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.23 KB | None | 0 0
  1. #==============================================================================
  2. # Szyu's Quests
  3. # Version 1.2
  4. # By Szyu
  5. #
  6. # About:
  7. # Create Quests, the player can do on his journey through the world,
  8. # intensifying the experience of a real RPG.
  9. #
  10. # Instructions:
  11. # - Place below "▼ Materials" but above "▼ Main Process".
  12. #
  13. # How to Use:
  14. # - $game_party.gain_quest(qid)
  15. # - $game_party.complete_quest(qid)
  16. # - $game_party.cancel_quest(qid, failed)
  17. # - $game_party.quest_completed?(qid)
  18. # - $game_party.has_quest?(qid)
  19. #
  20. # Requires:
  21. # - RPG Maker VX Ace
  22. #
  23. # Terms of Use:
  24. # - Free for commercal and non-commercial use. Please list me
  25. #   in the credits to support my work.
  26. #
  27. # Pastebin:
  28. # http://adf.ly/UoFvf
  29. #
  30. #==============================================================
  31. #   * Configuration
  32. #==============================================================
  33.  
  34. # Access menu in GameMenu
  35. SCENE_MENU_SZQL_ACCESS = true
  36.  
  37. # Terms
  38. SZ_QUESTLOG_TERM = "Questlog"         # Term for Menu Entry
  39. SZQL_OBJECTIVES_TERM = "Objectives"   # Term for Questlog Objectives Head
  40. SZQL_DESCRIPTION_TERM = "Description" # Term for Questlog Description Head
  41. SZQL_SW_TRUE_TERM = "True"            # Term for Switch Objective "true"
  42. SZQL_SW_FALSE_TERM = "False"          # Term for Switch Objective "false"
  43. SZQL_LOG_HEADER = ["Current Quests", "Completed Quests"]
  44.  
  45. # Text Coloration
  46. SZQL_COMPLETE_COLOR = 29    # Color or Color-Index (number for text_color(x))
  47. SZQL_INCOMPLETE_COLOR = 21  
  48.  
  49. # Icons
  50. SZQL_SWITCH_ICON = 16     # Index for standard switch icon
  51. SZQL_VAR_ICON = 16        # Index for standard variable icon
  52. SZQL_CURRENCY_ICON = 361  # Index for standard currency icon
  53.  
  54.  
  55. QUEST_LIST = {
  56. # "Quest Name" => [Description, is_active/autostart, objectives]
  57. # objectives:
  58. #"s: id, value, match, optional: icon_index, text" -> switch[id] == value?
  59. #"v: id, value, match, optional: icon_index, text" -> variable[id] matches value?
  60. #"g: value, match, optional: icon_index, text" -> gold matches value?
  61. #"i: id, value, match, optional: icon_index, text/item name will be taken if no optional text" -> item[id] matches value?
  62. #"w: id, value, match, optional: icon_index, text/item name will be taken if no optional text" -> weapon[id] matches value?
  63. #"a: id, value, match, optional: icon_index, text/item name will be taken if no optional text" -> armor[id] matches value?
  64.   "Stones, Stones, Stones" => ["Collect 3 stones and return to the quest giver.",
  65.             false,
  66.             ["i: 1, 3, >="]],
  67.   "A lonely sister" => ["Talk to sister Ann and return.", false,["s: 1, True, ==, 361,Talked to sister Ann"]],
  68.   "Autostart Quest" => ["Complete 2 quests and talk to \nthe big Monster.",true,["v: 1, 2, >=, -1,Quests completed"]],
  69.   "Get Gold" => ["Get Gold...", true,["g: 2000, >=, 361, Gold"]]
  70. }
  71.  
  72. #=====================================================================
  73. # Don't edit below this line
  74. #=====================================================================
  75.  
  76. #==============================================================
  77. #   * Scene_QuestLog
  78. #==============================================================
  79. class Scene_QuestLog < Scene_MenuBase
  80.    
  81.   def start
  82.     super
  83.     create_header_window
  84.     create_log_window
  85.   end
  86.  
  87.   def create_header_window
  88.     @header_window = Window_QuestHeader.new
  89.   end
  90.  
  91.   def create_log_window
  92.     @log_window = Window_QuestLog.new
  93.     @log_window.set_handler(:cancel, method(:return_scene))
  94.     @log_window.set_handler(:pageup, method(:switch_header))
  95.     @log_window.set_handler(:pagedown, method(:switch_header))
  96.     @info_window = Window_QuestInfo.new(@log_window.width)
  97.     @log_window.header = 0
  98.     @log_window.info_window = @info_window
  99.   end
  100.  
  101.   def switch_header
  102.     @header_window.header = (@header_window.head_index == 0) ? 1 : 0
  103.     @log_window.header = @header_window.head_index
  104.     @log_window.activate
  105.   end
  106.  
  107. end
  108.  
  109. #==============================================================
  110. #   * Window_QuestHeader
  111. #==============================================================
  112. class Window_QuestHeader < Window_Selectable
  113.   attr_reader :head_index
  114.  
  115.   def initialize
  116.     super(0,0,Graphics.width/3,line_height*2)
  117.     @head_index = 0
  118.     refresh
  119.   end
  120.  
  121.   def header=(head)
  122.     @head_index = head
  123.     refresh
  124.   end
  125.  
  126.   def refresh
  127.     contents.clear
  128.     draw_text(0,0,width-8,line_height, SZQL_LOG_HEADER[@head_index])
  129.   end
  130. end
  131.  
  132. #==============================================================
  133. #   * Window_QuestLog
  134. #==============================================================
  135. class Window_QuestLog < Window_Selectable
  136.   attr_reader :info_window
  137.   attr_reader :header
  138.  
  139.   alias sz_ql_help call_update_help
  140.  
  141.   def initialize
  142.     super(0,line_height*2,Graphics.width/3,Graphics.height-line_height*2)
  143.     @data = []
  144.     activate
  145.   end
  146.  
  147.   def header=(head)
  148.     @header = head
  149.     refresh
  150.   end
  151.  
  152.   def refresh
  153.     make_item_list
  154.     create_contents
  155.     draw_all_items
  156.   end
  157.  
  158.   def make_item_list
  159.     data = []
  160.     case @header
  161.     when 0
  162.       $game_party.active_quests.each do |q|
  163.         data.push($game_quests.data[q]) if $game_quests.data[q].active
  164.       end
  165.     when 1
  166.       $game_party.completed_quests.each do |q|
  167.         data.push($game_quests.data[q]) if $game_quests.data[q].completed
  168.       end
  169.     end
  170.     select(0) if  data.size > 0
  171.     @data = data
  172.   end
  173.  
  174.   def draw_item(index)
  175.     item = @data[index]
  176.     if item
  177.       rect = item_rect(index)
  178.       rect.width -= 4
  179.       change_color(SZQL_COMPLETE_COLOR.is_a?(Color) ? SZQL_COMPLETE_COLOR : text_color(SZQL_COMPLETE_COLOR)) if item.completed?
  180.       change_color(crisis_color) if item.failed
  181.       draw_text(rect, item.name); change_color(normal_color)
  182.     end
  183.   end
  184.  
  185.   def item
  186.     @data && index >= 0 ? @data[index] : nil
  187.   end
  188.  
  189.   def item_max
  190.     @data ? @data.size : 1
  191.   end
  192.  
  193.   def call_update_help
  194.     sz_ql_help
  195.     @info_window.set_item = item if @info_window
  196.     @info_window.contents.clear if @info_window && @header != 0
  197.   end
  198.  
  199.   def info_window=(qi)
  200.     @info_window = qi
  201.     refresh
  202.   end
  203. end
  204.  
  205. #==============================================================
  206. #   * Window_QuestInfo
  207. #==============================================================
  208. class Window_QuestInfo < Window_Selectable
  209.  
  210.   def initialize(x)
  211.     super(x,0,Graphics.width-x,Graphics.height)
  212.     @item = nil
  213.     refresh
  214.   end
  215.  
  216.   def refresh
  217.     contents.clear
  218.     return unless @item
  219.     change_color(system_color)
  220.     draw_text(0,0,width,line_height, SZQL_DESCRIPTION_TERM)
  221.     change_color(normal_color)
  222.    
  223.     desc = @item.description.split("\n")
  224.     line = 1
  225.     desc.each do |ln|
  226.       draw_text(0,line_height*line,width,line_height, ln)
  227.       line += 1
  228.     end
  229.    
  230.     line += 1
  231.     change_color(system_color)
  232.     draw_text(0, line_height*line, width,line_height, SZQL_OBJECTIVES_TERM)
  233.     line += 1
  234.     change_color(normal_color)
  235.    
  236.     for i in 0..@item.objectives.size-1
  237.       obj = @item.objectives[i]
  238.      
  239.       txt = (obj.type == "s" || obj.type == "v" || obj.type == "g" ||obj.text != nil) ? sprintf("%s?", obj.text) : sprintf("%s?", obj.data.name)
  240.       valtxt = (obj.type == "s" || obj.type == "v" || obj.type == "g") ? obj.data.to_s : $game_party.item_number(obj.data)
  241.       ico_index = nil
  242.       if obj.icon_index == -1
  243.         case obj.type
  244.         when "s"
  245.           ico_index = SZQL_SWITCH_ICON || nil
  246.         when "v"
  247.           ico_index = SZQL_VAR_ICON || nil
  248.         when "g"
  249.           ico_index = SZQL_CURRENCY_ICON || nil
  250.         else
  251.           ico_index = obj.data.icon_index || nil
  252.         end
  253.       else
  254.         ico_index = obj.icon_index
  255.       end
  256.       if obj.achieved?
  257.         change_color(SZQL_COMPLETE_COLOR.is_a?(Color) ? SZQL_COMPLETE_COLOR : text_color(SZQL_COMPLETE_COLOR))
  258.       else
  259.         change_color(SZQL_INCOMPLETE_COLOR.is_a?(Color) ? SZQL_INCOMPLETE_COLOR : text_color(SZQL_INCOMPLETE_COLOR))
  260.       end
  261.       draw_text(0, line_height*(2*i+line), width, line_height, txt)
  262.       change_color(normal_color)
  263.       draw_icon(ico_index,0,line_height*(2*i+line+1)) if ico_index
  264.       draw_text(32, line_height*(2*i+line+1), width, line_height, sprintf("%s %s %s", valtxt,obj.match, obj.value))
  265.      
  266.     end
  267.   end
  268.  
  269.   def set_item=(q)
  270.     @item = q
  271.     refresh
  272.   end
  273. end
  274.  
  275. #==============================================================
  276. #   * Game_Quest
  277. #==============================================================
  278. class Game_Quest
  279.   attr_reader :name
  280.   attr_reader :description
  281.   attr_reader :id
  282.   attr_accessor :completed
  283.   attr_reader :objectives
  284.   attr_reader :active
  285.   attr_accessor :failed
  286.  
  287.   def initialize(id,name, args)
  288.     @id = id
  289.     @name = name
  290.     @description = args[0]
  291.     @active = args[1]
  292.     @objectives = []
  293.     @completed = false
  294.     @failed = false
  295.     args[2].each do |obj|
  296.       @objectives.push(Quest_Objective.new(obj))
  297.     end
  298.   end
  299.  
  300.   def activate
  301.     @active = true if !@active
  302.   end
  303.  
  304.   def deactivate
  305.     @active = false if @active
  306.   end
  307.  
  308.   def complete
  309.     @completed = true
  310.   end
  311.  
  312.   def completed?
  313.     @objectives.each do |obj|
  314.       return false if !obj.achieved?
  315.     end
  316.     return true
  317.   end
  318. end
  319.  
  320. #==============================================================
  321. #   * Quest_Objective
  322. #==============================================================
  323. class Quest_Objective
  324.   attr_reader :type
  325.   attr_reader :id
  326.   attr_reader :value
  327.   attr_reader :match
  328.   attr_accessor :text
  329.   attr_accessor :icon_index
  330.  
  331.   def initialize(objectives)
  332.     if objectives =~ /(\w+):\s*?(\d+),\s*?(\w+),?\s*([><=!]+),?\s*([-]?\d+)?,?\s*([\w\W]+)?/i
  333.       @type = $1
  334.       @id = $2.to_i
  335.       @value = @type == "s" ? $3 : $3.to_i
  336.       @match = $4
  337.       @icon_index = $5 ? $5.to_i : -1
  338.       @text = $6
  339.     elsif objectives =~ /(\w+):\s*?(\d+),?\s*([><=!]+),?\s*([-]?\d+)?,?\s*([\w\W]+)?/i
  340.       @type = $1
  341.       @id = nil
  342.       @value = $2.to_i
  343.       @match = $3
  344.       @icon_index = $4 ? $4.to_i : -1
  345.       @text = $5
  346.     end
  347.   end
  348.  
  349.   def data
  350.     case @type
  351.     when "s"
  352.       $game_switches[@id]? SZQL_SW_TRUE_TERM : SZQL_SW_FALSE_TERM
  353.     when "v"
  354.       $game_variables[@id]
  355.     when "g"
  356.       $game_party.gold
  357.     when "i"
  358.       $data_items[@id]
  359.     when "w"
  360.       $data_weapons[@id]
  361.     when "a"
  362.       $data_armors[@id]
  363.     end
  364.  
  365.   end
  366.  
  367.   def achieved?
  368.     case @match
  369.     when ">"
  370.       return (data.is_a?(RPG::BaseItem)? $game_party.item_number(data) : data) > @value
  371.     when "<"
  372.       return (data.is_a?(RPG::BaseItem)? $game_party.item_number(data) : data) < @value
  373.     when ">="
  374.       return (data.is_a?(RPG::BaseItem)? $game_party.item_number(data) : data) >= @value
  375.     when "<="
  376.       return (data.is_a?(RPG::BaseItem)? $game_party.item_number(data) : data) <= @value
  377.     when "=="
  378.       return (data.is_a?(RPG::BaseItem)? $game_party.item_number(data) : data) == @value
  379.     when "!="
  380.       return (data.is_a?(RPG::BaseItem)? $game_party.item_number(data) : data) != @value
  381.     end
  382.     return true
  383.   end
  384. end
  385.  
  386. #==============================================================
  387. #   * Game_Quests
  388. #==============================================================
  389. class Game_Quests
  390.   attr_reader :data
  391.  
  392.   def initialize
  393.     @data = [nil]
  394.     i=1
  395.     QUEST_LIST.each_pair do |name, q|
  396.       @data.push(Game_Quest.new(i,name, q))
  397.       i += 1
  398.     end
  399.   end
  400.  
  401.   def [](quest_id)
  402.     return nil unless @data[quest_id]
  403.     @data[quest_id] ||= Game_Quest.new(quest_id)
  404.   end
  405. end
  406.  
  407. #==============================================================
  408. #   * Alias Object Creation
  409. #==============================================================
  410. module DataManager
  411.   class << self
  412.     alias sz_qlog_init_quests create_game_objects
  413.   end
  414.  
  415.   def self.create_game_objects
  416.     sz_qlog_init_quests
  417.     $game_quests = Game_Quests.new
  418.     $game_party.initialize_autoquests
  419.   end
  420. end
  421.  
  422. #==============================================================
  423. #   * Alias Game_Party Quests
  424. #==============================================================
  425. class Game_Party < Game_Unit
  426.   attr_reader :active_quests
  427.   attr_reader :completed_quests
  428.  
  429.   alias szql_init initialize
  430.   def initialize
  431.     szql_init
  432.     @active_quests = []
  433.     @completed_quests = []
  434.   end
  435.  
  436.   def initialize_autoquests
  437.     $game_quests.data.select {|q| q != nil && q.active }.each do |q|
  438.       $game_party.gain_quest(q.id)
  439.     end
  440.   end
  441.  
  442.   def has_quest?(qid)
  443.     return true if @completed_quests.include?(qid)
  444.     return @active_quests.include?(qid)
  445.   end
  446.  
  447.  
  448.   def gain_quest(qid)
  449.     return false if has_quest?(qid)
  450.     return false if $game_quests[qid].completed
  451.     @active_quests.push(qid)
  452.     $game_quests[qid].activate
  453.     $game_quests[qid].failed = false
  454.     return true
  455.   end
  456.  
  457.   def quest_completed?(qid)
  458.     return false if !has_quest?(qid)
  459.     return $game_quests[qid].completed?
  460.   end
  461.  
  462.   def complete_quest(qid)
  463.     return true if $game_quests[qid].completed
  464.     return false if !has_quest?(qid)
  465.     return false if !quest_completed?(qid)
  466.     @completed_quests.push(qid)
  467.     @active_quests.delete(qid)
  468.     $game_quests[qid].complete
  469.     return true
  470.   end
  471.  
  472.   def cancel_quest(qid, failed=false)
  473.     return unless has_quest?(qid)
  474.     @active_quests.delete(qid)
  475.     $game_quests[qid].deactivate
  476.     $game_quests[qid].failed = true if failed
  477.   end
  478. end
  479.  
  480. #==============================================================
  481. #   * Add Entry to Menu
  482. #==============================================================
  483. class Scene_Menu < Scene_MenuBase
  484.   alias :command_szql_wincom :create_command_window
  485.  
  486.   def create_command_window
  487.     command_szql_wincom
  488.     @command_window.set_handler(:swclass, method(:call_sz_questlog)) if SCENE_MENU_SZQL_ACCESS
  489.   end
  490.  
  491.   def call_sz_questlog
  492.     SceneManager.call(Scene_QuestLog)
  493.   end
  494. end
  495.  
  496. #==============================================================
  497. #   * Add Entry to Menu
  498. #==============================================================
  499. class Window_MenuCommand < Window_Command
  500.   alias :command_szql_wincom :add_main_commands
  501.  
  502.   def add_main_commands
  503.     command_szql_wincom
  504.     add_command(SZ_QUESTLOG_TERM, :swclass, main_commands_enabled) if SCENE_MENU_SZQL_ACCESS
  505.   end
  506. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement