2018-03-18 03:35:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
2018-03-18 13:57:01 +00:00
|
|
|
"path/filepath"
|
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-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-22 04:15:31 +00:00
|
|
|
step int
|
2018-03-18 13:57:01 +00:00
|
|
|
|
|
|
|
cam pixel.Matrix
|
|
|
|
camPos pixel.Vec
|
|
|
|
|
|
|
|
zoom float64
|
|
|
|
|
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-03-22 04:15:31 +00:00
|
|
|
objects := make(map[string]*conv.Object)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
objects[name] = conv.ConvertObject(obj, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
env := &env{gameMap: gameMap, set: mapSet, objects: objects}
|
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() {
|
|
|
|
win, err := ui.NewWindow("View Map " + *mapFile)
|
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)),
|
|
|
|
zoom: 1.0,
|
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-22 04:15:31 +00:00
|
|
|
if oldState != *state || oldState.step == 0 {
|
|
|
|
log.Printf("zoom=%.2f zIdx=%v camPos=%#v", state.zoom, state.zIdx, state.camPos)
|
|
|
|
state.present(pWin)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
|
|
|
state.step += 1
|
2018-03-18 20:41:17 +00:00
|
|
|
})
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, *conv.Object) {
|
|
|
|
name := palette[ref.Index()]
|
|
|
|
obj := e.objects[name]
|
|
|
|
if obj == nil {
|
|
|
|
log.Printf("Failed to find surface sprite %#v -> %q", ref, name)
|
|
|
|
return nil, nil
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
if ref.Frame() >= len(obj.Sprites) {
|
|
|
|
log.Printf("Out-of-index sprite %v requested for %v", ref.Frame(), name)
|
|
|
|
return nil, obj
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
return &obj.Sprites[ref.Frame()], obj
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
var (
|
|
|
|
cellWidth = 128 // I think, anyway
|
|
|
|
cellHeight = 64
|
|
|
|
)
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
// TODO: build all the sprites in the set into a single spritesheet so we can
|
|
|
|
// use pixel.Batch
|
|
|
|
func (s *state) present(pWin *pixelgl.Window) {
|
|
|
|
gameMap := s.env.gameMap
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
pWin.Clear(colornames.Black)
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2018-03-22 04:15:31 +00:00
|
|
|
center := pWin.Bounds().Center()
|
2018-03-18 13:57:01 +00:00
|
|
|
|
|
|
|
cam := pixel.IM
|
2018-03-22 12:41:32 +00:00
|
|
|
cam = cam.ScaledXY(center, pixel.Vec{1.0, -1.0}) // invert the Y axis
|
|
|
|
cam = cam.Scaled(pixel.ZV, s.zoom) // apply current zoom factor
|
|
|
|
cam = cam.Moved(center.Sub(s.camPos)) // Make it central
|
|
|
|
cam = cam.Rotated(center.Sub(s.camPos), -0.785) // Apply isometric angle
|
2018-03-22 04:15:31 +00:00
|
|
|
s.cam = cam
|
|
|
|
pWin.SetMatrix(s.cam)
|
|
|
|
|
|
|
|
// TODO: bounds clipping
|
|
|
|
z := int(s.zIdx)
|
|
|
|
for y := int(gameMap.MinLength); y < int(gameMap.MaxLength); y++ {
|
|
|
|
for x := int(gameMap.MinWidth); x < int(gameMap.MaxWidth); x++ {
|
|
|
|
|
|
|
|
cell := gameMap.Cells.At(x, y, z)
|
|
|
|
|
|
|
|
// TODO: optimize drawing, etc
|
|
|
|
surfaceSprite, surfaceObj := s.env.getSprite(s.env.set.SurfacePalette, cell.Surface)
|
|
|
|
if surfaceSprite == nil {
|
|
|
|
log.Printf("no surfaceSprite")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if surfaceSprite.Width != cellWidth {
|
|
|
|
log.Printf("WARN: Surface sprite has wrong width: %v", surfaceSprite.Width)
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:41:32 +00:00
|
|
|
yPos := float64((y - int(gameMap.MinLength)) * cellHeight / 2)
|
|
|
|
xPos := float64((x - int(gameMap.MinWidth)) * cellWidth)
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2018-03-22 12:41:32 +00:00
|
|
|
// Tiles should be flush to each other. Offset even-numbered tiles
|
|
|
|
// across to get this effect
|
|
|
|
if y%2 == 0 {
|
|
|
|
xPos += float64(cellWidth) / 2.0
|
2018-03-22 04:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pic := surfaceSprite.Pic
|
|
|
|
spr := pixel.NewSprite(pic, pic.Bounds())
|
|
|
|
log.Printf(
|
|
|
|
"cell(%v,%v,%v): %s %d: pix(%v,%v - %v,%v)",
|
|
|
|
x, y, z, surfaceObj.Name, cell.Surface.Index(),
|
2018-03-22 12:41:32 +00:00
|
|
|
xPos, yPos, xPos+float64(surfaceSprite.Width), yPos+float64(surfaceSprite.Height),
|
2018-03-22 04:15:31 +00:00
|
|
|
)
|
|
|
|
spr.Draw(pWin, pixel.IM.Moved(pixel.V(float64(xPos), float64(yPos))))
|
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-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) {
|
|
|
|
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-22 04:15:31 +00:00
|
|
|
/* TODO: restore this
|
2018-03-18 13:57:01 +00:00
|
|
|
// Show details of clicked-on cell in termal
|
2018-03-22 04:15:31 +00:00
|
|
|
if pWin.JustPressed(pixelgl.MouseButtonLeft) {
|
|
|
|
vec := s.cam.Unproject(pWin.MousePosition())
|
2018-03-18 13:57:01 +00:00
|
|
|
x, y := vecToCell(vec)
|
|
|
|
log.Printf("%#v -> %d,%d", vec, x, y)
|
|
|
|
cell := state.env.gameMap.Cells.At(x, y, state.zIdx)
|
|
|
|
log.Printf(
|
2018-03-18 15:39:50 +00:00
|
|
|
"x=%d y=%d z=%d SurfaceTile=%d (%s) SurfaceFrame=%d SquadRelated=%d",
|
2018-03-18 13:57:01 +00:00
|
|
|
x, y, state.zIdx,
|
2018-03-18 17:27:32 +00:00
|
|
|
cell.Surface.Index(), state.env.set.SurfacePalette[int(cell.Surface.Index())], cell.Surface.Frame(),
|
2018-03-18 13:57:01 +00:00
|
|
|
cell.SquadRelated,
|
|
|
|
)
|
|
|
|
log.Printf("CellIdx%d=%d. Full cell data: %#v", state.cellIdx, cell.At(state.cellIdx), cell)
|
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
*/
|
2018-03-18 13:57:01 +00:00
|
|
|
|
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
|
|
|
}
|