Class: PFM::Quests

Inherits:
Object show all
Defined in:
scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb,
scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00002 PFM Quests Quest.rb

Overview

The quest management

The main object is stored in $quests and PFM.game_state.quests

Defined Under Namespace

Classes: Quest

Constant Summary collapse

AUTO_CHECK_SIGNAL_ON_TEST =

Tell if the system should check the signal when we test finished?(id) or failed?(id)

true
AUTO_CHECK_SIGNAL_ON_ALL_OBJECTIVE_VALIDATED =

Tell if the system should check the signal when we check the quest termination

false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuests

Create a new Quest management object



23
24
25
26
27
28
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 23

def initialize
  @active_quests = {}
  @finished_quests = {}
  @failed_quests = {}
  @signal = { start: [], finish: [], failed: [] }
end

Instance Attribute Details

#active_questsHash<Integer => Quest>

The list of active_quests

Returns:



12
13
14
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 12

def active_quests
  @active_quests
end

#failed_questsHash<Integer => Quest>

The list of failed_quests

Returns:



18
19
20
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 18

def failed_quests
  @failed_quests
end

#finished_questsHash<Integer => Quest>

The list of finished_quests

Returns:



15
16
17
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 15

def finished_quests
  @finished_quests
end

#signalHash<start: Array<Integer>, finish: Array<Integer>, failed: Array<Integer>>

The signals that inform the game what quest started or has been finished

Returns:



21
22
23
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 21

def signal
  @signal
end

Instance Method Details

#active_quest(quest_id) ⇒ Quest

Return an active quest by its id

Parameters:

Returns:



46
47
48
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 46

def active_quest(quest_id)
  return @active_quests[quest_id]
end

#add_item(item_id)

Inform the manager that an item has been added to the bag of the Player

Parameters:

  • item_id (Integer)

    ID of the item in the database



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 133

def add_item(item_id)
  item_db_symbol = data_item(item_id).db_symbol
  active_quests.each_value do |quest|
    if quest.objective?(:objective_obtain_item, item_db_symbol)
      old_count = quest.data_get(:obtained_items, item_db_symbol, 0)
      quest.data_set(:obtained_items, item_db_symbol, old_count + 1)
      check_quest(quest.quest_id)
      next
    end
    next unless quest.objective?(:objective_obtain_item, item_id)

    old_count = quest.data_get(:obtained_items, item_id, 0)
    quest.data_set(:obtained_items, item_id, old_count + 1)
    check_quest(quest.quest_id)
  end
end

#beat_npc(quest_id, npc_name_index) ⇒ Boolean

Inform the manager that a NPC has been beaten

Parameters:

  • quest_id (Integer)

    the ID of the quest in the database

  • npc_name_index (Integer)

    the index of the name of the NPC in the quest data

Returns:

  • (Boolean)

    if the quest has been updated



108
109
110
111
112
113
114
115
116
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 108

def beat_npc(quest_id, npc_name_index)
  return false unless (quest = active_quest(quest_id))
  return false unless quest.objective?(:objective_beat_npc, npc_name_index)

  old_count = quest.data_get(:npc_beaten, npc_name_index, 0)
  quest.data_set(:npc_beaten, npc_name_index, old_count + 1)
  check_quest(quest_id)
  return true
end

#beat_pokemon(pokemon_id)

Inform the manager that a Pokemon has been beaten

Parameters:

  • pokemon_id (Integer)

    ID of the Pokemon in the database



152
153
154
155
156
157
158
159
160
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 152

def beat_pokemon(pokemon_id)
  active_quests.each_value do |quest|
    next unless quest.objective?(:objective_beat_pokemon, pokemon_id)

    old_count = quest.data_get(:pokemon_beaten, pokemon_id, 0)
    quest.data_set(:pokemon_beaten, pokemon_id, old_count + 1)
    check_quest(quest.quest_id)
  end
end

#catch_pokemon(pokemon)

Inform the manager that a Pokemon has been captured

Parameters:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 164

