forked from jewalky/srvmgr
-
Notifications
You must be signed in to change notification settings - Fork 5
/
scanrange.cpp
147 lines (126 loc) · 4.58 KB
/
scanrange.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
#include "scanrange.h"
#include "zxmgr.h"
#include "player_info.h"
#include "srvmgr.h"
#include "lib/ScanrangeCalc.hpp"
CScanrangeCalc Scanrange;
bool Scanrange_Initialized = false;
byte* Scanrange_Player = NULL;
// are coordinates on the map valid.. regular case up to 240, but
// in come cases got there not pure coords, but calculatuins, eg:
// (x - 20) + x, (y - 20) + y
bool srvmgr_CheckValid(int16_t x, int16_t y)
{
// max size of the map is 240 (255, but cause of borders -(8+8))
uint32_t mapwidth = *(uint32_t*)(*(uint32_t*)(0x006B16A8) + 0x50000); // get value from ALM
uint32_t mapheight = *(uint32_t*)(*(uint32_t*)(0x006B16A8) + 0x50004);
return (x >= 7 && y >= 7 && // non-valid for values less than 6 (see map editor 'corners')
x <= mapwidth - 7 &&
y <= mapheight - 7);
}
// get relief from coordinates (up to 240, 240)
uint8_t srvmgr_GetHeight(int16_t x, int16_t y)
{
if (!srvmgr_CheckValid(x, y)) return 0;
uint8_t* mapheights = (uint8_t*)(*(byte**)(0x006B16A8) + 0x944F4); // value from ALM
return *(mapheights + y * 256 + x);
}
void SR_UpdateUnit(byte* unit)
{
if (!Scanrange_Initialized)
{
Scanrange.InitializeTables();
Scanrange_Initialized = true;
}
if (!unit) return;
byte* unit_player = *(byte**)(unit+0x14);
if (!unit_player) return;
Scanrange_Player = unit_player;
// number of milliseconds from the moment when system was started
uint32_t ticks_count = GetTickCount();
bool dCalc = false;
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;
if (Players[i].Class == unit_player || (zxmgr::GetDiplomacy(unit_player, Players[i].Class) & 0x10))
{
uint8_t unit_x = *(uint8_t*)(*(byte**)(unit + 0x10));
uint8_t unit_y = *(uint8_t*)(*(byte**)(unit + 0x10)+1);
uint16_t unit_vision = *(uint16_t*)(unit+0xA4);
if (!dCalc)
{
// unit_x, unit_y - it's uint8_t values (up to 255), which we send to 32b 'baskets'
Scanrange.CalculateVision(unit_x, unit_y, unit_vision, &srvmgr_GetHeight, &srvmgr_CheckValid);
dCalc = true;
}
int16_t gen_x = unit_x - 20;
int16_t gen_y = unit_y - 20;
for (uint8_t x = 0; x < 41; x++)
{
for (uint8_t y = 0; y < 41; y++)
{
if (srvmgr_CheckValid(gen_x+x, gen_y+y) && (Scanrange.pTablesVision[x][y] > 0))
Players[i].Vision[gen_x+x][gen_y+y] = ticks_count;
}
}
}
}
}
void SR_Step()
{
std::vector<byte*> units = zxmgr::GetUnits();
for (int i = 0; i < 32; i++)
{
if (!Players[i].Exists) continue;
if (!Players[i].Class) continue;
memset(Players[i].Vision, 0, sizeof(Players[i].Vision));
}
for (std::vector<byte*>::iterator it = units.begin();
it != units.end(); ++it)
{
byte* unit = (*it);
if (unit && unit != *(byte**)(*(byte**)(unit+0x14)+0x38))
SR_UpdateUnit(unit);
}
for (int i = 0; i < 32; i++)
{
if (!Players[i].Exists) continue;
if (!Players[i].Class) continue;
if (*(byte**)(Players[i].Class+0x38))
SR_UpdateUnit(*(byte**)(Players[i].Class+0x38));
}
}
bool SR_CheckVision(byte* player, uint8_t check_x, uint8_t check_y)
{
Player* pi = PI_Get(player);
if (!pi) return false;
if (!srvmgr_CheckValid(check_x, check_y)) return false;
uint32_t ticks_count = GetTickCount();
for (int x = -1; x <= 1; x++)
for (int y = -1; y <= 1; y++)
if (ticks_count - pi->Vision[check_x+x][check_y+y] <= 3000) return true;
return false;
}
// can not be used from console
void SR_DumpToFile(byte* player)
{
Player* pi = PI_Get(player); // get player ID
if (!pi) return;
uint32_t ticks_count = GetTickCount(); // ms from the moment when system was started
// 'player+0x18' - player name
log_format("SR_DumpToFile: dump initiated (for player %s).\n", *(const char**)(player+0x18));
for (uint32_t y = 0; y < 256; y++)
{
for (uint32_t x = 0; x < 256; x++)
{
if (ticks_count - (pi->Vision[x][y]) <= 3000) // uint32_t Vision[256][256];
log_format2("\x7F"); // ASCII 127 (delete)
else log_format2(" ");
}
log_format2("\n");
}
log_format("SR_DumpToFile: dump finished.\n");
}