Method: Gemwarrior::Evaluator#evaluate

Defined in:
lib/gemwarrior/evaluator.rb

#evaluate(input) ⇒ Object



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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/gemwarrior/evaluator.rb', line 88

def evaluate(input)
  case input
  # Ctrl-D or empty command
  when nil, ""
    return
  # real command
  else
    tokens = input.split
    unless input_valid?(input)
      return ERROR_COMMAND_INVALID
    end
  end

  command = tokens.first.downcase
  param1 = tokens[1]
  param2 = tokens[2]
  param3 = tokens[3]

  # dev commands
  if world.debug_mode
    case command
    when 'god', 'gd'
      return world.player.god_mode = !world.player.god_mode
    when 'beast', 'bs'
      return world.player.beast_mode = !world.player.beast_mode
    when 'vars', 'v'
      world.print_vars
    when 'list', 'ls'
      if param1.nil?
        puts ERROR_LIST_PARAM_MISSING
        return DEBUG_LIST_PARAMS
      else
        return world.list(param1, param2)
      end
    when 'map', 'm'
      world.print_map(param1)
    when 'stat', 'st', 's'
      if param1.nil?
        puts ERROR_DEBUG_STAT_PARAM_MISSING
        return DEBUG_STAT_PARAMS
      else
        case param1
        when 'hp_cur'
          unless param2.nil?
            param2 = param2.to_i
            if param2.is_a? Numeric
              if param2 > 0
                world.player.hp_cur = param2
              end
            end
          end
        when 'atk_lo'
          unless param2.nil?
            param2 = param2.to_i
            if param2.is_a? Numeric
              if param2 >= 0
                world.player.atk_lo = param2
              end
            end
          end
        when 'atk_hi'
          unless param2.nil?
            param2 = param2.to_i
            if param2.is_a? Numeric
              if param2 >= 0
                world.player.atk_hi = param2
              end
            end
          end
        when 'strength', 'str', 'st'
          unless param2.nil?
            param2 = param2.to_i
            if param2.is_a? Numeric
              if param2 >= 0
                world.player.atk_lo = param2
                world.player.atk_hi = param2
              end
            end
          end
        when 'dexterity', 'dex', 'd'
          unless param2.nil?
            param2 = param2.to_i
            if param2.is_a? Numeric
              if param2 >= 0
                world.player.dexterity = param2
              end
            end
          end
        when 'rox', 'r', '$'
          unless param2.nil?
            param2 = param2.to_i
            if param2.is_a? Numeric
              if param2 >= 0
                world.player.rox = param2
              end
            end
          end
        else
          return ERROR_DEBUG_STAT_PARAM_INVALID
        end
      end
    when 'spawn', 'sp'
      cur_loc = world.location_by_coords(world.player.cur_coords)
      cur_loc.populate_monsters(world.monsters, true)
      return world.describe(cur_loc)
    when 'teleport', 'tp'
      if param1.nil?
        return ERROR_DEBUG_TELEPORT_PARAMS_MISSING
      else
        if (param1.to_i.to_s == param1)
          # we got at least an x coordinate
          if (param2.to_i.to_s == param2)
            # we got a y coordinate, too
            x_coord = param1.to_i
            y_coord = param2.to_i
            # grab the z coordinate, if present, otherwise default to current level
            z_coord = param3.to_i.to_s == param3 ? param3.to_i : world.player.cur_coords[:z]

            # check to make sure new location exists
            if world.location_by_coords({:x => x_coord, :y => y_coord, :z => z_coord})
              world.player.cur_coords = {:x => x_coord, :y => y_coord, :z => z_coord}
            else
              return ERROR_DEBUG_TELEPORT_PARAMS_INVALID
            end
          else
            # we only got an x coordinate
            return ERROR_DEBUG_TELEPORT_PARAMS_NEEDED
          end
        else
          # we got a place name instead, potentially
          place_to_match = tokens[1..tokens.length].join(' ').downcase
          locations = []
          world.locations.each do |l|
            locations << l.name.downcase
          end
          if locations.include?(place_to_match)
            world.player.cur_coords = world.location_coords_by_name(place_to_match)
          else
            return ERROR_DEBUG_TELEPORT_PARAMS_INVALID
          end
        end

        # stats
        world.player.movements_made += 1
        
        Animation::run({:phrase => '** TELEPORT! **', :speed => :insane})
        return world.describe(world.location_by_coords(world.player.cur_coords))
      end
    end
  end

  # normal commands
  case command
  when 'character', 'c'
    world.player.check_self(world.debug_mode)
  when 'inventory', 'i'
    if param1
      world.player.inventory.describe_item(param1)
    else
      world.player.list_inventory
    end
  when 'rest', 'r'
    world.player.rest(world)
  when 'look', 'l'
    if param1
      world.describe_entity(world.location_by_coords(world.player.cur_coords), param1)
    else
      world.describe(world.location_by_coords(world.player.cur_coords))
    end
  when 'take', 't'
    if param1.nil?
      ERROR_TAKE_PARAM_MISSING
    else
      world.player.inventory.add_item(world.location_by_coords(world.player.cur_coords), param1, world.player)
    end
  when 'use', 'u'
    if param1.nil?
      ERROR_USE_PARAM_MISSING
    else
      item_name = param1
      result = nil
      location_inventory = world.location_by_coords(world.player.cur_coords).items

      if location_inventory.map(&:name).include?(item_name)
        location_inventory.each do |i|
          if i.name.eql?(item_name)
            if i.useable
              result = i.use(world.player)
            else
              return ERROR_USE_PARAM_UNUSEABLE
            end
          end
        end
      elsif
        player_inventory = world.player.inventory.items
        if player_inventory.map(&:name).include?(item_name)
          player_inventory.each do |i|
            if i.name.eql?(item_name)
              if i.useable
                result = i.use(world.player)
              else
                return ERROR_USE_PARAM_UNUSEABLE
              end
            end
          end
        end
      end

      if result.nil?
        ERROR_USE_PARAM_INVALID
      else
        case result[:type]
        when 'move'
          world.player.cur_coords = world.location_coords_by_name(result[:data])
          world.describe(world.location_by_coords(world.player.cur_coords))
        when 'move_dangerous'
          world.player.take_damage(rand(0..2))
          world.player.cur_coords = world.location_coords_by_name(result[:data])
          world.describe(world.location_by_coords(world.player.cur_coords))
        when 'dmg'
          world.player.take_damage(result[:data])
          return
        when 'rest', 'health'
          world.player.heal_damage(result[:data])
          return
        when 'action'
          case result[:data]
          when 'rest'
            world.player.rest(world)
          when 'map'
            world.print_map
          end
        when 'arena'
          arena = Arena.new({:world => world, :player => world.player})
          arena.start
        when 'item'
          world.location_by_coords(world.player.cur_coords).add_item(result[:data])
          return
        when 'purchase'
          result[:data].each do |i|
            world.player.inventory.items.push(i)
          end
        else
          return
        end
      end
    end
  when 'drop', 'd'
    if param1.nil?
      ERROR_DROP_PARAM_MISSING
    else
      world.player.inventory.remove_item(param1)
    end  
  when 'equip', 'e'
    if param1.nil?
      ERROR_EQUIP_PARAM_MISSING
    else
      world.player.inventory.equip_item(param1)
    end
  when 'unequip', 'ue'
    if param1.nil?
      ERROR_UNEQUIP_PARAM_MISSING
    else
      world.player.inventory.unequip_item(param1)
    end
  when 'go', 'g'
    if param1.nil?
      puts ERROR_GO_PARAM_MISSING
      GO_PARAMS
    else
      direction = param1
      if world.can_move?(direction)
        world.player.go(world.locations, param1, world.sound)
        world.location_by_coords(world.player.cur_coords).checked_for_monsters = false
        world.describe(world.location_by_coords(world.player.cur_coords))
      else
        ERROR_GO_PARAM_INVALID
      end
    end
  when 'attack', 'a', 'fight', 'f'
    if param1.nil?
      ERROR_ATTACK_PARAM_MISSING
    else
      monster_name = param1
      if world.has_monster_to_attack?(monster_name)
        monster = world.location_by_coords(world.player.cur_coords).monster_by_name(monster_name)
        world.player.attack(world, monster)
      else
        ERROR_ATTACK_PARAM_INVALID
      end
    end
  when 'change', 'ch'
    if param1.nil?
      puts ERROR_CHANGE_PARAM_MISSING
      CHANGE_PARAMS
    else
      case param1
      when 'name'
        world.player.modify_name
      else
        ERROR_CHANGE_PARAM_INVALID
      end
    end
  when 'help', 'h'
    list_commands
  when 'quit', 'exit', 'q', 'x'
    puts "You sure you want to quit? (y/n): "
    response = gets.chomp.downcase
    if (response.eql?("y") || response.eql?("yes"))
      puts QUIT_MESSAGE
      return 'exit'
    else
      puts RESUME_MESSAGE
    end
  when 'quit!', 'exit!', 'qq', 'xx'
    puts QUIT_MESSAGE
    return 'exit'
  else
    return
  end
end