Wednesday, January 30, 2008

APE Google Blog

I'm always scrambling around for this link to the blog site of APE Physics engine.
http://groups.google.com/group/ape-general

JAGs 1.4m coming soon!

Eureka! I've done a lot since last post.

I'm close to releasing an alpha JAGS 1.4m.
The 'm' stands for multi game. This means one JAGs server will search an FTP directory, load up all those java games and connect the client to the correct backend game space.

For instance, I can set up an FTP folder and you will be able to upload your java game right along with mine.

This will be fantastic for testing, as I can have my own games going and so can you -- even ones that need java resources on the backend.

And if your game doesn't need backend java resources, JAGs 1.4m will still give you a virtual game space based on your whatever you want to call it in your Actionscript call.

Of course, groups are still supported within your game space. This allows you to further refine who everyone is playing with.

I've also improved the Actionscript client library. Now, it's much cleaner and uses events heavily. This means much lighter integration with any game you want to write = easier in general.

---------

The example I will release soon brings me to my final exciting bit of news: multiplayer physics APE.

Yes, that's right. I successfully sent complete APE "WheelParticle"s across the server from one Actionscript client to another! Yeah.

In this video, you can see two windows running some APE physics. The top window, when refreshed sends its wheel particles to the bottom window. The only way the bottom window gets the WheelParticle is through JAGS.

I hit refresh two times, and you can see where it made a couple of sets of wheels and sent them over the wire.



I'll have to post the code and write up how to make use of the IExternalizable interface so you can go crazy with sending any object you want over the net too.

Just need to sleep a little right now...

Thursday, January 10, 2008

Setting up Java Game Server 1.4

Couple of main points.

Project is ready to be opened in Netbeans 5.5.

Go to Trace.java class and take comments off of this one section.

