2020-06-13 18:32:09 -04:00
|
|
|
package d2client
|
|
|
|
|
|
|
|
import (
|
2020-06-26 17:12:19 -04:00
|
|
|
"fmt"
|
2020-06-22 20:31:42 -04:00
|
|
|
"os"
|
2020-06-13 18:32:09 -04:00
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapgen"
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
|
|
|
2020-08-25 09:10:26 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
|
|
|
|
2020-07-11 11:24:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-07-21 08:50:45 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
2020-11-21 05:33:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-06-21 18:40:37 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapentity"
|
2020-10-10 18:47:51 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
|
2020-06-26 17:12:19 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2localclient"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2remoteclient"
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
2020-07-11 11:24:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2script"
|
2020-06-13 18:32:09 -04:00
|
|
|
)
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
const logPrefix = "Game Client"
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
const (
|
|
|
|
numSubtilesPerTile = 5
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GameClient manages a connection to d2server.GameServer
|
2020-07-17 22:11:16 -04:00
|
|
|
// and keeps a synchronized copy of the map and entities.
|
2020-06-13 18:32:09 -04:00
|
|
|
type GameClient struct {
|
2020-07-02 16:29:28 -04:00
|
|
|
clientConnection ServerConnection // Abstract local/remote connection
|
2020-06-29 17:01:26 -04:00
|
|
|
connectionType d2clientconnectiontype.ClientConnectionType // Type of connection (local or remote)
|
2020-09-12 16:51:30 -04:00
|
|
|
asset *d2asset.AssetManager
|
2020-07-11 11:24:04 -04:00
|
|
|
scriptEngine *d2script.ScriptEngine
|
2020-09-20 17:52:01 -04:00
|
|
|
GameState *d2hero.HeroState // local player state
|
2020-07-11 11:24:04 -04:00
|
|
|
MapEngine *d2mapengine.MapEngine // Map and entities
|
2020-09-20 17:52:01 -04:00
|
|
|
mapGen *d2mapgen.MapGenerator // map generator
|
2020-07-17 22:11:16 -04:00
|
|
|
PlayerID string // ID of the local player
|
2020-07-11 11:24:04 -04:00
|
|
|
Players map[string]*d2mapentity.Player // IDs of the other players
|
|
|
|
Seed int64 // Map seed
|
|
|
|
RegenMap bool // Regenerate tile cache on render (map has changed)
|
2020-11-21 05:33:22 -05:00
|
|
|
|
|
|
|
*d2util.Logger
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Create constructs a new GameClient and returns a pointer to it.
|
2020-09-12 16:51:30 -04:00
|
|
|
func Create(connectionType d2clientconnectiontype.ClientConnectionType,
|
2020-11-21 05:33:22 -05:00
|
|
|
asset *d2asset.AssetManager,
|
|
|
|
l d2util.LogLevel,
|
|
|
|
scriptEngine *d2script.ScriptEngine) (*GameClient, error) {
|
2020-06-26 17:12:19 -04:00
|
|
|
result := &GameClient{
|
2020-09-12 16:51:30 -04:00
|
|
|
asset: asset,
|
2020-11-21 05:33:22 -05:00
|
|
|
MapEngine: d2mapengine.CreateMapEngine(l, asset),
|
2020-06-26 17:12:19 -04:00
|
|
|
Players: make(map[string]*d2mapentity.Player),
|
|
|
|
connectionType: connectionType,
|
2020-07-11 11:24:04 -04:00
|
|
|
scriptEngine: scriptEngine,
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
result.Logger = d2util.NewLogger()
|
|
|
|
result.Logger.SetPrefix(logPrefix)
|
|
|
|
result.Logger.SetLevel(l)
|
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
// for a remote client connection, set loading to true - wait until we process the GenerateMapPacket
|
|
|
|
// before we start updating map entites
|
|
|
|
result.MapEngine.IsLoading = connectionType == d2clientconnectiontype.LANClient
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
mapGen, err := d2mapgen.NewMapGenerator(asset, l, result.MapEngine)
|
2020-09-20 17:52:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.mapGen = mapGen
|
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
switch connectionType {
|
|
|
|
case d2clientconnectiontype.LANClient:
|
2020-11-21 05:33:22 -05:00
|
|
|
result.clientConnection, err = d2remoteclient.Create(l, asset)
|
2020-06-26 17:12:19 -04:00
|
|
|
case d2clientconnectiontype.LANServer:
|
2020-11-21 05:33:22 -05:00
|
|
|
result.clientConnection, err = d2localclient.Create(asset, l, true)
|
2020-06-26 17:12:19 -04:00
|
|
|
case d2clientconnectiontype.Local:
|
2020-11-21 05:33:22 -05:00
|
|
|
result.clientConnection, err = d2localclient.Create(asset, l, false)
|
2020-06-26 17:12:19 -04:00
|
|
|
default:
|
2020-09-20 17:52:01 -04:00
|
|
|
err = fmt.Errorf("unknown client connection type specified: %d", connectionType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
result.clientConnection.SetClientListener(result)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Open creates the server and connects to it if the client is local.
|
|
|
|
// If the client is remote it sends a PlayerConnectionRequestPacket to the
|
|
|
|
// server (see d2netpacket).
|
2020-07-17 22:11:16 -04:00
|
|
|
func (g *GameClient) Open(connectionString, saveFilePath string) error {
|
2020-07-11 11:24:04 -04:00
|
|
|
switch g.connectionType {
|
|
|
|
case d2clientconnectiontype.LANServer, d2clientconnectiontype.Local:
|
|
|
|
g.scriptEngine.AllowEval()
|
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
return g.clientConnection.Open(connectionString, saveFilePath)
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Close destroys the server if the client is local. For remote clients
|
|
|
|
// it sends a DisconnectRequestPacket (see d2netpacket).
|
2020-06-13 18:32:09 -04:00
|
|
|
func (g *GameClient) Close() error {
|
2020-07-11 11:24:04 -04:00
|
|
|
switch g.connectionType {
|
|
|
|
case d2clientconnectiontype.LANServer, d2clientconnectiontype.Local:
|
|
|
|
g.scriptEngine.DisallowEval()
|
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
return g.clientConnection.Close()
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Destroy does the same thing as Close.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (g *GameClient) Destroy() error {
|
2020-07-11 11:24:04 -04:00
|
|
|
return g.Close()
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// OnPacketReceived is called by the ClientConection and processes incoming
|
|
|
|
// packets.
|
2020-10-26 05:24:04 -04:00
|
|
|
// nolint:gocyclo // switch statement on packet type makes sense, no need to change
|
2020-06-13 18:32:09 -04:00
|
|
|
func (g *GameClient) OnPacketReceived(packet d2netpacket.NetPacket) error {
|
|
|
|
switch packet.PacketType {
|
2020-06-26 17:12:19 -04:00
|
|
|
case d2netpackettype.GenerateMap:
|
2020-07-17 22:11:16 -04:00
|
|
|
if err := g.handleGenerateMapPacket(packet); err != nil {
|
|
|
|
return err
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
case d2netpackettype.UpdateServerInfo:
|
2020-07-17 22:11:16 -04:00
|
|
|
if err := g.handleUpdateServerInfoPacket(packet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
case d2netpackettype.AddPlayer:
|
2020-07-17 22:11:16 -04:00
|
|
|
if err := g.handleAddPlayerPacket(packet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
case d2netpackettype.MovePlayer:
|
2020-07-17 22:11:16 -04:00
|
|
|
if err := g.handleMovePlayerPacket(packet); err != nil {
|
|
|
|
return err
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
case d2netpackettype.CastSkill:
|
2020-07-17 22:11:16 -04:00
|
|
|
if err := g.handleCastSkillPacket(packet); err != nil {
|
2020-06-26 20:03:00 -04:00
|
|
|
return err
|
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
case d2netpackettype.SpawnItem:
|
|
|
|
if err := g.handleSpawnItemPacket(packet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-22 20:31:42 -04:00
|
|
|
case d2netpackettype.Ping:
|
2020-07-17 22:11:16 -04:00
|
|
|
if err := g.handlePingPacket(); err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameClient: error responding to server ping: %s", err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-07-02 16:29:28 -04:00
|
|
|
case d2netpackettype.PlayerDisconnectionNotification:
|
|
|
|
// Not implemented
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("RemoteClientConnection: received disconnect: %s", packet.PacketData)
|
2020-06-22 20:31:42 -04:00
|
|
|
case d2netpackettype.ServerClosed:
|
2020-10-25 10:21:14 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/802
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Server has been closed")
|
2020-06-26 17:12:19 -04:00
|
|
|
os.Exit(0)
|
2020-11-01 08:26:15 -05:00
|
|
|
case d2netpackettype.ServerFull:
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Server is full") // need to be verified
|
2020-11-01 08:26:15 -05:00
|
|
|
os.Exit(0)
|
2020-06-13 18:32:09 -04:00
|
|
|
default:
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Fatalf("Invalid packet type: %d", packet.PacketType)
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SendPacketToServer calls server.OnPacketReceived if the client is local.
|
|
|
|
// If it is remote the NetPacket sent over a UDP connection to the server.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (g *GameClient) SendPacketToServer(packet d2netpacket.NetPacket) error {
|
|
|
|
return g.clientConnection.SendPacketToServer(packet)
|
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
func (g *GameClient) handleGenerateMapPacket(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
mapData, err := d2netpacket.UnmarshalGenerateMap(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
if mapData.RegionType == d2enum.RegionAct1Town {
|
2020-09-20 17:52:01 -04:00
|
|
|
g.mapGen.GenerateAct1Overworld()
|
2020-07-17 22:11:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
g.RegenMap = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) handleUpdateServerInfoPacket(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
serverInfo, err := d2netpacket.UnmarshalUpdateServerInfo(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
g.MapEngine.SetSeed(serverInfo.Seed)
|
|
|
|
g.PlayerID = serverInfo.PlayerID
|
|
|
|
g.Seed = serverInfo.Seed
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Player id set to %s", serverInfo.PlayerID)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) handleAddPlayerPacket(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
player, err := d2netpacket.UnmarshalAddPlayer(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
d2hero.HydrateSkills(player.Skills, g.asset)
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
newPlayer := g.MapEngine.NewPlayer(player.ID, player.Name, player.X, player.Y, 0,
|
2020-11-22 17:53:33 -05:00
|
|
|
player.HeroType, player.Stats, player.Skills, &player.Equipment, player.LeftSkill, player.RightSkill, player.Gold)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-05 21:27:45 -04:00
|
|
|
g.Players[newPlayer.ID()] = newPlayer
|
2020-07-17 22:11:16 -04:00
|
|
|
g.MapEngine.AddEntity(newPlayer)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 15:04:05 -04:00
|
|
|
func (g *GameClient) handleSpawnItemPacket(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
item, err := d2netpacket.UnmarshalSpawnItem(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
itemEntity, err := g.MapEngine.NewItem(item.X, item.Y, item.Codes...)
|
2020-07-30 15:04:05 -04:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
g.MapEngine.AddEntity(itemEntity)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
func (g *GameClient) handleMovePlayerPacket(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
movePlayer, err := d2netpacket.UnmarshalMovePlayer(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
player := g.Players[movePlayer.PlayerID]
|
2020-07-21 08:50:45 -04:00
|
|
|
start := d2vector.NewPositionTile(movePlayer.StartX, movePlayer.StartY)
|
|
|
|
dest := d2vector.NewPositionTile(movePlayer.DestX, movePlayer.DestY)
|
|
|
|
path := g.MapEngine.PathFind(start, dest)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
if len(path) > 0 {
|
|
|
|
player.SetPath(path, func() {
|
|
|
|
tilePosition := player.Position.Tile()
|
|
|
|
tile := g.MapEngine.TileAt(int(tilePosition.X()), int(tilePosition.Y()))
|
|
|
|
|
|
|
|
if tile == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:49:31 -04:00
|
|
|
player.SetIsInTown(tile.RegionType == d2enum.RegionAct1Town)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
err := player.SetAnimationMode(player.GetAnimationMode())
|
|
|
|
|
|
|
|
if err != nil {
|
2020-08-05 21:27:45 -04:00
|
|
|
fmtStr := "GameClient: error setting animation mode for player %s: %s"
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf(fmtStr, player.ID(), err)
|
2020-07-17 22:11:16 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) handleCastSkillPacket(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
playerCast, err := d2netpacket.UnmarshalCast(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
player := g.Players[playerCast.SourceEntityID]
|
2020-09-14 14:49:31 -04:00
|
|
|
player.StopMoving()
|
|
|
|
|
|
|
|
castX := playerCast.TargetX * numSubtilesPerTile
|
|
|
|
castY := playerCast.TargetY * numSubtilesPerTile
|
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
direction := player.Position.DirectionTo(*d2vector.NewVector(castX, castY))
|
|
|
|
player.SetDirection(direction)
|
|
|
|
|
|
|
|
skillRecord := g.asset.Records.Skill.Details[playerCast.SkillID]
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-10-24 10:08:45 -04:00
|
|
|
missileEntities, err := g.createMissileEntities(skillRecord, player, castX, castY)
|
2020-10-10 18:47:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-24 10:08:45 -04:00
|
|
|
var summonedNpcEntity *d2mapentity.NPC
|
|
|
|
if skillRecord.Summon != "" {
|
|
|
|
summonedNpcEntity, err = g.createSummonedNpcEntity(skillRecord, int(castX), int(castY))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
player.StartCasting(skillRecord.Anim, func() {
|
2020-10-24 10:08:45 -04:00
|
|
|
if len(missileEntities) > 0 {
|
|
|
|
// shoot the missiles of the skill after the player has finished casting
|
|
|
|
for _, missileEntity := range missileEntities {
|
|
|
|
g.MapEngine.AddEntity(missileEntity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if summonedNpcEntity != nil {
|
|
|
|
// summon the referenced NPC after the player has finished casting
|
|
|
|
g.MapEngine.AddEntity(summonedNpcEntity)
|
2020-10-10 18:47:51 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
overlayRecord := g.asset.Records.Layout.Overlays[skillRecord.Castoverlay]
|
|
|
|
|
2020-10-25 21:11:04 -04:00
|
|
|
return g.playCastOverlay(overlayRecord, int(player.Position.X()), int(player.Position.Y()))
|
2020-10-10 18:47:51 -04:00
|
|
|
}
|
|
|
|
|
2020-10-25 20:03:23 -04:00
|
|
|
func (g *GameClient) createSummonedNpcEntity(skillRecord *d2records.SkillRecord, x, y int) (*d2mapentity.NPC, error) {
|
2020-10-24 10:08:45 -04:00
|
|
|
monsterStatsRecord := g.asset.Records.Monster.Stats[skillRecord.Summon]
|
|
|
|
|
|
|
|
if monsterStatsRecord == nil {
|
2020-10-25 21:11:04 -04:00
|
|
|
fmtErr := "cannot cast skill - No monstat entry for \"%s\""
|
|
|
|
return nil, fmt.Errorf(fmtErr, skillRecord.Summon)
|
2020-10-24 10:08:45 -04:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:21:14 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/803
|
2020-10-25 20:03:23 -04:00
|
|
|
summonedNpcEntity, err := g.MapEngine.NewNPC(x, y, monsterStatsRecord, 0)
|
2020-10-24 10:08:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return summonedNpcEntity, nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:11:04 -04:00
|
|
|
func (g *GameClient) createMissileEntities(
|
|
|
|
skillRecord *d2records.SkillRecord,
|
|
|
|
player *d2mapentity.Player,
|
|
|
|
castX, castY float64,
|
|
|
|
) ([]*d2mapentity.Missile, error) {
|
2020-10-24 10:08:45 -04:00
|
|
|
missileRecords := []*d2records.MissileRecord{
|
|
|
|
g.asset.Records.GetMissileByName(skillRecord.Cltmissile),
|
|
|
|
g.asset.Records.GetMissileByName(skillRecord.Cltmissilea),
|
|
|
|
g.asset.Records.GetMissileByName(skillRecord.Cltmissileb),
|
|
|
|
g.asset.Records.GetMissileByName(skillRecord.Cltmissilec),
|
|
|
|
g.asset.Records.GetMissileByName(skillRecord.Cltmissiled),
|
|
|
|
}
|
|
|
|
|
|
|
|
missileEntities := make([]*d2mapentity.Missile, 0)
|
2020-10-25 20:28:40 -04:00
|
|
|
|
2020-10-24 10:08:45 -04:00
|
|
|
for _, missileRecord := range missileRecords {
|
|
|
|
if missileRecord == nil {
|
2020-10-25 10:21:14 -04:00
|
|
|
continue
|
2020-10-24 10:08:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
missileEntity, err := g.createMissileEntity(missileRecord, player, castX, castY)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
missileEntities = append(missileEntities, missileEntity)
|
|
|
|
}
|
|
|
|
|
|
|
|
return missileEntities, nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:11:04 -04:00
|
|
|
func (g *GameClient) createMissileEntity(
|
|
|
|
missileRecord *d2records.MissileRecord,
|
|
|
|
player *d2mapentity.Player,
|
|
|
|
castX, castY float64,
|
|
|
|
) (*d2mapentity.Missile, error) {
|
2020-10-10 18:47:51 -04:00
|
|
|
if missileRecord == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
radians := d2math.GetRadiansBetween(
|
2020-09-14 14:49:31 -04:00
|
|
|
player.Position.X(),
|
|
|
|
player.Position.Y(),
|
|
|
|
castX,
|
|
|
|
castY,
|
|
|
|
)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
missileEntity, err := g.MapEngine.NewMissile(
|
|
|
|
int(player.Position.X()),
|
|
|
|
int(player.Position.Y()),
|
|
|
|
g.asset.Records.Missiles[missileRecord.Id],
|
|
|
|
)
|
2020-09-20 11:55:44 -04:00
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
missileEntity.SetRadians(radians, func() {
|
|
|
|
g.MapEngine.RemoveEntity(missileEntity)
|
|
|
|
})
|
|
|
|
|
|
|
|
return missileEntity, nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 20:03:23 -04:00
|
|
|
func (g *GameClient) playCastOverlay(overlayRecord *d2records.OverlayRecord, x, y int) error {
|
2020-10-10 18:47:51 -04:00
|
|
|
if overlayRecord == nil {
|
2020-09-20 11:55:44 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
overlayEntity, err := g.MapEngine.NewCastOverlay(
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
overlayRecord,
|
2020-07-17 22:11:16 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
overlayEntity.SetOnDoneFunc(func() {
|
|
|
|
g.MapEngine.RemoveEntity(overlayEntity)
|
2020-07-17 22:11:16 -04:00
|
|
|
})
|
|
|
|
|
2020-10-10 18:47:51 -04:00
|
|
|
g.MapEngine.AddEntity(overlayEntity)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GameClient) handlePingPacket() error {
|
2020-11-23 08:18:30 -05:00
|
|
|
pongPacket, err := d2netpacket.CreatePongPacket(g.PlayerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.clientConnection.SendPacketToServer(pongPacket)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-25 09:10:26 -04:00
|
|
|
|
2020-10-22 02:41:21 -04:00
|
|
|
// IsSinglePlayer returns a bool for whether the game is a single-player game
|
2020-08-25 09:10:26 -04:00
|
|
|
func (g *GameClient) IsSinglePlayer() bool {
|
|
|
|
return g.connectionType == d2clientconnectiontype.Local
|
|
|
|
}
|