forked from iand/amberfell
/
functions.go
102 lines (85 loc) · 2.35 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
To the extent possible under law, Ian Davis has waived all copyright
and related or neighboring rights to this Amberfell Source Code file.
This work is published from the United Kingdom.
*/
package main
import (
"fmt"
"github.com/kierdavis/go/mm3dmodel"
"math"
"os"
)
func IntPosition(pos Vectorf) Vectori {
return Vectori{uint16(Round(pos[XAXIS], 0)),
uint16(Round(pos[YAXIS], 0)),
uint16(Round(pos[ZAXIS], 0))}
}
// Round a float to given precision
func Round(val float64, prec int) float64 {
var rounder float64
intermed := val * math.Pow(10, float64(prec))
if intermed > 0 {
rounder = math.Floor(intermed + 0.5)
} else {
rounder = math.Ceil(intermed - 0.5)
}
return rounder / math.Pow(10, float64(prec))
}
// Function LoadModel loads and returns an MM3D model.
func LoadModel(filename string) (model *mm3dmodel.Model) {
fmt.Printf("Loading model: %s\n", filename)
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
model, err = mm3dmodel.Read(f)
if err != nil {
panic(err)
}
if model.NDirtySegments() > 0 {
fmt.Fprintf(os.Stderr, "***** MM3D Warning: Found %d dirty segments in %s! Tell Kier to add more functionality!\n", model.NDirtySegments(), filename)
for i := 0; i < model.NDirtySegments(); i++ {
fmt.Fprintf(os.Stderr, "***** MM3D Warning: * Offset type 0x%04X\n", model.DirtySegment(i).OffsetType())
}
}
return model
}
func HeadingToCompass(heading float64) string {
if heading > 337.5 || heading <= 22.5 {
return "E"
} else if heading > 22.5 && heading <= 67.5 {
return "NE"
} else if heading > 67.5 && heading <= 112.5 {
return "N"
} else if heading > 112.5 && heading <= 157.5 {
return "NW"
} else if heading > 157.5 && heading <= 202.5 {
return "W"
} else if heading > 202.5 && heading <= 247.5 {
return "SW"
} else if heading > 247.5 && heading <= 292.5 {
return "S"
}
return "SE"
}
// orientation is opposite to direction (for block placement)
func HeadingToOrientation(heading float64) uint8 {
if heading > 45 && heading <= 135 {
return ORIENT_SOUTH
} else if heading > 135 && heading <= 225 {
return ORIENT_EAST
} else if heading > 225 && heading <= 315 {
return ORIENT_NORTH
}
return ORIENT_WEST
}
func HasVisibleFaces(neighbours [18]BlockId) bool {
for i := 0; i < 6; i++ {
if items[ItemId(neighbours[i])].transparent {
return true
}
}
return false
}