Better way to do this?
#1

My OnPlayerSpawn is absolutely huge and repetitive, and I'm almost positive that there's a better (easier) way to do this. If you have any ideas, let me know please.

pawn Код:
public OnPlayerSpawn(playerid)
{
    switch(pTeam[playerid])
    {
        case TEAM_COP:
        {
            switch(curCity)
            {
                case CITY_LS:
                {
                    new
                        r = random( sizeof(CopSpawnsLS) );

                    SetPlayerPos(playerid, CopSpawnsLS[r][0], CopSpawnsLS[r][1], CopSpawnsLS[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsLS[r][3]);
                }
                case CITY_SF:
                {
                    new
                        r = random( sizeof(CopSpawnsSF) );
                       
                    SetPlayerPos(playerid, CopSpawnsSF[r][0], CopSpawnsSF[r][1], CopSpawnsSF[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsSF[r][3]);
                }
                case CITY_LV:
                {
                    new
                        r = random( sizeof(CopSpawnsLV) );

                    SetPlayerPos(playerid, CopSpawnsLV[r][0], CopSpawnsLV[r][1], CopSpawnsLV[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsLV[r][3]);
                }
            }
           
            #if defined USE_ANTI_TEAMKILL
                SetPlayerTeam(playerid, TEAM_COP);
            #endif
        }
        case TEAM_BUM:
        {
            switch(curCity)
            {
                case CITY_LS:
                {
                    new
                        r = random( sizeof(BumSpawnsLS) );

                    SetPlayerPos(playerid, BumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
                    SetPlayerFacingAngle(playerid, BumSpawnsLS[r][3]);
                }
            }
            #if defined USE_ANTI_TEAMKILL
                SetPlayerTeam(playerid, TEAM_BUM);
            #endif
        }
    }
    return 1;
}
Reply
#2

What do you mean? I'm using arrays for the Spawn coordinates.

pawn Код:
new Float: CopSpawnsLV[][] =
{
    // LV
    {2264.8987,2444.5085,10.8203,359.6645},
    {2286.6130,2444.3784,10.8203,0.2678},
    {2286.6145,2457.4800,10.8203,179.4727},
    {2264.9092,2457.9253,10.8203,180.0994},
    {2247.4749,2458.0510,10.8203,177.2560},
    {2247.3513,2475.6191,10.8203,176.9426},
    {2264.9541,2475.2744,10.8203,178.8226},
    {2286.6162,2475.5596,10.8203,177.8829}
};
new Float: CopSpawnsSF[][] =
{
    // SF
    {-1616.4147,685.5795,7.1875,179.0297},
    {-1620.4370,689.7841,7.1875,179.9463},
    {-1620.6316,692.2170,7.1875,89.3921},
    {-1601.0592,735.7070,-4.9063,43.3082},
    {-1586.8950,721.4505,-4.9063,228.7799},
    {-1586.6743,703.1400,-4.9063,314.0074},
    {-1576.1183,702.2892,-4.9063,45.5015},
    {-1575.9702,695.7166,-4.9063,136.3690},
    {-1592.7874,679.3915,-4.9140,50.8283},
    {-1611.3551,679.5194,-4.9063,320.5873}
};
new Float: CopSpawnsLS[][] =
{
    // LS
    {1552.5785,-1675.6554,16.1953,90.0000},
    {1552.7822,-1677.6567,16.1953,73.2857},
    {1552.8923,-1673.5137,16.1953,91.4592},
    {1551.2966,-1673.8447,15.7761,90.8326},
    {1550.4934,-1675.7137,15.4330,90.2059},
    {1550.0320,-1677.5917,15.2408,90.2059},
    {1577.8595,-1636.6252,13.5556,90.5508},
    {1588.5712,-1682.7086,6.2252,317.3594},
    {1573.5282,-1697.7344,6.2188,135.3110}
};
Like so.
Reply
#3

You could put the code you currently have inside a stock or a callback, it doesn't really matter, as long it does get called. (Not sure if this is what you meant, but for me, to indent a code, I place it inside a callback or a stock to make it looks clean.

pawn Код:
public OnPlayerSpawn(playerid)
{
    SetPlayerTeamPos(playerid);
    return true;
}

stock SetPlayerTeamPos(playerid)
{
    switch(pTeam[playerid])
    {
        case TEAM_COP:
        {
            switch(curCity)
            {
                case CITY_LS:
                {
                    new
                        r = random( sizeof(CopSpawnsLS) );

                    SetPlayerPos(playerid, CopSpawnsLS[r][0], CopSpawnsLS[r][1], CopSpawnsLS[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsLS[r][3]);
                }
                case CITY_SF:
                {
                    new
                        r = random( sizeof(CopSpawnsSF) );
                       
                    SetPlayerPos(playerid, CopSpawnsSF[r][0], CopSpawnsSF[r][1], CopSpawnsSF[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsSF[r][3]);
                }
                case CITY_LV:
                {
                    new
                        r = random( sizeof(CopSpawnsLV) );

                    SetPlayerPos(playerid, CopSpawnsLV[r][0], CopSpawnsLV[r][1], CopSpawnsLV[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsLV[r][3]);
                }
            }
           
            #if defined USE_ANTI_TEAMKILL
                SetPlayerTeam(playerid, TEAM_COP);
            #endif
        }
        case TEAM_BUM:
        {
            switch(curCity)
            {
                case CITY_LS:
                {
                    new
                        r = random( sizeof(BumSpawnsLS) );

                    SetPlayerPos(playerid, BumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
                    SetPlayerFacingAngle(playerid, BumSpawnsLS[r][3]);
                }
            }
            #if defined USE_ANTI_TEAMKILL
                SetPlayerTeam(playerid, TEAM_BUM);
            #endif
        }
    }
    return true;
}
Reply
#4

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
You could put the code you currently have inside a stock or a callback, it doesn't really matter, as long it does get called.

pawn Код:
public OnPlayerSpawn(playerid)
{
    SetPlayerTeamPos(playerid);
    return true;
}

stock SetPlayerTeamPos(playerid)
{
    switch(pTeam[playerid])
    {
        case TEAM_COP:
        {
            switch(curCity)
            {
                case CITY_LS:
                {
                    new
                        r = random( sizeof(CopSpawnsLS) );

                    SetPlayerPos(playerid, CopSpawnsLS[r][0], CopSpawnsLS[r][1], CopSpawnsLS[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsLS[r][3]);
                }
                case CITY_SF:
                {
                    new
                        r = random( sizeof(CopSpawnsSF) );
                       
                    SetPlayerPos(playerid, CopSpawnsSF[r][0], CopSpawnsSF[r][1], CopSpawnsSF[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsSF[r][3]);
                }
                case CITY_LV:
                {
                    new
                        r = random( sizeof(CopSpawnsLV) );

                    SetPlayerPos(playerid, CopSpawnsLV[r][0], CopSpawnsLV[r][1], CopSpawnsLV[r][2]);
                    SetPlayerFacingAngle(playerid, CopSpawnsLV[r][3]);
                }
            }
           
            #if defined USE_ANTI_TEAMKILL
                SetPlayerTeam(playerid, TEAM_COP);
            #endif
        }
        case TEAM_BUM:
        {
            switch(curCity)
            {
                case CITY_LS:
                {
                    new
                        r = random( sizeof(BumSpawnsLS) );

                    SetPlayerPos(playerid, BumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
                    SetPlayerFacingAngle(playerid, BumSpawnsLS[r][3]);
                }
            }
            #if defined USE_ANTI_TEAMKILL
                SetPlayerTeam(playerid, TEAM_BUM);
            #endif
        }
    }
    return true;
}
You know, that's not really a bad idea. Still curious as to what ****** said, though.
Reply
#5

//EDIT2:
Quote:
Originally Posted by 2KY
Посмотреть сообщение
Sorry, I should have made it clear that I was working on "TEAM_BUM" and that they're going to have LS, LV, and SF coordinates aswell.
ah well, okey


------

you don't need a switch in your TEAM_BUM case, as from the code you supplyed, there's only one option. CITY_LS

so instead of:
PHP код:
 case TEAM_BUM:
        {
            switch(
curCity)
            {
                case 
CITY_LS:
                {
                    new
                        
randomsizeof(BumSpawnsLS) );
                    
SetPlayerPos(playeridBumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
                    
SetPlayerFacingAngle(playeridBumSpawnsLS[r][3]);
                }
            }
            
#if defined USE_ANTI_TEAMKILL
                
SetPlayerTeam(playeridTEAM_BUM);
            
#endif
        

you could just write

PHP код:
     case TEAM_BUM:
    {
        new 
randomsizeof(BumSpawnsLS) );
        
SetPlayerPos(playeridBumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
        
SetPlayerFacingAngle(playeridBumSpawnsLS[r][3]);
        
#if defined USE_ANTI_TEAMKILL
            
SetPlayerTeam(playeridTEAM_BUM);
        
#endif
    

//EDIT oh found something else,
before your main switch you could do
PHP код:
new r
and then just fill it in every case like
PHP код:
randomsizeof(BumSpawnsLS) );
//and so on... 
this doesen't really "boosts up" your code but...
well, in general, that code is good (my oppinion)
Reply
#6

Quote:
Originally Posted by CutX
Посмотреть сообщение
you don't need a switch in your TEAM_BUM case, as from the code you supplyed, there's only one option. CITY_LS

so instead of:
PHP код:
 case TEAM_BUM:
        {
            switch(
curCity)
            {
                case 
CITY_LS:
                {
                    new
                        
randomsizeof(BumSpawnsLS) );
                    
SetPlayerPos(playeridBumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
                    
SetPlayerFacingAngle(playeridBumSpawnsLS[r][3]);
                }
            }
            
#if defined USE_ANTI_TEAMKILL
                
SetPlayerTeam(playeridTEAM_BUM);
            
#endif
        

you could just write

PHP код:
     case TEAM_BUM:
    {
        new 
randomsizeof(BumSpawnsLS) );
        
SetPlayerPos(playeridBumSpawnsLS[r][0], BumSpawnsLS[r][1], BumSpawnsLS[r][2]);
        
SetPlayerFacingAngle(playeridBumSpawnsLS[r][3]);
        
#if defined USE_ANTI_TEAMKILL
            
SetPlayerTeam(playeridTEAM_BUM);
        
#endif
    

Sorry, I should have made it clear that I was working on "TEAM_BUM" and that they're going to have LS, LV, and SF coordinates aswell.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)