void CSprite::free() {
	assert(this);
	CImage *image = this->images.head;
	while (image) {
		CImage *nextImage = image->link.next;
		image->free();
		image = nextImage;
	}

	const int y = CLAMP(this->position.y / 32, 0, mapTileSize->height - 1);
	const CListExtern<CSprite, &CSprite::link>
		spritesOnTileRow(spritesOnTileRow->heads[y],
			spritesOnTileRow->tails[y]);

	spritesOnTileRow.unlink(this);
	unusedSprites->insertAfterHead<&CSprite::link>(this);
}
void CImage::free() {
  assert(this);
  if (!(screenLayers->game.hasBeenRefreshed))
    scbw::refreshScreen(this->screenPosition.x,
                        this->screenPosition.y,
                        this->screenPosition.x + this->grpSize.right,
                        this->screenPosition.y + this->grpSize.bottom
                        );

  CSprite* const parent = this->parentSprite;
  const CListExtern<CImage, &CImage::link>
    imageList(parent->imageHead, parent->imageTail);

  imageList.unlink(this);
  this->grpOffset = NULL;

  unusedImages.insertAfterHead(this);
}
//Identical to function @ 0x00498E00
CImage* CSprite::createOverlay(u32 imageId, s8 x, s8 y, u32 direction) {
  assert(this);
  CImage *overlay = unusedImages.popHead();
  
  if (overlay) {
    const CListExtern<CImage, &CImage::link> images(this->imageHead, this->imageTail);
    if (this->imageHead) {
      images.insertBefore(overlay, this->mainGraphic);
    }
    else {
      this->mainGraphic = overlay;
      images.insertAfterHead(overlay);
    }

    overlay->initializeData(this, imageId, x, y);
    createUpdateImageSomething(overlay);
    updateImageDirection(overlay, direction);
  }
  return overlay;
}
void CSprite::setPosition(u16 x, u16 y) {
	assert(this);
	if (this->position.x == x && this->position.y == y) return;

	const int oldTileY = CLAMP(this->position.y / 32, 0, mapTileSize->height - 1);
	const int newTileY = CLAMP(y / 32, 0, mapTileSize->height - 1);

	this->position.x = x;
	this->position.y = y;

	if (oldTileY != newTileY) {
		const CListExtern<CSprite, &CSprite::link>
			spritesOnOldTileRow(spritesOnTileRow->heads[oldTileY],
				spritesOnTileRow->tails[oldTileY]),
			spritesOnNewTileRow(spritesOnTileRow->heads[newTileY],
				spritesOnTileRow->tails[newTileY]);

		spritesOnOldTileRow.unlink(this);
		spritesOnNewTileRow.insertAfterHead(this);
	}

	for (CImage *i = this->images.head; i; i = i->link.next)
		i->flags |= 1;
}