/
pwned.rb
234 lines (191 loc) · 5.03 KB
/
pwned.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
# Author: Chris St. John
# Email: chris@stjohnstudios.com
# Webume: http://www.linkedin.com/in/stjohncj
module Pwned
def to_s; '__pwned'; end
def move
if should_rest?
return [:rest, nil]
else
return [:attack, opponent_to_attack]
end
end
private
def opponent_to_attack
attackee = best_opponent_to_kill
if attackee.nil?
if alive_opponents.count > reaper_trigger
if groups_roaming?
attackee = highest_rated_from(grouped_opponents)
else
attackee = highest_rated_from(alive_opponents)
end
else
attackee = opponent_with_least_health
end
end
if attackee.nil? #should be just me and my clones
attackee = random_from(alive_pwned)
end
return attackee
end
def groups_roaming?
alive_opponents.each do |opponent|
return true if is_in_a_group?(opponent)
end
return false
end
def grouped_opponents
opponents = []
alive_opponents.each do |opponent|
opponents << opponent if is_in_a_group?(opponent)
end
end
def reaper_trigger
return (most_health? or am_leading?) ? 1 : 5
end
def most_health?
alive_opponents.each do |opponent|
return false if opponent.stats[:health] > health
end
return true
end
def least_health?
alive_opponents.each do |opponent|
return false if opponent.stats[:health] < health
end
return true
end
def opponent_with_least_health
min_health = 999999999
potential_attackees = []
alive_opponents.each do |opponent|
min_health = opponent.stats[:health] if opponent.stats[:health] < min_health
end
alive_opponents.each do |opponent|
potential_attackees << opponent if opponent.stats[:health] == min_health
end
return potential_attackees.first
end
def best_opponent_to_kill
earned_xp = 0
potential_attackees = []
alive_opponents.each do |opponent|
opponent_player = player_for(opponent)
if can_kill?(opponent) and opponent_player.max_health > earned_xp
earned_xp = opponent_player.max_health
end
end
alive_opponents.each do |opponent|
opponent_player = player_for(opponent)
if can_kill?(opponent) and (opponent_player.max_health == earned_xp)
potential_attackees << opponent
end
end
count = potential_attackees.count
if count >= 5
potential_attackees.shift
potential_attackees.pop
elsif count == 4
potential_attackees.shift
end
target = my_target_in(potential_attackees)
return target
end
def my_target_in(opponents)
opponents[0]
end
def alive_pwned
Game.world[:players].select{|p| p.alive && p.to_s =~ /^__pwned/ }
end
def random_from(opponents)
opponents[rand(opponents.count-1)]
end
def highest_rated_from(opponents)
opponents.sort_by {|o| [ o.stats[:experience], o.stats[:strength], o.stats[:defense], o.stats[:health] ] }
opponents.last
end
def second_highest_rated_from(opponents)
opponents.sort_by {|o| [ o.stats[:experience], o.stats[:strength], o.stats[:defense], o.stats[:health] ] }
opponents.count>=2 ? opponents[opponents.count-2] : opponents[opponents.count-1]
end
def should_rest?
return true if is_vulnerable? or in_mob_attack_danger?
return false
end
def is_vulnerable?
alive_opponents.each do |opponent|
return true if could_kill_me?(opponent)
end
return false
end
def in_mob_attack_danger?
return true if am_leader? or least_health?
end
def can_kill?(opponent)
hp_left = opponent.stats[:health] - damage_for(self,opponent)
return hp_left <= 0
end
def could_kill_me?(opponent)
hp_left = health - damage_for(opponent,self)
return hp_left <= 0
end
def damage_for(attacker, attackee)
attacker.stats[:strength] - attackee.stats[:defense]/2
end
def alive_opponents
Game.world[:players].select{|p| p != self && p.alive && p.to_s !~ /^__pwned/ }
end
def health
player.health
end
def max_health
player.max_health
end
def strength
player.strength
end
def defense
player.defense
end
def experience
player.instance_variable_get :@experience
end
def player
player = self.instance_variable_get :@player
end
def player_for(opponent)
player = opponent.instance_variable_get :@player
end
def am_leader?
alive_opponents.each do |opponent|
return false if opponent.stats[:experience] > experience
end
return true
end
def is_in_a_group?(opponent)
player_for(opponent).class.method_defined?(:friends)
end
end
module PwnedClone
include Pwned
def should_rest?
return true if is_vulnerable? or in_mob_attack_danger?
return true if alive_opponents.count == 0 # bow to my master
return false
end
end
module PwnedCloneLeft
include PwnedClone
def to_s; "__pwned_clone_left"; end
def my_target_in(opponents)
opponents[-1]
end
end
module PwnedCloneRight
include PwnedClone
def to_s; "__pwned_clone_right"; end
def my_target_in(opponents)
opponents[1]
end
end