-
Notifications
You must be signed in to change notification settings - Fork 5
/
personal-space.inc
228 lines (178 loc) · 4.77 KB
/
personal-space.inc
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
// built-in include guard removal
// just in case the user has a local dependency with the same file name
#if defined _inc_personal_space
#undef _inc_personal_space
#endif
// custom include-guard to ensure we don't duplicate
#if defined _personal_space_included
#endinput
#endif
#define _personal_space_included
#include <a_samp>
#include <logger>
#include <YSI_Data\y_iterate>
#include <streamer>
#include <mathutil>
#include <YSI_Coding\y_hooks>
#if !defined STREAMER_TAG_AREA
#define STREAMER_TAG_AREA _
#endif
enum E_APPROACH_DIRECTION {
APPROACH_FROM_FRONT,
APPROACH_FROM_LEFT,
APPROACH_FROM_BACK,
APPROACH_FROM_RIGHT,
}
#define PSPACE_STREAMER_AREA_IDENTIFIER 1098
forward IsPlayerNextToPlayer(playerid, targetid);
/*
# Description
Checks if a player is standing next to another player.
*/
forward GetPlayersNextToPlayer(playerid, players[], len = sizeof players);
/*
# Description
Returns a list of players stood next to the player.
*/
forward E_APPROACH_DIRECTION:GetApproachDirection(Float:angle);
/*
# Description
Returns a direction from the given angle.
*/
forward OnPlayerApproachPlayer(playerid, targetid, E_APPROACH_DIRECTION:fromDirection, Float:angle);
/*
# Called
When a player walks within 2m of another player.
*/
forward OnPlayerLeavePlayer(playerid, targetid);
/*
# Called
When a player leaves the 2m boundary of another player.
*/
static
STREAMER_TAG_AREA:Area[MAX_PLAYERS],
Iterator:NearbyPlayers[MAX_PLAYERS]<MAX_PLAYERS>;
hook OnGameModeInit() {
Iter_Init(NearbyPlayers);
}
#if defined FILTERSCRIPT
hook OnFilterScriptInit() {
foreach (new i : Player) {
_pspace_connect(i);
}
}
hook OnFilterScriptExit() {
foreach (new i : Player) {
_pspace_disconnect(i);
}
}
#endif
hook OnPlayerConnect(playerid) {
_pspace_connect(playerid);
}
hook OnPlayerDisconnect(playerid) {
_pspace_disconnect(playerid);
}
static _pspace_connect(playerid) {
new data[2];
data[0] = PSPACE_STREAMER_AREA_IDENTIFIER;
data[1] = playerid;
Area[playerid] = CreateDynamicSphere(0.0, 0.0, 0.0, 2.0);
Streamer_SetArrayData(STREAMER_TYPE_AREA, Area[playerid], E_STREAMER_EXTRA_ID, data, 2);
}
static _pspace_disconnect(playerid) {
DestroyDynamicArea(Area[playerid]);
Area[playerid] = STREAMER_TAG_AREA:INVALID_STREAMER_ID;
}
hook OnPlayerSpawn(playerid) {
AttachDynamicAreaToPlayer(Area[playerid], playerid);
}
hook OnPlayerEnterDynArea(playerid, STREAMER_TAG_AREA:areaid) {
if(GetPlayerState(playerid) != PLAYER_STATE_SPECTATING) {
new data[2];
Streamer_GetArrayData(STREAMER_TYPE_AREA, areaid, E_STREAMER_EXTRA_ID, data, 2);
if(data[0] == PSPACE_STREAMER_AREA_IDENTIFIER) {
if(data[1] == playerid) {
return 0;
}
if(GetPlayerState(data[1]) == PLAYER_STATE_SPECTATING) {
return 0;
}
if(areaid != Area[data[1]]) {
Logger_Err("player area id mismatch");
return 0;
}
Iter_Add(NearbyPlayers[playerid], data[1]);
Iter_Add(NearbyPlayers[data[1]], playerid);
new
Float:px,
Float:py,
Float:pz,
Float:tx,
Float:ty,
Float:tz,
Float:angle,
E_APPROACH_DIRECTION:direction;
GetPlayerPos(playerid, px, py, pz);
GetPlayerPos(data[1], tx, ty, tz);
angle = GetAbsoluteAngle(GetAngleToPoint(tx, ty, px, py));
direction = GetApproachDirection(angle);
CallLocalFunction("OnPlayerApproachPlayer", "dddf", playerid, data[1], _:direction, angle);
}
}
return 0;
}
hook OnPlayerLeaveDynArea(playerid, STREAMER_TAG_AREA:areaid) {
if(GetPlayerState(playerid) != PLAYER_STATE_SPECTATING) {
new data[2];
Streamer_GetArrayData(STREAMER_TYPE_AREA, areaid, E_STREAMER_EXTRA_ID, data, 2);
if(data[0] == PSPACE_STREAMER_AREA_IDENTIFIER) {
if(data[1] == playerid) {
return 0;
}
if(GetPlayerState(data[1]) == PLAYER_STATE_SPECTATING) {
return 0;
}
if(areaid != Area[data[1]]) {
Logger_Err("player area id mismatch");
} else {
CallLocalFunction("OnPlayerLeavePlayer", "dd", playerid, data[1]);
}
Iter_Remove(NearbyPlayers[playerid], data[1]);
Iter_Remove(NearbyPlayers[data[1]], playerid);
}
}
return 0;
}
stock IsPlayerNextToPlayer(playerid, targetid) {
if(playerid == targetid) {
return 0;
}
if(GetPlayerState(playerid) == PLAYER_STATE_SPECTATING) {
return 0;
}
if(GetPlayerState(targetid) == PLAYER_STATE_SPECTATING) {
return 0;
}
return IsPlayerInDynamicArea(playerid, Area[targetid]);
}
stock GetPlayersNextToPlayer(playerid, players[], len = sizeof players) {
new idx;
foreach(new i : NearbyPlayers[playerid]) {
if(idx == len - 1) {
break;
}
players[idx++] = i;
}
return idx;
}
stock E_APPROACH_DIRECTION:GetApproachDirection(Float:angle) {
if(155.0 < angle < 205.0) {
return APPROACH_FROM_BACK;
} else if(225.0 < angle < 315.0) {
return APPROACH_FROM_RIGHT;
} else if(45.0 < angle < 155.0) {
return APPROACH_FROM_LEFT;
}
return APPROACH_FROM_FRONT;
}