forked from quag/mcobj
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprt.go
181 lines (149 loc) · 4.41 KB
/
prt.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
"os"
)
type PrtGenerator struct {
enclosedsChan chan *EnclosedChunkJob
completeChan chan bool
outFile *os.File
w *bufio.Writer
zw io.WriteCloser
particleCount int64
total int
boundary *BoundaryLocator
}
func (o *PrtGenerator) Start(outFilename string, total int, maxProcs int, boundary *BoundaryLocator) {
o.enclosedsChan = make(chan *EnclosedChunkJob, maxProcs*2)
o.completeChan = make(chan bool)
o.total = total
o.boundary = boundary
maxProcs = 1
for i := 0; i < maxProcs; i++ {
go o.chunkProcessor()
}
var openErr os.Error
o.outFile, openErr = os.Open(outFilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if openErr != nil {
fmt.Fprintln(os.Stderr, openErr) // TODO: return openErr
return
}
o.w = bufio.NewWriter(o.outFile)
WriteHeader(o.w, -1, []ChannelDefinition{{"Position", 4, 3, 0}, {"BlockID", 1, 1, 12}})
var zErr os.Error
o.zw, zErr = zlib.NewWriterLevel(o.w, zlib.NoCompression)
if zErr != nil {
fmt.Fprintln(os.Stderr, zErr) // TODO: return zErr
return
}
}
func (o *PrtGenerator) chunkProcessor() {
var chunkCount = 0
for {
var job = <-o.enclosedsChan
var e = job.enclosed
for i := 0; i < len(e.blocks); i += 128 {
var x, z = (i / 128) / 16, (i / 128) % 16
var column = BlockColumn(e.blocks[i : i+128])
for y, blockId := range column {
if y < yMin {
continue
}
switch {
case o.boundary.IsBoundary(blockId, e.Get(x, y-1, z)):
fallthrough
case o.boundary.IsBoundary(blockId, e.Get(x, y+1, z)):
fallthrough
case o.boundary.IsBoundary(blockId, e.Get(x-1, y, z)):
fallthrough
case o.boundary.IsBoundary(blockId, e.Get(x+1, y, z)):
fallthrough
case o.boundary.IsBoundary(blockId, e.Get(x, y, z-1)):
fallthrough
case o.boundary.IsBoundary(blockId, e.Get(x, y, z+1)):
o.particleCount++
var (
xa = -(x + e.xPos*16)
ya = y - 64
za = z + e.zPos*16
)
binary.Write(o.zw, binary.LittleEndian, float32(xa*2))
binary.Write(o.zw, binary.LittleEndian, float32(za*2))
binary.Write(o.zw, binary.LittleEndian, float32(ya*2))
binary.Write(o.zw, binary.LittleEndian, int32(blockId))
}
}
}
chunkCount++
fmt.Printf("%4v/%-4v (%3v,%3v) Particles: %d\n", chunkCount, o.total, job.enclosed.xPos, job.enclosed.zPos, o.particleCount)
if job.last {
o.completeChan <- true
break
}
}
}
func (o *PrtGenerator) Close() {
o.zw.Close()
o.w.Flush()
UpdateParticleCount(o.outFile, o.particleCount)
o.outFile.Close()
}
func (o *PrtGenerator) GetEnclosedJobsChan() chan *EnclosedChunkJob {
return o.enclosedsChan
}
func (o *PrtGenerator) GetCompleteChan() chan bool {
return o.completeChan
}
type ChannelDefinition struct {
Name string // max of 31 characters and must meet the regex [a-zA-Z_][0-9a-zA-Z_]*
DataType, Arity, Offset int32
}
// http://software.primefocusworld.com/software/support/krakatoa/prt_file_format.php
func WriteHeader(w io.Writer, particleCount int64, channels []ChannelDefinition) {
// Header (56 bytes)
var magic = []byte{192, 'P', 'R', 'T', '\r', '\n', 26, '\n'}
w.Write(magic)
var headerLength = uint32(56)
binary.Write(w, binary.LittleEndian, headerLength)
var signature = make([]byte, 32)
copy(signature, []byte("Extensible Particle Format"))
w.Write(signature)
var version = uint32(1)
binary.Write(w, binary.LittleEndian, version)
binary.Write(w, binary.LittleEndian, particleCount)
// Reserved bytes (4 bytes)
var reserved = int32(4)
binary.Write(w, binary.LittleEndian, reserved)
// Channel definition header (8 bytes)
binary.Write(w, binary.LittleEndian, int32(len(channels)))
var channelDefinitionSize = int32(44)
binary.Write(w, binary.LittleEndian, channelDefinitionSize)
for _, channel := range channels {
var nameBytes = make([]byte, 32)
copy(nameBytes, []byte(channel.Name))
w.Write(nameBytes)
binary.Write(w, binary.LittleEndian, channel.DataType)
binary.Write(w, binary.LittleEndian, channel.Arity)
binary.Write(w, binary.LittleEndian, channel.Offset)
}
}
func UpdateParticleCount(file *os.File, particleCount int64) os.Error {
var storedOffset, err = file.Seek(0, 1)
if err != nil {
return err
}
_, err = file.Seek(0x30, 0)
if err != nil {
return err
}
err = binary.Write(file, binary.LittleEndian, particleCount)
if err != nil {
return err
}
_, err = file.Seek(storedOffset, 0)
return err
}