Posts Tagged ‘trekwar’

Oups

Monday, March 15th, 2010

While cleaning up in the core classes of Trekwar yesterday (documenting functions, formatting/structuring code, minor improvements) I also saw lots of equals(Object o) methods. And in my infinite wisdom I decided to use generics since it is used pretty extensively throughout the code anyway, so I changed basically all the equals methods.

example:

public boolean equals(Object o) {
        if(o instanceof Structure)
            return equals((Structure)o);
        return false;
    }

was changed to:

public boolean equals(Structure s2) {
        return name.equals(s2.getName());
    }

Now this is not a problem when writing code, as you pretty much always compare objects of the same type. However, the code also uses the Java Collections contains() method a few places, and it always uses the equals(Object) version. So when my classes no longer provided this method, the default one inherited from java.lang.Object was used instead. This caused a strange array of bugs to appear (star systems on the map not shown with faction color/icon except for the Cardassians for some strange reason, users not having any technologies, unable to build any structures, generally erratic behavior, etc..). Luckily I discovered the cause pretty quickly and only wasted about an hour or so on this :)

I guess the lesson is that objects that needs to be compared, directly or indirectly by contains() or other methods, should always define an equals(Object) method that just passes the call along to the equals method written with generics.

public boolean equals(Object o) {
        if(o instanceof Structure) {
            return equals((Structure)o);
        }
        return false;
    }

And perhaps that you should not mess around with your code for hours on end without stopping to test if you broke it once in a while :)

Trekwar cargo system

Sunday, March 7th, 2010

Today I mostly finished the Trekwar Cargo system, so now it’s possible to collect resources and bring them back to your starsystems.

Also included 4 images from the design and implementation of the UI:

Read full devblog post at trekwar.org

Trekwar sensor system

Thursday, February 18th, 2010

I haven’t had much time working on Trekwar lately, but I’ve recently made a prototype of the sensor system:

I’ll have to add this, as well as fog of war when I’m done with the fleet management tools which are almost ready.

> Read the whole devblog post @ trekwar.org

Resetting password in JSPWiki

Thursday, October 22nd, 2009

The Trekwar Wiki runs on JSPWiki 2.8.1 (which is pretty awesome BTW).

I just reinstalled my OS and had lost the password, and the Wiki is not set up to use email, so I could not reset it by web.

So I had to find a way to do it by console, and a google search did not provide a complete solution, so here it is:

  1. Log into the server where the Wiki is hosted
  2. generate your new password (the password in this example is “password” and it’s hash is the green text below, you can use this if you want)
    echo -n password | sha1sum
  3. The command above gives you a “encrypted” password: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
  4. The user database in JSPWiki is a xml file that resides in the WEB-APP directory and is called userdatabase.xml
  5. This file has a simple format, each user has a <user> tag. So just find the user you want to reset the password for
  6. The user tag has a password filed that looks something like this:
    password="{SSHA}XXXXXXXXXXXXXXXXXXXXXXXXX"

    Change this to: (remember to use your own password hash if you made one, or use the green one above):
    password="{SHA}YYYYYYYYYYYYYYYYYYYYYYYYYY"
  7. NOTE: changing from SSHA to SHA
  8. Log in with your new password (or “password” if you used the green hash above)
  9. Change your password!

Using WeakReference to confirm/find memory leaks in Java

Friday, September 4th, 2009

I recently had a memory leak in my Trekwar game, and to find it (or at least confirm it’s there) I used the WeakReference object in Java..

This is how I proceeded in locating the memory leak, this method could easily be adapted to other programs.

Trekwar is turn based, so each turn the client downloads a Galaxy object from the server. There are many references to this objects inside action listeners, threads, etc..

WeakReference
To make a long story short, a weak reference is like a normal reference, except it will not prevent the object it points to from being garbage collected.

This means that is you  have a normal and a weak reference to object A, the weak reference will be null if you remove the “hard” reference.

1) In the client main executable (the class that holds the method that updates the map from the server). Import, declare and make a list of weak references

import java.lang.ref.WeakReference;
private ArrayList<WeakReference<Galaxy>> weakRefs;
 
weakRefs = new ArrayList<WeakReference<Galaxy>>();

2) Find the place in your program where a new object is being added, in my place this is the localGalaxy object. Create a weak reference to this object, and list all your weak references.

localGalaxy = (Galaxy) objStream.readObject();
weakRefs.add(new WeakReference(localGalaxy));
for(int i = 0; i < weakRefs.size(); i++) {
System.out.print("ref # " + i + " = ");
try {
Galaxy testG = (Galaxy)weakRefs.get(i).get();
System.out.println(testG + ", tick = " + testG.currentTick);
}
catch(NullPointerException npe) {
System.out.println("null");
}
}

