-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.coffee
204 lines (148 loc) · 5.46 KB
/
app.coffee
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
###
RobotQUEST AI
###
# On any error: I want to log the error, then exit and reconnect. (Throw the error)
{map, find, extend, filter, intersect, sortBy, last, id} = require 'underscore'
lib = require './lib'
{robotQuestApi, adjacent, dir, navigate, random, randomElement, id} = lib
{UP, DOWN, LEFT, RIGHT, STOP, ATTACK, MOVE} = lib
{directions, pointKey, distance, isHit, attack, move, stop} = lib
HOST = process.env.HOST || "http://localhost:3026"
AINAME = "AI"
REPO = "http://github.com/seanhess/robotquest"
MONSTERS = process.env.MONSTERS || 10
start = (host) ->
console.log "AI starting MONSTERS=#{MONSTERS} HOST=#{HOST}"
player =
name: AINAME
source: REPO
bots = []
# standard error handling
# should cause everything to exit
# OS will respawn it
onError = (err) -> console.log "ERROR", err.message
api = robotQuestApi host, onError
## START
api.gameInfo (info) ->
api.createPlayer player, (id) ->
player.id = id
poll = ->
api.objects (objects) ->
tick objects
setInterval poll, info.tick
## MONSTER ACTONS
# api: the api
# objects: the world
# player: the player
# bot: the bot
# info: the game info
act = (objects, bot) ->
ai = find ais, (a) -> a.name() is bot.name
ai.act api, info, player, objects, bot
## MAIN GAME
# objects: the world
tick = (objects) ->
# update all our bots with info from the server
bots = objects.filter(isAi).map (newBot) ->
bot = find bots, (b) -> b.id is newBot.id
extend bot ? {}, newBot
# if there are fewer than MONSTERS objects, then make some AI!
if bots.length < MONSTERS
x = random info.width
y = random info.height
type = randomElement ais
spawn(x, y, type.sprite(), type.name())
bots.forEach (bot) ->
act objects, bot
# SPAWN
spawn = (x, y, sprite, name) ->
bot = {x, y, sprite, name}
api.createMinion player, bot, ->
## HELPERS
isAi = (bot) -> bot.player == AINAME
wander = ->
direction = randomElement directions
action = randomElement ["Stop", "Stop", "Move"]
# action = randomElement ["Move"]
{action, direction}
## AI!
# RAT (boring little guys, they never attack, they don't move that much!)
rat =
name: -> "rat"
sprite: -> randomElement ["monster1-0-4", "monster1-1-4", "monster1-2-4", "monster1-3-4"]
act: (api, info, player, objects, bot) ->
api.command player, bot, wander(), ->
# ORC: will sometimes attack you if you are next to it for 2 turns
# they are slow, they take an extra turn to hit you, only if you are still next to them
orc =
name: -> "orc"
sprite: -> randomElement ["monster1-0-2", "monster1-1-2", "monster1-5-1"]
act: (api, info, player, objects, bot) ->
targets = filter objects, adjacent(bot)
targetIds = map targets, id
slowTargetIds = intersect bot.oldTargetIds, targetIds
command = if slowTargetIds.length
# attack them!!!
slowTarget = find targets, (b) -> b.id is slowTargetIds[0]
attack(navigate(bot, slowTarget))
else
wander()
bot.oldTargetIds = targetIds
api.command player, bot, command, ->
# BLARG: Wanders, but attacks perfectly if something comes near
blarg =
name: -> "blarg"
sprite: -> randomElement [
"monster2-2-6", "monster2-3-6", "monster2-4-6", "monster2-5-6"
"monster2-0-7", "monster2-1-7", "monster2-2-7", "monster2-3-7", "monster2-4-7", "monster2-5-7",
"monster2-0-8", "monster2-1-8", "monster2-2-8", "monster2-3-8", "monster2-4-8", "monster2-5-8"
]
act: (api, info, player, objects, bot) ->
targets = filter objects, adjacent(bot)
command = if targets.length > 0
attack(navigate(bot, targets[0]))
else wander()
api.command player, bot, command, ->
# GOOBER: Picks the nearest target within 3 spaces or so, then attacks
# TODO fix oscillation by allowing a NONE direction, in addition to a stop action
demon =
name: -> "demon"
sprite: -> randomElement ["monster1-0-5", "monster1-1-5", "monster1-2-5", "monster1-3-5", "monster1-4-5", "monster1-5-5"]
act: (api, info, player, objects, bot) ->
ds = map objects, (b) ->
bot: b
distance: distance(bot, b)
# this will also prevent you from picking yourself
ds = ds.filter (obj) -> 0 < obj.distance < 3
ds = sortBy ds, (obj) -> obj.distance
target = ds[0]
command = if target?
dir = navigate bot, target.bot
if target.distance is 1 then attack dir
else move dir
else wander()
api.command player, bot, command, ->
sorcerer =
name: -> "sorcerer"
sprite: -> "monster1-4-1"
act: (api, info, player, objects, bot) ->
leaders = sortBy objects, (b) -> b.kills
target = last leaders
command = if target? and target.id != bot.id
dir = navigate bot, target
if adjacent bot, target then attack dir
else move dir
else wander()
api.command player, bot, command, ->
# SLUDGE: umm...
# MAGE: will hunt down the person with the most kills. At the top of the leaderboard :) Booyah!
# once it acquires a target it will NEVER give up!
# you must destroy it!
# DRAGON: never moves. Attacks anything near it immediately.
ais = [rat, rat, rat, rat, orc, orc, blarg, blarg, demon, sorcerer]
#ais = [orc, demon, sorcerer]
#ais = [sorcerer]
if module == require.main
start HOST
console.log "STARTED AI. HOST=#{HOST}"
## When you command something that doesn't exist any more you get a not authorized