def catch_pokemon(pokemon)
  active_quests.each_value do |quest|
    next unless quest.objective?(:objective_catch_pokemon)

    quest_data = data_quest(quest.quest_id)
    quest_data.objectives.each do |objective|
      next unless objective.test_method_name == :objective_catch_pokemon

      pokemon_id = objective.test_method_args.first
      next unless quest.objective_catch_pokemon_test(pokemon_id, pokemon)

      old_count = quest.data_get(:pokemon_caught, pokemon_id, 0)
      quest.data_set(:pokemon_caught, pokemon_id, old_count + 1)
      check_quest(quest.quest_id)
    end
  end
end

#check_quest(quest_id)

Check if a quest is done or not

Parameters:

  • quest_id (Integer)

    ID of the quest in the database



239
240
241
242
243
244
245
246
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 239

def check_quest(quest_id)
  return unless (quest = active_quest(quest_id))
  return if @signal[:finish].include?(quest_id)
  return unless quest.finished?

  @signal[:finish] << quest_id
  check_up_signal if AUTO_CHECK_SIGNAL_ON_ALL_OBJECTIVE_VALIDATED
end

#check_up_signal

Check the signals and display them



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 217

def check_up_signal
  return unless $scene.is_a?(Scene_Map)

  if @signal[:start].any?
    start_names = @signal[:start].map { |quest_id| data_quest(quest_id).name }
    show_quest_inform(start_names, true)
  end
  if @signal[:finish].any?
    finish_names = @signal[:finish].collect { |quest_id| data_quest(quest_id).name }
    show_quest_inform(finish_names, false)
    # Switch the quests from stack to stack
    @signal[:finish].each do |quest_id|
      @finished_quests[quest_id] = @active_quests[quest_id] if @active_quests[quest_id]
      @active_quests.delete(quest_id)
    end
  end
  @signal[:start].clear
  @signal[:finish].clear
end

#earnings_got?(quest_id) ⇒ Boolean

Does the earning of a quest has been taken

Parameters:

  • quest_id (Integer)

    ID of the quest in the database

Returns:

  • (Boolean)


277
278
279
280
281
282
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 277

def earnings_got?(quest_id)
  check_up_signal if AUTO_CHECK_SIGNAL_ON_TEST
  return false unless (quest = finished_quest(quest_id))

  return quest.data_get(:earnings_distributed, false)
end

#egg_found Also known as: get_egg

Inform the manager an egg has been found



194
195
196
197
198
199
200
201
202
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 194

def egg_found
  active_quests.each_value do |quest|
    next unless quest.objective?(:objective_obtain_egg)

    old_count = quest.data_get(:obtained_eggs, 0)
    quest.data_set(:obtained_eggs, old_count + 1)
    check_quest(quest.quest_id)
  end
end

#failed?(quest_id) ⇒ Boolean

Is a quest failed ?

Parameters:

  • quest_id (Integer)

    ID of the quest in the database

Returns:

  • (Boolean)


259
260
261
262
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 259

def failed?(quest_id)
  check_up_signal if AUTO_CHECK_SIGNAL_ON_TEST
  return !@failed_quests.fetch(quest_id, nil).nil?
end

#failed_quest(quest_id) ⇒ Quest

Return a failed quest by its id

Parameters:

Returns:



60
61
62
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 60

def failed_quest(quest_id)
  return @failed_quests[quest_id]
end

#finished?(quest_id) ⇒ Boolean

Is a quest finished ?

Parameters:

  • quest_id (Integer)

    ID of the quest in the database

Returns:

  • (Boolean)


251
252
253
254
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 251

def finished?(quest_id)
  check_up_signal if AUTO_CHECK_SIGNAL_ON_TEST
  return !@finished_quests.fetch(quest_id, nil).nil?
end

#finished_quest(quest_id) ⇒ Quest

Return a finished quest by its id

Parameters:

Returns:



53
54
55
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 53

def finished_quest(quest_id)
  return @finished_quests[quest_id]
end

#get_earnings(quest_id) ⇒ Boolean

Get the earnings of a quest

Parameters:

  • quest_id (Integer)

    ID of the quest in the database

Returns:

  • (Boolean)

    if the earning were givent to the player



267
268
269
270
271
272
273
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 267

def get_earnings(quest_id)
  return false unless (quest = finished_quest(quest_id))
  return false if quest.data_get(:earnings_distributed, false)

  quest.distribute_earnings
  return true
end

#get_goal_data_index(quest_id, goal_index) ⇒ Integer

Get the goal data index (if array like items / speak_to return the index of the goal in the array info from data/quest data)

