Skip to content

Item Count

Vilsol edited this page Sep 6, 2021 · 2 revisions

Shows the current item count in the core on a large display.

package main

import "github.com/Vilsol/go-mlog/m"

const (
	displaySize = 176

	cellHeight = 22
	cellWidth  = 80

	imageSize   = 22.0
	imageOffset = 10

	textOffsetX = 25
	textOffsetY = 5
	textScale   = 1.2

	digitHeight = 10
)

func main() {
	m.DrawStroke(2)
	m.DrawColor(255, 255, 255, 255)
	m.DrawClear(0, 0, 0)

	offset := 0
	offset += DS(offset, m.Const("@copper"))
	offset += DS(offset, m.Const("@lead"))
	offset += DS(offset, m.Const("@metaglass"))
	offset += DS(offset, m.Const("@graphite"))
	offset += DS(offset, m.Const("@sand"))
	offset += DS(offset, m.Const("@coal"))
	offset += DS(offset, m.Const("@titanium"))
	offset += DS(offset, m.Const("@thorium"))
	offset += DS(offset, m.Const("@scrap"))
	offset += DS(offset, m.Const("@silicon"))
	offset += DS(offset, m.Const("@plastanium"))
	offset += DS(offset, m.Const("@phase-fabric"))
	offset += DS(offset, m.Const("@surge-alloy"))
	offset += DS(offset, m.Const("@spore-pod"))
	offset += DS(offset, m.Const("@blast-compound"))
	offset += DS(offset, m.Const("@pyratite"))

	m.DrawFlush("display1")
}

func DS(offset int, sense string) int {
	count := m.Sensor(m.B("nucleus1"), sense)
	if count > 0 {
		x := m.Floor(float64(offset/displaySize)) * cellWidth
		y := (displaySize - offset%displaySize) - cellHeight
		m.DrawImage(x+imageOffset, y+imageOffset, sense, imageSize, 0)
		DrawNumber(m.Floor(count), x+textOffsetX, y+textOffsetY, textScale)
		return cellHeight
	}
	return 0
}

func DrawNumber(num int, x int, y int, scale float64) {
	digitSize := m.Floor(digitHeight*scale/2 + 4)
	digits := m.Floor(m.Log10(float64(num)))
	offset := digitSize * digits
	n := num
	for i := 0; i <= digits; i++ {
		digit := n % 10
		DrawDigit(digit, x+offset, y, scale)
		offset -= digitSize
		n = m.Floor(float64(n / 10))
	}
}

func DrawDigit(num int, x int, y int, scale float64) {
	height := digitHeight * scale
	width := height / 2
	switch num {
	case 0:
		m.DrawLineRect(x-1, y-1, m.Floor(width)+2, m.Floor(height)+2)
		break
	case 1:
		x1 := x + m.Floor(width)
		y2 := y + m.Floor(height)
		m.DrawLine(x1, y, x1, y2)
		m.DrawLine(x, y+m.Floor(height/2), x1, y2)
		break
	case 2:
		half := y + m.Floor(height/2)
		right := x + m.Floor(width)
		top := y + m.Floor(height)
		m.DrawLine(x, top, right, top)
		m.DrawLine(right, top, right, half)
		m.DrawLine(x, half, right, half)
		m.DrawLine(x, half, x, y)
		m.DrawLine(x, y, right, y)
		break
	case 3:
		half := y + m.Floor(height/2)
		right := x + m.Floor(width)
		top := y + m.Floor(height)
		m.DrawLine(x, top, right, top)
		m.DrawLine(x, half, right, half)
		m.DrawLine(x, y, right, y)
		m.DrawLine(right, top, right, y)
		break
	case 4:
		half := y + m.Floor(height/2)
		right := x + m.Floor(width)
		top := y + m.Floor(height)
		m.DrawLine(x, top, x, half)
		m.DrawLine(x, half, right, half)
		m.DrawLine(right, top, right, y)
		break
	case 5:
		half := y + m.Floor(height/2)
		right := x + m.Floor(width)
		top := y + m.Floor(height)
		m.DrawLine(x, top, right, top)
		m.DrawLine(x, top, x, half)
		m.DrawLine(x, half, right, half)
		m.DrawLine(right, half, right, y)
		m.DrawLine(x, y, right, y)
		break
	case 6:
		half := m.Floor(height / 2)
		right := x + m.Floor(width)
		top := y + m.Floor(height)
		m.DrawLine(x, top, right, top)
		m.DrawLine(x, top, x, y+half)
		m.DrawLineRect(x-1, y-1, m.Floor(width)+2, half+2)
		break
	case 7:
		right := x + m.Floor(width)
		top := y + m.Floor(height)
		m.DrawLine(x, top, right, top)
		m.DrawLine(x, y, right, top)
		break
	case 8:
		half := y + m.Floor(height/2)
		m.DrawLineRect(x-1, y-1, m.Floor(width)+2, m.Floor(height)+2)
		m.DrawLine(x, half, x+m.Floor(width), half)
		break
	case 9:
		half := m.Floor(height / 2)
		right := x + m.Floor(width)
		m.DrawLine(x, y, right, y)
		m.DrawLine(right, y+half, right, y)
		m.DrawLineRect(x-1, y+half-1, m.Floor(width)+2, half+2)
		break
	}
}
Clone this wiki locally