2018-03-18 03:35:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2018-03-28 01:00:55 +01:00
|
|
|
"fmt"
|
2019-12-30 00:51:20 +00:00
|
|
|
"image"
|
2018-03-18 03:35:03 +00:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
2018-03-18 13:57:01 +00:00
|
|
|
"path/filepath"
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-31 01:55:58 +00:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/conv"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/data"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/maps"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/sets"
|
|
|
|
"code.ur.gs/lupine/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-28 01:15:35 +01:00
|
|
|
step int
|
2019-12-30 00:51:20 +00:00
|
|
|
state state
|
|
|
|
lastState state
|
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
type state struct {
|
|
|
|
zoom float64
|
|
|
|
origin image.Point
|
|
|
|
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
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
objects := make([]*conv.Object, 0, len(mapSet.Palette))
|
2018-03-22 04:15:31 +00:00
|
|
|
for _, name := range mapSet.Palette {
|
|
|
|
objFile := filepath.Join(*gamePath, "Obj", name+".obj")
|
2019-12-30 00:51:20 +00:00
|
|
|
rawObj, err := data.LoadObject(objFile)
|
2018-03-22 04:15:31 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load %s: %v", name, err)
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
rawObj.Name = name
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
obj, err := conv.ConvertObject(rawObj, name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-10-12 23:02:24 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
objects = append(objects, obj)
|
2018-03-28 01:15:35 +01:00
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
state := state{
|
|
|
|
zoom: 1.0,
|
|
|
|
origin: image.Point{0, -3000}, // FIXME: haxxx
|
|
|
|
}
|
|
|
|
env := &env{
|
|
|
|
gameMap: gameMap,
|
|
|
|
set: mapSet,
|
|
|
|
objects: conv.MapByName(objects),
|
|
|
|
state: state,
|
|
|
|
lastState: state,
|
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
// TODO: click to view cell data
|
|
|
|
|
|
|
|
win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(+64, +0))
|
|
|
|
win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(-64, +0))
|
|
|
|
win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, +64))
|
|
|
|
win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, -64))
|
|
|
|
win.OnMouseWheel(env.changeZoom)
|
|
|
|
|
|
|
|
for i := 0; i <= 6; i++ {
|
|
|
|
win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i))
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
if err := win.Run(env.Update, env.Draw); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) Update() error {
|
|
|
|
if e.step == 0 || e.lastState != e.state {
|
|
|
|
log.Printf("zoom=%.2f zIdx=%v camPos=%#v", e.state.zoom, e.state.zIdx, e.state.origin)
|
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
e.lastState = e.state
|
|
|
|
e.step += 1
|
2018-03-28 01:15:35 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
return nil
|
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
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) Draw(screen *ebiten.Image) error {
|
2018-03-25 12:11:48 +01:00
|
|
|
// TODO: we should be able to perform bounds clipping on these
|
2019-12-30 00:51:20 +00:00
|
|
|
minX := int(e.gameMap.MinWidth)
|
|
|
|
maxX := int(e.gameMap.MaxWidth)
|
|
|
|
minY := int(e.gameMap.MinLength)
|
|
|
|
maxY := int(e.gameMap.MaxLength)
|
2018-03-25 12:11:48 +01:00
|
|
|
minZ := 0
|
2019-12-30 00:51:20 +00:00
|
|
|
maxZ := int(e.state.zIdx) + 1
|
2018-03-25 12:11:48 +01:00
|
|
|
|
|
|
|
for z := minZ; z < maxZ; z++ {
|
|
|
|
for y := minY; y < maxY; y++ {
|
|
|
|
for x := minX; x < maxX; x++ {
|
2019-12-30 00:51:20 +00:00
|
|
|
if err := e.renderCell(x, y, z, screen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-25 12:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 23:02:24 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
return nil
|
2018-03-25 12:11:48 +01:00
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
|
2018-03-25 12:11:48 +01:00
|
|
|
var sprites []*conv.Sprite
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
cell := e.gameMap.Cells.At(x, y, z)
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Surface); err != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
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
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Center); err != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Left); err != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Right); err != nil {
|
2018-03-28 01:00:55 +01:00
|
|
|
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
|
2019-12-30 00:51:20 +00:00
|
|
|
fx, fy := cellToPix(float64(x), float64(y))
|
|
|
|
fx += float64(e.state.origin.X)
|
|
|
|
fy += float64(e.state.origin.Y)
|
2018-03-23 23:54:20 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
iso := ebiten.GeoM{}
|
|
|
|
iso.Translate(fx, fy)
|
|
|
|
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
|
|
|
|
iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
2018-03-25 00:36:23 +00:00
|
|
|
|
2018-03-25 12:11:48 +01:00
|
|
|
for _, sprite := range sprites {
|
2019-12-30 00:51:20 +00:00
|
|
|
if err := screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2018-03-25 12:49:36 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
return nil
|
2018-03-25 12:49:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) changeOrigin(byX, byY int) func() {
|
|
|
|
return func() {
|
|
|
|
e.state.origin.X += byX
|
|
|
|
e.state.origin.Y += byY
|
|
|
|
}
|
2018-03-25 12:49:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) changeZoom(_, y float64) {
|
|
|
|
// Zoom in and out with the mouse wheel
|
|
|
|
e.state.zoom *= math.Pow(1.2, y)
|
2018-03-22 04:15:31 +00:00
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) setZIdx(to int) func() {
|
|
|
|
return func() {
|
|
|
|
e.state.zIdx = to
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2019-12-30 00:51:20 +00:00
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
var (
|
|
|
|
cellWidth = 64.0
|
|
|
|
cellHeight = 64.0
|
|
|
|
)
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
// Doesn't take the camera or Z level into account
|
|
|
|
func cellToPix(x, y float64) (float64, float64) {
|
|
|
|
return (x - y) * cellWidth, (x + y) * cellHeight / 2.0
|
|
|
|
}
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
// Doesn't take the camera or Z level into account
|
|
|
|
func pixToCell(x, y float64) (float64, float64) {
|
|
|
|
return y/cellHeight + x/(cellWidth*2.0), y/cellHeight - x/(cellWidth*2.0)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|