Make 'BOTS'

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

UrbaLoca
Posts: 86
Joined: 29 Nov 2006, 16:44

Make 'BOTS'

Postby UrbaLoca » 06 Mar 2008, 22:25

Hello! Im making my mmo like Habbo and I need some Information.

What is a BOT?
------------------

A Bot is a character controlled by the Computer (or server).

My question is:
How I can add bots to my game? (static bots). For example, you say to bot:
Hello! He returns Hello.
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 07 Mar 2008, 07:47

Bots can be implemented in your server side extension: you can create "fake users" on the server side and control them with your custom logic so that they move around or respond to questions or execute any other activity...

SmartFoxServer expects users to be connected to an end-point (a socket), so you will have to open a socket connection for each bot created in order to make SFS treat them as real users.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 28 Apr 2008, 19:34

So, how can you open a socket connection? Can this be done on the same computer the server is running or the "host" has to do it? It is possible to make fake socket connection?

Also: Server side code cannot be excuted unless the client "fires" it? I was hoping that the server side code could run by it self, i.e like _root.onEnterFrame = function () but similar
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 29 Apr 2008, 07:35

Yes, you should open a socket connection to the localhost.
This would allow you to create a User object that behaves exactly like any other users. So it can send and receive messages, events etc...

Also: Server side code cannot be excuted unless the client "fires" it? I was hoping that the server side code could run by it self, i.e like _root.onEnterFrame = function () but similar


The server code can run its own processes, using setInterval in the case of Actionscript or Java threads

_root.onEnterFrame of course doesn't exist as you don't have timelines on the server side :D

Please check the documentation for all the details!
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Postby darnpunk » 05 May 2008, 02:37

Lapo wrote:Yes, you should open a socket connection to the localhost.
This would allow you to create a User object that behaves exactly like any other users. So it can send and receive messages, events etc...


Can you explain this a little bit more? To open a socket connection to the localhost, can this be done through an extension with server side code (create a new user object) or do I need to create a custom client(s) to connect as "fake users"?

I can't seem to find help on this in the documentation. I apologize if I overlooked.
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 05 May 2008, 05:32

A real User is identified by a SocketChanel object, which is unique and represents a user session.

This is the constructor for the User object:

Code: Select all

public User(SocketChannel channel, String name, String zone)


If you want to create "bots" that act like real players under all circumstances you should create a User, otherwise you could simply implement the bots via an extension and send additional messages to the client to update the status of these automated clients...

The advantage of using the User object is that you fool the server into thinking that this is a real client connected via socket from somewhere, so it will appear in the user list, it will be able to send and receive messages etc...

Of course you will have to create your own Bot-Manager or so, a class that will manage the activities of these users and don't forget to send a keep-alive message every once in a while or the server will disconnect them.

By the way creating the SocketChannel can be done like this:

Code: Select all

SocketChannel channel = SocketChannel.open()
channel.connect(new InetSocketAddress("127.0.0.1", 9339))
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Postby darnpunk » 05 May 2008, 14:32

I tried but somehow I kept getting an error. Here is my code used in Actionscript extension:

Code: Select all

var channel;
var inet;
var user;
var name;
var zone;

function init()
{

   inet = new java.net.InetSocketAddress("127.0.0.1", 9339);
   name = new java.lang.String("darnpunk");
   zone = new java.lang.String("dbZone");
   channel = new java.nio.channels.SocketChannel.open();
   channel.connect(inet);
   createBot(channel, name, zone);
   

}

function createBot(channel, name, zone) {
   
   try {
      
      user = new Packages.it.gotoandplay.smartfoxserver.data.User(channel, name, zone);
      
   } catch (e) {
      
      trace(e);
      
   }
   
}


The trace returned this exception - JavaException: java.lang.NullPointerException: null

Where am I going wrong? :?

Cheers,
darnpunk
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 05 May 2008, 15:49

The server is not yet listening on port 9339 when the Zones are setup, so you can't connect at that moment... I think.
Try delaying that function by 3-4 seconds
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
darnpunk
Posts: 229
Joined: 22 Jun 2007, 02:58
Location: SG

Postby darnpunk » 06 May 2008, 00:52

Great! It works and one thing I overlooked is that the connection address must be the same as the server ip address. Thanks!
turtlesoup
Posts: 77
Joined: 28 Dec 2007, 20:56

Postby turtlesoup » 06 May 2008, 16:23

Lapo wrote:A real User is identified by a SocketChanel object, which is unique and represents a user session.

This is the constructor for the User object:

Code: Select all

public User(SocketChannel channel, String name, String zone)


If you want to create "bots" that act like real players under all circumstances you should create a User, otherwise you could simply implement the bots via an extension and send additional messages to the client to update the status of these automated clients...