public final class Trace {

public Trace() {
}

public static void out(String s) {
System.out.println(s);
}


In my production code I had the System.out.println(s); commented out.

Next, realize that once you fire the program off inside Netbeans (or from command line), you have to use Windows Task manager to stop it.

It's not like a regular java program where you can push stop. You'll find it floating around as a 6-8Meg java process in the task manager.

JAGS runs on port 81, so I you try to fire two of them off, or don't notice the other one still running, you'll get a startup message saying address already in use.

Here's a successful startup:


Here's what happens to me all the time when I try and run two of them (bad thing):
java.net.BindException: Address already in use: JVM_Bind



Once you've got your server running, and you've modified your Actionscript Client to point to your localhost, fire that Flex program off.

You should see messages talking about starting connections like this:

LISTENING on PORT 81
CONN # 0 :127.0.0.1
CONNECTION STARTED: 0
# Conns:1


And this...

--------------------
SECURITY POLICY SENT
--------------------
IN conn_0c: 1199952686796>no id0.17506256978958845>!server>Hello.
MESSAGE:
TIME: 1199952686843
FROM: no id0.17506256978958845
TO: !server
Action Verb: Hello.

COMMAND not found.Hello.
IN conn_0c: 1199952686859>no id0.17506256978958845>!server>!whats_my_name
MESSAGE:
TIME: 1199952686859
FROM: no id0.17506256978958845
TO: !server
Action Verb: !whats_my_name

OUT: !your_id:conn_0c

IN conn_0c: 1199952686859>no id0.17506256978958845>!server>!auto_match:2
MESSAGE:
TIME: 1199952686859
FROM: no id0.17506256978958845
TO: !server
Action Verb: !auto_match:2

:conn_0c looking for up to 2 total players.
OUT: !name_group:group_0g

OUT: !you_host:conn_0c

NEW GAME: group_0g


Now the arbitrary commands in the Actionscript Client project will start to make a little more sense.

Good luck! I've got to get some sleep...

Wednesday, January 9, 2008

JAGS Google Code Site

Just wanted to keep an easy link back to the google code site.

Go here to download code or see the code project page.
Project page
Download page

My site fireleg.com/jags.html also works, but I think I'll be switching over to the google code to post any new code.

Configuring the Actionscript Client

Once your Flex project is working, you may want to know how to change the most important things in the code.

Look at the Config.as file.

package GameKit
{
public final class Config
{
public static var URL:String = "fireleg.com";
//public static var URL:String = "localhost";
public static var port:uint = 81;

public static var framesPerSecond:int = 24;

// Zero means we synchronize the world every frame.
// 1,2,3 means we count 1,2,3 frames before synching.
// This keeps your world looking smooth while the network
// can chill out a little bit.
public static var dampenNetSyncBy:int = 0;


If you are going to run the Java server on your own machine, then switch out my "fireleg.com" address with your test environment, probably "localhost".

I chose port 81 since it's one of those ports that almost everyone's firewall will allow through, but does not interfere with web servers.

The frames per second can be adjusted as needed for your game's performance.

And finally, you may not want to send a game message for every frame. Your game may be able to do just fine with only sending a particle update every nth frame.

For instance, a checkers game might only need a refresh 4 times per second. If you have framesPerSecond=16, you would set dampenNetSyncBy=4 to achieve your goal.

Now look at MXMLPong.mxml
mx:application mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationcomplete="onAppComplete();"

This applicationcomplete event is different than many Actionscript examples you'll see. It actually waits until everything is created and avoids some serious errors. Be sure you use that exact function to launch your games framework.

Inside this function, we start the main game loop. Which is the heartbeat of the entire game:
private function onAppComplete():void
{
var framesPerSecond:int = GameKit.Config.framesPerSecond;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;

game = new GameLoop(framesPerSecond, this);
This causes us to dive down into the GameLoop.as:

The MXMLPong file created the GameLoop object, which you can see is hungry for a sprite to display the game in.

 // sprite is the main container to display the game in
public function GameLoop(framesPerSecond:uint, sprite:Sprite) {
var refreshMillis:uint = 1000/framesPerSecond;


This function then calls the init function, which has one very important line of code:

gameTimer = new Timer(refreshMillis,0);
gameTimer.addEventListener(TimerEvent.TIMER, myLoop);


This kicks off the whole game at whatever frames per second you specified earlier, and will constantly call the myLoop() function.

Looking at myLoop(), you will see a whole checklist of things it tries to do again and again.

For instance, the first thing it does is check that a connection with the game server is made.

public function myLoop(timer:TimerEvent):void {
// this is that warmup time from the message sent above
if (!gameServer.connected || gameServer.msgCount < 1) {
trace("waiting for connection or first response...");
return;
}


You may think it's strange to keep "return"ing out of this function, but don't worry, it will be called many times until a connection is made.

Read through the rest of this function and you'll be on your way to understanding some of the code.

World.as contains the collision stuff.

Particle.as determines how the physical universe works in your game.

Commands.as is where you have to add how the network will pack/unpack the objects. It's a little ugly looking and you may just want to skip down to this part of the code:
 if (command != null && command.length > 1) {
if (command[0] == "place") { ...


Even then, my method requires hard-coded parsing of each element. I'm sure you may be able to improve this part of the code, but it's a very basic approach.

NetSynchronizer.as isn't used. I was probably thinking of making a generic way to avoid all the ugly parsing in commands.as. Ideally, there is probably a simple Flex way to package up any object.

The thing is, I want to be able to send a package with a header plus that AMF or whatever object. Obviously I just need a little more knowledge here so I can make use of the full Actionscript power.

NetworkConnection.as is super important. It takes care of the arbitrary protocol that I invented to speak with the Java server.

You will see all kinds of strange commands in there to join a group, send a message to your group or to everyone. This class is worth reading carefully once you're feeling good about the project.

You can see what's being sent back and forth by binding to these variables.

[Bindable] public var lastMessageIn:String; // from Server
[Bindable] public var lastMessageOut:String; // To Server


You may have noticed in the MXMLPong.mxml I put some text objects at the bottom of the screen to show you what is being said through NetworkConnection.as.

For those of you new to binding in Actionscript 3.0, just put something like this on your page:

<mx:Text x="315" y="482" text="{game.gameServer.lastMessageOut}" id="last_sent">
<mx:Text x="315" y="482" text="{game.gameServer.lastMessageIn}" id="last_in">

Creating the Flex Project

After unpacking the jags_client1.4.rar into normal folders, open up Flex 2.0 IDE.
Go File > Import > Existing Projects into Workspace.


Browse to the top level folder like so.


Right click > Run Application on MXMLPong.mxml to start the game.
Do this twice (so you have someone to play against).


You should see a game screen something like this.
If so, you've loaded the Actionscript 3.0 Pong client!


Tuesday, January 8, 2008

fireleg.com game server

I just want to get project JAGS (java actionscript game server) on people's radar.

This is a working game server that can teach you the basics of Flash/Flex security, dealing with multiple threads, sending messages from one Actionscript 3.0 program to another through sockets, etc.

The great thing is that you can start using it right away with no java configuration and no flex data services configuration. That's because I pay for a java server that is always up 24/7 and it's available for client to server requests.

For now, I'll just post the Actionscript 3.0 game code, and the java server code here.

To play the pong game browse to fireleg.com and play the pong demo.

Learn what you can from the code, enjoy. Feel free to email me at travis@fireleg.com.