For every example you must import the botHelper library.
Code:1
dofile("sys/lua/botHelper.lua")
Lets start by adding a bot, and making it join the terrorist team.
Code:1
2
3
bot=newBot()
bot.joinGame()
bot.joinTeam(1)
Wasn't that easy? There's a problem, it doesn't spawn.
Let's continue.
Hmm well it doesn't respawn when it dies.
Time to introduce hooks, similar to addhook found in the CS2D library.
Code:1
2
3
4
5
bot.onDeadFrame(
function()
bot.spawn()
end
)
As you can see, for every frame (50 times per second) the bot is dead it will run the function placed inside.
So when it's dead it will automatically respawn. (Unless on standard)
Okay how do I make the bot respond to other people talking?
Code:1
2
3
4
5
6
7
bot.onChat(
function(source,msg)
if msg=="go away" then
bot.leaveGame()
end
end
)
onChat has 3 parameters:
source: the player who is talking
msg: the message
team: if the message was sent via teamspeak
In this example the bot quits when someone says "go away".
How can we make the bot follow someone?
Code:1
2
3
4
5
6
bot.onLiveFrame(
function()
bot.moveTowards(player(1,"tilex"),player(1,"tiley"))
bot.aimCursor(player(1,"x"),player(1,"y"))
end
)
onLiveFrame is run when a bot is living (similar to onDeadFrame).
moveTowards will go to a tile, and avoid crashing into walls.
aimCursor simply aims the bot's rotation to the player it is following. (some math and bot.rotate() should work as well)
Okay lets bring say, moveTowards and everything together.
Code: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
dofile("sys/lua/botHelper.lua")
bot=newBot()
bot.joinGame()
bot.onDeadFrame(
function()
bot.spawn()
end
)
bot.onChat(
function(source,msg)
if source~=bot.getID() then
if string.lower(msg)=="follow" then
bot.say("I'm going to follow "..player(source,"name")..".")
bot.onLiveFrame(
function()
bot.moveTowards(player(source,"tilex"),player(source,"tiley"))
bot.aimCursor(player(source,"x"),player(source,"y"))
end
)
elseif string.lower(msg)=="stop" then
bot.say("I'm no longer following.")
bot.onLiveFrame()
end
end
end
)
bot.joinTeam(1)
To stop hooks simply repeat the function with nothing in the brackets. (nil) or ()