The advantage of using the User object is that you fool the server into thinking that this is a real client connected via socket from somewhere, so it will appear in the user list, it will be able to send and receive messages etc...

Of course you will have to create your own Bot-Manager or so, a class that will manage the activities of these users and don't forget to send a keep-alive message every once in a while or the server will disconnect them.

By the way creating the SocketChannel can be done like this:

Code: Select all

SocketChannel channel = SocketChannel.open()
channel.connect(new InetSocketAddress("127.0.0.1", 9339))



Hey Lapo,

Sorry if I'm being dense, but are you suggesting that if we create the bots as Users it should be from a separate application (e.g. flash client, java client, etc.) or the bots should still be handled from a server extension?

Thanks.
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 08 May 2008, 15:15

Your question is not clear, but no I am not saying to create bots from an outside application.

We create a "fake" connection to the localhost in order to simulate an external client for the Server. This way we "fool" it into thinking that this is a real user. This means that we'll see that user in each room where he's connected, can be added to buddy lists etc... and it can be "animated" by server side A.I.
Lapo

--

gotoAndPlay()

...addicted to flash games
turtlesoup
Posts: 77
Joined: 28 Dec 2007, 20:56

Postby turtlesoup » 12 Jun 2008, 23:27

Finally got around to trying this.
I set up the channels the same as darnpunk but when I try to create the new User I get an Null pointer exception in User.java line 141.

I used the scheduler to delay the channel setup by 5 seconds.

This is written in java btw.

Code: Select all

SocketChannel channel = SocketChannel.open();
InetSocketAddress inet = new InetSocketAddress("127.0.0.1", 9339);
channel.connect(inet);
           
User u = new User(channel, "zippy", "test");



Any hints on what happens in User.java at line 141?
turtlesoup
Posts: 77
Joined: 28 Dec 2007, 20:56

Postby turtlesoup » 14 Jun 2008, 01:25

Also,

For Lapo,

in this post, you say using Fake Users is not recommended

viewtopic.php?t=1319

:?:


in this post, you say there's some extra lines needed

viewtopic.php?t=2508

namely:

Code: Select all

chan.configureBlocking(false);
chan.register(SmartFoxServer.getInstance().getReadSelector(), SelectionKey.OP_READ, new Attachment());


and the Fake User is logged in


So...
1) Is it safe to use Fake Users?
If so..
2) What's the proper way to create this Fake User?
a)

Code: Select all

SocketChannel channel = SocketChannel.open();
InetSocketAddress inet = new InetSocketAddress("127.0.0.1", 9339);
channel.connect(inet);
           
User u = new User(channel, "zippy", "test");


or
b)

Code: Select all

SocketChannel chan = SocketChannel.open(new InetSocketAddress(ConfigData.SERVER_ADDR, ConfigData.SERVER_PORT));

chan.configureBlocking(false);
chan.register(SmartFoxServer.getInstance().getReadSelector(), SelectionKey.OP_READ, new Attachment());

theUser = helper.canLogin("User Name", "", chan, this.getOwnerZone());



and if it's the second code snippet, how would you write

Code: Select all

chan.register(SmartFoxServer.getInstance().getReadSelector(), SelectionKey.OP_READ, new Attachment());


in java?

Thanks! I've been using method a) but still can't get past the null exception in User.java:141
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 16 Jun 2008, 13:35

In SFS 1.6.x there're some extra operations that are needed for the BlueBox in each User object.
This involves referring to the server socket selector, which will return null for connections created outside of the server engine. Hence the error.

I think we can provide a workaround to this, but we have to do some preliminary tests. I will provide you a patch in the next days if you drop us an email as a reminder.

Thank you
Lapo

--

gotoAndPlay()

...addicted to flash games
samyphp
Posts: 55
Joined: 26 Mar 2008, 05:25
Location: Chennai - India

Postby samyphp » 16 Dec 2008, 07:55

i tried this..


var channel;
var inet;
var user;
var name;
var zone;
var loadTest;

function init()
{

inet = new java.net.InetSocketAddress("127.0.0.1", 9339);
name = new java.lang.String("darnpunk");
zone = new java.lang.String("dbZone");
channel = new java.nio.channels.SocketChannel.open();
channel.connect(inet);
//createBot(channel, name, zone);
loadTest=setInterval("createBot",5000);

}

function createBot() {
clearInterval(loadTest);
try {

user = new Packages.it.gotoandplay.smartfoxserver.data.User(channel, name, zone);

} catch (e) {

trace(e);

}

}


But am getting connection refused error in log...
Any help would be greatly appreciated.

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 61 guests