Parameters:

  • quest_id (Integer)

    the ID of the quest in the database

  • goal_index (Integer)

    the index of the goal in the goal order

Returns:

Raises:

  • (ScriptError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 88

def get_goal_data_index(quest_id, goal_index)
  raise ScriptError, 'This method should be removed!!!!'
  if (quest = @active_quests.fetch(quest_id, nil)).nil?
    if (quest = @finished_quests.fetch(quest_id, nil)).nil?
      return 0 if (quest = @failed_quests.fetch(quest_id, nil)).nil?
    end
  end
  goal_sym = quest[:order][goal_index]
  cnt = 0
  quest[:order].each_with_index do |sym, i|
    break if i >= goal_index
    cnt += 1 if sym == goal_sym
  end
  return cnt
end

#goal_shown?(quest_id, goal_index) ⇒ Boolean

Tell if a goal is shown or not

Parameters:

  • quest_id (Integer)

    the ID of the quest in the database

  • goal_index (Integer)

    the index of the goal in the goal order

Returns:

  • (Boolean)


77
78
79
80
81
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 77

def goal_shown?(quest_id, goal_index)
  return false unless (quest = active_quest(quest_id))

  return quest.data_get(:goals_visibility, goal_index, false)
end

#hatch_egg

Inform the manager an egg has hatched



206
207
208
209
210
211
212
213
214
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 206

def hatch_egg
  active_quests.each_value do |quest|
    next unless quest.objective?(:objective_hatch_egg)

    old_count = quest.data_get(:hatched_eggs, nil, 0)
    quest.data_set(:hatched_eggs, nil, old_count + 1)
    check_quest(quest.quest_id)
  end
end

#import_from_dot24



284
285
286
287
288
289
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 284

def import_from_dot24
  mapper = ->((id, quest)) { [id, convert_quest_from_dot24_to_dot25(id, quest)] }
  @active_quests = @active_quests.map(&mapper).to_h
  @finished_quests = @finished_quests.map(&mapper).to_h
  @failed_quests = @failed_quests.map(&mapper).to_h
end

#see_pokemon(pokemon_id)

Inform the manager that a Pokemon has been seen

Parameters:

  • pokemon_id (Integer)

    ID of the Pokemon in the database



184
185
186
187
188
189
190
191
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 184

def see_pokemon(pokemon_id)
  active_quests.each_value do |quest|
    next unless quest.objective?(:objective_see_pokemon, pokemon_id)

    quest.data_set(:pokemon_seen, pokemon_id, true)
    check_quest(quest.quest_id)
  end
end

#show_goal(quest_id, goal_index)

Show a goal of a quest

Parameters:

  • quest_id (Integer)

    the ID of the quest in the database

  • goal_index (Integer)

    the index of the goal in the goal order



67
68
69
70
71
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 67

def show_goal(quest_id, goal_index)
  return unless (quest = active_quest(quest_id))

  quest.data_set(:goals_visibility, goal_index, true)
end

#speak_to_npc(quest_id, npc_name_index) ⇒ Boolean

Inform the manager that a NPC has been spoken to

Parameters:

  • quest_id (Integer)

    the ID of the quest in the database

  • npc_name_index (Integer)

    the index of the name of the NPC in the quest data

Returns:

  • (Boolean)

    if the quest has been updated



122
123
124
125
126
127
128
129
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 122

def speak_to_npc(quest_id, npc_name_index)
  return false unless (quest = active_quest(quest_id))
  return false unless quest.objective?(:objective_speak_to, npc_name_index)

  quest.data_set(:spoken, npc_name_index, true)
  check_quest(quest_id)
  return true
end

#start(quest_id) ⇒ Boolean

Start a new quest if possible

Parameters:

  • quest_id (Integer)

    the ID of the quest in the database

Returns:

  • (Boolean)

    if the quest started



33
34
35
36
37
38
39
40
41
# File 'scripts/01450 Systems/08000 Quest/00001 PFM_Quest/00001 PFM Quests.rb', line 33

def start(quest_id)
  return false if data_quest(quest_id).db_symbol == :__undef__
  return false if finished?(quest_id)
  return false if @active_quests.fetch(quest_id, nil)

  @active_quests[quest_id] = Quest.new(quest_id)
  @signal[:start] << quest_id
  return true
end