DonHodges.com Logo

 

Controlling the barrels in Donkey Kong

By Don Hodges

Posted July 20, 2008

Last updated 8/14/2015

 

Jeff Kulczyckiís web site has some very good information on how to control the barrels in Donkey Kong. However, I believe that his information is incomplete. For example, he mentions probabilities X and Y but does not state what these are.

Also, Jeff writes that "It will not work for the first barrel that comes down."  The very first barrel of every stage is a "crazy" barrel; these crazy barrels never go down ladders, and they can never be controlled.

As we shall see, the second barrel will always take the first few ladders it encounters, until the oil can is on fire.  After that, this barrel can be controlled with the same chances as any other.

Every time a barrel reaches the top of a ladder, a subroutine is called to determine whether or not to take the ladder.  By analyzing the assembly language code we discover the mechanism that is used to determine whether or not a barrel will descend a ladder.

The first thing that is checked is whether or not the oil can is on fire. If the oil can is not yet on fire, then the barrel will take the ladder every time.

If the oil can is already on fire, then the game checks to see if the barrel is near the same level as Jumpman or below. If the barrel is near the same level or below, then the barrel will never take the ladder.

If neither of the above conditions are met, then the game looks at memory location #6380. I call this the difficulty level; it can vary from 1 to 5. This value starts out as the same as the level number, up to a maximum of  5. If the value is less than 5, it will increase by one after every 33 seconds (approximately) has passed on the level.

For example, on level 1, the difficulty starts out at 1. It will increase to 2 after 33 seconds, and it will increase to 3 after another 33 seconds have passed.  It will increase to 4 after 33 more seconds, but this usually happens right around the time the bonus timer is running out.

This number is grabbed and then divided by 2 with the remainder discarded, then 1 is added to this result. The possible answers generated by this algorithm are 1, 2, or 3. Letís call this answer the modified difficulty.

Then the program gets a random number between 0 and 255. This number is not truly random; it generated by adding the value of a couple of timers/counters which are constantly changing. This pseudo random number is saved in case it needs to be used at the end of the routine. Then this number is ANDed with #03, which then results in a random number between 0 and 3.

The higher the modified difficulty, the higher the chance of being able to control the barrel:  the modified difficulty is compared to this random number. If the random number is greater or equal, then the subroutine returns and the barrel will never go down the ladder.  If the random number is less than the modified difficulty, then the following checks are done.

If the Barrelís X position is equal to jumpmanís X position, then the barrel will go down the ladder. This means if jumpman is on the ladder, or on a ladder exactly underneath the one the barrel is deciding to go down, or standing exactly underneath it, then the barrel will take the ladder.

If not, then the game checks to see if the barrel is to the left or to the right of jumpmanís position. If the barrel is to the left of jumpman and his joystick is pushed to the left, then the barrel will take the ladder. If the barrel is to the right of jumpman and the joystick is pushed to the right, then the barrel will take the ladder.

Finally, if neither of the above three checks make the barrel come down, then the game takes the original random number and ANDs it with hex value #18. If the result is not zero, the barrel will not take the ladder; this happens approximately 75% of the time.  Otherwise the barrel will take the ladder; this happens approximately 25% of the time.

The bottom line:

On level 4 and above, all non-"crazy" barrels thrown after the oil fire is lit, and are above jumpman, have a 25% chance of completely ignoring the ladder and a 75% chance of being controlled to come down. Those that could be controlled but arenít, have a 25% chance of taking the ladder anyway.  For levels below 4, consult the following table:

Table1:  The chance that a player is able to control a barrel in Donkey Kong
Level 0 - 33 seconds  34 - 66 seconds 67 - 99 seconds  100 seconds and up
1 25% 50% 50% 75%
2 50% 50% 75% 75%
3 50% 75% 75% 75%
4 and up 75% 75% 75% 75%

 
// A barrel has reached a ladder. Decide whether or not to go down it.
// Decompiled from Z80 Assembly by Don Hodges // July, 2008

Take_ladder = false ;
If oil_fire == not_lit then {
	Take_ladder = true;
	Return ; }
End if ;
If Mario_height >= barrel_height then Return ;
R = random(255) ; 		// random number between 0 and 255 inclusive
R2 = R mod 3 ; 			// random number between 0 and 3 inclusive, based on R
If R2 >= ((difficulty div 2 ) + 1) then Return ;
If (barrel_x_position == Mario_x_position) or 
  ((barrel_x_position < Mario_x_position) and (joystick_direction == left)) or
  ((barrel_x_position > Mario_x_position) and (joystick_direction == right))
	then {
	Take_ladder = true;
	Return ; }
End if ;
If (R & 0x18) != 0 then Return ; // 75% chance of return without ladder
Take_ladder = true;		 // 25% chance of taking ladder
Return 

// original Z80 assembly code of the subroutine
2178 3A4863	LD A,(#6348) 	; get status of the oil can fire
217B A7		AND A 		; is the fire lit ?
217C CAB221	JP Z,#21B2 	; no, always take ladders before oil is lit
217F 3A0562	LD A,(#6205) 	; else load A with Mario's Y position + 5
2182 D604	SUB #04 	; subtract 4
2184 BA		CP D 		; is the barrel already below or same level as Mario ?
2185 D8		RET C 		; yes, return without taking ladder
2186 3A8063	LD A,(#6380) 	; else load A with difficulty from 1 to 5.
2189 1F		RRA 		; divide by 2, result can be 0, 1, or 2
218A 3C		INC A 		; increment. result is now 1, 2, or 3
218B 47		LD B,A 		; store into B
218C 3A1860	LD A,(#6018) 	; load A with random timer
218F 4F 	LD C,A 		; store into C for later use
2190 E603 	AND #03 	; mask bits. result now random number between 0 and 3
2192 B8 	CP B 		; is it greater than modified difficulty?
2193 D0 	RET NC 		; yes, return without taking ladder
2194 211060 	LD HL,#6010 	; else load HL with player input. 
2197 3A0362 	LD A,(#6203) 	; load A with Mario's X position
219A BB 	CP E 		; compare with barrel's X position
219B CAB221 	JP Z,#21B2 	; if equal, then go down ladder
219E D2A921 	JP NC,#21A9 	; if barrel is to right of Mario, check for moving to left
21A1 CB46 	BIT 0,(HL) 	; else is Mario trying to move right ?
21A3 CAAE21 	JP Z,#21AE 	; no, skip ahead
21A6 C3B221 	JP #21B2 	; yes, make barrel take ladder
21A9 CB4E 	BIT 1,(HL) 	; is Mario trying to move left ?
21AB C2B221 	JP NZ,#21B2 	; yes, make barrel take ladder
21AE 79 	LD A,C 		; else load A with random timer computed above
21AF E618 	AND #18 	; mask with #18. 25% chance of being zero
21B1 C0 	RET NZ 		; if not zero, return without taking ladder
21B2 DD3407 	INC (IX+#07) 	; else increase barrel animation
21B5 DDCB02C6	SET 0,(IX+#02) 	; set barrel to take the ladder
21B9 C9 	RET 		; return
 

 

In accordance with Title 17 U.S.C. Section 107, some of the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: http://www.law.cornell.edu/uscode/17/107.shtml. If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.

 

All content Copyright © 2015 Don Hodges
Various logos are trademarks of their respective companies.
Send Email to Don Hodges