Jasmin/Example

From Robowiki
Jump to navigation Jump to search

Example of a simple robot written in Jasmin Assembly code.

This robot moves perpendicular to the enemy very slowly (enough to dodge head on targeting). While firing head on at the enemy. When it hits a wall, it reverses direction.

; --- Copyright Robert Maupin 2012. All rights reserved. -----------------
; File:      cs/Nano.j
; Author:    Robert Maupin, 3 December 2012
; -------------------------------------------------------------------------

; WARNING: you may not be able to decompile this class

.class public cs/Nano
.super robocode/AdvancedRobot

;specify a field
.field private direction D

; initializer
.method public <init>()V
	.limit stack 4
	aload_0
	invokenonvirtual robocode/AdvancedRobot/<init>()V
	
	aload_0
	dconst_1
	putfield cs/Nano/direction D
	
	return
.end method

;run method
.method public run()V
	;these don't have to be precise, just big enough to hold everything :)
	.limit stack 8
	.limit locals 4
	aload_0 ; the 'this' statement...
	
	;setAdjustGunForRobotTurn(true);
	iconst_1
	invokevirtual robocode/AdvancedRobot/setAdjustGunForRobotTurn(Z)V
	
	aload_0
	;turnRadarRightRadians(1.0/0.0);
	dconst_1
	dconst_0
	ddiv
	invokevirtual robocode/AdvancedRobot/turnRadarRightRadians(D)V
    
	return
.end method

;turn onScannedRobot...
.method public onScannedRobot(Lrobocode.ScannedRobotEvent;)V
	;these don't have to be precise, just big enough to hold everything :)
	.limit stack 8
	.limit locals 4
	
	;if (1 < getOthers())
	aload_0
	invokevirtual robocode/AdvancedRobot/getOthers()I
	iconst_1
	
	;this is 3 bytes.... all jumps are (cept jsr_w, which is 5)
	if_icmplt skipInfinityLock
	
	;Infinity Lock Radar
	;setTurnRadarLeftRadians(getRadarTurnRemainingRadians());
	aload_0
	dup
	invokevirtual robocode/AdvancedRobot/getRadarTurnRemainingRadians()D
	invokevirtual robocode/AdvancedRobot/setTurnRadarLeftRadians(D)V
	
	skipInfinityLock:
	
	;setTurnGunRightRadians(Utils.normalRelativeAngle(getHeadingRadians() +
	;	e.getBearingRadians() - getGunHeadingRadians()))
	
	;load this early for setTurnGunRightRadians
	;so we don't have to do stack manipulation later
	aload_0 
	
	;;;get the angle to the enemy
	;getHeadingRadians()
	aload_0
	invokevirtual robocode/AdvancedRobot/getHeadingRadians()D
	
	;e.getBearingRadians()
	aload_1
	invokevirtual robocode/ScannedRobotEvent/getBearingRadians()D
	dadd ;add the two values
	
	;getGunHeadingRadians()
	aload_0
	invokevirtual robocode/AdvancedRobot/getGunHeadingRadians()D
	dsub
	
	;normalRelativeAngle(value)
	invokestatic robocode/util/Utils/normalRelativeAngle(D)D
	
	;setTurnGunRightRadians(value)
	invokevirtual robocode/AdvancedRobot/setTurnGunRightRadians(D)V
	
	;setTurnRightRadians(Math.cos(e.getBearingRadians()));
	aload_0 
	aload_1
	invokevirtual robocode/ScannedRobotEvent/getBearingRadians()D
	invokestatic java/lang/Math/cos(D)D
	invokevirtual robocode/AdvancedRobot/setTurnRightRadians(D)V
	
	;setAhead(5*direction)
	aload_0
	iconst_5
	i2d
	aload_0
	getfield cs/Nano/direction D
	dmul
	invokevirtual robocode/AdvancedRobot/setAhead(D)V
	
	;setFire(3)
	aload_0
	iconst_3
	i2d
	invokevirtual robocode/AdvancedRobot/setFire(D)V
	
	return
.end method


;turn onHitWall...
.method public onHitWall(Lrobocode.HitWallEvent;)V
	;these don't have to be precise, just big enough to hold everything :)
	.limit stack 4
	.limit locals 4
	
	;direction = -direction;
	
	aload 0
	dup
	getfield cs/Nano/direction D
	dneg
	putfield cs/Nano/direction D
	
	return
.end method