2018-03-18 03:35:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2018-03-28 01:00:55 +01:00
|
|
|
"fmt"
|
2018-03-18 03:35:03 +00:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
2018-03-18 13:57:01 +00:00
|
|
|
"path/filepath"
|
2018-03-28 01:15:35 +01:00
|
|
|
"time"
|
2018-03-18 03:35:03 +00:00
|
|
|
|
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"github.com/faiface/pixel/pixelgl"
|
|
|
|
"golang.org/x/image/colornames"
|
|
|
|
|
2018-03-22 20:31:10 +00:00
|
|
|
"ur.gs/ordoor/internal/conv"
|
|
|
|
"ur.gs/ordoor/internal/data"
|
|
|
|
"ur.gs/ordoor/internal/maps"
|
|
|
|
"ur.gs/ordoor/internal/sets"
|
|
|
|
"ur.gs/ordoor/internal/ui"
|
2018-03-18 03:35:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gamePath = flag.String("game-path", "./orig", "Path to a WH40K: Chaos Gate installation")
|
|
|
|
mapFile = flag.String("map", "", "Prefix path to a .map file, e.g. ./orig/Maps/Chapter01.MAP")
|
|
|
|
txtFile = flag.String("txt", "", "Prefix path to a .txt file, e.g. ./orig/Maps/Chapter01.txt")
|
|
|
|
)
|
|
|
|
|
2018-03-18 13:57:01 +00:00
|
|
|
type env struct {
|
|
|
|
gameMap *maps.GameMap
|
2018-03-18 15:39:50 +00:00
|
|
|
set *sets.MapSet
|
2018-03-22 04:15:31 +00:00
|
|
|
objects map[string]*conv.Object
|
2018-03-23 01:27:38 +00:00
|
|
|
sprites map[string][]*pixel.Sprite
|
2018-10-12 23:02:24 +01:00
|
|
|
batch *pixel.Batch
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
type state struct {
|
2018-03-18 13:57:01 +00:00
|
|
|
env *env
|
|
|
|
|
2018-03-28 01:15:35 +01:00
|
|
|
step int
|
|
|
|
fpsTicker <-chan time.Time
|
2018-03-18 13:57:01 +00:00
|
|
|
|
|
|
|
cam pixel.Matrix
|
|
|
|
camPos pixel.Vec
|
|
|
|
|
|
|
|
zoom float64
|
2018-03-23 01:27:38 +00:00
|
|
|
rot float64
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
zIdx int
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 03:35:03 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *gamePath == "" || *mapFile == "" || *txtFile == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
gameMap, err := maps.LoadGameMapByFiles(*mapFile, *txtFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't load map file: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-18 13:57:01 +00:00
|
|
|
setFile := filepath.Join(*gamePath, "Sets", gameMap.MapSetFilename())
|
|
|
|
log.Println(setFile)
|
|
|
|
mapSet, err := sets.LoadSet(setFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't load set file %s: %v", setFile, err)
|
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2018-10-12 23:02:24 +01:00
|
|
|
rawObjs := []*data.Object{}
|
2018-03-22 04:15:31 +00:00
|
|
|
for _, name := range mapSet.Palette {
|
|
|
|
objFile := filepath.Join(*gamePath, "Obj", name+".obj")
|
|
|
|
obj, err := data.LoadObject(objFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load %s: %v", name, err)
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:02:24 +01:00
|
|
|
obj.Name = name
|
|
|
|
rawObjs = append(rawObjs, obj)
|
2018-03-22 04:15:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 23:02:24 +01:00
|
|
|
objects, spritesheet := conv.ConvertObjects(rawObjs)
|
|
|
|
batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
|
|
|
|
2018-03-28 01:15:35 +01:00
|
|
|
env := &env{
|
|
|
|
gameMap: gameMap,
|
|
|
|
set: mapSet,
|
2018-12-30 23:22:01 +00:00
|
|
|
objects: conv.MapByName(objects),
|
2018-10-12 23:02:24 +01:00
|
|
|
batch: batch,
|
2018-03-28 01:15:35 +01:00
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2018-03-18 13:57:01 +00:00
|
|
|
// The main thread now belongs to pixelgl
|
2018-03-18 20:41:17 +00:00
|
|
|
pixelgl.Run(env.run)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:41:17 +00:00
|
|
|
func (e *env) run() {
|
2018-03-28 01:15:35 +01:00
|
|
|
title := "View Map " + *mapFile
|
|
|
|
win, err := ui.NewWindow(title + " | FPS: ?")
|
2018-03-18 03:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Couldn't create window: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:41:17 +00:00
|
|
|
pWin := win.PixelWindow
|
2018-03-22 04:15:31 +00:00
|
|
|
state := &state{
|
|
|
|
env: e,
|
|
|
|
// camPos: pixel.V(0, float64(-pWin.Bounds().Size().Y)),
|
|
|
|
// camPos: pixel.V(float64(3700), float64(0)),
|
2018-03-28 01:15:35 +01:00
|
|
|
zoom: 1.0,
|
|
|
|
rot: 0.785,
|
|
|
|
step: -1,
|
|
|
|
fpsTicker: time.Tick(time.Second),
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2018-03-23 01:27:38 +00:00
|
|
|
pWin.SetSmooth(true)
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2018-03-18 20:41:17 +00:00
|
|
|
win.Run(func() {
|
2018-03-18 13:57:01 +00:00
|
|
|
oldState := *state
|
2018-03-22 04:15:31 +00:00
|
|
|
state = state.runStep(pWin)
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2018-03-28 01:15:35 +01:00
|
|
|
if oldState != *state || oldState.step == -1 {
|
2018-03-23 01:27:38 +00:00
|
|
|
log.Printf("zoom=%.2f rot=%.4f zIdx=%v camPos=%#v", state.zoom, state.rot, state.zIdx, state.camPos)
|
2018-03-22 04:15:31 +00:00
|
|
|
state.present(pWin)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
|
|
|
state.step += 1
|
2018-03-28 01:15:35 +01:00
|
|
|
select {
|
|
|
|
case <-state.fpsTicker:
|
|
|
|
pWin.SetTitle(fmt.Sprintf("%s | FPS: %d", title, state.step))
|
|
|
|
state.step = 0
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:41:17 +00:00
|
|
|
})
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 01:00:55 +01:00
|
|
|
func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, error) {
|
|
|
|
// There seems to be an active bit that hides many sins
|
|
|
|
if !ref.IsActive() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:54:20 +00:00
|
|
|
if ref.Index() >= len(palette) {
|
2018-03-28 01:00:55 +01:00
|
|
|
return nil, fmt.Errorf("Palette too small: %v requested", ref.Index())
|
2018-03-23 23:54:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
name := palette[ref.Index()]
|
2018-03-23 23:54:20 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
obj := e.objects[name]
|
|
|
|
if obj == nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
return nil, fmt.Errorf("Failed to find surface sprite %#v -> %q", ref, name)
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 01:00:55 +01:00
|
|
|
if ref.Sprite() >= len(obj.Sprites) {
|
|
|
|
return nil, fmt.Errorf("Out-of-index sprite %v requested for %v", ref.Sprite(), name)
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:37:44 +01:00
|
|
|
return obj.Sprites[ref.Sprite()], nil
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
func (s *state) present(pWin *pixelgl.Window) {
|
|
|
|
pWin.Clear(colornames.Black)
|
2018-10-12 23:02:24 +01:00
|
|
|
s.env.batch.Clear()
|
2018-10-13 01:37:44 +01:00
|
|
|
|
|
|
|
center := pWin.Bounds().Center()
|
|
|
|
|
|
|
|
cam := pixel.IM
|
|
|
|
cam = cam.ScaledXY(center, pixel.Vec{1.0, -1.0}) // invert the Y axis
|
|
|
|
cam = cam.Scaled(center, s.zoom) // apply current zoom factor
|
|
|
|
cam = cam.Moved(center.Sub(s.camPos)) // Make it central
|
|
|
|
s.cam = cam
|
|
|
|
pWin.SetMatrix(cam)
|
|
|
|
|
2018-03-25 12:11:48 +01:00
|
|
|
// TODO: we should be able to perform bounds clipping on these
|
|
|
|
minX := int(s.env.gameMap.MinWidth)
|
|
|
|
maxX := int(s.env.gameMap.MaxWidth)
|
|
|
|
minY := int(s.env.gameMap.MinLength)
|
|
|
|
maxY := int(s.env.gameMap.MaxLength)
|
|
|
|
minZ := 0
|
|
|
|
maxZ := int(s.zIdx) + 1
|
|
|
|
|
|
|
|
for z := minZ; z < maxZ; z++ {
|
|
|
|
for y := minY; y < maxY; y++ {
|
|
|
|
for x := minX; x < maxX; x++ {
|
2018-10-12 23:02:24 +01:00
|
|
|
s.renderCell(x, y, z, s.env.batch)
|
2018-03-25 12:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 23:02:24 +01:00
|
|
|
|
|
|
|
s.env.batch.Draw(pWin)
|
|
|
|
pWin.Update()
|
2018-03-25 12:11:48 +01:00
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2018-10-13 01:37:44 +01:00
|
|
|
func (s *state) renderCell(x, y, z int, target pixel.Target) {
|
2018-03-25 12:11:48 +01:00
|
|
|
var sprites []*conv.Sprite
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2018-03-25 12:11:48 +01:00
|
|
|
cell := s.env.gameMap.Cells.At(x, y, z)
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2018-03-28 01:00:55 +01:00
|
|
|
if spr, err := s.env.getSprite(s.env.set.Palette, cell.Surface); err != nil {
|
|
|
|
log.Printf("%v %v %v surface: %v", x, y, z, err)
|
2018-10-13 01:37:44 +01:00
|
|
|
} else if spr != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
sprites = append(sprites, spr)
|
|
|
|
}
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2018-03-28 01:00:55 +01:00
|
|
|
if spr, err := s.env.getSprite(s.env.set.Palette, cell.Center); err != nil {
|
|
|
|
log.Printf("%v %v %v center: %v", x, y, z, err)
|
2018-10-13 01:37:44 +01:00
|
|
|
} else if spr != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
sprites = append(sprites, spr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if spr, err := s.env.getSprite(s.env.set.Palette, cell.Left); err != nil {
|
|
|
|
log.Printf("%v %v %v left: %v", x, y, z, err)
|
2018-10-13 01:37:44 +01:00
|
|
|
} else if spr != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
sprites = append(sprites, spr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if spr, err := s.env.getSprite(s.env.set.Palette, cell.Right); err != nil {
|
|
|
|
log.Printf("%v %v %v right: %v", x, y, z, err)
|
2018-10-13 01:37:44 +01:00
|
|
|
} else if spr != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
sprites = append(sprites, spr)
|
|
|
|
}
|
2018-03-23 23:54:20 +00:00
|
|
|
|
2018-03-25 12:11:48 +01:00
|
|
|
// Taking the Z index away *seems* to draw the object in the correct place.
|
|
|
|
// FIXME: There are some artifacts, investigate more
|
2018-10-13 02:02:57 +01:00
|
|
|
fX := float64(x)
|
|
|
|
fY := float64(y)
|
2018-03-23 23:54:20 +00:00
|
|
|
|
2018-03-25 12:49:36 +01:00
|
|
|
iso := s.cellToPix(pixel.V(fX, fY))
|
2018-10-13 02:02:57 +01:00
|
|
|
iso = iso.Add(pixel.Vec{0.0, -float64(z * 48.0)})
|
2018-03-25 00:36:23 +00:00
|
|
|
|
2018-03-25 12:11:48 +01:00
|
|
|
for _, sprite := range sprites {
|
2018-10-13 01:37:44 +01:00
|
|
|
sprite.Spr.Draw(target, pixel.IM.Moved(iso))
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2018-03-25 12:49:36 +01:00
|
|
|
var (
|
|
|
|
cellWidth = 64.0
|
|
|
|
cellHeight = 64.0
|
|
|
|
)
|
|
|
|
|
|
|
|
// Doesn't take the camera or Z level into account
|
|
|
|
func (s *state) cellToPix(cell pixel.Vec) pixel.Vec {
|
|
|
|
return pixel.V(
|
|
|
|
(cell.X-cell.Y)*cellWidth,
|
|
|
|
(cell.X+cell.Y)*cellHeight/2.0,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't take the camera or Z level into account
|
|
|
|
func (s *state) pixToCell(pix pixel.Vec) pixel.Vec {
|
|
|
|
return pixel.V(
|
|
|
|
pix.Y/cellHeight+pix.X/(cellWidth*2.0),
|
|
|
|
pix.Y/cellHeight-pix.X/(cellWidth*2.0),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
func (s *state) runStep(pWin *pixelgl.Window) *state {
|
|
|
|
newState := *s
|
|
|
|
newState.handleKeys(pWin)
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
return &newState
|
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
func (s *state) handleKeys(pWin *pixelgl.Window) {
|
2018-03-25 12:49:36 +01:00
|
|
|
// Do this first to avoid taking the below mutations into account
|
|
|
|
// FIXME: this suggests we should pass the next state into here and
|
|
|
|
// modify it instead
|
|
|
|
if pWin.JustPressed(pixelgl.MouseButton1) {
|
|
|
|
if s.zIdx != 0 {
|
|
|
|
log.Printf("WARNING: z-index not yet taken into account")
|
|
|
|
}
|
2018-03-28 01:00:55 +01:00
|
|
|
|
2018-10-13 01:37:44 +01:00
|
|
|
log.Printf("cam: %#v", s.cam)
|
|
|
|
|
2018-03-28 01:00:55 +01:00
|
|
|
pos := s.pixToCell(s.cam.Unproject(pWin.MousePosition()))
|
|
|
|
log.Printf("X=%v Y=%v, zIdx=%v", pos.X, pos.Y, s.zIdx)
|
2018-10-13 01:37:44 +01:00
|
|
|
|
|
|
|
cell := s.env.gameMap.Cells.At(int(pos.X), int(pos.Y), s.zIdx)
|
2018-03-28 01:00:55 +01:00
|
|
|
log.Printf("Cell=%#v", cell)
|
2018-03-25 12:49:36 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
if pWin.Pressed(pixelgl.KeyLeft) {
|
|
|
|
s.camPos.X -= 64
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
if pWin.Pressed(pixelgl.KeyRight) {
|
|
|
|
s.camPos.X += 64
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
if pWin.Pressed(pixelgl.KeyDown) {
|
|
|
|
s.camPos.Y -= 64
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
if pWin.Pressed(pixelgl.KeyUp) {
|
|
|
|
s.camPos.Y += 64
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
for i := 1; i <= 7; i++ {
|
|
|
|
if pWin.JustPressed(pixelgl.Key0 + pixelgl.Button(i)) {
|
|
|
|
s.zIdx = i - 1
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:27:38 +00:00
|
|
|
if pWin.Pressed(pixelgl.KeyMinus) {
|
|
|
|
s.rot -= 0.001
|
|
|
|
}
|
|
|
|
|
|
|
|
if pWin.Pressed(pixelgl.KeyEqual) {
|
|
|
|
s.rot += 0.001
|
|
|
|
}
|
|
|
|
|
2018-03-18 03:35:03 +00:00
|
|
|
// Zoom in and out with the mouse wheel
|
2018-03-22 04:15:31 +00:00
|
|
|
s.zoom *= math.Pow(1.2, pWin.MouseScroll().Y)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|