forked from jo3bingham/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateOnMap.cs
More file actions
85 lines (73 loc) · 2.9 KB
/
CreateOnMap.cs
File metadata and controls
85 lines (73 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using OXGaming.TibiaAPI.Appearances;
using OXGaming.TibiaAPI.Constants;
using OXGaming.TibiaAPI.Creatures;
using OXGaming.TibiaAPI.Utilities;
namespace OXGaming.TibiaAPI.Network.ServerPackets
{
public class CreateOnMap : ServerPacket
{
private const int MapSizeW = 10;
public Creature Creature { get; set; }
public ObjectInstance ObjectInstance { get; set; }
public Position Position { get; set; }
public ushort Id { get; set; }
public byte StackPosition { get; set; }
public CreateOnMap(Client client)
{
Client = client;
PacketType = ServerPacketType.CreateOnMap;
}
public override void ParseFromNetworkMessage(NetworkMessage message)
{
Position = message.ReadPosition();
if (!Client.WorldMapStorage.IsVisible(Position.X, Position.Y, Position.Z, true))
{
throw new Exception($"[CreateOnMap.ParseFromNetworkMessage] Co-ordinate {Position} is out of range.");
}
var mapPosition = Client.WorldMapStorage.ToMap(Position);
StackPosition = message.ReadByte();
Id = message.ReadUInt16();
if (Id == (int)CreatureInstanceType.UnknownCreature ||
Id == (int)CreatureInstanceType.OutdatedCreature ||
Id == (int)CreatureInstanceType.Creature)
{
Creature = message.ReadCreatureInstance(Id, Position);
ObjectInstance = Client.AppearanceStorage.CreateObjectInstance((uint)CreatureInstanceType.Creature, Creature.Id);
}
else
{
ObjectInstance = message.ReadObjectInstance(Id);
}
if (StackPosition == 255)
{
Client.WorldMapStorage.PutObject(mapPosition.X, mapPosition.Y, mapPosition.Z, ObjectInstance);
}
else
{
if (StackPosition > MapSizeW)
{
throw new Exception("[CreateOnMap.ParseFromNetworkMessage] Invalid position.");
}
Client.WorldMapStorage.InsertObject(mapPosition.X, mapPosition.Y, mapPosition.Z, StackPosition, ObjectInstance);
}
}
public override void AppendToNetworkMessage(NetworkMessage message)
{
message.Write((byte)ServerPacketType.CreateOnMap);
message.Write(Position);
message.Write(StackPosition);
if (Id == (int)CreatureInstanceType.UnknownCreature ||
Id == (int)CreatureInstanceType.OutdatedCreature ||
Id == (int)CreatureInstanceType.Creature)
{
message.Write(Id);
message.Write(Creature, (CreatureInstanceType)Id);
}
else
{
message.Write(ObjectInstance);
}
}
}
}