-
Notifications
You must be signed in to change notification settings - Fork 35
/
character.cpp
260 lines (230 loc) · 6.49 KB
/
character.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <QFile>
#include <QDir>
#include "widget.h"
#include "character.h"
#include "message.h"
SceneEntity::SceneEntity()
{
modelName = QString();
id = 0;
netviewId = 0;
pos=UVector(0,0,0);
rot=UQuaternion(0,0,0,0);
sceneName = QString();
}
Pony::Pony() : SceneEntity()
{
modelName = "PlayerBase";
}
Player::Player()
{
connected=false;
inGame=0;
lastPingNumber=0;
lastPingTime=timestampNow();
port=0;
IP=QString();
receivedDatas = new QByteArray;
pony = Pony();
for (int i=0;i<33;i++)
udpSequenceNumbers[i]=0;
for (int i=0;i<33;i++)
udpRecvSequenceNumbers[i]=0;
}
void Player::reset()
{
name.clear();
connected=false;
inGame=0;
lastPingNumber=0;
lastPingTime=timestampNow();
port=0;
IP.clear();
receivedDatas->clear();
pony = Pony();
for (int i=0;i<33;i++)
udpSequenceNumbers[i]=0;
for (int i=0;i<33;i++)
udpRecvSequenceNumbers[i]=0;
}
bool Player::savePlayers(QList<Player> playersData)
{
QFile playersFile("data/players/players.dat");
if (!playersFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
{
win.logStatusMessage("Error saving players database");
win.stopServer();
return false;
}
for (int i=0;i<playersData.size();i++)
{
playersFile.write(playersData[i].name.toLatin1());
playersFile.write("\31");
playersFile.write(playersData[i].passhash.toLatin1());
if (i+1!=playersData.size())
playersFile.write("\n");
}
return true;
}
QList<Player> Player::loadPlayers()
{
QList<Player> players;
QFile playersFile("data/players/players.dat");
if (!playersFile.open(QIODevice::ReadOnly))
{
win.logStatusMessage("Error reading players database");
win.stopServer();
return players;
}
QList<QByteArray> data = playersFile.readAll().split('\n');
if (data.size()==1 && data[0].isEmpty())
{
win.logMessage("Player database is empty. Continuing happily");
return players;
}
for (int i=0;i<data.size();i++)
{
QList<QByteArray> line = data[i].split('\31');
if (line.size()!=2)
{
win.logStatusMessage("Error reading players database");
win.stopServer();
return players;
}
Player newPlayer;
newPlayer.name = line[0];
newPlayer.passhash = line[1];
players << newPlayer;
}
win.logMessage(QString("Got ")+QString().setNum(players.size())+" players in database");
return players;
}
Player& Player::findPlayer(QList<Player>& players, QString uname)
{
for (int i=0; i<players.size(); i++)
{
if (players[i].name == uname)
return players[i];
}
Player* emptyPlayer = new Player();
return *emptyPlayer;
}
Player& Player::findPlayer(QList<Player>& players, QString uIP, quint16 uport)
{
for (int i=0; i<players.size(); i++)
{
if (players[i].IP == uIP && players[i].port == uport)
return players[i];
}
Player* emptyPlayer = new Player();
return *emptyPlayer;
}
Player& Player::findPlayer(QList<Player>& players, quint16 netviewId)
{
for (int i=0; i<players.size(); i++)
{
if (players[i].pony.netviewId == netviewId)
return players[i];
}
Player* emptyPlayer = new Player();
return *emptyPlayer;
}
void Player::savePonies(Player& player, QList<Pony> ponies)
{
win.logMessage("UDP: Saving ponies for "+QString().setNum(player.pony.netviewId)+" ("+player.name+")");
QDir playerPath(QDir::currentPath());
playerPath.cd("data");
playerPath.cd("players");
playerPath.mkdir(player.name.toLatin1());
QFile file(QDir::currentPath()+"/data/players/"+player.name.toLatin1()+"/ponies.dat");
file.open(QIODevice::ReadWrite | QIODevice::Truncate);
for (int i=0; i<ponies.size(); i++)
{
file.write(ponies[i].ponyData);
file.write(vectorToData(ponies[i].pos));
file.write(stringToData(ponies[i].sceneName));
}
}
QList<Pony> Player::loadPonies(Player& player)
{
QList<Pony> ponies;
QFile file(QDir::currentPath()+"/data/players/"+player.name.toLatin1()+"/ponies.dat");
if (!file.open(QIODevice::ReadOnly))
return ponies;
QByteArray data = file.readAll();
int i=0;
while (i<data.size())
{
Pony pony;
// Read the ponyData
unsigned strlen;
unsigned lensize=0;
{
unsigned char num3; int num=0, num2=0;
do {
num3 = data[i+lensize]; lensize++;
num |= (num3 & 0x7f) << num2;
num2 += 7;
} while ((num3 & 0x80) != 0);
strlen = (uint) num;
}
int ponyDataSize = strlen+lensize+43;
pony.ponyData = data.mid(i,ponyDataSize);
i+=ponyDataSize;
// Read pos
UVector pos = dataToVector(data.mid(i,12));
pony.pos = pos;
i+=12;
// Read sceneName
unsigned strlen2;
unsigned lensize2=0;
{
unsigned char num3; int num=0, num2=0;
do {
num3 = data[i+lensize2]; lensize2++;
num |= (num3 & 0x7f) << num2;
num2 += 7;
} while ((num3 & 0x80) != 0);
strlen2 = (uint) num;
}
pony.sceneName = data.mid(i+lensize2, strlen2);
i+=strlen2+lensize2;
ponies << pony;
}
return ponies;
}
void Player::removePlayer(QList<Player>* players, QString uIP, quint16 uport)
{
for (int i=0; i<players->size(); i++)
{
if ((*players)[i].IP == uIP && (*players)[i].port == uport)
players->removeAt(i);
}
}
void Player::disconnectPlayerCleanup(Player& player)
{
// Save the pony
QList<Pony> ponies = loadPonies(player);
for (int i=0; i<ponies.size(); i++)
if (ponies[i].ponyData == player.pony.ponyData)
ponies[i] = player.pony;
savePonies(player, ponies);
QString uIP = player.IP;
quint16 uPort = player.port;
Scene* scene = findScene(player.pony.sceneName);
if (scene->name.isEmpty())
win.logMessage("UDP: Can't find scene for player cleanup");
removePlayer(&(scene->players), uIP, uPort);
for (int i=0; i<scene->players.size(); i++)
sendNetviewRemove(scene->players[i], player.pony.netviewId);
removePlayer(&(win.udpPlayers), uIP, uPort);
}
WearableItem::WearableItem()
{
id=0;
index=0;
}
InventoryItem::InventoryItem() : WearableItem()
{
amount=0;
}