And you’re done!

Now when you run the program, you should see something like this each time the method is called

ref # 0 = org.aakretech.trekwar2.common.Galaxy@11ed166, tick = 37
ref # 1 = org.aakretech.trekwar2.common.Galaxy@9801f4, tick = 37
ref # 2 = org.aakretech.trekwar2.common.Galaxy@1c5d81c, tick = 38
ref # 3 = org.aakretech.trekwar2.common.Galaxy@101f8f4, tick = 39
ref # 4 = org.aakretech.trekwar2.common.Galaxy@1ed1e7e, tick = 40
ref # 5 = org.aakretech.trekwar2.common.Galaxy@e0a7ea, tick = 41

Unless you want to wait until your GC decides it’s time to go, use a tool like VisualVM to force the Garbage Collector to run.

After the GC has run, the output should look like this:

ref # 0 = null
ref # 1 = null
ref # 2 = null
ref # 3 = null
ref # 4 = null
ref # 5 = null
ref # 6 = org.aakretech.trekwar2.common.Galaxy@2b3c91, tick = 42

Now, this means that the object IS being garbage collected, and there is no memory leak.. If you run this and the object you are monitoring (in this example the localGalaxy object) is not GC’ed because of stray references, you can tell because none of the references will point to null.

JavaEaster

Friday, April 10th, 2009

Easter is almost over, and I used most of the time to complete the Trekwar shipdesigner, and set up lots of hulls for it.. I also created a chat system for the game.

ShipDesigner:
trekwar_shipdesigner_boomship

Faction Hulls:
xtrekwar_hulls_easter09

Chat:
trekwar_chat

I was planning to implement the ship actions, like colonization and ship combat this easter. But I think I’ll work on the Starsystem management system instead, as I redesigned it heavily this easter.

Trekwar shipdesigner demo

Friday, March 13th, 2009

Almost done for the Trekwar shipdesigner which allows you to custom fit your star ships with available components.

2H or not 2H?

Tuesday, February 10th, 2009

I decided to add Deuterium as a resource in Trekwar, it can be collected from nebulas or by owning star systems that have gas giants in them. Ships also now need deuterium to move, and can run out of gas if you don’t plan your logistics.
trekwar_deuterium

Deuterium and maintenance has been added to the menu bar, and fuel to the temporary fleet box

The need for speed

Saturday, February 7th, 2009

I finally got around to implement the new ship system for the Trekwar game.

Ships are no longer static objects with fixed attributes (weapon, shields, armor, etc..).

All ships are no based on a specific hull, which has some base armor and crap, + X number of slots you can put equiptment in. Below is the starting ship of all factions in the game:
trekwar_slots
If you noticed that this is the design of a Maquis raider, give yourself a cookie!

I got lot of Starship components to add, and so far I’ve just added ‘Warp Core’, ‘Colonization module’, ‘armor’, ‘beam emitters’ (phasers) and ‘Shield emitters’.

One new thing I implemented today was ship speeds. Unlike in Civilizzation / Birth of the Federation, where units can only move 1 or 2 tiles each turn. Ships in Trekwar can have a speed of 1.0 (which lets it always move 1 tile each turn) or 0.7 like the colonization ship in the screenshot below:

trekwar_race1

The upper most ship is a scoutship with a speed of 1.0

The lower ship is a colonization ship with a speed of 0.7, that means when it starts moving it will generate 0.7 movement points.. this is not enough to move a full tile. The next turn it generates 0.7 more and then gets 1.4 movement points.. It can the move 1 tile, and has 0.4 movement points “to spare”.. On the third turn in this example it gets 1.1 movement points and can move again..

Ships with a speed below 1.0 will occationally “miss” a movement while moving a distance.  Ships with speeds greater than 1.0 will occationally move 2 (or maybe even 3) tiles in a single turn.

Trekwar GUI updates

Tuesday, February 3rd, 2009

Recently much of my spare time programming has gone to giving the Trekwar client a graphical overhaul.

Now I’ve managed to get most of what I broke, working again (Research and Starsystem Control), and I’ve added a glasspane that shows up and prevents the clicking of buttons (and thus creating a weird state of the client) while each tick is being processed by the server.

trekwar_feb09_01
Currently the test galaxy is 100×100 tiles and takes about 0.5 seconds to load to client

trekwar_feb09_02

Unlike the previous version of the game, it is now possible to have many windows active on top of the actual game map.
The system box (far left of the screen) has been changed to display more information, and next up is a complete redesign of the fleet box.

But before making the new fleet box and fleet management system, I’ll have to do some major rewrite of the internal ship system and logic, to support user created ship classes.

I’m looking forward to 8-10 hours of pure java and logic.. There is only so much Swing a man can take :)