View difference between Paste ID: Vsz2ekHV and Stn9JV6R
SHOW: | | - or go back to the newest paste.
1
//***** Made by TopAz in 15 minutes.
2
3
4
//----- Header
5
#if defined _getnearest_included
6
	#endinput
7
#endif
8
#define _getnearest_included
9
#pragma library samp
10
//----------------
11
12
//----- External Library(ies)
13
#include <a_samp>
14
//------------------------
15
16
//----- Constants 
17
/*#if defined USE_STREAMER
18
	#include "streamer"
19
#endif*/
20
21
#define	OBJECT				(0)
22
#define PLAYER				(1)
23
#define VEHICLE				(2)
24
#define DYNAMIC_OBJECT		(3)
25
26
#define MAX_DISTANCE		(1000.0)			
27
//----------------
28
29
//------ Variables
30
31
//Floats
32
new 
33
	Float:px, 
34
	Float:py, 
35
	Float:pz, 
36
	Float:ox,
37
	Float:oy,
38
	Float:oz,
39
	Float:distance = MAX_DISTANCE
40
;
41
42
//----------------
43
44-
GetNearest(playerid, type);
44+
45
stock GetNearest(playerid, type)
46
{
47
	switch(type)
48
	{
49
		case OBJECT:
50
		{
51
			GetNearest_Object(playerid);
52
		}
53
		case PLAYER:
54
		{
55
			GetNearest_Player(playerid);
56
		}
57
		case VEHICLE:
58
		{
59
			GetNearest_Vehicle(playerid);
60
		}
61
		case DYNAMIC_OBJECT:
62
		{
63
			GetNearest_DynObject(playerid);
64
		}
65
	}
66
}
67
68
stock GetNearest_Object(playerid)
69
{
70
	new currentobject = -1;
71
    GetPlayerPos(playerid, px, py, pz);
72
    for(new i = 0; i < MAX_OBJECTS; i++)
73
    {
74
		if(!IsValidObject(i)) continue;
75
        GetDynamicObjectPos(i, ox, oy, oz);
76
        new Float:odist = floatsqroot(
77
            floatpower(floatabs(floatsub(ox, px)), 2.0) +
78
            floatpower(floatabs(floatsub(oy, py)), 2.0) +
79
            floatpower(floatabs(floatsub(oz, pz)), 2.0)
80
        );
81
		if (currentobject == -1)
82
        {
83
            currentobject = i;
84
            distance = odist;
85
        }
86
		else if (odist < distance)
87
        {
88
            currentobject = i;
89
            distance = odist;
90
        }
91
    }
92
    return currentobject;
93
}
94
95
stock GetNearest_Player(playerid)
96
{
97
	new currentplayer = -1;
98
    GetPlayerPos(playerid, px, py, pz);
99
    for(new i = 0; i < MAX_PLAYERS; i++)
100
    {
101
		if(i == playerid) continue;
102
        GetPlayerPos(i, ox, oy, oz);
103
        new Float:odist = floatsqroot(
104
            floatpower(floatabs(floatsub(ox, px)), 2.0) +
105
            floatpower(floatabs(floatsub(oy, py)), 2.0) +
106
            floatpower(floatabs(floatsub(oz, pz)), 2.0)
107
        );
108
		if (currentplayer == -1)
109
        {
110
            currentplayer = i;
111
            distance = odist;
112
        }
113
		else if (odist < distance)
114
        {
115
            currentplayer = i;
116
            distance = odist;
117
        }
118
    }
119
    return currentplayer;
120
}
121
122
stock GetNearest_Vehicle(playerid)
123
{
124
	new currentvehicle = -1;
125
    GetPlayerPos(playerid, px, py, pz);
126
    for(new i = 0; i < MAX_VEHICLES; i++)
127
    {
128
        GetVehiclePos(i, ox, oy, oz);
129
        new Float:odist = floatsqroot(
130
            floatpower(floatabs(floatsub(ox, px)), 2.0) +
131
            floatpower(floatabs(floatsub(oy, py)), 2.0) +
132
            floatpower(floatabs(floatsub(oz, pz)), 2.0)
133
        );
134
		if (currentvehicle == -1)
135
        {
136
            currentvehicle = i;
137
            distance = odist;
138
        }
139
		else if (odist < distance)
140
        {
141
            currentvehicle = i;
142
            distance = odist;
143
        }
144
    }
145
    return currentvehicle;
146
}
147
148
stock GetNearest_DynObject(playerid)
149
{
150
	new currentdynobject = -1;
151
    GetPlayerPos(playerid, px, py, pz);
152
    for(new i = 0; i < CountDynamicObjects(); i++)
153
    {
154
		if(!IsValidDynamicObject(i)) continue;
155
        GetDynamicObjectPos(i, ox, oy, oz);
156
        new Float:odist = floatsqroot(
157
            floatpower(floatabs(floatsub(ox, px)), 2.0) +
158
            floatpower(floatabs(floatsub(oy, py)), 2.0) +
159
            floatpower(floatabs(floatsub(oz, pz)), 2.0)
160
        );
161
		if (currentdynobject == -1)
162
        {
163
            currentdynobject = i;
164
            distance = odist;
165
        }
166
		else if (odist < distance)
167
        {
168
            currentdynobject = i;
169
            distance = odist;
170
        }
171
    }
172
    return currentdynobject;
173
}