Skip to content

Commit

Permalink
Use Entity Decors for Spawning, Syncing and Despawning
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed May 27, 2018
1 parent c3e8ad8 commit 540bc44
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 52 deletions.
2 changes: 2 additions & 0 deletions 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="Spawners\Despawner.cs" />
<Compile Include="Spawners\Ambient\AmbientWarNoiseSpawner.cs" />
<Compile Include="Spawners\Ambient\FlyingByPlaneSpawner.cs" />
<Compile Include="Spawners\Events\RebelSquadSpawner.cs" />
Expand All @@ -59,6 +60,7 @@
<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 @@ -40,8 +40,8 @@ private async Task OnTick()
else if (flyingByPlane != null)
if (!Utils.IsPosShitSpawn(Players, flyingByPlane.Plane.Position, SpawnerHost.SPAWN_DESPAWN_DISTANCE * 3))
{
flyingByPlane.Plane.MarkAsNoLongerNeeded();
flyingByPlane.Pilot.MarkAsNoLongerNeeded();
flyingByPlane.Plane.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
flyingByPlane.Pilot.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
flyingByPlane = null;
}
}
Expand Down
31 changes: 31 additions & 0 deletions CorruptSnail/Spawners/Despawner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CitizenFX.Core;
using CorruptSnail.Util;
using System.Threading.Tasks;

namespace CorruptSnail.Spawners
{
class Despawner : BaseScript
{
public Despawner()
{
Tick += OnTick;
}

private async Task OnTick()
{
await Delay(SpawnerHost.SPAWN_TICK_RATE);

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

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

foreach (Vehicle veh in EntityEnum.GetVehicles())
if (veh.HasDecor(SpawnerHost.SPAWN_DESPAWN_DECOR))
veh.MarkAsNoLongerNeeded();
}
}
}
21 changes: 16 additions & 5 deletions CorruptSnail/Spawners/Events/ArmyHeliSquadSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ArmyHeliSquadSpawner : BaseScript
private const int ARMYHELI_SPAWNHEIGHT_MIN_OFFSET = 100;
private const int ARMYHELI_SPAWNHEIGHT_MAX_OFFSET = 500;
private const int ARMYHELI_MIN_SPEED = 10;
private const string ARMYHELI_DECOR = "_TOUGH_AF_FRIENDS";
private static VehicleHash[] HELI_LIST { get; } = { VehicleHash.Buzzard, VehicleHash.Buzzard2,
VehicleHash.Savage, VehicleHash.Cargobob };

Expand Down Expand Up @@ -48,12 +49,14 @@ private async Task OnTick()
else if (armyHeliSquad != null)
if (!Utils.IsPosShitSpawn(Players, armyHeliSquad.Heli.Position, SpawnerHost.SPAWN_DESPAWN_DISTANCE * 3))
{
armyHeliSquad.Heli.MarkAsNoLongerNeeded();
armyHeliSquad.Pilot.MarkAsNoLongerNeeded();
armyHeliSquad.Gunman1.MarkAsNoLongerNeeded();
armyHeliSquad.Gunman2.MarkAsNoLongerNeeded();
armyHeliSquad.Heli.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
armyHeliSquad.Pilot.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
armyHeliSquad.Gunman1.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
armyHeliSquad.Gunman2.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
armyHeliSquad = null;
}

HandleArmyHeliSquads();
}

private async void SpawnRandomArmyHeli()
Expand All @@ -73,6 +76,7 @@ private async void SpawnRandomArmyHeli()
pilot.RelationshipGroup = ArmyHeliSquadGroup;
pilot.SetIntoVehicle(heli, VehicleSeat.Driver);
pilot.AlwaysKeepTask = true;
pilot.SetDecor(ARMYHELI_DECOR, true);
Vector3 targetPos = Game.PlayerPed
.GetOffsetPosition(new Vector3(0f, -SpawnerHost.SPAWN_DESPAWN_DISTANCE * 100f, 0f));
API.TaskHeliMission(pilot.Handle, heli.Handle, 0, 0, targetPos.X, targetPos.Y, targetPos.Z,
Expand All @@ -88,15 +92,22 @@ private async void SpawnRandomArmyHeli()
gunman.Accuracy = 100;
gunman.AlwaysKeepTask = true;
gunman.Task.FightAgainstHatedTargets(float.MaxValue);
gunman.SetDecor(ARMYHELI_DECOR, true);
gunmans[i] = gunman;
}
gunmans[0].SetIntoVehicle(heli, VehicleSeat.LeftRear);
gunmans[1].SetIntoVehicle(heli, VehicleSeat.RightRear);
ArmyHeliSquadGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Respect, true);
ArmyHeliSquadGroup.SetRelationshipBetweenGroups(ZombieSpawner.ZombieGroup, Relationship.Hate);

