Coordinate Functions

From the CreationKit Wiki
Jump to navigation Jump to search

Putting my coordinate functions here since my personal pages are getting too big.

This function converts the objects current x, y position in the world to coordinates. These functions are to be used in exterior cells only.

Int[] Function GetCellCoordinates(ObjectReference akRef)
    ; Returns an array of this references' cell coordinates.
  
    int posX = akRef.GetPositionX() as int
    int posY = akRef.GetPositionY() as int
   
    if (posX < 0)
        posX = (posX / 4096) - 1
    else
        posX = posX / 4096
    endif
   
    if (posY < 0)
        posY = (posY / 4096) - 1
    else
        posY = posY / 4096
    endif
   
    Int[] CellCoordinates = new Int[2]
    CellCoordinates[0] = posX
    CellCoordinates[1] = posY
   
    return CellCoordinates
EndFunction


The following requires GetCellCoordinates(). To explain what the function below does, the origin in the game, for example has the x axis starting position of 0.

Int[] Function GetCellStartingWorldPositions(Int[] aiCoordinates)
    ; Returns an array of starting world positions for this cell.
	Int coordinateX = aiCoordinates[0]
	Int coordinateY = aiCoordinates[1]
	Int[] startPos = new int[2]
	
	if coordinateX < 0
	    coordinateX = coordinateX * 4096 + 4096
	else
	    coordinateX + 4096
        endif
	
	if coordinateY < 0
	    coordinateY = coordinateY * 4096 + 4096
	else
	    coordinateY + 4096
	endif
	
	startPos[0] = coordinateX
	startPos[1] = coordinateY
	
	return startPos
EndFunction


This function returns all the cells that is around the object.

Int[] Function GetAdjacentCells(Int aiCoordinateX, Int aiCoordinateY)
	; returns the north, south, east and west cells adajcent to the current cell.
	; 0-1: North XY, 2-3 = South XY, 4-5 = East XY, 6-7 = West XY
	Int[] AdjacentCells = new Int[8]
	AdjacentCells[0] = aiCoordinateX
	AdjacentCells[1] = aiCoordinateY + 1
	AdjacentCells[2] = aiCoordinateX 
	AdjacentCells[3] = aiCoordinateY - 1
	AdjacentCells[4] = aiCoordinateX + 1
	AdjacentCells[5] = aiCoordinateY
	AdjacentCells[6] = aiCoordinateX - 1
	AdjacentCells[7] = aiCoordinateY

	return AdjacentCells
EndFunction


How far long an object is on either x or y axis of particular cell. While GetPostionXYZ returns the positions in the world, GetLocalX and Y returns positions in the cell.

Int Function GetLocalPositionX(Int aiPosX, Int aiCellStartX)
  ; Returns the position of an object local to the cell it is in.

	if aiPosX < 0
	    return (aiPosX - aicellStartX) * -1
	endif
	
	return aiPosX - aicellStartX
endFunction

Int Function GetLocalPositionY(Int aiPosY, Int aiCellStartY)
  ; Return the y position of an object local to the cell it is in.

    	if aiPosY < 0
	    return (aiPosY - aicellStartY) * -1
	endif
	
	return aiPosY - aicellStartY
EndFunction