AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Sorting spawning points (https://forums.alliedmods.net/showthread.php?t=313136)

Xalus 12-29-2018 13:23

Sorting spawning points
 
So, a friend of me made me a function in the past in 'Python'..
Can someone make for me a 'pawn' version of it?

So the point is, there are 4 sides, each side got 3 spawns.
This function put's in a variable, all 3 spawns for each side.

Code:

def group_spawnpoints(per_side):
        spawnpoints = findentities(None, 'classname', 'info_player_deathmatch')
        sorted_x = sorted(spawnpoints, key=lambda x: x.origin[0])
        sorted_y = sorted(spawnpoints, key=lambda x: x.origin[1])
        return [sorted_x[0:per_side], sorted_x[-per_side:], sorted_y[0:per_side], sorted_y[-per_side:]]


Thanks.

klippy 01-15-2019 07:41

Re: Sorting spawning points
 
Do you still need this? I'm willing to give it a shot, I just forgot about the topic.

Xalus 01-18-2019 13:24

Re: Sorting spawning points
 
Well, I made my version of it.
If you can show me a better way, I'm open for it!

PHP Code:

// [Team] Spawns
//                Group all spawns for each team
team_spawns()
{
    
// Get the positions of all spawn-spots
    
new Float:spawn_origins[ (GAME_TEAMPLAYERS GAME_TEAMS)+][3],
        
spawn_entity,
        
spawn_index

    
while( (spawn_entity find_ent_by_class(spawn_entity"info_player_deathmatch")) )
    {
        
pev(spawn_entitypev_originspawn_origins[spawn_index])
        
spawn_origins[spawn_index][2] -= 30.0
        spawn_index 
+= 1
    
}

    
log_amx("[CRASH BALL-DEBUG] Found %i spawningpoints (%.2f %.2f)"spawn_indexspawn_origins[0][0], spawn_origins[0][1])

    
// Sort all the spawns by x & y
    
new Float:sorted_x[ (GAME_TEAMPLAYERS GAME_TEAMS)+][3],
        
Float:sorted_y[ (GAME_TEAMPLAYERS GAME_TEAMS)+][3],
        
swap_typeswap_index

    
//xs_vec_copy(spawn_origins[0], sorted_x[0])
    //xs_vec_copy(spawn_origins[0], sorted_y[0])

    
for(new 0spawn_indexi++)
    {
        
swap_type 0
        swap_index 
i
        xs_vec_copy
(spawn_origins[i], sorted_x[i])
        
xs_vec_copy(spawn_origins[i], sorted_y[i])

        if(!
i)
            continue

        
reswap:

        if(
sorted_x[i-1][0] > sorted_x[i][0])
        {
            
swap_origin_position(sorted_x[i-1], sorted_x[i])
            
swap_type 1
        
}
        if(
sorted_y[i-1][1] > sorted_y[i][1])
        {
            
swap_origin_position(sorted_y[i-1], sorted_y[i])
            
swap_type 1
        
}

        
// If swaped, check if it can go even lower..
        
if(swap_type 0
            
&& 1)
        {
            
swap_type = -1
            i 
-= 1
            
goto reswap
        
}
        
// If swaped all it could
        
if(swap_type == -1
            
|| <= 1)
        {
            
swap_index
        
}
    }

    
// Now we have a list sorted by x and y
    
spawn_index -= 1
    
for(new 0GAME_TEAMPLAYERSi++)
    {
        
// #Team 1
        
xs_vec_copy(sorted_x[i], g_team_spawns[0][i])

        
// #Team 2
        
xs_vec_copy(sorted_x[ (spawn_index i) ], g_team_spawns[1][i])

        
// #Team 3
        
xs_vec_copy(sorted_y[i], g_team_spawns[2][i])

        
// #Team 4
        
xs_vec_copy(sorted_y[ (spawn_index i) ], g_team_spawns[3][i])
    }



klippy 01-23-2019 07:59

Re: Sorting spawning points
 
Untested. This is pretty much a direct translation of the original Python code. Requires 1.8.3 (1.9) though. If you need it for 1.8.2, you can replace ArrayClone(x) with CellArraySlice(x, 0, ArraySize(x)) and use ArraySort instead of ArraySortEx (check the include file to see the differences, you'll need to change the callback code a bit). It will function the same but 1.9 will perform better with new natives (although not even measurable for a small amount of spawn points probably), and the code is tidier as a bonus.

I guess this is for one of your mini-games, where you would split 4 teams into 4 sides on the map?

PHP Code:

#include <amxmodx>
#include <fakemeta>

#pragma semicolon 1

////////// The usage //////////
new Array:g_teamSpawnPoints[4];

public 
plugin_init() {
    
g_teamSpawnPoints GroupSpawnPoints(4);
}

public 
plugin_end() {
    for(new 
0sizeof g_teamSpawnPoints; ++i) {
        
ArrayDestroy(g_teamSpawnPoints[i]);
    }
}

////////// The logic itself //////////
@SortEntitiesByOrigin(Array:entitiesent1ent2, const data[], dataSize) {
    new const 
axis data[0];
    new 
Float:origin1[3]; pev(ent1pev_originorigin1);
    new 
Float:origin2[3]; pev(ent2pev_originorigin2);
    return 
floatcmp(origin1[axis], origin2[axis]);
}

Array:
FindEntities(const byProperty[], const value[]) {
    new const Array:
entities ArrayCreate();
    new 
entId = -1;
    while((
entId engfunc(EngFunc_FindEntityByStringentIdbyPropertyvalue)) > 0) {
        
ArrayPushCell(entitiesentId);
    }
    
    return 
entities;
}

Array:
CellArraySlice(Array:instartend) {
    new const Array:
out ArrayCreate();
    for(new 
startend; ++i) {
        
ArrayPushCell(outArrayGetCell(ini));
    }
    
    return 
out;
}

Array:
GroupSpawnPoints(perSide) {
    new Array:
sortedXSpawns FindEntities("classname""info_player_deathmatch");
    new Array:
sortedYSpawns ArrayClone(sortedXSpawns);
    
ArraySortEx(sortedXSpawns"@SortEntitiesByOrigin", { }, 1);
    
ArraySortEx(sortedYSpawns"@SortEntitiesByOrigin", { }, 1);
    
    new const 
size ArraySize(sortedXSpawns);
    new Array:
teamSpawnPoints[4];
    
teamSpawnPoints[0] = CellArraySlice(sortedXSpawns0perSide);
    
teamSpawnPoints[1] = CellArraySlice(sortedXSpawnssize perSidesize);
    
teamSpawnPoints[2] = CellArraySlice(sortedYSpawns0perSide);
    
teamSpawnPoints[3] = CellArraySlice(sortedYSpawnssize perSidesize);
    
    
ArrayDestroy(sortedXSpawns);
    
ArrayDestroy(sortedYSpawns);
    return 
teamSpawnPoints;



Xalus 01-23-2019 11:48

Re: Sorting spawning points
 
Quote:

Originally Posted by KliPPy (Post 2636175)
Untested. This is pretty much a direct translation of the original Python code. Requires 1.8.3 (1.9) though. If you need it for 1.8.2, you can replace ArrayClone(x) with CellArraySlice(x, 0, ArraySize(x)) and use ArraySort instead of ArraySortEx (check the include file to see the differences, you'll need to change the callback code a bit). It will function the same but 1.9 will perform better with new natives (although not even measurable for a small amount of spawn points probably), and the code is tidier as a bonus.

I guess this is for one of your mini-games, where you would split 4 teams into 4 sides on the map?

PHP Code:

#include <amxmodx>
#include <fakemeta>

#pragma semicolon 1

////////// The usage //////////
new Array:g_teamSpawnPoints[4];

public 
plugin_init() {
    
g_teamSpawnPoints GroupSpawnPoints(4);
}

public 
plugin_end() {
    for(new 
0sizeof g_teamSpawnPoints; ++i) {
        
ArrayDestroy(g_teamSpawnPoints[i]);
    }
}

////////// The logic itself //////////
@SortEntitiesByOrigin(Array:entitiesent1ent2, const data[], dataSize) {
    new const 
axis data[0];
    new 
Float:origin1[3]; pev(ent1pev_originorigin1);
    new 
Float:origin2[3]; pev(ent2pev_originorigin2);
    return 
floatcmp(origin1[axis], origin2[axis]);
}

Array:
FindEntities(const byProperty[], const value[]) {
    new const Array:
entities ArrayCreate();
    new 
entId = -1;
    while((
entId engfunc(EngFunc_FindEntityByStringentIdbyPropertyvalue)) > 0) {
        
ArrayPushCell(entitiesentId);
    }
    
    return 
entities;
}

Array:
CellArraySlice(Array:instartend) {
    new const Array:
out ArrayCreate();
    for(new 
startend; ++i) {
        
ArrayPushCell(outArrayGetCell(ini));
    }
    
    return 
out;
}

Array:
GroupSpawnPoints(perSide) {
    new Array:
sortedXSpawns FindEntities("classname""info_player_deathmatch");
    new Array:
sortedYSpawns ArrayClone(sortedXSpawns);
    
ArraySortEx(sortedXSpawns"@SortEntitiesByOrigin", { }, 1);
    
ArraySortEx(sortedYSpawns"@SortEntitiesByOrigin", { }, 1);
    
    new const 
size ArraySize(sortedXSpawns);
    new Array:
teamSpawnPoints[4];
    
teamSpawnPoints[0] = CellArraySlice(sortedXSpawns0perSide);
    
teamSpawnPoints[1] = CellArraySlice(sortedXSpawnssize perSidesize);
    
teamSpawnPoints[2] = CellArraySlice(sortedYSpawns0perSide);
    
teamSpawnPoints[3] = CellArraySlice(sortedYSpawnssize perSidesize);
    
    
ArrayDestroy(sortedXSpawns);
    
ArrayDestroy(sortedYSpawns);
    return 
teamSpawnPoints;



Yeah exactly, doing a remake of 'Crashball'-Minimod.
I'll test it later, thanks!


All times are GMT -4. The time now is 07:36.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.