Skip to content

Commit

Permalink
feat: first pass at vehicles and actors
Browse files Browse the repository at this point in the history
  • Loading branch information
karimcambridge committed Apr 12, 2024
1 parent 2bab0c1 commit e6d9b7c
Show file tree
Hide file tree
Showing 3 changed files with 502 additions and 93 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies/
# Dependency versions lockfile
pawn.lock


#
# Server/gamemode related files
#
Expand Down
154 changes: 151 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# SA-MP-foreach

[![Join the chat at https://gitter.im/Kar2k/SAMP-foreach](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Kar2k/SAMP-foreach?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

foreach standalone include (non y_iterate version)
foreach standalone include (non y_iterate version).

Y_Less dropped support for this version of foreach.

Expand All @@ -19,3 +17,153 @@ Then simply include into code:
```pawn
#include <foreach>
```

## Option with more iterators

https://github.com/Open-GTO/foreach

## How to use
#### Gives $100 to all connected players
```Pawn
foreach (new playerid : Player) {
GivePlayerMoney(playerid, 100);
}
```

#### Sets 130 and 170 colors to all vehicles with model 400
```Pawn
foreach (new vehicleid : Vehicle) {
if (GetVehicleModel(vehicleid) == 400) {
ChangeVehicleColor(vehicleid, 130, 170);
}
}
```

#### Remove all existed vehicles and actors
For removing `vehicles` or `actors` in foreach loop you should use `Safe` functions:
```Pawn
foreach (new vehicleid : Vehicle) {
DestroyVehicleSafe(vehicleid);
}
foreach (new actorid : Actor) {
DestroyActorSafe(actorid);
}
```

#### Custom iterators
Adds two iterators with players in team 1 and 2.
```Pawn
new
Iterator:Team1<MAX_PLAYERS>,
Iterator:Team2<MAX_PLAYERS>;
stock Team_SetPlayerTeam(playerid, teamid)
{
switch (GetPlayerTeam(playerid)) {
case 1: {
Iter_Remove(Team1, playerid);
}
case 2: {
Iter_Remove(Team2, playerid);
}
}
switch (teamid) {
case 1: {
Iter_Add(Team1, playerid);
}
case 2: {
Iter_Add(Team2, playerid);
}
}
return SetPlayerTeam(playerid, teamid);
}
#if defined _ALS_SetPlayerTeam
#undef SetPlayerTeam
#else
#define _ALS_SetPlayerTeam
#endif
#define SetPlayerTeam Team_SetPlayerTeam
```
It can be used like this:
```Pawn
foreach (new i : Team1) {
GivePlayerMoney(i, 1000);
}
foreach (new i : Team2) {
SetPlayerHealth(i, 0.0);
}
```
This script gives $1000 to all players from team with id 1, and kills players from team with id 2.

## Custom array of iterators
Adds array of iterators with players in teams:

```Pawn
#define MAX_TEAMS 255
new
Iterator:Team[MAX_TEAMS]<MAX_PLAYERS>;
public OnGameModeInit()
{
Iter_Init(Team);
#if defined Team_OnGameModeInit
return Team_OnGameModeInit();
#else
return 1;
#endif
}
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit Team_OnGameModeInit
#if defined Team_OnGameModeInit
forward Team_OnGameModeInit();
#endif
stock Team_SetPlayerTeam(playerid, teamid)
{
new current_team = GetPlayerTeam(playerid);
if (current_team != NO_TEAM) {
Iter_Remove(Team[current_team], playerid);
}
if (teamid != NO_TEAM) {
Iter_Add(Team[teamid], playerid);
}
return SetPlayerTeam(playerid, teamid);
}
#if defined _ALS_SetPlayerTeam
#undef SetPlayerTeam
#else
#define _ALS_SetPlayerTeam
#endif
#define SetPlayerTeam Team_SetPlayerTeam
```
It can be used like this:
```Pawn
new scores[MAX_TEAMS];
for (new teamid; teamid < MAX_TEAMS; teamid++) {
foreach (new playerid : Team[teamid]) {
scores[teamid] += GetPlayerScore(playerid);
}
}
for (new teamid; teamid < MAX_TEAMS; teamid++) {
printf("Team %d: %d", teamid, scores[teamid]);
}
```
This script sums up score from each team and prints it.
Loading

0 comments on commit e6d9b7c

Please sign in to comment.