multiserver/active_object.go

183 lines
3.6 KiB
Go
Raw Permalink Normal View History

2021-01-24 05:00:26 -08:00
package main
2021-01-08 11:03:41 -08:00
import (
2021-03-29 09:57:30 -07:00
"bytes"
"encoding/binary"
2021-03-31 11:41:48 -07:00
"io"
"log"
"github.com/anon55555/mt/rudp"
)
2021-01-08 11:03:41 -08:00
const (
AoCmdSetProps = iota
AoCmdUpdatePos
AoCmdSetTextureMod
AoCmdSetSprite
AoCmdPunched
AoCmdUpdateArmorGroups
AoCmdSetAnimation
AoCmdSetBonePos
AoCmdAttachTo
AoCmdSetPhysicsOverride
AoCmdObsolete1
AoCmdSpawnInfant
AoCmdSetAnimSpeed
)
2021-03-29 09:57:30 -07:00
func processAoRmAdd(c *Conn, r *bytes.Reader) []byte {
2021-03-31 11:41:48 -07:00
w := &bytes.Buffer{}
2021-04-01 07:23:47 -07:00
countRm := ReadUint16(r)
WriteUint16(w, countRm)
2021-03-29 09:57:30 -07:00
2021-02-13 09:25:42 -08:00
var aoRm []uint16
2021-03-31 11:41:48 -07:00
for i := uint16(0); i < countRm; i++ {
2021-04-01 07:23:47 -07:00
id := ReadUint16(r)
2021-03-31 11:41:48 -07:00
2021-03-29 09:57:30 -07:00
if id == c.localPlayerCao {
id = c.currentPlayerCao
2021-02-20 04:17:52 -08:00
}
2021-04-01 07:23:47 -07:00
WriteUint16(w, id)
2021-02-20 04:17:52 -08:00
aoRm = append(aoRm, id)
2021-01-08 11:03:41 -08:00
}
2021-01-09 03:26:30 -08:00
2021-04-01 07:23:47 -07:00
countAdd := ReadUint16(r)
WriteUint16(w, countAdd)
2021-03-31 11:41:48 -07:00
2021-02-13 09:25:42 -08:00
var aoAdd []uint16
2021-03-31 11:41:48 -07:00
for i := uint16(0); i < countAdd; i++ {
2021-04-01 07:23:47 -07:00
id := ReadUint16(r)
objType := ReadUint8(r)
initData := ReadBytes32(r)
2021-03-31 11:41:48 -07:00
dr := bytes.NewReader(initData)
dr.Seek(1, io.SeekStart)
2021-02-20 04:17:52 -08:00
2021-04-01 07:23:47 -07:00
name := string(ReadBytes16(dr))
2021-01-09 03:26:30 -08:00
2021-04-01 07:23:47 -07:00
if name == c.Username() {
2021-03-29 09:57:30 -07:00
if c.initAoReceived {
// Read the messages from the packet
// They need to be forwarded
2021-03-31 11:41:48 -07:00
dr.Seek(30, io.SeekCurrent)
msgcountByte, _ := dr.ReadByte()
msgcount := uint8(msgcountByte)
var msgs [][]byte
for j := uint8(0); j < msgcount; j++ {
2021-03-31 11:41:48 -07:00
dr.Seek(2, io.SeekCurrent)
msglenBytes := make([]byte, 2)
dr.Read(msglenBytes)
msglen := binary.BigEndian.Uint16(msglenBytes)
2021-03-31 11:41:48 -07:00
msg := make([]byte, msglen)
dr.Read(msg)
msgs = append(msgs, msg)
}
// Generate message packet
msgpkt := []byte{0x00, ToClientActiveObjectMessages}
for _, msg := range msgs {
msgdata := make([]byte, 4+len(msg))
2021-03-29 09:57:30 -07:00
binary.BigEndian.PutUint16(msgdata[0:2], c.localPlayerCao)
binary.BigEndian.PutUint16(msgdata[2:4], uint16(len(msg)))
2021-03-29 09:57:30 -07:00
copy(msgdata[4:], aoMsgReplaceIDs(c, msg))
msgpkt = append(msgpkt, msgdata...)
}
2021-03-29 09:57:30 -07:00
ack, err := c.Send(rudp.Pkt{Reader: bytes.NewReader(msgpkt)})
if err != nil {
log.Print(err)
}
<-ack
2021-03-31 11:41:48 -07:00
data := w.Bytes()
2021-02-13 09:25:42 -08:00
binary.BigEndian.PutUint16(data[4+countRm*2:6+countRm*2], countAdd-1)
2021-03-31 11:41:48 -07:00
w = bytes.NewBuffer(data)
2021-03-29 09:57:30 -07:00
c.currentPlayerCao = id
2021-03-31 11:41:48 -07:00
continue
2021-02-13 09:25:42 -08:00
} else {
2021-03-29 09:57:30 -07:00
c.initAoReceived = true
c.localPlayerCao = id
c.currentPlayerCao = id
}
2021-03-29 09:57:30 -07:00
} else if id == c.localPlayerCao {
id = c.currentPlayerCao
2021-01-08 11:03:41 -08:00
}
2021-01-09 03:26:30 -08:00
2021-04-01 07:23:47 -07:00
if name != c.Username() {
2021-03-31 11:41:48 -07:00
aoAdd = append(aoAdd, id)
}
2021-02-13 09:25:42 -08:00
2021-04-01 07:23:47 -07:00
WriteUint16(w, id)
WriteUint8(w, objType)
WriteBytes32(w, initData)
2021-01-08 11:03:41 -08:00
}
2021-01-09 03:26:30 -08:00
2021-03-29 09:57:30 -07:00
c.redirectMu.Lock()
2021-01-08 11:03:41 -08:00
for i := range aoAdd {
if aoAdd[i] != 0 {
2021-03-29 09:57:30 -07:00
c.aoIDs[aoAdd[i]] = true
2021-01-08 11:03:41 -08:00
}
}
2021-01-09 03:26:30 -08:00
2021-01-08 11:03:41 -08:00
for i := range aoRm {
2021-03-29 09:57:30 -07:00
c.aoIDs[aoRm[i]] = false
2021-01-08 11:03:41 -08:00
}
2021-03-29 09:57:30 -07:00
c.redirectMu.Unlock()
2021-01-10 13:37:42 -08:00
2021-03-31 11:41:48 -07:00
return w.Bytes()
2021-01-08 11:03:41 -08:00
}
2021-02-20 04:17:52 -08:00
2021-03-29 09:57:30 -07:00
func processAoMsgs(c *Conn, r *bytes.Reader) []byte {
2021-03-31 12:13:29 -07:00
w := &bytes.Buffer{}
for r.Len() >= 4 {
2021-04-01 07:27:05 -07:00
id := ReadUint16(r)
2021-03-31 12:13:29 -07:00
2021-04-01 07:27:05 -07:00
msg := aoMsgReplaceIDs(c, ReadBytes16(r))
2021-02-20 04:17:52 -08:00
2021-03-29 09:57:30 -07:00
if id == c.currentPlayerCao {
id = c.localPlayerCao
} else if id == c.localPlayerCao {
id = c.currentPlayerCao
2021-02-20 04:17:52 -08:00
}
2021-04-01 07:27:05 -07:00
WriteUint16(w, id)
WriteBytes16(w, msg)
}
2021-03-29 09:57:30 -07:00
2021-03-31 12:13:29 -07:00
return w.Bytes()
}
2021-03-29 09:57:30 -07:00
func aoMsgReplaceIDs(c *Conn, data []byte) []byte {
switch cmd := data[0]; cmd {
case AoCmdAttachTo:
id := binary.BigEndian.Uint16(data[1:3])
2021-03-29 09:57:30 -07:00
if id == c.currentPlayerCao {
id = c.localPlayerCao
binary.BigEndian.PutUint16(data[1:3], id)
2021-03-29 09:57:30 -07:00
} else if id == c.localPlayerCao {
id = c.currentPlayerCao
binary.BigEndian.PutUint16(data[1:3], id)
}
case AoCmdSpawnInfant:
id := binary.BigEndian.Uint16(data[1:3])
2021-03-29 09:57:30 -07:00
if id == c.currentPlayerCao {
id = c.localPlayerCao
binary.BigEndian.PutUint16(data[1:3], id)
2021-03-29 09:57:30 -07:00
} else if id == c.localPlayerCao {
id = c.currentPlayerCao
binary.BigEndian.PutUint16(data[1:3], id)
}
2021-02-20 04:17:52 -08:00
}
2021-03-29 09:57:30 -07:00
2021-02-20 04:17:52 -08:00
return data
}