armyHeliSquad = new ArmyHeliSquad(heli, pilot, gunmans[0], gunmans[1]);
}
}

private void HandleArmyHeliSquads()
{
foreach (Ped ped in EntityEnum.GetPeds())
if (ped.HasDecor(ARMYHELI_DECOR))
ped.RelationshipGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Like, true);
}
}
}
19 changes: 10 additions & 9 deletions CorruptSnail/Spawners/Events/RebelSquadSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CorruptSnail.Spawners.Events
class RebelSquadSpawner : BaseScript
{
private const int REBELSQUAD_MAXMEMBERS = 8;
private const float REBELSQUAD_FRIENDLY_CHANCE = 0.5f;
private const string REBELSQUAD_DECOR = "_HOSTILE_REBELS";
private static WeaponHash[] WEAPON_LIST { get; } = { WeaponHash.Pistol, WeaponHash.AssaultRifle, WeaponHash.PumpShotgun,
WeaponHash.Bat };

Expand Down Expand Up @@ -48,7 +48,7 @@ private async Task OnTick()
{
if (!Utils.IsPosShitSpawn(Players, rebel.Position, SpawnerHost.SPAWN_DESPAWN_DISTANCE)
|| rebel.IsDead)
rebel.MarkAsNoLongerNeeded();
rebel.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
else
allObsolete = false;
}
Expand All @@ -69,9 +69,6 @@ private async void SpawnRandomRebelSquad()
Ped[] rebels = new Ped[rebelAmount];
for (int i = 0; i < rebelAmount; i++)
{
spawnPos.X += 5;
spawnPos.Y += 5;

Ped rebel = await World.CreatePed(PedHash.Hillbilly01AMM, spawnPos);
API.SetPedCombatRange(rebel.Handle, 2);
API.SetPedHearingRange(rebel.Handle, float.MaxValue);
Expand All @@ -80,17 +77,21 @@ private async void SpawnRandomRebelSquad()
rebel.Accuracy = 100;
rebel.Armor = 100;
rebel.Task.WanderAround();
rebel.SetDecor(REBELSQUAD_DECOR, true);
rebels[i] = rebel;
}
if (Utils.GetRandomFloat(1f) > REBELSQUAD_FRIENDLY_CHANCE)
RebelSquadGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Hate, true);
else
RebelSquadGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Respect, true);
RebelSquadGroup.SetRelationshipBetweenGroups(ZombieSpawner.ZombieGroup, Relationship.Hate, true);
RebelSquadGroup.SetRelationshipBetweenGroups(ArmyHeliSquadSpawner.ArmyHeliSquadGroup, Relationship.Hate, true);

rebelSquad = new RebelSquad(rebels);
}
}

private void HandleRebelSquads()
{
foreach (Ped ped in EntityEnum.GetPeds())
if (ped.HasDecor(REBELSQUAD_DECOR))
ped.RelationshipGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Hate, true);
}
}
}
2 changes: 1 addition & 1 deletion CorruptSnail/Spawners/ObjectSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private async Task OnTick()
foreach (Prop obstacle in obstacles.ToArray())
if (!Utils.IsPosShitSpawn(Players, obstacle.Position, SpawnerHost.SPAWN_DESPAWN_DISTANCE))
{
obstacle.Delete();
obstacle.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
obstacles.Remove(obstacle);
}
}
Expand Down
2 changes: 2 additions & 0 deletions CorruptSnail/Spawners/SpawnerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SpawnerHost : BaseScript
public const float SPAWN_DESPAWN_DISTANCE = 350f;
public const double SPAWN_EVENT_CHANCE = 0.005;
public const int SPAWN_TICK_RATE = 100;
public const string SPAWN_DESPAWN_DECOR = "_MARKED_FOR_DESPAWN";
private const float SPAWN_HOST_DECIDE_DISTANCE = 500f;

public static bool IsHost { get; private set; }
Expand All @@ -20,6 +21,7 @@ public SpawnerHost()
{
IsHost = false;

EntityDecoration.RegisterProperty(SPAWN_DESPAWN_DECOR, DecorationType.Bool);
Tick += OnTick;
}

