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
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
|
class ActivityProcessor
attr_reader :character, :activity, :results
def initialize(character)
@character = character
@activity = character.activity
@results = []
process
end
def process
return unless @character.activity && @character.activity_time_remaining <= 0
if @character.resting?
@results.replace([{ type: "error", message: "You can't do anything while you're resting." }])
@character.stop_activity
return
end
unless @character.can_do_activity?(@activity)
message = "You can't do this right now."
message += " (requires #{@activity.requirements&.join(", ")})" if @activity.requirements.any?
message += " (costs #{@activity.costs&.join(", ")})" if @activity.costs.any?
@results.replace([{ type: "error", message: message }])
@character.stop_activity
return
end
Character.transaction do
if @character.rested_duration > 0
remaining_rested_duration = @character.rested_duration - @character.rested_duration_to_spend_on_activity
@character.update!(rested_duration: remaining_rested_duration)
end
@character.pay_activity_cost
@activity.whatnot[:results].each do |result|
type = result[:type]
case type
when "xp"
puts "Result: #{result}"
handle_xp_result(result)
when "monster_spawn"
raise TooManyWoundsError unless @character.can_fight?
next if rand > (result[:chance] || 1)
@results.push({ type: "br" })
monster_spawn = MonsterSpawn.where(location: Location.find_by_gid(result[:location])).select(&:alive?).first
raise MonsterSpawnError unless monster_spawn
@results.push({ type: type, monster_spawn: monster_spawn })
resolve_combat_with(monster_spawn)
break
when "monster"
raise TooManyWoundsError unless @character.can_fight?
next if rand > (result[:chance] || 1)
@results.push({ type: "br" })
if result[:table].pluck(:gid).include?(@character.trophy_monster_gid)
monster = Monster.find_by_gid(@character.trophy_monster_gid)
@results.push({ type: type, monster: monster })
resolve_combat_with(monster)
break
end
table_roll = rand
result[:table].sort_by { |t| -t[:score] }.each do |table_entry|
score = table_entry[:score]
if table_roll >= score
monster = Monster.find_by_gid(table_entry[:gid])
@results.push({ type: type, monster: monster })
resolve_combat_with(monster)
break
end
end
when "item"
handle_item_result(result, with_xp: true)
when "hearth_amenity"
bhi = @character.hearth.built_hearth_amenities
.find_or_initialize_by(hearth_amenity: HearthAmenity.find_by_gid(result[:gid]))
bhi.update(level: result[:level])
@results.push({ type: type, hearth_amenity: bhi.hearth_amenity })
when "hearth_planting"
unless @character.hearth.available_planting_spots > 0
@results.replace([{ type: "error", message: "You're out of space to plant seeds." }])
@character.stop_activity
return
end
item = Item.find_by_gid(result[:gid])
hp = @character.hearth.hearth_plantings.create(item: item)
@results.push({ type: type, hearth_planting: hp })
when "condition"
Character.transaction do
condition = Condition.find_by_gid(result[:gid])
@character.states.create!(condition: condition, expires_at: Time.now + result[:duration])
@results.push({ type: "message", body: result[:message] })
@results.push({ type: type, condition: condition })
end
when "activity"
next if rand > (result[:chance] || 1)
table_roll = rand
result[:table].sort_by { |t| -t[:score] }.each do |table_entry|
score = table_entry[:score]
if table_roll >= score
new_activity = Activity.find_by_gid(table_entry[:gid])
raise "Invalid activity gid (#{table_entry[:gid]})" unless new_activity
unless @character.learned_activities.exists?(activity: new_activity)
@character.learned_activities.create(activity: new_activity)
@results.push({ type: type, activity: new_activity })
end
end
end
else
raise "Invalid result type (#{type})" # TODO: Improve this.
end
end
# Note: This will result in equipment being checked for breakage twice (after combat, and now) if it provides the
# `beastslay_speed` stat. At the time of this writing, that stat doesn't exist.
# But just something to keep in mind.
# Note: This will result in equipment being checked twice if it provides two speed stats.
# Fine for now since no equipment gives two skill speed stats, but may want to refine in the future.
if @activity.whatnot[:requirements]&.any?
required_skill_gids = @activity.whatnot[:requirements].select { |r| r[:type] == "skill" }.map { |r| r[:gid] }.uniq
required_skill_gids.each do |required_skill_gid|
skill = Skill.find_by_gid(required_skill_gid)
broken_item = @character.do_equipment_break_check(skill: skill)
if broken_item
@results.push({ type: "warning", message: "Your #{broken_item.name} was damaged beyond repair!" })
end
broken_infix_item = @character.do_item_infix_break_check(skill: skill)
if broken_infix_item
@results.push({ type: "warning", message: "Your #{broken_infix_item.name} omen faded away." })
end
end
end
if @character.activity && @character.queued_actions
if @character.queued_actions > 0
@character.queued_actions -= 1
@character.activity_started_at = Time.now
@character.save
else
@character.stop_activity
@results.push({ type: "message", body: "You have finished your work." })
return
end
else
@character.update(activity_started_at: Time.now)
end
unless @results.any?
@results.push({ type: "message", body: "You come up empty." })
end
# HACK: To display any titles that were gained indirectly (not as part of the activity results).
@character.title_awards.where(created_at: 5.seconds.ago..).each do |title_award|
@results.push({ type: "title", title: title_award.title })
end
end
rescue ItemQuantityError
@character.stop_activity
@results.replace([{ type: "error", message: "You don't have enough items to complete this activity." }])
rescue HearthPlantingError
@character.stop_activity
@results.replace([{ type: "error", message: "You don't have that crop planted." }])
rescue TooManyWoundsError
@character.stop_activity
@results.replace([{ type: "error",
message: "You can't fight in your condition. You'll have to heal a wound." }])
rescue MonsterSpawnError
@character.stop_activity
@results.replace([{ type: "error",
message: "There are no living leviathans here." }])
end
private
def roll(sides)
rand(sides) + 1
end
def give_item(data, quantity, with_xp: false)
item = Item.find_by_gid(data[:gid])
xp_awards = []
if with_xp
xp_awards = data[:xp]&.map { |xpe| { skill: Skill.find_by_gid(xpe[:gid]), amount: xpe[:value] } }
xp_awards&.each do |award|
@character.add_skill_xp(award[:skill], (award[:amount] * quantity))
end
end
@character.shift_item(item, quantity)
@results.push({ type: "item", item: item, quantity: quantity, xp: xp_awards })
end
def handle_title_result(data)
title = Title.find_by_gid(data[:gid])
if @character.award_title(title)
@results.push({ type: "title", title: title })
end
end
def handle_xp_result(data)
skill = Skill.find_by_gid(data[:gid])
amount = data[:base]
@character.add_skill_xp(skill, amount)
@results.push({ type: "xp", skill: skill, xp: amount })
end
def handle_item_result(data, with_xp: false)
return if rand > (data[:chance] || 1)
if data[:table]
table_roll = rand
data[:table].sort_by { |t| -t[:score] }.each do |table_entry|
min_quantity = table_entry[:min_quantity] || table_entry[:quantity] || 1
max_quantity = table_entry[:max_quantity] || table_entry[:quantity] || 1
quantity = rand(min_quantity..max_quantity)
score = table_entry[:score]
if table_roll >= score
give_item(table_entry, quantity, with_xp: with_xp)
table_entry[:titles]&.each do |title_data|
handle_title_result(title_data)
end
break
end
end
else
min_quantity = data[:min_quantity] || data[:quantity] || 1
max_quantity = data[:max_quantity] || data[:quantity] || 1
quantity = rand(min_quantity..max_quantity)
give_item(data, quantity, with_xp: with_xp)
end
end
def resolve_combat_with(mon)
monster_spawn = nil
if mon.is_a? MonsterSpawn
monster_spawn = mon
mon = monster_spawn.monster
end
char = @character
char_hp = @character.max_hp
mon_hp = monster_spawn.present? ? monster_spawn.remaining_hp : mon.max_hp
combat_message = ->(msg) { @results.push({ type: "message", body: "[#{char_hp}/#{char.max_hp}] #{msg}" }) }
char_initiative = roll(20) + char.speed
mon_initative = roll(20) + mon.speed
if char_initiative > mon_initative
turn_order = [[char, mon], [mon, char]]
elsif mon_initative > char_initiative
turn_order = [[mon, char], [char, mon]]
else
turn_order = [[char, mon], [mon, char]].shuffle
end
turn_order.cycle do |actor, target|
base_accuracy_roll = roll(20)
accuracy_roll = base_accuracy_roll + actor.accuracy(with_combat_style: true)
if actor == mon
accuracy_roll += char.total_enemy_stat_change("accuracy")
end
evasion_roll = roll(20) + target.evasion(with_combat_style: true)
if accuracy_roll >= evasion_roll || base_accuracy_roll == 20
dealt_damage = {}
actor.damage_ranges.each do |data|
damage_roll = rand(data[:min]..data[:max])
dealt_damage[data[:gid]] = damage_roll
end
# If you can't do damage any other way, hit 'em with your fists!
if actor.damage_ranges.none?
dealt_damage["bash"] = rand(1..2)
combat_message.call("Lacking any other weapon, #{actor.name} thrashes wildly!")
end
if base_accuracy_roll == 20
combat_message.call("#{actor.name} landed a critical hit!")
dealt_damage.each do |gid, amount|
dealt_damage[gid] = amount * 2
end
end
# Apply power to random damages
actor.power.times do
dealt_damage[dealt_damage.keys.sample] += 1
end
resolved_damage = {}
dealt_damage.each do |gid, amount|
effective_resistance = [target.resistance(gid), amount].min
resolved_damage[gid] = amount - (effective_resistance * rand(0.5..1)).round
end
total_damage = resolved_damage.values.sum
actor == char ? mon_hp -= total_damage : char_hp -= total_damage
damage_text = "#{total_damage} damage (#{resolved_damage.to_a.map { |gid, amount| "#{amount} #{gid}" }.join(", ")})"
combat_message.call("#{actor.name} hit for #{damage_text}")
elsif evasion_roll > accuracy_roll
combat_message.call("#{target.name} evaded #{actor.name}'s attack.")
end
# HACK: DoT is char-only, and one-damage-type-only for now.
if actor == char
actor.dots.each do |data|
damage_roll = rand(data[:min]..data[:max])
effective_resistance = [target.resistance(data[:gid]), damage_roll].min
resolved_damage = damage_roll - (effective_resistance * rand(0.5..1)).round
mon_hp -= resolved_damage
combat_message.call("#{data[:message]} (#{resolved_damage} #{data[:gid]})")
end
end
if char_hp < 1 || mon_hp < 1
broken_item = @character.do_equipment_break_check
if broken_item
@results.push({ type: "warning", message: "Your #{broken_item.name} was damaged beyond repair!" })
end
if monster_spawn
hp_lost = monster_spawn.remaining_hp - mon_hp
if hp_lost > 0
monster_spawn.monster_spawn_combats.create(monster_spawn: monster_spawn, character: char,
hp_lost: monster_spawn.remaining_hp - mon_hp)
end
end
if char_hp < 1
@results.push({ type: "message", body: "You were defeated! You retreat, wounded." })
char.wounds += 1
char.save!
unless char.can_fight?
@results.push({ type: "error",
message: "You can't fight in your condition. You'll have to heal a wound." })
char.stop_activity
return
end
else
@results.push({ type: "message", body: "You slew the #{mon.name}." })
monster_kills = character.monster_kills.find_or_initialize_by(monster: mon)
monster_kills.quantity ? monster_kills.quantity += 1 : monster_kills.quantity = 1
monster_kills.save
if monster_spawn
char.stop_activity
return
else
mon.whatnot[:awards]&.each do |award_data|
case award_data[:type]
when "title"
handle_title_result(award_data)
when "xp"
handle_xp_result(award_data)
when "item"
handle_item_result(award_data)
else
raise "Invalid award type string (#{award_data[:type]})"
end
end
end
end
break
end
end
end
end
|