forked from marcosvf132/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.cs
More file actions
138 lines (107 loc) · 3.95 KB
/
Field.cs
File metadata and controls
138 lines (107 loc) · 3.95 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using OXGaming.TibiaAPI.Appearances;
namespace OXGaming.TibiaAPI.WorldMap
{
public class Field
{
private const int MapSizeW = 10;
private ObjectInstance[] objectsNetwork;
private int objectsCount = 0;
public Field()
{
objectsNetwork = new ObjectInstance[MapSizeW];
}
public void Reset()
{
ResetObjects();
}
public void ResetObjects()
{
objectsNetwork = new ObjectInstance[MapSizeW];
objectsCount = 0;
}
public ObjectInstance ChangeObject(ObjectInstance objectInstance, int stackPosition)
{
if (objectInstance == null)
return null;
if (stackPosition < 0 || stackPosition >= objectsCount)
return null;
var oldThing = objectsNetwork[stackPosition];
objectsNetwork[stackPosition] = objectInstance;
return oldThing;
}
public ObjectInstance DeleteObject(int stackPosition)
{
if (stackPosition < 0 || stackPosition >= objectsCount)
return null;
var removedThing = objectsNetwork[stackPosition];
objectsCount = Math.Max(0, objectsCount - 1);
while (stackPosition < objectsCount) {
objectsNetwork[stackPosition] = objectsNetwork[stackPosition + 1];
stackPosition++;
}
objectsNetwork[objectsCount] = null;
return removedThing;
}
public ObjectInstance GetObject(int stackPosition)
{
if (stackPosition == 0 && objectsCount == 0)
return null;
if (stackPosition < 0 || stackPosition >= objectsCount)
return null;
return objectsNetwork[stackPosition];
}
public ObjectInstance PutObject(ObjectInstance objectInstance, int stackPosition)
{
if (objectInstance == null)
return null;
if (stackPosition < 0 || stackPosition == MapSizeW) {
stackPosition = 0;
var newPriority = GetObjectPriority(objectInstance);
while (stackPosition < objectsCount) {
var currentPriority = GetObjectPriority(objectsNetwork[stackPosition]);
if (currentPriority > newPriority || (currentPriority == newPriority && currentPriority == 5))
break;
stackPosition++;
}
if (stackPosition >= MapSizeW)
return objectInstance;
} else if (stackPosition <= objectsCount || stackPosition == MapSizeW) {
stackPosition = Math.Min(Math.Min(stackPosition, objectsCount), (MapSizeW - 1));
} else {
return null;
}
ObjectInstance removedThing = null;
if (objectsCount >= MapSizeW) {
objectsCount = MapSizeW;
removedThing = objectsNetwork[MapSizeW - 1];
} else {
objectsCount++;
}
var count = objectsCount - 1;
while (count > stackPosition) {
objectsNetwork[count] = objectsNetwork[count - 1];
count--;
}
objectsNetwork[stackPosition] = objectInstance;
return removedThing;
}
public static int GetObjectPriority(ObjectInstance objectInstance)
{
if (objectInstance.Id >= 97 && objectInstance.Id <= 99) // 99
return 4;
var type = objectInstance.Type;
if (type == null)
return 5;
if (type.Flags.Bank != null)
return 0;
if (type.Flags.Clip)
return 1;
if (type.Flags.Bottom)
return 2;
if (type.Flags.Top)
return 3;
return 5;
}
}
}