forked from jewalky/srvmgr
-
Notifications
You must be signed in to change notification settings - Fork 5
/
srvmgr_new.cpp
297 lines (249 loc) · 8.67 KB
/
srvmgr_new.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "lib\utils.hpp"
#include "config_new.h"
#include "syslib.h"
#include "srvmgrdef.h"
#include "protolayer_hat.h"
#include "crash_filter.h"
#include "srvmgr.h"
#include "zxmgr.h"
#include "player_info.h"
#include "scanrange.h"
#include "unit_info.h"
#include "forbidden_items.h"
#include "multiplayer_shop.h"
void ChangeWndTitle(const char* title)
{
byte* mainWnd = zxmgr::GetMainWnd();
HWND hWnd = *(HWND*)(mainWnd + 0x1C);
SetWindowTextA(hWnd, title);
}
void OnInitializeServer()
{
SetExceptionFilter();
ChangeWndTitle(Format("Server ID %u (map not loaded)", Config::ServerID).c_str());
Printf("Server started.");
Config::MapLoaded = false;
Config::ServerStarted = true;
//if ((Config::ServerCaps & SVC_SAVE_DATABASE) && !SQL_Init()) Quit();
}
void OnInitializeMap()
{
byte* map_data = *(byte**)(0x00642C2C);
Config::CurrentMapName = *(const char**)(map_data+0x90);
Config::CurrentMapTitle = *(const char**)(map_data+0x1C8);
Config::MapLoaded = true;
std::string map_title = "";
if (!Config::CurrentMapTitle.length())
Config::CurrentMapTitle = Basename(Config::CurrentMapName);
else if (Config::ServerFlags & SVF_PVM)
map_title = "PvM: ";
map_title += Config::CurrentMapTitle;
ChangeWndTitle(Format("Server ID %u (%s)", Config::ServerID, map_title.c_str()).c_str());
Printf("Loaded map \"%s\".", Config::CurrentMapName.c_str());
// update PlayerInfo structures for new map
PI_Reset();
}
void OnPreInitializeMap()
{
// update UnitInfo
ClearShops();
UI_Reset();
Config::MapLoaded = false;
ChangeWndTitle(Format("Server ID %u (loading map...)", Config::ServerID).c_str());
}
void OnInitializeMapError(const char* mapfile, const char* error)
{
Config::MapLoaded = false;
ChangeWndTitle(Format("Server ID %u (map not loaded)", Config::ServerID).c_str());
Printf("Error: map \"%s\" not loaded (%s)", mapfile, error);
}
void OnServerClosed()
{
Config::ExitingCleanly = true;
Config::MapLoaded = false;
ChangeWndTitle(Format("Server ID %u (map not loaded)", Config::ServerID).c_str());
if (!NetCmd_Shutdown())
NetHat::Connected = false;
Printf("Server closed.");
log_format("\n");
}
void OnLocalMessageBox(const char* message)
{
Printf("Error: %s", message);
}
void OnServerTic()
{
Net_RegularProc();
// check for unit in astral & forbidden items in pack
for (int i = 0; i < 32; i++)
{
if (!Players[i].Exists) continue;
if (!Players[i].Class) continue;
if (*(uint32_t*)(Players[i].Class + 0x2C)) continue;
if (!*(byte**)(Players[i].Class + 0x38)) continue;
byte* unit = *(byte**)(Players[i].Class + 0x38);
// milliseconds from system start - Players[i].LastReturn...
if (Players[i].ShouldReturn && GetTickCount()-Players[i].LastReturn > 5000) // every 5 seconds
{
if (zxmgr::ReturnUnit(*(byte**)(Players[i].Class + 0x38)))
Players[i].ShouldReturn = false;
}
// check items that should be removed
/*std::vector<byte*> forbidden_items = ItemRemover_Process(unit);
for (std::vector<byte*>::iterator it = forbidden_items.begin();
it != forbidden_items.end(); ++it)
{
byte* item_vec = (*it);
byte* item_vec_info = *(byte**)(item_vec + 0x3C);
for (std::vector<byte*>::iterator jt = Players[i].SavedItems.begin();
jt != Players[i].SavedItems.end(); ++jt)
{
byte* item_saved = (*jt);
byte* item_saved_info = *(byte**)(item_saved + 0x3C);
if (item_saved_info == item_vec_info)
{
*(uint16_t*)(item_saved + 0x42) += *(uint16_t*)(item_vec + 0x42);
zxmgr::DestroyItem(item_vec);
item_vec = NULL;
item_vec_info = NULL;
break;
}
}
// existing item not found
if (item_vec) Players[i].SavedItems.push_back(item_vec);
}
if (forbidden_items.size()) zxmgr::UpdateUnit(unit, Players[i].Class, 0x00282000, 0, 0, 0);*/
}
//UI_Tick();
SR_Step();
Sleep(1);
}
void OnShopError()
{
Printf("Shop error: amount > 1000!");
}
void LogIP(byte* player)
{
const char* player_name = "(null)";
if (player) player_name = *(const char**)(player + 0x18);
byte* vd = zxmgr::GetNetworkStruct(player);
const char* player_addr = "n/a";
if (vd) player_addr = (const char*)(vd + 8);
Printf("Player %s has joined the game (from: %s)", player_name, player_addr);
}
byte* CreateItemParameter(byte* param, byte* item)
{
if (!item) return NULL;
if (!param) return NULL;
uint32_t prm1 = *(uint8_t*)(param + 0x3C);
uint32_t val1 = *(uint16_t*)(param + 0x40);
uint32_t val2 = *(uint16_t*)(param + 0x42);
uint32_t item_class = *(uint8_t*)(item + 0x45);
uint32_t item_material = *(uint8_t*)(item + 0x46);
uint32_t item_option = *(uint16_t*)(item + 0x0C);
uint32_t item_slot = *(uint8_t*)(item + 0x58);
/*if ((item_slot == 4 ||
item_slot == 5) &&
(prm1 == 2))
{
if (val1 > 2)
val1 = 2;
}*/
*(uint16_t*)(param + 0x40) = val1;
*(uint16_t*)(param + 0x42) = val2;
return param;
}
bool CheckItemUpgradable(byte* item)
{
if (!item) return false;
if (*(uint32_t*)(item + 0x1C) == 2) return false; // quest item
bool upgradable = true;
uint32_t item_class = *(uint8_t*)(item + 0x45);
uint32_t item_material = *(uint8_t*)(item + 0x46);
uint32_t item_option = *(uint16_t*)(item + 0x0C);
uint32_t item_slot = *(uint8_t*)(item + 0x58);
byte* parms = *(byte**)(item + 0x28);
while (parms)
{
byte* parm = *(byte**)(parms + 8);
if (parm)
{
uint32_t prm1 = *(uint8_t*)(parm + 0x3C);
uint32_t val1 = *(uint16_t*)(parm + 0x40);
uint32_t val2 = *(uint16_t*)(parm + 0x42);
/*if (prm1 == 2) // body
{
if(item_slot == 4 ||
item_slot == 5)
{
if(val1 > 2) val1 = 2;
if(val1 == 2) upgradable = false;
}
}*/
*(uint8_t*)(parm + 0x3C) = prm1;
*(uint16_t*)(parm + 0x40) = val1;
*(uint16_t*)(parm + 0x42) = val2;
}
parms = *(byte**)(parms + 4);
}
return upgradable;
}
bool Sv_ProcessClientPacket(int16_t id, byte* player, Packet& pack)
{
if (id == -1) return true; // hat
return true;
}
void _stdcall ExtDiplomacy(byte* player, uint32_t setd)
{
std::vector<byte*> players = zxmgr::GetPlayers();
for (size_t i = 0; i < players.size(); i++)
{
byte* player2 = players[i];
if (player2 == NULL)
continue;
if (player == player2)
{
zxmgr::SetDiplomacy(player, player2, 0x12);
}
else
{
// gm ally monsters, gm ally players, gm vision players
bool has_rights = false;
uint32_t rights = *(uint32_t*)(player + 0x14);
if ((rights & GMF_ANY) != GMF_ANY)
rights = 0;
else has_rights = true;
bool has_rights2 = false;
uint32_t rights2 = *(uint32_t*)(player2 + 0x14);
if ((rights2 & GMF_ANY) != GMF_ANY)
rights2 = 0;
else has_rights2 = true;
if (*(uint32_t*)(player2 + 0x2C))
rights2 = 0;
rights &= 0xFFFFFF;
rights2 &= 0xFFFFFF;
// ai
if ((rights & GMF_AI_ALLY) && *(uint32_t*)(player2 + 0x2C))
{
zxmgr::SetDiplomacy(player, player2, 0x02); // from this to others
zxmgr::SetDiplomacy(player2, player, 0x02); // from others to this
}
// not ai
if (((rights & GMF_PLAYERS_ALLY)||(rights2 & GMF_PLAYERS_ALLY)) && !*(uint32_t*)(player2 + 0x2C))
{
zxmgr::SetDiplomacy(player, player2, 0x02); // from this to others
zxmgr::SetDiplomacy(player2, player, 0x02); // from others to this
}
if ((rights & GMF_PLAYERS_VISION) && !*(uint32_t*)(player2 + 0x2C)) // not ai, vision
{
// from others to this
zxmgr::SetDiplomacy(player2, player, 0x10|zxmgr::GetDiplomacy(player2, player));
}
if (rights2 & GMF_PLAYERS_VISION) // other player has vision flag
{
// from this to others
zxmgr::SetDiplomacy(player, player2, 0x10|zxmgr::GetDiplomacy(player, player2));
}
}
}
}