Перебор машин по принципу foreach
Бешеный перебор машин на основе foreach от Траккера(Триггера)
Переделал под машины: Caypen
В самый верх мода:
PHP код:
new forveh_vehicles[MAX_VEHICLES];
new forveh_count = 0;
#if !defined IsValidVehicle
native IsValidVehicle(vehicleid);
#endif
#define forveh(%0) for(new ouf, %0=forveh_vehicles[ouf]; ouf<forveh_count; %0=forveh_vehicles[++ouf])
stock Forveh_CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay)
{
new vehid = CreateVehicle(modelid, x, y, z,angle, color1, color2, respawn_delay);
forveh_vehicles[forveh_count++]=vehid;
return vehid;
}
#if defined _ALS_CreateVehicle
#undef CreateVehicle
#else
#define _ALS_CreateVehicle
#endif
#define CreateVehicle Forveh_CreateVehicle
stock Forveh_AddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2)
{
new vehid = AddStaticVehicle(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2);
forveh_vehicles[forveh_count++]=vehid;
return vehid;
}
#if defined _ALS_AddStaticVehicle
#undef AddStaticVehicle
#else
#define _ALS_AddStaticVehicle
#endif
#define AddStaticVehicle Forveh_AddStaticVehicle
stock Forveh_AddStaticVehicleEx(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2, respawn_delay)
{
new vehid = AddStaticVehicleEx(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2,respawn_delay);
forveh_vehicles[forveh_count++]=vehid;
return vehid;
}
#if defined _ALS_AddStaticVehicleEx
#undef AddStaticVehicleEx
#else
#define _ALS_AddStaticVehicleEx
#endif
#define AddStaticVehicleEx Forveh_AddStaticVehicleEx
stock Forveh_DestroyVehicle(vehicleid)
{
if(!IsValidVehicle(vehicleid)) return 0;
for(new i=0;i<forveh_count;i++)
{
if(forveh_vehicles[i]==vehicleid)
{
forveh_vehicles[i]=forveh_vehicles[--forveh_count];
break;
}
}
return DestroyVehicle(vehicleid);
}
#if defined _ALS_DestroyVehicle
#undef DestroyVehicle
#else
#define _ALS_DestroyVehicle
#endif
#define DestroyVehicle Forveh_DestroyVehicle
Перебираем машины вот так:
PHP код:
forveh(c)
{
Действие(c);
}
Таким образом мы перебираем только те машины которые были созданы а те которые удалены пропускаем даже не интерпретируя.
За основу взят метод Траккера для игроков.
Тесты:
Цитата:
Сообщение от
Salvador
тесты?
Щас будут.
- - - Добавлено - - -
PHP код:
[09:47:37] Тестируем дедовский метод перебора машин
[09:47:37] 125 машин создали
[09:47:37] 50-74 ID машин уничтожены
[09:47:37] Стартуем глобальный цикл 1000000 раз
[09:58:05] тест дедовского метода перебора машин ЗАВЕРШЕН. время: 628328 ms.
[09:58:05]
[09:58:05] Тестируем forveh машин
[09:58:05] 125 машин создали
[09:58:05] 50-74 ID машин уничтожены
[09:58:05] Стартуем глобальный цикл 1000000 раз
[09:58:29] тест forveh машин ЗАВЕРШЕН. время: 23553 ms.
Почти в 27 раз быстрее.
Был создан цикл на 1000000 раз в нутри которого был цикл машин через forveh а в первом же тесте через дедовский метод.
код теста (Идея такого теста тоже спизжена у Траккера :D):
Открыть/закрыть
PHP код:
#include <a_samp>
new forveh_vehicles[MAX_VEHICLES];
new forveh_count = 0;
#if !defined IsValidVehicle
native IsValidVehicle(vehicleid);
#endif
#define forveh(%0) for(new ouf, %0=forveh_vehicles[ouf]; ouf<forveh_count; %0=forveh_vehicles[++ouf])
stock Forveh_CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay)
{
new vehid = CreateVehicle(modelid, x, y, z,angle, color1, color2, respawn_delay);
forveh_vehicles[forveh_count++]=vehid;
return vehid;
}
#if defined _ALS_CreateVehicle
#undef CreateVehicle
#else
#define _ALS_CreateVehicle
#endif
#define CreateVehicle Forveh_CreateVehicle
stock Forveh_AddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2)
{
new vehid = AddStaticVehicle(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2);
forveh_vehicles[forveh_count++]=vehid;
return vehid;
}
#if defined _ALS_AddStaticVehicle
#undef AddStaticVehicle
#else
#define _ALS_AddStaticVehicle
#endif
#define AddStaticVehicle Forveh_AddStaticVehicle
stock Forveh_AddStaticVehicleEx(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2, respawn_delay)
{
new vehid = Forveh_AddStaticVehicleEx(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2,respawn_delay);
forveh_vehicles[forveh_count++]=vehid;
return vehid;
}
#if defined _ALS_AddStaticVehicleEx
#undef AddStaticVehicleEx
#else
#define _ALS_AddStaticVehicleEx
#endif
#define AddStaticVehicleEx Forveh_AddStaticVehicleEx
stock Forveh_DestroyVehicle(vehicleid)
{
if(!IsValidVehicle(vehicleid)) return 0;
for(new i=0;i<forveh_count;i++)
{
if(forveh_vehicles[i]==vehicleid)
{
forveh_vehicles[i]=forveh_vehicles[--forveh_count];
break;
}
}
return DestroyVehicle(vehicleid);
}
#if defined _ALS_DestroyVehicle
#undef DestroyVehicle
#else
#define _ALS_DestroyVehicle
#endif
#define DestroyVehicle Forveh_DestroyVehicle
stock test()
{
new time,vehid[MAX_VEHICLES];
printf("Тестируем дедовский метод перебора машин");
for(new i=0;i<125;i++)
{
vehid[i] = CreateVehicle(520, 2109.17630+random(50), 1503.0453, 32.2887, 82.2873, 0, 1, 60);
}
printf("125 машин создали");
for(new i=50;i<75;i++)
{
DestroyVehicle(vehid[i]);
}
printf("50-74 ID машин уничтожены");
printf("Стартуем глобальный цикл 1000000 раз");
time=GetTickCount();
for(new i=0;i<1000000;i++)
for(new x=0;x<MAX_VEHICLES;x++)
{
if(IsValidVehicle(x)){}
continue;
//#pragma unused x
}
printf("тест дедовского метода перебора машин ЗАВЕРШЕН. время: %d ms.",GetTickCount()-time);
//
print("\n");
printf("Тестируем forveh машин");
for(new i=0;i<125;i++)
forveh_vehicles[forveh_count++]=i;
printf("125 машин создали");
for(new i=50;i<75;i++)
{
for(new j=0;j<forveh_count;j++)
{
if(forveh_vehicles[j]==i)
{
forveh_vehicles[j]=forveh_vehicles[--forveh_count];
forveh_vehicles[forveh_count]=-1;
break;
}
}
}
printf("50-74 ID машин уничтожены");
printf("Стартуем глобальный цикл 1000000 раз");
time=GetTickCount();
for(new i=0;i<1000000;i++)
forveh(x)
{
continue;
#pragma unused x
}
printf("тест forveh машин ЗАВЕРШЕН. время: %d ms.",GetTickCount()-time);
//
print("\n");
}
Автор кода для машин: Caypen
Критика и корректирование: Daniel_Cortez