Expand Down
2 changes: 1 addition & 1 deletion CorruptSnail/Spawners/VehicleSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private async Task OnTick()
if (!Utils.IsPosShitSpawn(Players, spawnedVeh.Position, SpawnerHost.SPAWN_DESPAWN_DISTANCE)
|| spawnedVeh.EngineHealth == 0f)
{
spawnedVeh.MarkAsNoLongerNeeded();
spawnedVeh.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
spawnedVeh = null;
}
}
Expand Down
31 changes: 18 additions & 13 deletions CorruptSnail/Spawners/ZombieSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class ZombieSpawner : BaseScript
private const int ZOMBIE_AMOUNT = 30;
private const double ZOMBIE_ATTR_CHANCE = 0.5;
private const int ZOMBIE_MAX_HEALTH = 500;
private const int ZOMBIE_MAX_ARMOR = 500;
private const int ZOMBIE_MAX_ARMOR = 500;
private const string ZOMBIE_DECOR = "_I_AM_RAWWRRR";

private List<Ped> zombies;
public static RelationshipGroup ZombieGroup { get; private set; }
Expand All @@ -22,7 +23,7 @@ public ZombieSpawner()
zombies = new List<Ped>();
ZombieGroup = World.AddRelationshipGroup("zombies");

EventHandlers["corruptsnail:client:newZombie"] += new Action<int>(OnNewZombie);
EntityDecoration.RegisterProperty(ZOMBIE_DECOR, DecorationType.Bool);
Tick += OnTick;
}

Expand All @@ -37,9 +38,11 @@ private async Task OnTick()
if (!Utils.IsPosShitSpawn(Players, zombie.Position, SpawnerHost.SPAWN_DESPAWN_DISTANCE)
|| zombie.IsDead)
{
zombie.MarkAsNoLongerNeeded();
zombie.SetDecor(SpawnerHost.SPAWN_DESPAWN_DECOR, true);
zombies.Remove(zombie);
}

HandleZombies();
}

private async void SpawnRandomZombie()
Expand All @@ -66,27 +69,29 @@ private async void SpawnRandomZombie()
zombie.Health = randHealth;
zombie.Armor = Utils.GetRandomInt(ZOMBIE_MAX_ARMOR);
zombie.RelationshipGroup = ZombieGroup;
ZombieGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Hate, true);
ZombieAttrChances(zombie);

zombie.Task.WanderAround();
TriggerServerEvent("corruptsnail:newZombie", API.PedToNet(zombieHandle));
zombie.SetDecor(ZOMBIE_DECOR, true);

zombies.Add(zombie);
}
}

private void OnNewZombie(int zombieNetHandle)
private void HandleZombies()
{
int zombieHandle = API.NetToPed(zombieNetHandle);
Ped zombie = new Ped(zombieHandle)
foreach (Ped ped in EntityEnum.GetPeds())
{
Voice = "ALIENS",
IsPainAudioEnabled = false
};
if (ped.HasDecor(ZOMBIE_DECOR))
{
ped.Voice = "ALIENS";
ped.IsPainAudioEnabled = false;
ped.RelationshipGroup.SetRelationshipBetweenGroups(Game.PlayerPed.RelationshipGroup, Relationship.Hate, true);

API.RequestAnimSet("move_m@drunk@verydrunk");
API.SetPedMovementClipset(zombieHandle, "move_m@drunk@verydrunk", 1f);
API.RequestAnimSet("move_m@drunk@verydrunk");
API.SetPedMovementClipset(ped.Handle, "move_m@drunk@verydrunk", 1f);
}
}
}

private void ZombieAttrChances(Ped zombie)
Expand Down
19 changes: 4 additions & 15 deletions CorruptSnail/TimeSync.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;
using System;

namespace CorruptSnail
{
class TimeSync : BaseScript
{
private bool synced = false;

public TimeSync()
{
EventHandlers["playerSpawned"] += new Action(delegate {
if (!synced)
TriggerServerEvent("corruptsnail:timeSync");
else
{
TimeSpan currentTime = World.CurrentDayTime;
TriggerServerEvent("corruptsnail:updateTime", currentTime.Hours, currentTime.Minutes, currentTime.Seconds);
}
});

EventHandlers["corruptsnail:client:timeSync"] += new Action<int, int, int>((timeH, timeM, timeS) =>
{
World.CurrentDayTime = new TimeSpan(timeH, timeM, timeS);
synced = true;
int h = 0; int m = 0; int s = 0;
API.NetworkGetServerTime(ref h, ref m, ref s);
API.NetworkOverrideClockTime(h, m, s);
});
}
}
Expand Down
Loading

0 comments on commit 540bc44

Please sign in to comment.