[Home]TipsToMakeAMod

Robo Home | Changes | Preferences | AllPages

Put here any idea you have about making a mod for robocode.

Also visit ModdedBot for a working template to make a mod.

Dealing with security

If your class is in the robocode package then the security manager won't do anything.

Executing code at the end of a round

The easiest way to change robocode is by redefining the execute() methods. Then you can make some test, operation or wathever you need to do at the end of each round. Don't forget to declare it as final so Bots can't redifine it and bypass your test, operation and so on. Don't forget, also, to redefine all blocking call like ahead() by using execute like this :

public final void ahead(double dist){
    this.setAhead(dist)
    do{
        execute()
    }while(getDistanceRemaining()>0);
}

With this you can execute code at the end of each round.

Can't you just redefine tick() instead? That way you shouldn't have to redo all the blocking calls, unless i've misunderstood something. -- Tango

Everything in this page refers to what overrides AdvancedRobot; the tick() method is within the RobotPeer?, so you can't override it. Modifying the peer is not really possible without some rather large modifications to Robocode, because it is created by the game; you can't simply extend RobotPeer? because the game will ignore your object and still use RobotPeer?. -- Vuen

Executing code at the begining of a round

To execute code at the beginning create a custom event like this :

addCustomEvent(new Condition("My name",99){
    public boolean test(){codeToExecute();return false;}

})
private void codeToExecute(){
    Things you need to do
}

Won't that fire at the beginning of each tick, rather than each round? To get something to happen at the beginning of each round, you just need an init() method called from the run() method. -- Tango

Yes, this will fire at the beginning of each tick; the way to handle it firing at each round is basically override anything the user might call, and make it call your own init method before performing the action the robot called. This does mean that the very first tick, the robot may perform some calculations before the robot is actually inited; however this usually doesn't matter, and can be ignored. You can make your init() method protected and suggest to the bot authers to call init() at the start of their run() method if they are worried about this, but it shouldn't be needed. Note that your init method should return immediately if the robot is already inited. -- Vuen


This is handy to avoid rennaming methods to get informed about events.

Vector events;
private void codeToExecute(){
    events.clear(); //don't forget to make this or you will have a unhandled recursivity (or something like this)
    events = this.getAllEvents();
    this.clearAllEvents();
    for(int i=0;i<events.size();i++){
       privateMethodsForEvents(events.get(i));
    }
}

Then just after execute call dispatchEvent

public void dispatchEvent(){
    for(int i=0;i<events.size();i++){
       if(events.get(i) instanceof ScannedRobot)this.onScannedRobot(events.get(i));
        else if(... etc.
    }

}
This tips for accessing events was found by Vuen, thanks to him.

If you have problem accessing a method of an original robocode class try to access via a class in the same package.

-- Synnalagma


(my bad, read wrong again...) Hmm... Event management in the underlying modbot is much more complicated than this. You have to override addCustomEvent and make it init() before continuing so that your custom event gets added first. You also have to override all getXXXEvents() methods to allow the user to still use these methods, and pull the appropriate events from the events vector into a new vector and return it. You also have to remove any events from the Vector you don't want the robot to get, and you have to add to it any events you'd like to create.

I think what I'll do is I'll write up a big template for a ModdedBot that has all of this functionality built in and post it up here, so you just have to edit the appropriate methods to your liking. I'll see if I can get this done in the next couple days...

-- Vuen

I Agree, I just wanted to share this ideas. -- Synnalagma

You talk about making new events that extend other events, but that would force bot makers to change their code to make it work. It is possible (and quiet easy) to modify the events in the original robocode source, and distribute them. I can't see it effecting normal gameplay, and it would be much simpler. What do people thing of this? My reason for asking is I would like to add an isFrozen() method to ScannedRobotEvent. -- Tango

Hmm. You could probably do this; the only problem I can see occurring is if some other mod decides to also modify ScannedRobotEvent; one of the two wouldn't work. What you could do is make for example a FrozenScannedRobotEvent? that extends ScannedRobotEvent; you could set it up so that it gets sent to onScannedRobot the same way, and if people want to use it, they simply type-cast it to FrozenScannedRobotEvent? to use it. It's really up to you. -- Vuen

Yeah, that is indeed a problem. I hadn't thought of that... I could do it the other way, i know, but i'd like to avoid forcing the coder to change things... I'll think about it, there might be a solution... -- Tango

The extend way, the coder doesn't really have to change anything, since it's still a ScannedRobotEvent; they only need to typecast it if they want to read isFrozen(). -- Vuen

True... i guess they would have to change something to use a new method anyway, so they might as well type cast it at the same time. Good point. I'll still try thinking about it though. -- Tango

OK, i've got half an idea. How does wiki-based coding sound? Basically we have a page on this wiki that anyone can edit and that consists of the code for the ScannedRobotEvent (and other pages for the other events), once a day, or something, the server compiles the code and lets people download it. When someone writes a new mod they add the methods they need to the wiki-page. It would be more sensitive to vandals and mistakes than a usual wiki-page, because there is a chance to destroy computers if you want to, but i'm sure we can monitor it successfully. I don't think any mods will conflict with eachother, as long as they don't use the same name for different things. Please give me your thoughts. -- Tango

Hmm... That seems a bit hardcore, considering there likely won't be that many mods out that want to edit the events; you can probably just go ahead and edit ScannedRobotEvent, since the additions you are making won't conflict with the game or with any other mods anyway. If another mod comes out that wants to modify ScannedRobotEvent, we can deal with it then : ). -- Vuen

