Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
Item = Struct.new(:name, :requirements)

BLUE_ORB = Item.new("Blue Orb", [])
RED_ORB = Item.new("Red Orb", [])
METEORITE = Item.new("Meteorite", [])
SUBMARINE = Item.new("Submarine", [])
ERUPTION = Item.new("Eruption", [METEORITE])
GROUDON = Item.new("Groudon", [RED_ORB])
KYOGRE = Item.new("Kyogre", [BLUE_ORB])

Location = Struct.new(:name)

LITTLEROOT = Location.new("Littleroot Town")
ODALE = Location.new("Odale Town")
PETALBURG = Location.new("Petalburg City")
RUSTBORO = Location.new("Rustboro City")
DEWFORD = Location.new("Dewford Town")
SLATEPORT = Location.new("Slateport City")
MAUVILLE = Location.new("Mauville City")
VERDANTURF = Location.new("Verdanturf Town")
FALLARBOR = Location.new("Fallarbor Town")
MT_CHIMNEY = Location.new("Mt. Chimney")
LAVARIDGE = Location.new("Lavaridge Town")
FORTREE = Location.new("Fortree City")
MT_PYRE = Location.new("Mt. Pyre")
LILYCOVE = Location.new("Lilycove City")
MOSSDEEP = Location.new("Mossdeep City")
SOOTOPOLIS = Location.new("Sootopolis City")
PACIFIDLOG = Location.new("Pacifidlog Town")
EVERGRANDE = Location.new("Ever Grande City")
MARINE_CAVE = Location.new("Marine Cave")
TERRA_CAVE = Location.new("Terra Cave")

LOCATIONS = ObjectSpace.each_object(Location).to_a

NEIGHBORS = {
  LITTLEROOT => [ODALE],
  ODALE => [LITTLEROOT, PETALBURG, SLATEPORT, MAUVILLE],
  PETALBURG => [ODALE, RUSTBORO, DEWFORD],
  RUSTBORO => [PETALBURG, VERDANTURF],
  DEWFORD => [PETALBURG, SLATEPORT],
  SLATEPORT => [ODALE, DEWFORD, MAUVILLE],
  MAUVILLE => [SLATEPORT, VERDANTURF, MT_CHIMNEY, FALLARBOR, FORTREE],
  VERDANTURF => [RUSTBORO, MAUVILLE],
  FALLARBOR => [RUSTBORO, MAUVILLE],
  MT_CHIMNEY => [MAUVILLE, LAVARIDGE, FALLARBOR],
  LAVARIDGE => [MAUVILLE],
  FORTREE => [MAUVILLE, MT_PYRE, LILYCOVE],
  MT_PYRE => [MAUVILLE, FORTREE, LILYCOVE],
  LILYCOVE => [FORTREE, MT_PYRE, MOSSDEEP, SOOTOPOLIS, PACIFIDLOG, EVERGRANDE],
  MOSSDEEP => [LILYCOVE, SOOTOPOLIS, PACIFIDLOG, EVERGRANDE],
  SOOTOPOLIS => [LILYCOVE, MOSSDEEP, PACIFIDLOG, EVERGRANDE],
  PACIFIDLOG => [SLATEPORT, LILYCOVE, MOSSDEEP, PACIFIDLOG, EVERGRANDE],
  EVERGRANDE => [LILYCOVE, MOSSDEEP, SOOTOPOLIS, PACIFIDLOG],
  MARINE_CAVE => [],
  TERRA_CAVE => [],
}

MARINE_NEIGHBORS = [DEWFORD, MOSSDEEP, SOOTOPOLIS]
TERRA_NEIGHBORS = [MAUVILLE, VERDANTURF, FALLARBOR]

ITEM_LOCATIONS = {
  BLUE_ORB => MT_PYRE,
  ERUPTION => MT_CHIMNEY,
  GROUDON => TERRA_CAVE,
  KYOGRE => MARINE_CAVE,
  METEORITE => FALLARBOR,
  RED_ORB => MT_PYRE,
  SUBMARINE => SLATEPORT,
}

Faction = Struct.new(:name, :hideout, :goals)

TEAM_AQUA = Faction.new("Team Aqua", LILYCOVE, [KYOGRE, METEORITE, SUBMARINE])
TEAM_MAGMA = Faction.new("Team Magma", MT_CHIMNEY, [ERUPTION, GROUDON, SUBMARINE])

# Dijkstra's algorithm.
def dijkstra(from, to, &neighbors)
  current = from
  unvisited = {from => 0}
  visited = {}
  previous = {}

  loop do
    tentative = unvisited[current] + 1
    neighbors.call(current).each do |neighbor|
      next if visited.key?(neighbor)
      if !unvisited.key?(neighbor) || unvisited[neighbor] > tentative
        unvisited[neighbor] = tentative
        previous[neighbor] = current
      end
    end
    visited[current] = nil
    unvisited.delete(current)
    break if unvisited.empty?
    min = unvisited.values.min
    current = unvisited.select {|_, v| v == min}.shift[0]

    if current == to
      path = [to]
      while current != from
        current = previous[current]
        path << current
      end
      return path.reverse!
    end
  end

  return nil
end

# Dependencies.
def dependencies(roots, complete, &depends)
  dependencies = []
  queue = roots.reject {|r| complete.include?(r)}

  until queue.empty?
    node = queue.shift
    missing = depends.call(node).reject {|d| complete.include?(d)}
    if missing.empty?
      dependencies << node
    else
      queue += missing
    end
  end

  return dependencies
end

# Pseudo-random number generator. Random contains an infinite number of
# sequences, and 0 is the "seed" which chooses which sequence we want to
# use. This means that the "random" numbers will be the same each time
# the script is run, but you could generate the seed based on the time
# and then the sequence would effectively be random.
RNG = Random.new(0)

# Spawn Marine Cave and Terra Cave.
marine_neighbor = MARINE_NEIGHBORS.sample(random: RNG)
NEIGHBORS[MARINE_CAVE] << marine_neighbor
NEIGHBORS[marine_neighbor] << MARINE_CAVE

terra_neighbor = TERRA_NEIGHBORS.sample(random: RNG)
NEIGHBORS[TERRA_CAVE] << terra_neighbor
NEIGHBORS[terra_neighbor] << TERRA_CAVE

Agent = Struct.new(:faction, :location, :state, :items)
Travel = Struct.new(:location)
Acquire = Struct.new(:ticks)
Fight = Struct.new(:ticks)

# Run.
AGENTS = ObjectSpace.each_object(Faction).map {|f| Agent.new(f, f.hideout, nil, [])}

Player = Struct.new(:location, :known, :items)
PLAYER = Player.new(LITTLEROOT, [METEORITE, RED_ORB, BLUE_ORB], [])

def tick
  arrivals = {}
  departures = {}
  location_agents = {}
  agent_message = {}

  AGENTS.each do |agent|
    # Find a new target location.
    if agent.state.nil?
      items = dependencies(agent.faction.goals, agent.items, &:requirements)
      item = items.select {|i| ITEM_LOCATIONS.key?(i)}.sample(random: RNG)
      agent.state = Travel.new(ITEM_LOCATIONS[item])
    # Travel to target location.
    elsif agent.state.is_a?(Travel)
      steps = dijkstra(agent.location, agent.state.location) {|l| NEIGHBORS[l]}
      if steps
        agent.location = steps[1]
      else
        agent.state = Acquire.new(3)
        (arrivals[agent.location] ||= []) << agent
      end
      (location_agents[agent.location] ||= []) << agent
    # Capture the item at location.
    elsif agent.state.is_a?(Acquire)
      agent.state.ticks -= 1
      if agent.state.ticks == 0
        agent.state = nil
        pickup = dependencies(agent.faction.goals, agent.items, &:requirements)
        pickup.each do |item|
          if ITEM_LOCATIONS[item] == agent.location
            ITEM_LOCATIONS.delete(item)
            agent.items << item
          end
        end
        (departures[agent.location] ||= []) << agent
      else
        (location_agents[agent.location] ||= []) << agent
      end
    elsif agent.state.is_a?(Fight)
      agent.state.ticks -= 1
      if agent.state.ticks == 0
        agent.state = nil
        (departures[agent.location] ||= []) << agent
        agent.location = agent.faction.hideout
      end
    end
  end

  if !ITEM_LOCATIONS.key?(ERUPTION)
    puts "Using the power of the Meteorite, Team Magma have caused Mt. Chimney to erupt!"
    return false
  elsif !ITEM_LOCATIONS.key?(GROUDON)
    puts "Team Magma have captured the Legendary Pokémon Groudon!"
    return false
  elsif !ITEM_LOCATIONS.key?(KYOGRE)
    puts "Team Aqua have captured the Legendary Pokémon Kyogre!"
    return false
  end

  location_agents.each do |location, agents|
    if agents.length > 1
      agents.each do |agent|
        agent.state = Fight.new(3)
        agent_message[agent] = nil
      end
      puts "\"Brendan, #{agents.map(&:faction).map(&:name).join(' and ')} are fighting in the streets of #{location.name}!\" *click*"
    end
  end

  arrivals.each do |location, agents|
    next if agents.any? {|a| agent_message.key?(a)}
    agents.each {|a| agent_message[a] = nil}
    puts "\"Brendan, #{agents.map(&:faction).map(&:name).join(' and ')} are causing havoc in #{location.name}!\" *click*"
  end

  departures.each do |location, agents|
    next if agents.any? {|a| agent_message.key?(a)}
    agents.each {|a| agent_message[a] = nil}
    puts "\"Brendan, #{agents.map(&:faction).map(&:name).join(' and ')} have ceased operations in #{location.name}. I wonder what their aim was...\" *click*."
  end

  # Get user input.
  neighbors = NEIGHBORS[PLAYER.location]
  agents = AGENTS.select {|a| a.location == PLAYER.location && !a.state.nil? && !a.state.is_a?(Travel)}
  items = agents.empty? ? ITEM_LOCATIONS.select {|i, l| l == PLAYER.location}.map {|i, _| i} : []
  puts "[0] Wait"
  i = 0
  neighbor_start = 1
  neighbors.each {|n| puts "[#{i+=1}] #{n.name}"}
  agent_start = i + 1
  agents.each {|a| puts "[#{i+=1}] Fight #{a.faction.name}"}
  item_start = i + 1
  items.each {|it| puts "[#{i+=1}] Take #{it.name}"}

  loop do
    selection = gets.to_i
    next if selection.nil? || selection < 0 || selection > i
    if selection >= neighbor_start && selection < agent_start
      PLAYER.location = neighbors[selection - neighbor_start]
    elsif selection >= agent_start && selection < item_start
      agent = agents[selection - agent_start]
      agent.state = nil
      agents.each {|a| a.state = nil} if agents.length <= 2
      agent.location = agent.faction.hideout
    elsif selection >= item_start && selection <= i
      item = items[selection - item_start]
      ITEM_LOCATIONS.delete(item)
      PLAYER.items << item
    end
    break
  end

  return true
end

puts "You are in #{PLAYER.location.name}."
while tick; end
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]