2019-10-26 17:30:00 -04:00
|
|
|
package Core
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-11-05 18:29:02 -05:00
|
|
|
"fmt"
|
2019-10-24 09:31:59 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2019-10-25 20:28:14 -04:00
|
|
|
"math"
|
2019-10-30 01:09:39 -04:00
|
|
|
"os"
|
2019-10-24 09:31:59 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2019-11-02 17:38:39 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/PaletteDefs"
|
2019-11-01 14:12:23 -04:00
|
|
|
|
2019-11-02 17:38:39 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Sound"
|
2019-10-25 20:28:14 -04:00
|
|
|
|
2019-11-02 17:38:39 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Common"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/MPQ"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/ResourcePaths"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Scenes"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/UI"
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2019-10-30 01:09:39 -04:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-10-24 09:31:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// EngineConfig defines the configuration for the engine, loaded from config.json
|
|
|
|
type EngineConfig struct {
|
2019-11-01 00:21:39 -04:00
|
|
|
Language string
|
2019-10-24 09:31:59 -04:00
|
|
|
FullScreen bool
|
|
|
|
Scale float64
|
|
|
|
RunInBackground bool
|
|
|
|
TicksPerSecond int
|
2019-11-01 22:41:49 -04:00
|
|
|
FpsCap int
|
2019-10-24 09:31:59 -04:00
|
|
|
VsyncEnabled bool
|
|
|
|
MpqPath string
|
|
|
|
MpqLoadOrder []string
|
2019-10-26 20:09:33 -04:00
|
|
|
SfxVolume float64
|
|
|
|
BgmVolume float64
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2019-10-25 18:40:27 -04:00
|
|
|
// Engine is the core OpenDiablo2 engine
|
2019-10-24 09:31:59 -04:00
|
|
|
type Engine struct {
|
2019-11-01 14:12:23 -04:00
|
|
|
Settings *EngineConfig // Engine configuration settings from json file
|
|
|
|
Files map[string]*Common.MpqFileRecord // Map that defines which files are in which MPQs
|
2019-11-05 18:29:02 -05:00
|
|
|
CheckedPatch map[string]bool // First time we check a file, we'll check if it's in the patch. This notes that we've already checked that.
|
2019-11-01 14:12:23 -04:00
|
|
|
LoadingSprite *Common.Sprite // The sprite shown when loading stuff
|
|
|
|
loadingProgress float64 // LoadingProcess is a range between 0.0 and 1.0. If set, loading screen displays.
|
|
|
|
stepLoadingSize float64 // The size for each loading step
|
|
|
|
CurrentScene Scenes.Scene // The current scene being rendered
|
|
|
|
UIManager *UI.Manager // The UI manager
|
|
|
|
SoundManager *Sound.Manager // The sound manager
|
|
|
|
nextScene Scenes.Scene // The next scene to be loaded at the end of the game loop
|
|
|
|
fullscreenKey bool // When true, the fullscreen toggle is still being pressed
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEngine creates and instance of the OpenDiablo2 engine
|
|
|
|
func CreateEngine() *Engine {
|
|
|
|
result := &Engine{
|
2019-10-25 20:28:14 -04:00
|
|
|
CurrentScene: nil,
|
|
|
|
nextScene: nil,
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
result.loadConfigurationFile()
|
2019-11-01 00:21:39 -04:00
|
|
|
ResourcePaths.LanguageCode = result.Settings.Language
|
2019-10-24 09:31:59 -04:00
|
|
|
result.mapMpqFiles()
|
2019-11-01 14:12:23 -04:00
|
|
|
Common.LoadPalettes(result.Files, result)
|
2019-10-27 17:24:21 -04:00
|
|
|
Common.LoadTextDictionary(result)
|
2019-10-28 17:51:17 -04:00
|
|
|
Common.LoadLevelTypes(result)
|
2019-10-29 02:24:05 -04:00
|
|
|
Common.LoadLevelPresets(result)
|
2019-11-03 11:04:58 -05:00
|
|
|
Common.LoadLevelWarps(result)
|
|
|
|
Common.LoadObjectTypes(result)
|
|
|
|
Common.LoadObjects(result)
|
2019-11-03 22:56:26 -05:00
|
|
|
Common.LoadWeapons(result)
|
2019-11-03 22:03:22 -05:00
|
|
|
Common.LoadMissiles(result)
|
2019-11-01 00:21:39 -04:00
|
|
|
Common.LoadSounds(result)
|
2019-10-25 20:28:14 -04:00
|
|
|
result.SoundManager = Sound.CreateManager(result)
|
2019-10-26 20:09:33 -04:00
|
|
|
result.SoundManager.SetVolumes(result.Settings.BgmVolume, result.Settings.SfxVolume)
|
|
|
|
result.UIManager = UI.CreateManager(result, *result.SoundManager)
|
2019-11-01 14:12:23 -04:00
|
|
|
result.LoadingSprite = result.LoadSprite(ResourcePaths.LoadingScreen, PaletteDefs.Loading)
|
2019-10-24 09:31:59 -04:00
|
|
|
loadingSpriteSizeX, loadingSpriteSizeY := result.LoadingSprite.GetSize()
|
|
|
|
result.LoadingSprite.MoveTo(int(400-(loadingSpriteSizeX/2)), int(300+(loadingSpriteSizeY/2)))
|
2019-10-26 00:26:48 -04:00
|
|
|
result.SetNextScene(Scenes.CreateMainMenu(result, result, result.UIManager, result.SoundManager))
|
2019-10-31 21:17:13 -04:00
|
|
|
//result.SetNextScene(Scenes.CreateBlizzardIntro(result, result))
|
2019-10-24 09:31:59 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) loadConfigurationFile() {
|
2019-10-30 01:09:39 -04:00
|
|
|
log.Println("Loading configuration file")
|
2019-10-24 09:31:59 -04:00
|
|
|
configJSON, err := ioutil.ReadFile("config.json")
|
|
|
|
if err != nil {
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal(err)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
var config EngineConfig
|
|
|
|
|
2019-10-26 20:09:33 -04:00
|
|
|
err = json.Unmarshal(configJSON, &config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-30 01:09:39 -04:00
|
|
|
v.Settings = &config
|
|
|
|
// Path fixup for wine-installed diablo 2 in linux
|
|
|
|
if v.Settings.MpqPath[0] != '/' {
|
|
|
|
if _, err := os.Stat(v.Settings.MpqPath); os.IsNotExist(err) {
|
|
|
|
homeDir, _ := homedir.Dir()
|
2019-10-31 21:17:13 -04:00
|
|
|
newPath := strings.ReplaceAll(v.Settings.MpqPath, `C:\`, homeDir+"/.wine/drive_c/")
|
|
|
|
newPath = strings.ReplaceAll(newPath, "C:/", homeDir+"/.wine/drive_c/")
|
2019-10-30 01:09:39 -04:00
|
|
|
newPath = strings.ReplaceAll(newPath, `\`, "/")
|
|
|
|
if _, err := os.Stat(newPath); !os.IsNotExist(err) {
|
|
|
|
log.Printf("Detected linux wine installation, path updated to wine prefix path.")
|
|
|
|
v.Settings.MpqPath = newPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Engine) mapMpqFiles() {
|
|
|
|
log.Println("mapping mpq file structure")
|
2019-11-01 14:12:23 -04:00
|
|
|
v.Files = make(map[string]*Common.MpqFileRecord)
|
2019-11-03 22:03:22 -05:00
|
|
|
v.CheckedPatch = make(map[string]bool)
|
2019-10-24 09:31:59 -04:00
|
|
|
for _, mpqFileName := range v.Settings.MpqLoadOrder {
|
|
|
|
mpqPath := path.Join(v.Settings.MpqPath, mpqFileName)
|
2019-10-25 19:37:04 -04:00
|
|
|
mpq, err := MPQ.Load(mpqPath)
|
2019-11-03 22:03:22 -05:00
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
if err != nil {
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal(err)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
fileListText, err := mpq.ReadFile("(listfile)")
|
2019-11-01 00:21:39 -04:00
|
|
|
if err != nil || fileListText == nil {
|
|
|
|
// Super secret patch file activate!
|
|
|
|
continue
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
fileList := strings.Split(string(fileListText), "\r\n")
|
2019-11-01 00:21:39 -04:00
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
for _, filePath := range fileList {
|
2019-11-05 18:29:02 -05:00
|
|
|
transFilePath := `/` + strings.ReplaceAll(strings.ToLower(filePath), `\`, `/`)
|
2019-11-03 22:03:22 -05:00
|
|
|
if _, exists := v.Files[transFilePath]; exists {
|
|
|
|
if v.Files[transFilePath].IsPatch {
|
|
|
|
v.Files[transFilePath].UnpatchedMpqFile = mpqPath
|
2019-11-01 00:21:39 -04:00
|
|
|
}
|
2019-10-24 09:31:59 -04:00
|
|
|
continue
|
|
|
|
}
|
2019-11-03 22:03:22 -05:00
|
|
|
v.Files[transFilePath] = &Common.MpqFileRecord{
|
2019-11-01 14:12:23 -04:00
|
|
|
mpqPath, false, ""}
|
2019-11-03 22:03:22 -05:00
|
|
|
v.CheckedPatch[transFilePath] = false
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 22:20:36 -04:00
|
|
|
var mutex sync.Mutex
|
|
|
|
|
2019-10-25 19:37:04 -04:00
|
|
|
// LoadFile loads a file from the specified mpq and returns the data as a byte array
|
|
|
|
func (v *Engine) LoadFile(fileName string) []byte {
|
2019-11-01 00:21:39 -04:00
|
|
|
fileName = strings.ReplaceAll(fileName, "{LANG}", ResourcePaths.LanguageCode)
|
2019-11-03 22:03:22 -05:00
|
|
|
fileName = strings.ReplaceAll(fileName, `\`, `/`)
|
|
|
|
var mpqLookupFileName string
|
2019-11-05 18:29:02 -05:00
|
|
|
if strings.HasPrefix(fileName, "/") || strings.HasPrefix(fileName, "\\") {
|
2019-11-03 22:03:22 -05:00
|
|
|
mpqLookupFileName = strings.ReplaceAll(fileName, `/`, `\`)[1:]
|
|
|
|
} else {
|
|
|
|
mpqLookupFileName = strings.ReplaceAll(fileName, `/`, `\`)
|
|
|
|
}
|
2019-11-05 18:29:02 -05:00
|
|
|
|
2019-10-25 22:20:36 -04:00
|
|
|
mutex.Lock()
|
2019-10-24 09:31:59 -04:00
|
|
|
// TODO: May want to cache some things if performance becomes an issue
|
|
|
|
mpqFile := v.Files[strings.ToLower(fileName)]
|
2019-11-01 00:21:39 -04:00
|
|
|
var mpq MPQ.MPQ
|
|
|
|
var err error
|
2019-11-03 22:03:22 -05:00
|
|
|
|
|
|
|
// always try to load from patch first
|
|
|
|
checked, checkok := v.CheckedPatch[strings.ToLower(fileName)]
|
|
|
|
patchLoaded := false
|
|
|
|
if !checked || !checkok {
|
|
|
|
patchMpqFilePath := path.Join(v.Settings.MpqPath, v.Settings.MpqLoadOrder[0])
|
|
|
|
mpq, err = MPQ.Load(patchMpqFilePath)
|
|
|
|
if err == nil {
|
|
|
|
// loaded patch mpq. check if this file exists in it
|
|
|
|
fileInPatch := mpq.FileExists(mpqLookupFileName)
|
|
|
|
if fileInPatch {
|
|
|
|
patchLoaded = true
|
|
|
|
// set the path to the patch so it will be loaded there in the future
|
|
|
|
mpqFile = &Common.MpqFileRecord{patchMpqFilePath, false, ""}
|
|
|
|
v.Files[strings.ToLower(fileName)] = mpqFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.CheckedPatch[strings.ToLower(fileName)] = true
|
|
|
|
}
|
2019-11-05 18:29:02 -05:00
|
|
|
|
2019-11-03 22:03:22 -05:00
|
|
|
if patchLoaded {
|
|
|
|
// if we already loaded the correct mpq from the patch check, don't bother reloading it
|
|
|
|
} else if mpqFile == nil {
|
2019-11-01 00:21:39 -04:00
|
|
|
// Super secret non-listed file?
|
2019-11-03 22:03:22 -05:00
|
|
|
found := false
|
2019-11-01 00:21:39 -04:00
|
|
|
for _, mpqFile := range v.Settings.MpqLoadOrder {
|
|
|
|
mpqFilePath := path.Join(v.Settings.MpqPath, mpqFile)
|
|
|
|
mpq, err = MPQ.Load(mpqFilePath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-05 18:29:02 -05:00
|
|
|
if !mpq.FileExists(strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(fileName, "/data", "data"), "/", `\`))) {
|
2019-11-01 00:21:39 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We found the super-secret file!
|
2019-11-03 22:03:22 -05:00
|
|
|
found = true
|
2019-11-01 14:12:23 -04:00
|
|
|
v.Files[strings.ToLower(fileName)] = &Common.MpqFileRecord{mpqFilePath, false, ""}
|
2019-11-01 00:21:39 -04:00
|
|
|
break
|
|
|
|
}
|
2019-11-03 22:03:22 -05:00
|
|
|
if !found {
|
|
|
|
log.Fatal(fmt.Sprintf("File '%s' not found during preload of listfiles, and could not be located in any MPQ checking manually.", fileName))
|
|
|
|
}
|
2019-11-01 00:21:39 -04:00
|
|
|
} else if mpqFile.IsPatch {
|
|
|
|
log.Fatal("Tried to load a patchfile")
|
|
|
|
} else {
|
|
|
|
mpq, err = MPQ.Load(mpqFile.MpqFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error loading file '%s'", fileName)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2019-11-01 00:21:39 -04:00
|
|
|
|
2019-11-03 22:03:22 -05:00
|
|
|
blockTableEntry, err := mpq.GetFileBlockData(mpqLookupFileName)
|
2019-10-24 09:31:59 -04:00
|
|
|
if err != nil {
|
2019-11-03 22:03:22 -05:00
|
|
|
log.Printf("Error locating block data entry for '%s' in mpq file '%s'", mpqLookupFileName, mpq.FileName)
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal(err)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2019-11-03 22:03:22 -05:00
|
|
|
mpqStream := MPQ.CreateStream(mpq, blockTableEntry, mpqLookupFileName)
|
2019-10-24 09:31:59 -04:00
|
|
|
result := make([]byte, blockTableEntry.UncompressedFileSize)
|
|
|
|
mpqStream.Read(result, 0, blockTableEntry.UncompressedFileSize)
|
2019-10-25 22:20:36 -04:00
|
|
|
mutex.Unlock()
|
2019-10-24 09:31:59 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLoading returns true if the engine is currently in a loading state
|
|
|
|
func (v *Engine) IsLoading() bool {
|
2019-10-25 20:28:14 -04:00
|
|
|
return v.loadingProgress < 1.0
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadSprite loads a sprite from the game's data files
|
2019-11-01 14:12:23 -04:00
|
|
|
func (v *Engine) LoadSprite(fileName string, palette PaletteDefs.PaletteType) *Common.Sprite {
|
2019-10-25 19:37:04 -04:00
|
|
|
data := v.LoadFile(fileName)
|
2019-11-01 14:12:23 -04:00
|
|
|
sprite := Common.CreateSprite(data, Common.Palettes[palette])
|
2019-10-24 09:31:59 -04:00
|
|
|
return sprite
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateScene handles the scene maintenance for the engine
|
|
|
|
func (v *Engine) updateScene() {
|
|
|
|
if v.nextScene == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if v.CurrentScene != nil {
|
|
|
|
v.CurrentScene.Unload()
|
|
|
|
}
|
|
|
|
v.CurrentScene = v.nextScene
|
|
|
|
v.nextScene = nil
|
2019-10-25 19:12:42 -04:00
|
|
|
v.UIManager.Reset()
|
2019-10-25 20:28:14 -04:00
|
|
|
thingsToLoad := v.CurrentScene.Load()
|
|
|
|
v.SetLoadingStepSize(1.0 / float64(len(thingsToLoad)))
|
|
|
|
v.ResetLoading()
|
|
|
|
go func() {
|
|
|
|
for _, f := range thingsToLoad {
|
|
|
|
f()
|
|
|
|
v.StepLoading()
|
|
|
|
}
|
|
|
|
v.FinishLoading()
|
|
|
|
}()
|
2019-10-25 15:06:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
// Update updates the internal state of the engine
|
|
|
|
func (v *Engine) Update() {
|
2019-10-25 17:15:44 -04:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyAlt) && ebiten.IsKeyPressed(ebiten.KeyEnter) {
|
|
|
|
if !v.fullscreenKey {
|
|
|
|
ebiten.SetFullscreen(!ebiten.IsFullscreen())
|
|
|
|
}
|
|
|
|
v.fullscreenKey = true
|
|
|
|
} else {
|
|
|
|
v.fullscreenKey = false
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
v.updateScene()
|
|
|
|
if v.CurrentScene == nil {
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal("no scene loaded")
|
|
|
|
}
|
2019-10-25 19:12:42 -04:00
|
|
|
|
|
|
|
if v.IsLoading() {
|
|
|
|
return
|
|
|
|
}
|
2019-10-25 20:28:14 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
v.CurrentScene.Update(float64(1) / ebiten.CurrentTPS())
|
2019-10-25 19:12:42 -04:00
|
|
|
v.UIManager.Update()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws the game
|
|
|
|
func (v *Engine) Draw(screen *ebiten.Image) {
|
2019-10-25 20:28:14 -04:00
|
|
|
if v.loadingProgress < 1.0 {
|
|
|
|
v.LoadingSprite.Frame = uint8(Common.Max(0, Common.Min(uint32(len(v.LoadingSprite.Frames)-1), uint32(float64(len(v.LoadingSprite.Frames)-1)*v.loadingProgress))))
|
2019-10-24 09:31:59 -04:00
|
|
|
v.LoadingSprite.Draw(screen)
|
|
|
|
} else {
|
|
|
|
if v.CurrentScene == nil {
|
2019-10-25 15:06:47 -04:00
|
|
|
log.Fatal("no scene loaded")
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
v.CurrentScene.Render(screen)
|
2019-10-25 19:12:42 -04:00
|
|
|
v.UIManager.Draw(screen)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNextScene tells the engine what scene to load on the next update cycle
|
2019-10-25 20:28:14 -04:00
|
|
|
func (v *Engine) SetNextScene(nextScene Scenes.Scene) {
|
2019-10-24 09:31:59 -04:00
|
|
|
v.nextScene = nextScene
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:28:14 -04:00
|
|
|
// SetLoadingStepSize sets the size of the loading step
|
|
|
|
func (v *Engine) SetLoadingStepSize(size float64) {
|
|
|
|
v.stepLoadingSize = size
|
|
|
|
}
|
2019-10-25 15:06:47 -04:00
|
|
|
|
2019-10-25 20:28:14 -04:00
|
|
|
// ResetLoading resets the loading progress
|
|
|
|
func (v *Engine) ResetLoading() {
|
|
|
|
v.loadingProgress = 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
// StepLoading increments the loading progress
|
|
|
|
func (v *Engine) StepLoading() {
|
|
|
|
v.loadingProgress = math.Min(1.0, v.loadingProgress+v.stepLoadingSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinishLoading terminates the loading phase
|
|
|
|
func (v *Engine) FinishLoading() {
|
|
|
|
v.loadingProgress = 1.0
|
2019-10-25 15:06:47 -04:00
|
|
|
}
|