mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-20 03:16:26 -05:00
fe47e51351
* WIP refactor of d2map stuff * more d2map refactor adding realm init to game client passing map engine from client and server into realm at init change `generate map packet` to have act and level index as data * client explodes, but getting there * realm now initializes, networking works, but map generators dont currently do anything * changed the way that level type records are loaded * fixed funcs for level data lookups * started implementing level generator, currently crashing * client no longer exploding * d2networking refactor put exports into d2client.go and d2server.go kept GameClient and GameServer methods into their respective files made methods for packet handlers instead of the giant switch statements * bugfix: getting first level id by act * minor refactor of gamescreen for readability * towns now generate on server start, create player takes act and level id as args, levels have their own map engine
129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package d2player
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory"
|
|
)
|
|
|
|
type PlayerState struct {
|
|
HeroName string `json:"heroName"`
|
|
HeroType d2enum.Hero `json:"heroType"`
|
|
HeroLevel int `json:"heroLevel"`
|
|
Act int `json:"act"`
|
|
Level int `json:"actLevel"`
|
|
FilePath string `json:"-"`
|
|
Equipment d2inventory.CharacterEquipment `json:"equipment"`
|
|
Stats *d2hero.HeroStatsState `json:"stats"`
|
|
X float64 `json:"x"`
|
|
Y float64 `json:"y"`
|
|
}
|
|
|
|
func HasGameStates() bool {
|
|
basePath, _ := getGameBaseSavePath()
|
|
files, _ := ioutil.ReadDir(basePath)
|
|
return len(files) > 0
|
|
}
|
|
|
|
func GetAllPlayerStates() []*PlayerState {
|
|
basePath, _ := getGameBaseSavePath()
|
|
files, _ := ioutil.ReadDir(basePath)
|
|
result := make([]*PlayerState, 0)
|
|
for _, file := range files {
|
|
fileName := file.Name()
|
|
if file.IsDir() || len(fileName) < 5 || strings.ToLower(fileName[len(fileName)-4:]) != ".od2" {
|
|
continue
|
|
}
|
|
gameState := LoadPlayerState(path.Join(basePath, file.Name()))
|
|
if gameState == nil || gameState.HeroType == d2enum.HeroNone {
|
|
continue
|
|
// temporarily loading default class stats if the character was created before saving stats was introduced
|
|
// to be removed in the future
|
|
} else if gameState.Stats == nil {
|
|
gameState.Stats = d2hero.CreateHeroStatsState(gameState.HeroType, *d2datadict.CharStats[gameState.HeroType], 1, 0)
|
|
gameState.Save()
|
|
}
|
|
|
|
result = append(result, gameState)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// CreateTestGameState is used for the map engine previewer
|
|
func CreateTestGameState() *PlayerState {
|
|
result := &PlayerState{}
|
|
return result
|
|
}
|
|
|
|
func LoadPlayerState(path string) *PlayerState {
|
|
strData, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
result := &PlayerState{
|
|
FilePath: path,
|
|
}
|
|
err = json.Unmarshal(strData, result)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return result
|
|
}
|
|
|
|
func CreatePlayerState(heroName string, hero d2enum.Hero, classStats d2datadict.CharStatsRecord, hardcore bool) *PlayerState {
|
|
result := &PlayerState{
|
|
HeroName: heroName,
|
|
HeroType: hero,
|
|
Act: 0,
|
|
Level: 1,
|
|
Stats: d2hero.CreateHeroStatsState(hero, classStats, 1, 0),
|
|
Equipment: d2inventory.HeroObjects[hero],
|
|
FilePath: "",
|
|
}
|
|
|
|
result.Save()
|
|
return result
|
|
}
|
|
|
|
func getGameBaseSavePath() (string, error) {
|
|
configDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return path.Join(configDir, "OpenDiablo2/Saves"), nil
|
|
}
|
|
|
|
func getFirstFreeFileName() string {
|
|
i := 0
|
|
basePath, _ := getGameBaseSavePath()
|
|
for {
|
|
filePath := path.Join(basePath, strconv.Itoa(i)+".od2")
|
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
return filePath
|
|
}
|
|
i++
|
|
}
|
|
}
|
|
|
|
func (v *PlayerState) Save() {
|
|
if v.FilePath == "" {
|
|
v.FilePath = getFirstFreeFileName()
|
|
}
|
|
if err := os.MkdirAll(path.Dir(v.FilePath), 0755); err != nil {
|
|
log.Panic(err.Error())
|
|
}
|
|
fileJson, _ := json.MarshalIndent(v, "", " ")
|
|
ioutil.WriteFile(v.FilePath, fileJson, 0644)
|
|
}
|