Installation:
Copy BotHelper.lua & Server.lua into CS2D/sys/lua/*.*
IMPORTANT: Navigate to CS2D/bots/*.* and rename or move Standard AI.lua.
Code Examples:
More
For every example you must import the botHelper library.
Lets start by adding a bot, and making it join the terrorist team.
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.
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?
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?
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.
To stop hooks simply repeat the function with nothing in the brackets. (nil) or ()
1
dofile("sys/lua/botHelper.lua")
Lets start by adding a bot, and making it join the terrorist team.
1
2
3
2
3
bot=newBot() bot.joinGame() bot.joinTeam(1)
Wasn't that easy? There's a problem, it doesn't spawn.
Let's continue.
1
bot.spawn()
Hmm well it doesn't respawn when it dies.
Time to introduce hooks, similar to addhook found in the CS2D library.
1
2
3
4
5
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?
1
2
3
4
5
6
7
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?
1
2
3
4
5
6
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.
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
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 ()
ChangeLog: botHelper
More
[Major.Version.Revision]
[1.0.0]
[RELEASE] expect bugs and malfunctions
[1.0.1][Revision Change]
Typo bot, should be bots
Errors fixed in example code
Changelog
[1.0.0]
[RELEASE] expect bugs and malfunctions
[1.0.1][Revision Change]
Typo bot, should be bots
Errors fixed in example code
Changelog
edited 6×, last 26.01.12 07:22:11 pm
Approved by Seekay
Download
2 kb, 829 Downloads