I think it would even still make more sense to just make a ScannedRobotEventFM? that extends ScannedRobotEvent and has the desired extra stuff in it. Then you could make an optional onScannedRobotFM? where the parameter is explicitly a ScannedRobotEventFM? and also put the same object into the normal onScannedRobot if you wanted to (assuming that people won't override both without a good reason in mind). -- Kawigi

Both idea would work, but i would like something tidier. There are lots of mods that would want to change ScannedRobotEvent. For example a power-up mod would want isShielded(), hasBigGun?(), etc. -- Tango (in fact, once i have FreezeMod working, i think i will do a power-up mod, it should be quiet easy)

My way of doing the things follow this rule Never override any original robocode file otherwise people must have differents robocode installation. Subclassing is a good way and if you need something new there are great chance that you need a new event hanlder (I mean a new onXXXEvent and new Event). Subclassing is great because it work for people that just want to make a quick bot port for the mod. -- Synnalagma

My idea was to make sure you didn't need a new installation by making sure it didn't conflict with anything. However, i think i will stick with making completely new events. I have decided to leave the FreezeMod alone for now, and am working on the PowerUpMod because it doesn't need a server, so is much easier. For that i have a ScannedPowerUpEvent? which extends (and is identical to, at the moment) ScannedRobotEvent. I've made the PowerUpRobot? class, and am now making the PowerUp? class. Should hava a beta version ready for the end of the day. -- Tango

Cool, but RobocodeFreeze was a good idea. If you make it I can see for integrating it in RobocodeDeathMatch --Synnalagma

I still plan to finish the FreezeMod, but it will take more time, and i am impatient and want to get a mod finished soon. I will probably start FreezeMod again from scratch using ModdedBot because it is so much better than my current one. I did have it working all apart from the server before, and it shouldn't take long to get back to that stage. I have looked at the DMServer and i think i understand what it is doing, so i just have to modify it to work for the FreezeMod. -- Tango

Help! It was working reasonably well, i make a few changes so it works better, and now i'm getting NullPointerExceptions?. I've even commented out the changes, and the exceptions are still there. The code causing the problems is this:

public class PowerUp extends TeamRobot
{
	int i;
	RobotPeerVector v;
	Hashtable robots;
	EnergySetter me;
	public void init() {
		v=peer.getBattle().getRobots();
		robots = new Hashtable();
		me=new EnergySetter(peer);
		//me.setEnergy(500);
		for (i=0;i<v.size();i++) {
			robots.put(v.elementAt(i).getName(),new EnergySetter(v.elementAt(i)));
		}		
	}
}

I can't see anything wrong with it, can anyone else? -- Tango

Fixed it. There was indeed nothing wrong with it. Robocode only looks at the class files when it loads, and remembers them, so if you change anything you have to restart robocode before it will work. I realised that some time ago, but forgot. I managed to waste a fair bit of time on that... The first Beta is ready now, so i'll upload it. -- Tango

How can you access peer? When I try, it doesn't let me --MrBob

You can't, and with good reason. You have to write your own subclass of Robot (or AdvancedRobot, etc) in the robocode package to have access.


Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited June 1, 2004 16:07 EST by Kawigi (diff)
Search: