Skip to content

Commit

Permalink
Fix potential spawning issues & use World Entity enumerator functions…
Browse files Browse the repository at this point in the history
…; v1.2.1
  • Loading branch information
pongo1231 committed Jun 26, 2018
1 parent 4b81c3b commit 93b5e2f
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 111 deletions.
2 changes: 1 addition & 1 deletion CorruptSnail/CorruptSnail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="CPlayer\PlayerSpawner.cs" />
<Compile Include="CPlayer\Safezones.cs" />
<Compile Include="Util\EntityUtil.cs" />
<Compile Include="Spawners\Despawner.cs" />
<Compile Include="Spawners\Ambient\AmbientWarNoiseSpawner.cs" />
<Compile Include="Spawners\Ambient\FlyingByPlaneSpawner.cs" />
Expand All @@ -60,7 +61,6 @@
<Compile Include="Spawners\ObjectSpawner.cs" />
<Compile Include="Atmosphere.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util\EntityEnum.cs" />
<Compile Include="Util\Utils.cs" />
<Compile Include="CVehicle\VehFuelHandler.cs" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions CorruptSnail/Spawners/Ambient/FlyingByPlaneSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ private async void SpawnRandomFlyingByPlane()

if (!Utils.IsPosShitSpawn(Players, spawnPos, SpawnerHost.SPAWN_DESPAWN_DISTANCE))
{
Vehicle plane = await World.CreateVehicle(PLANE_LIST[Utils.GetRandomInt(PLANE_LIST.Length)], spawnPos,
Vehicle plane = await EntityUtil.CreateVehicle(PLANE_LIST[Utils.GetRandomInt(PLANE_LIST.Length)], spawnPos,
Utils.GetRandomInt(360));
plane.IsInvincible = true;
plane.IsEngineRunning = true;

Ped pilot = await World.CreatePed(PedHash.Pilot01SMM, spawnPos);
Ped pilot = await EntityUtil.CreatePed(PedHash.Pilot01SMM, PedType.PED_TYPE_MISSION, spawnPos);
pilot.IsInvincible = true;
pilot.SetIntoVehicle(plane, VehicleSeat.Driver);
pilot.AlwaysKeepTask = true;
Expand Down
6 changes: 3 additions & 3 deletions CorruptSnail/Spawners/Despawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ private async Task OnTick()
{
await Delay(SpawnerHost.SPAWN_TICK_RATE);

foreach (Prop prop in EntityEnum.GetProps())
foreach (Prop prop in World.GetAllProps())
if (prop.HasDecor(SpawnerHost.SPAWN_DESPAWN_DECOR))
prop.MarkAsNoLongerNeeded();

foreach (Ped ped in EntityEnum.GetPeds())
foreach (Ped ped in World.GetAllPeds())
if (ped.HasDecor(SpawnerHost.SPAWN_DESPAWN_DECOR))
ped.MarkAsNoLongerNeeded();

foreach (Vehicle veh in EntityEnum.GetVehicles())
foreach (Vehicle veh in World.GetAllVehicles())
if (veh.HasDecor(SpawnerHost.SPAWN_DESPAWN_DECOR))
veh.MarkAsNoLongerNeeded();
}
Expand Down
6 changes: 3 additions & 3 deletions CorruptSnail/Spawners/Events/ArmyHeliSquadSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ private async void SpawnRandomArmyHeli()

if (!Utils.IsPosShitSpawn(Players, spawnPos, SpawnerHost.SPAWN_DESPAWN_DISTANCE))
{
Vehicle heli = await World.CreateVehicle(HELI_LIST[Utils.GetRandomInt(HELI_LIST.Length)], spawnPos,
Vehicle heli = await EntityUtil.CreateVehicle(HELI_LIST[Utils.GetRandomInt(HELI_LIST.Length)], spawnPos,
Utils.GetRandomInt(360));
heli.IsInvincible = true;
heli.IsEngineRunning = true;

Ped pilot = await World.CreatePed(PedHash.Blackops01SMY, spawnPos);
Ped pilot = await EntityUtil.CreatePed(PedHash.Blackops01SMY, PedType.PED_TYPE_MISSION, spawnPos);
pilot.IsInvincible = true;
pilot.RelationshipGroup = ArmyHeliSquadGroup;
pilot.SetIntoVehicle(heli, VehicleSeat.Driver);
Expand Down Expand Up @@ -105,7 +105,7 @@ private async void SpawnRandomArmyHeli()

private void HandleArmyHeliSquads()
{
foreach (Ped ped in EntityEnum.GetPeds())
foreach (Ped ped in World.GetAllPeds())
if (ped.HasDecor(ARMYHELI_DECOR))
ped.RelationshipGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Like, true);
}
Expand Down
4 changes: 2 additions & 2 deletions CorruptSnail/Spawners/Events/RebelSquadSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private async void SpawnRandomRebelSquad()
Ped[] rebels = new Ped[rebelAmount];
for (int i = 0; i < rebelAmount; i++)
{
Ped rebel = await World.CreatePed(PedHash.Hillbilly01AMM, spawnPos);
Ped rebel = await EntityUtil.CreatePed(PedHash.Hillbilly01AMM, PedType.PED_TYPE_MISSION, spawnPos);
API.SetPedCombatRange(rebel.Handle, 2);
API.SetPedHearingRange(rebel.Handle, float.MaxValue);
rebel.RelationshipGroup = RebelSquadGroup;
Expand All @@ -89,7 +89,7 @@ private async void SpawnRandomRebelSquad()

private void HandleRebelSquads()
{
foreach (Ped ped in EntityEnum.GetPeds())
foreach (Ped ped in World.GetAllPeds())
if (ped.HasDecor(REBELSQUAD_DECOR))
ped.RelationshipGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Hate, true);
}
Expand Down
3 changes: 1 addition & 2 deletions CorruptSnail/Spawners/ObjectSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ private async void SpawnRandomObstacle()

if (!Utils.IsPosShitSpawn(Players, spawnPos, SpawnerHost.SPAWN_MIN_DISTANCE))
{
Prop obstacle = await World
.CreateProp(API.GetHashKey(OBSTACLE_LIST[Utils.GetRandomInt(OBSTACLE_LIST.Length)]), spawnPos, false, true);
Prop obstacle = await EntityUtil.CreateProp(API.GetHashKey(OBSTACLE_LIST[Utils.GetRandomInt(OBSTACLE_LIST.Length)]), spawnPos, false, true);
obstacle.IsPositionFrozen = true;

obstacles.Add(obstacle);
Expand Down
3 changes: 1 addition & 2 deletions CorruptSnail/Spawners/VehicleSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ private async void SpawnRandomVeh()

if (!Utils.IsPosShitSpawn(Players, spawnPos, SpawnerHost.SPAWN_MIN_DISTANCE))
{
Vehicle veh = await World
.CreateVehicle(VEH_LIST[Utils.GetRandomInt(VEH_LIST.Length)], spawnPos);
Vehicle veh = await EntityUtil.CreateVehicle(VEH_LIST[Utils.GetRandomInt(VEH_LIST.Length)], spawnPos);
veh.Health = Utils.GetRandomInt(1000);
veh.EngineHealth = Utils.GetRandomInt(1000);
veh.PetrolTankHealth = Utils.GetRandomInt(1000);
Expand Down
5 changes: 2 additions & 3 deletions CorruptSnail/Spawners/ZombieSpawner.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;
using CorruptSnail.Util;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand Down Expand Up @@ -51,7 +50,7 @@ private async void SpawnRandomZombie()

if (!Utils.IsPosShitSpawn(Players, spawnPos, SpawnerHost.SPAWN_MIN_DISTANCE))
{
Ped zombie = await World.CreatePed(PedHash.Zombie01, spawnPos);
Ped zombie = await EntityUtil.CreatePed(PedHash.Zombie01, PedType.PED_TYPE_MISSION, spawnPos);
int zombieHandle = zombie.Handle;
API.SetPedCombatRange(zombieHandle, 2);
API.SetPedHearingRange(zombieHandle, float.MaxValue);
Expand Down Expand Up @@ -80,7 +79,7 @@ private async void SpawnRandomZombie()

private void HandleZombies()
{
foreach (Ped ped in EntityEnum.GetPeds())
foreach (Ped ped in World.GetAllPeds())
{
if (ped.HasDecor(ZOMBIE_DECOR))
{
Expand Down
93 changes: 0 additions & 93 deletions CorruptSnail/Util/EntityEnum.cs

This file was deleted.

105 changes: 105 additions & 0 deletions CorruptSnail/Util/EntityUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;
using System.Threading.Tasks;

namespace CorruptSnail.Util
{
public enum PedType
{
PED_TYPE_PLAYER_0,
PED_TYPE_PLAYER_1,
PED_TYPE_NETWORK_PLAYER,
PED_TYPE_PLAYER_2,
PED_TYPE_CIVMALE,
PED_TYPE_CIVFEMALE,
PED_TYPE_COP,
PED_TYPE_GANG_ALBANIAN,
PED_TYPE_GANG_BIKER_1,
PED_TYPE_GANG_BIKER_2,
PED_TYPE_GANG_ITALIAN,
PED_TYPE_GANG_RUSSIAN,
PED_TYPE_GANG_RUSSIAN_2,
PED_TYPE_GANG_IRISH,
PED_TYPE_GANG_JAMAICAN,
PED_TYPE_GANG_AFRICAN_AMERICAN,
PED_TYPE_GANG_KOREAN,
PED_TYPE_GANG_CHINESE_JAPANESE,
PED_TYPE_GANG_PUERTO_RICAN,
PED_TYPE_DEALER,
PED_TYPE_MEDIC,
PED_TYPE_FIREMAN,
PED_TYPE_CRIMINAL,
PED_TYPE_BUM,
PED_TYPE_PROSTITUTE,
PED_TYPE_SPECIAL,
PED_TYPE_MISSION,
PED_TYPE_SWAT,
PED_TYPE_ANIMAL,
PED_TYPE_ARMY
}

public static class EntityUtil
{
public static async Task<Ped> CreatePed(Model model, PedType pedType, Vector3 pos, float heading = 0f, bool networked = true)
{
if (API.IsModelAPed((uint)model.Hash))
{
model.Request();
while (!model.IsLoaded)
await BaseScript.Delay(1);
Ped ped = new Ped(API.CreatePed((int)pedType, (uint)model.Hash, pos.X, pos.Y, pos.Z, heading, networked, false));
model.MarkAsNoLongerNeeded();
return ped;
}
return null;
}

public static async Task<Vehicle> CreateVehicle(Model model, Vector3 pos, float heading = 0f, bool networked = true)
{
if (API.IsModelAVehicle((uint)model.Hash))
{
model.Request();
while (!model.IsLoaded)
await BaseScript.Delay(1);
Vehicle vehicle = new Vehicle(API.CreateVehicle((uint)model.Hash, pos.X, pos.Y, pos.Z, heading, networked, false));
model.MarkAsNoLongerNeeded();
return vehicle;
}
return null;
}

public static async Task<Prop> CreateProp(Model model, Vector3 pos, bool dynamic, bool placeOnGround = false, bool networked = true)
{
if (API.IsModelValid((uint)model.Hash))
{
model.Request();
while (!model.IsLoaded)
await BaseScript.Delay(1);
if (placeOnGround)
pos.Z = World.GetGroundHeight(pos);
Prop prop = new Prop(API.CreateObjectNoOffset((uint)model.Hash, pos.X, pos.Y, pos.Z, networked, false, dynamic));
model.MarkAsNoLongerNeeded();
return prop;
}
return null;
}

public static void _StartScenario(this Ped ped, string scenario)
{
API.TaskStartScenarioInPlace(ped.Handle, scenario, 0, true);
}
}

public static class VehicleUtil
{
public static bool _IsBroken(this Vehicle vehicle)
{
return vehicle.IsDead || vehicle.EngineHealth == 0f || vehicle.PetrolTankHealth == 0f;
}

public static string _GetLabel(this Vehicle vehicle)
{
return API.GetLabelText(API.GetDisplayNameFromVehicleModel((uint)vehicle.Model.Hash));
}
}
}

0 comments on commit 93b5e2f

Please sign in to comment.