Skip to content

Commit

Permalink
Add stubs for NPC interaction commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mbpolan committed Nov 14, 2023
1 parent b686bb8 commit a9f6988
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,11 @@ func (g *Game) DoUnequipItem(p *model.Player, itemID, interfaceID int, slotType
pe.DeferUnequipItem(targetItem, interfaceID, slotType)
}

// DoInteractWithNPC handles a player requesting to interact with an NPC
func (g *Game) DoInteractWithNPC(p *model.Player, actionIndex, targetID int) {
// TODO
}

// DoUseItem handles a player's request to use an item.
func (g *Game) DoUseItem(p *model.Player, itemID, interfaceID, actionID int) {
// TODO
Expand Down
49 changes: 49 additions & 0 deletions internal/network/request/interact_npc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package request

import (
"fmt"
"github.com/mbpolan/openmcs/internal/network"
)

const InteractWithNPCAction1RequestHeader byte = 0x9B
const InteractWithNPCAction2RequestHeader byte = 0x11
const InteractWithNPCAction3RequestHeader byte = 0x15
const InteractWithNPCAction4RequestHeader byte = 0x12

// InteractWithNPCRequest is sent when the player interacts with an NPC.
type InteractWithNPCRequest struct {
ActionIndex int
TargetID int
}

// Read parses the content of the request from a stream. If the data cannot be read, an error will be returned.
func (p *InteractWithNPCRequest) Read(r *network.ProtocolReader) error {
// read 1 byte for the header
header, err := r.Uint8()
if err != nil {
return err
}

// read 2 bytes for the target npc id
targetID, err := r.Uint16Alt()
if err != nil {
return err
}

// translate the header into an action index
switch header {
case InteractWithNPCAction1RequestHeader:
p.ActionIndex = 0
case InteractWithNPCAction2RequestHeader:
p.ActionIndex = 1
case InteractWithNPCAction3RequestHeader:
p.ActionIndex = 2
case InteractWithNPCAction4RequestHeader:
p.ActionIndex = 3
default:
return fmt.Errorf("unexpected interact with NPC header: %2x", header)
}

p.TargetID = int(targetID)
return nil
}
13 changes: 13 additions & 0 deletions internal/server/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,19 @@ func (c *ClientHandler) handleLoop() (clientState, error) {

c.game.DoSetPlayerDesign(c.player, req.Gender, req.Base, req.BodyColors)

case request.InteractWithNPCAction1RequestHeader,
request.InteractWithNPCAction2RequestHeader,
request.InteractWithNPCAction3RequestHeader,
request.InteractWithNPCAction4RequestHeader:
// the player interacted with an npc
var req request.InteractWithNPCRequest
err = req.Read(c.reader)
if err != nil {
break
}

c.game.DoInteractWithNPC(c.player, req.ActionIndex, req.TargetID)

default:
// unknown packet
err = fmt.Errorf("unexpected packet header: %2x", header)
Expand Down

0 comments on commit a9f6988

Please sign in to comment.