From 320583b5d496bb1298acf4b07909f4f804206875 Mon Sep 17 00:00:00 2001 From: Thomas Christlieb Date: Wed, 25 Nov 2020 11:48:23 +0100 Subject: [PATCH] save act and difficulty. Fixes #866 --- d2common/d2enum/difficulty.go | 13 +++++++++++++ d2core/d2hero/hero_state.go | 1 + d2core/d2map/d2mapentity/factory.go | 1 + d2core/d2map/d2mapentity/player.go | 1 + d2core/d2records/difficultylevels_loader.go | 10 +++++++++- d2core/d2records/difficultylevels_record.go | 4 +++- d2game/d2gamescreen/game.go | 2 +- d2networking/d2netpacket/packet_save_player.go | 10 +++++++--- d2networking/d2server/game_server.go | 2 ++ 9 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 d2common/d2enum/difficulty.go diff --git a/d2common/d2enum/difficulty.go b/d2common/d2enum/difficulty.go new file mode 100644 index 00000000..4acc7e61 --- /dev/null +++ b/d2common/d2enum/difficulty.go @@ -0,0 +1,13 @@ +package d2enum + +// DifficultyType is an enum for the possible difficulties +type DifficultyType int + +const ( + // DifficultyNormal is the normal difficulty + DifficultyNormal DifficultyType = iota + // DifficultyNightmare is the nightmare difficulty + DifficultyNightmare + // DifficultyHell is the hell difficulty + DifficultyHell +) diff --git a/d2core/d2hero/hero_state.go b/d2core/d2hero/hero_state.go index d571f1ce..a63dde02 100644 --- a/d2core/d2hero/hero_state.go +++ b/d2core/d2hero/hero_state.go @@ -19,4 +19,5 @@ type HeroState struct { LeftSkill int `json:"leftSkill"` RightSkill int `json:"rightSkill"` Gold int `json:"Gold"` + Difficulty d2enum.DifficultyType `json:"difficulty"` } diff --git a/d2core/d2map/d2mapentity/factory.go b/d2core/d2map/d2mapentity/factory.go index 4d449531..3d42c753 100644 --- a/d2core/d2map/d2mapentity/factory.go +++ b/d2core/d2map/d2mapentity/factory.go @@ -102,6 +102,7 @@ func (f *MapEntityFactory) NewPlayer(id, name string, x, y, direction int, heroT isInTown: true, isRunning: false, Gold: gold, + Act: 1, } result.mapEntity.uuid = id diff --git a/d2core/d2map/d2mapentity/player.go b/d2core/d2map/d2mapentity/player.go index c2d9d84e..741ea985 100644 --- a/d2core/d2map/d2mapentity/player.go +++ b/d2core/d2map/d2mapentity/player.go @@ -30,6 +30,7 @@ type Player struct { isRunning bool isCasting bool onFinishedCasting func() + Act int } // run speed should be walkspeed * 1.5, since in the original game it is 6 yards walk and 9 yards run. diff --git a/d2core/d2records/difficultylevels_loader.go b/d2core/d2records/difficultylevels_loader.go index 0d629972..c880e1e5 100644 --- a/d2core/d2records/difficultylevels_loader.go +++ b/d2core/d2records/difficultylevels_loader.go @@ -1,6 +1,7 @@ package d2records import ( + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt" ) @@ -27,7 +28,14 @@ func difficultyLevelsLoader(r *RecordManager, d *d2txt.DataDictionary) error { LifeStealDivisor: d.Number("LifeStealDivisor"), ManaStealDivisor: d.Number("ManaStealDivisor"), } - records[record.Name] = record + switch record.Name { + case "Normal": + records[d2enum.DifficultyNormal] = record + case "Nightmare": + records[d2enum.DifficultyNightmare] = record + case "Hell": + records[d2enum.DifficultyHell] = record + } } if d.Err != nil { diff --git a/d2core/d2records/difficultylevels_record.go b/d2core/d2records/difficultylevels_record.go index 7360e228..94718055 100644 --- a/d2core/d2records/difficultylevels_record.go +++ b/d2core/d2records/difficultylevels_record.go @@ -1,7 +1,9 @@ package d2records +import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" + // DifficultyLevels contain the difficulty records for each difficulty -type DifficultyLevels map[string]*DifficultyLevelRecord +type DifficultyLevels map[d2enum.DifficultyType]*DifficultyLevelRecord // DifficultyLevelRecord contain the parameters that change for different difficulties type DifficultyLevelRecord struct { diff --git a/d2game/d2gamescreen/game.go b/d2game/d2gamescreen/game.go index 7e0dd522..687b758f 100644 --- a/d2game/d2gamescreen/game.go +++ b/d2game/d2gamescreen/game.go @@ -343,7 +343,7 @@ func (v *Game) OnPlayerMove(targetX, targetY float64) { func (v *Game) OnPlayerSave() error { playerState := v.gameClient.Players[v.gameClient.PlayerID] - sp, err := d2netpacket.CreateSavePlayerPacket(playerState) + sp, err := d2netpacket.CreateSavePlayerPacket(playerState, d2enum.DifficultyNormal) if err != nil { return fmt.Errorf("SavePlayerPacket: %v", err) } diff --git a/d2networking/d2netpacket/packet_save_player.go b/d2networking/d2netpacket/packet_save_player.go index 86fd2f24..fffa0f01 100644 --- a/d2networking/d2netpacket/packet_save_player.go +++ b/d2networking/d2netpacket/packet_save_player.go @@ -3,6 +3,8 @@ package d2netpacket import ( "encoding/json" + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" + "github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapentity" "github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype" ) @@ -10,13 +12,15 @@ import ( // SavePlayerPacket has the actual selected left and right skill // the Server has to check if these skills are actually allowed for the Player type SavePlayerPacket struct { - Player *d2mapentity.Player `json:"Player"` + Player *d2mapentity.Player `json:"Player"` + Difficulty d2enum.DifficultyType `json:"Difficulty"` } // CreateSavePlayerPacket sends a packet which instructs the server to save the Player -func CreateSavePlayerPacket(playerState *d2mapentity.Player) (NetPacket, error) { +func CreateSavePlayerPacket(playerState *d2mapentity.Player, difficulty d2enum.DifficultyType) (NetPacket, error) { savePlayerData := SavePlayerPacket{ - Player: playerState, + Player: playerState, + Difficulty: difficulty, } b, err := json.Marshal(savePlayerData) diff --git a/d2networking/d2server/game_server.go b/d2networking/d2server/game_server.go index 0d4bec42..de20b456 100644 --- a/d2networking/d2server/game_server.go +++ b/d2networking/d2server/game_server.go @@ -483,6 +483,8 @@ func (g *GameServer) OnPacketReceived(client ClientConnection, packet d2netpacke playerState.LeftSkill = savePacket.Player.LeftSkill.Shallow.SkillID playerState.RightSkill = savePacket.Player.RightSkill.Shallow.SkillID playerState.Stats = savePacket.Player.Stats + playerState.Act = savePacket.Player.Act + playerState.Difficulty = savePacket.Difficulty err = g.heroStateFactory.Save(playerState) if err != nil {