Class: WizardsCastle::BattleRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/wizards-castle/battle_runner.rb

Defined Under Namespace

Modules: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player, enemy_symbol, printer, prompter) ⇒ BattleRunner

Returns a new instance of BattleRunner.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/wizards-castle/battle_runner.rb', line 11

def initialize(player,enemy_symbol,printer,prompter)
  raise "BattleRunner initialized with not-a-monster: #{enemy_symbol}" unless Castle::MONSTERS.include?(enemy_symbol)
  @player = player
  @enemy_symbol = enemy_symbol
  @printer = printer
  @prompter = prompter

  @enemy_power,@enemy_str = BattleRunner.enemy_stats(enemy_symbol)
  @bribable = true
  @web_counter = 0
end

Class Method Details

.enemy_stats(sym) ⇒ Object



77
78
79
80
# File 'lib/wizards-castle/battle_runner.rb', line 77

def self.enemy_stats(sym)
  code = sym==:vendor ? 13 : Castle::MONSTERS.index(sym)+1
  return [ (1+(code/2)), code+2 ]
end

Instance Method Details

#broken_weapon?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/wizards-castle/battle_runner.rb', line 139

def broken_weapon?
  Random.rand(8)==0 # 1/8 chance
end

#deathspell_kills_player?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/wizards-castle/battle_runner.rb', line 196

def deathspell_kills_player?
  @player.int < (Random.rand(4)+1+15)
end

#do_bribe?Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/wizards-castle/battle_runner.rb', line 144

def do_bribe?
  if @player.treasure_count < 1
    @printer.bribe_refused
  else
    desired_treasure = @player.random_treasure
    answer = @prompter.ask(['Y','N'],@printer.prompt_bribe_request(desired_treasure))
    if answer=='Y'
      @player.remove_treasure(desired_treasure)
      @printer.bribe_accepted
      return true
    end
  end
  false
end

#do_castObject



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
# File 'lib/wizards-castle/battle_runner.rb', line 160

def do_cast
  spell = @prompter.ask_for_anything(@printer.prompt_cast)[0]
  case spell
  when "W"
    @player.str(-1)
    @web_counter = random_web_duration
  when "F"
    @player.str(-1)
    @player.int(-1)
    dmg = random_fireball_damage
    @enemy_str -= dmg
    unless @player.dead?
      @printer.fireball_damage_report(dmg)
    end
  when "D"
    if deathspell_kills_player?
      @printer.deathspell_kills_player
      @player.int(-20)
    else
      @printer.deathspell_kills_enemy
      @enemy_str = 0
    end
  else
    @printer.cast_selection_error_msg
  end
  nil
end

#do_enemy_attackObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wizards-castle/battle_runner.rb', line 92

def do_enemy_attack
  @bribable = false

  if @web_counter > 0
    @web_counter -= 1
    @printer.the_web_broke if @web_counter < 1
  end

  if @web_counter > 0
    @printer.monster_stuck_in_web
  else
    @printer.the_monster_attacks
    if enemy_hit_player?
      @player.take_a_hit!(@enemy_power,@printer)
      @printer.he_hit_you
    else
      @printer.he_missed_you
    end
  end
end

#do_player_attackObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wizards-castle/battle_runner.rb', line 119

def do_player_attack
  if @player.weapon==:nothing
    @printer.unarmed_attack
  elsif @player.stickybook?
    @printer.book_attack
  else
    if player_hit_enemy?
      @printer.you_hit_him
      @enemy_str -= @player.weapon_value

      if [:gargoyle,:dragon].include?(@enemy_symbol)
        if broken_weapon?
          @printer.your_weapon_broke
          @player.set_weapon(:nothing)
        end
      end
    end
  end
end

#enemy_first_shot?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/wizards-castle/battle_runner.rb', line 82

def enemy_first_shot?
  @player.lethargic? || @player.blind? || (@player.dex < (Random.rand(9)+Random.rand(9)+2))
end

#enemy_hit_player?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/wizards-castle/battle_runner.rb', line 86

def enemy_hit_player?
  n = Random.rand(7)+Random.rand(7)+Random.rand(7)+3
  n += 3 if @player.blind?
  @player.dex < n
end

#player_hit_enemy?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/wizards-castle/battle_runner.rb', line 113

def player_hit_enemy?
  n = Random.rand(20)+1
  n += 3 if @player.blind?
  @player.dex >= n
end

#random_fireball_damageObject



192
193
194
# File 'lib/wizards-castle/battle_runner.rb', line 192

def random_fireball_damage
  Random.rand(7) + Random.rand(7) + 2
end

#random_web_durationObject



188
189
190
# File 'lib/wizards-castle/battle_runner.rb', line 188

def random_web_duration
  Random.rand(8) + 2
end

#runObject



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
# File 'lib/wizards-castle/battle_runner.rb', line 23

def run
  if enemy_first_shot?
    do_enemy_attack
    return Result::PLAYER_DEAD if @player.dead?
  end

  loop do
    @printer.youre_facing_a_monster
    @printer.combat_menu(@bribable)
    @printer.your_battle_stats

    allowed = ["A","R","C"]
    allowed << "B" if @bribable

    action = @prompter.ask_for_anything(@printer.prompt_combat)[0]
    if allowed.include?(action)
      case action
      when "A"
        @bribeable = false
        do_player_attack
        return Result::ENEMY_DEAD if @enemy_str<1
        do_enemy_attack
        return Result::PLAYER_DEAD if @player.dead?
      when "R" #retreat
        do_enemy_attack
        return @player.dead? ? Result::PLAYER_DEAD : Result::RETREAT
      when "B"
        return Result::BRIBED if do_bribe?
        do_enemy_attack
        return Result::PLAYER_DEAD if @player.dead?
      when "C"
        # Preserved authentic bug:
        #   In both the Powers and Stetson versions,
        #   you can cast whenever you can bribe,
        #   even if your INT is under 14.
        #   You can die if this cast drops your INT to 0.

        if (@player.int > 14) || @bribable
          do_cast
          return Result::PLAYER_DEAD if @player.dead? #due to cast-cost
          return Result::ENEMY_DEAD if @enemy_str<1
          do_enemy_attack
          return Result::PLAYER_DEAD if @player.dead?
        else
          @printer.cant_cast_now
        end
      end

    else
      @printer.combat_selection_error_msg
    end
  end
end