Worm call - codingstyle/swift-dune GitHub Wiki

image

Worm call animation relies on two sprite files :

  • SHAI.HSQ contains 45 images and the color palette
  • SHAI2.HSQ contains 18 additional images

Animations definitions can be found at the end of SHAI.HSQ. However, its format differs from characters, due to the size of the sprites used and how the animations must move across the screen.

Therefore, all coordinates are relative to the screen, and all components are expressed as an unsigned 16-bit integer (Long Endian). Sprite index go from 0 to 62, referring to either SHAI1.HSQ (0-44) or SHAI2.HSQ (45-62) sprites.

Frames are separated with a rectangle definition, defined by two coordinates for top-left and bottom-right of the region. Most plausible explanation is a rendering optimization where a framebuffer keeps a version with the sky and empty desert, and performs a blit operation to cover the previous sprite and avoid redrawing all the screen.

Algorithm

HEADER = READ_UINT16LE() // must be 0x0000 (animation header)
TOTAL_ANIM_BYTES = READ_UINT16LE() // must be 0x033A (total bytes for animation)
MAX_SPRITE_INDEX = 0

WHILE !EOF()
    X2 = READ_UINT16LE()
    Y2 = READ_UINT16LE()
    X2 = READ_UINT16LE()
    Y2 = READ_UINT16LE()
    
    // Test for rectangle
    IF X1 < Y1 AND X2 < Y2 AND X1 != MAX_SPRITE_INDEX + 1
       Clear the region with the rectangle coordinates
       SEEK(-8)
       CONTINUE
    ENDIF

    X = READ_UINT16LE()
    Y = READ_UINT16LE()
    SPRITE_INDEX = READ_UINT16LE()
    Draw the sprite at SPRITE_INDEX with the coordinates X, Y
    MAX_SPRITE_INDEX = SPRITE_INDEX
ENDWHILE

Comparing using the rectangle approach almost works, but the sprite at index 51 has a set of coordinates that are mistakenly interpreted as a rectangle. As most of the animation follows the sprite order in the HSQ files, my hack was to set a MAX_SPRITE_INDEX variable to ensure sprites and rectangles are correctly differentiated.

File interpretation

  • 🟨 = header
  • 🟧 = animation size
  • 🟦 = sprite index
  • 🟩 = coordinates (X,Y)
  • 🟪 = clear rectangle (X1,Y1,X2,Y2)