## Sunday, 18 March 2018

### RetroChallenge 2018/04: Dig Dug's Artificial Intelligence

The next major task at hand was to give the monsters intelligence.  I did this with a sequence of ON/GOSUB checks.  Here's the main LOOP

20 K=K(PEEK(2)ANDPEEK(17023)):C=1:ONKGOSUB2,4,6,8,50:FORC=1TO3:ONK(PEEK(R(C)+M+32))GOTO21,21,11,21,10
21 NEXT:FORC=2TO4:IFRND(20)>GTHENON2+SGN(X(1)-X(C))GOSUB2,19,4:ON2+SGN(Y(1)-Y(C))GOSUB6,19,8:NEXT:GOTO20
22 OND(C)GOSUB2,4,6,8:ONRND(9)GOSUB2,4,6,8:NEXT:IFJTHEN20

In line twenty there are two ON/GOSUB statements. The first is for the player's movement. It jumps to line 2, 4, 6, or 8, which process legal moves and then place the character. The next FOR/NEXT loop for three iterations checks the rocks and then drops them if they only have black space under them.

Line 21 and 22 are where the enemies move. They use the same character arrays as the player character to store their positions and strings (looking left, looking right) and so they can use the same 2,4,6,8 move check subroutines as the player character.

The first IF checks if a random number is higher than a level variable G. If so, then Xs and Ys coordinates for the character and the monster are compared to allow the monster to home in on the character's position. A direction for X is chosen first and checked with a GOSUB to 2 or 4 (horizontal directions) and the same happens for 6 or 8 (vertical directions). Then there is a jump to the beginning of the main loop.

Line 22 is done only if the IF level G variable check failed in line 21. It just tries to move the enemy in the last direction chosen in line 21. It also tries to move the character in a random direction.  It then checks if the character has been killed yet and jumps to the top of the main loop.

This is the basics of AI of the enemies. Next post I'll discuss some cleaning up and improvements to the main loop and the behavoir of the monsters.