Files
ordoor/cmd/view-map/main.go

275 lines
6.6 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
2019-12-30 00:51:20 +00:00
"image"
"log"
"math"
"os"
2018-03-18 13:57:01 +00:00
"path/filepath"
2019-12-30 00:51:20 +00:00
"github.com/hajimehoshi/ebiten"
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"
)
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
set *sets.MapSet
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
}
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)
}
2019-12-30 00:51:20 +00:00
objects := make([]*conv.Object, 0, len(mapSet.Palette))
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)
if err != nil {
log.Fatalf("Failed to load %s: %v", name, err)
}
2019-12-30 00:51:20 +00:00
rawObj.Name = name
2019-12-30 00:51:20 +00:00
obj, err := conv.ConvertObject(rawObj, name)
if err != nil {
log.Fatal(err)
}
2019-12-30 00:51:20 +00:00
objects = append(objects, obj)
2018-03-28 01:15:35 +01:00
}
2019-12-30 00:51:20 +00:00
state := state{
zoom: 1.0,
2019-12-31 01:38:39 +00:00
origin: image.Point{0, 3000}, // FIXME: haxxx
2019-12-30 00:51:20 +00:00
}
env := &env{
gameMap: gameMap,
set: mapSet,
objects: conv.MapByName(objects),
state: state,
lastState: state,
}
2019-12-30 00:51:20 +00:00
win, err := ui.NewWindow("View Map " + *mapFile)
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
2019-12-31 01:38:39 +00:00
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))
2019-12-30 00:51:20 +00:00
win.OnMouseWheel(env.changeZoom)
for i := 0; i <= 6; i++ {
win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i))
}
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)
}
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
}
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
}
if ref.Index() >= len(palette) {
return nil, fmt.Errorf("Palette too small: %v requested", ref.Index())
}
name := palette[ref.Index()]
obj := e.objects[name]
if obj == nil {
return nil, fmt.Errorf("Failed to find surface sprite %#v -> %q", ref, name)
2018-03-18 13:57:01 +00: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 {
2019-12-31 01:38:39 +00:00
// Bounds clipping
// http://www.java-gaming.org/index.php?topic=24922.0
// https://stackoverflow.com/questions/892811/drawing-isometric-game-worlds
// https://gamedev.stackexchange.com/questions/25896/how-do-i-find-which-isometric-tiles-are-inside-the-cameras-current-view
sw, sh := screen.Size()
topLeftX, topLeftY := pixToCell(
float64(e.state.origin.X),
float64(e.state.origin.Y),
)
topLeftX -= 1 // Otherwise we miss half a cell on alternate rows on the left
bottomRightX, bottomRightY := pixToCell(
float64(e.state.origin.X+sw),
float64(e.state.origin.Y+sh),
)
// X+Y is constant for all tiles in a column
// X-Y is constant for all tiles in a row
for a := int(topLeftX + topLeftY); a <= int(bottomRightX+bottomRightY); a++ {
for b := int(topLeftX - topLeftY); b <= int(bottomRightX-bottomRightY); b++ {
if b&1 != a&1 {
continue
}
x := (a + b) / 2
y := (a - b) / 2
if x < int(e.gameMap.MinWidth) || x >= int(e.gameMap.MaxWidth) ||
y < int(e.gameMap.MinLength) || y >= int(e.gameMap.MaxLength) {
continue
}
for z := 0; z <= e.state.zIdx; z++ {
e.renderCell(x, y, z, screen)
}
}
}
2019-12-30 00:51:20 +00:00
return nil
}
2019-12-30 00:51:20 +00:00
func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
var sprites []*conv.Sprite
2019-12-30 00:51:20 +00:00
cell := e.gameMap.Cells.At(x, y, z)
2019-12-30 00:51:20 +00:00
if spr, err := e.getSprite(e.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 {
sprites = append(sprites, spr)
}
2019-12-30 00:51:20 +00:00
if spr, err := e.getSprite(e.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 {
sprites = append(sprites, spr)
}
2019-12-30 00:51:20 +00:00
if spr, err := e.getSprite(e.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 {
sprites = append(sprites, spr)
}
2019-12-30 00:51:20 +00:00
if spr, err := e.getSprite(e.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 {
sprites = append(sprites, spr)
}
2019-12-31 01:38:39 +00:00
iso := ebiten.GeoM{}
iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y))
fx, fy := cellToPix(float64(x), float64(y))
iso.Translate(fx, fy)
// Taking the Z index away *seems* to draw the object in the correct place.
// FIXME: There are some artifacts, investigate more
2019-12-31 01:38:39 +00:00
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
2019-12-31 01:38:39 +00:00
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
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
}
}
2019-12-30 00:51:20 +00:00
return nil
}
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
}
}
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-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
}
2019-12-30 00:51:20 +00:00
}
2019-12-31 01:38:39 +00:00
const (
cellWidth = 64
cellHeight = 64
2019-12-30 00:51:20 +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
}
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)
}