Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudze committed Mar 29, 2020
0 parents commit c09020c
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vs
25 changes: 25 additions & 0 deletions GameEntityScript.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameEntityScript", "GameEntityScript\GameEntityScript.csproj", "{29BA8D5F-4D75-45E5-AF05-8FFC9DC0B7FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{29BA8D5F-4D75-45E5-AF05-8FFC9DC0B7FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29BA8D5F-4D75-45E5-AF05-8FFC9DC0B7FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29BA8D5F-4D75-45E5-AF05-8FFC9DC0B7FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29BA8D5F-4D75-45E5-AF05-8FFC9DC0B7FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {71257826-CAC3-475A-9FC6-8FF00E28C257}
EndGlobalSection
EndGlobal
182 changes: 182 additions & 0 deletions GameEntityScript/GameEntityResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using System.Numerics;
using AltV.Net;
using AltV.Net.EntitySync;
using AltV.Net.EntitySync.ServerEvent;
using AltV.Net.EntitySync.SpatialPartitions;

namespace GameEntityScript
{
public class GameEntityResource : Resource
{
private void InitEntitySync()
{
AltEntitySync.Init(
1,
100,
(threadCount, repository) => new ServerEventNetworkLayer(threadCount, repository),
(entity, threadCount) => (entity.Id % threadCount),
(entityId, entityType, threadCount) => (entityId % threadCount),
(threadId) => new LimitedGrid3(50_000, 50_000, 100, 10_000, 10_000, 600),
new IdProvider()
);
}

private void RegisterExports()
{
Alt.Export("createGameEntity", new Func<long, Vector3, int, uint, ulong>(this.CreateGameEntity));
Alt.Export("removeGameEntity", new Action<long, long>(this.RemoveGameEntity));
Alt.Export("setGameEntityPosition", new Action<long, long, Vector3>(this.SetGameEntityPosition));
Alt.Export("getGameEntityPosition", new Func<long, long, Vector3>(this.GetGameEntityPosition));
Alt.Export("getGameEntityRange", new Func<long, long, uint>(this.GetGameEntityRange));
Alt.Export("setGameEntityDimension", new Action<long, long, int>(this.SetGameEntityDimension));
Alt.Export("getGameEntityDimension", new Func<long, long, int>(this.GetGameEntityDimension));
Alt.Export("setGameEntityData", new Action<long, long, String, object>(this.SetGameEntityData));
Alt.Export("getGameEntityData", new Func<long, long, String, object>(this.GetGameEntityData));
Alt.Export("resetGameEntityData", new Action<long, long, String>(this.ResetGameEntityData));
}

private IEntity GetGameEntity(long id, long type)
{
IEntity entity;

if (!AltEntitySync.TryGetEntity((ulong)id, (ulong)type, out entity))
return null;

return entity;
}

private ulong CreateGameEntity(long type, Vector3 position, int dimension, uint range)
{
IEntity entity = AltEntitySync.CreateEntity((ulong) type, position, dimension, range);

return entity.Id;
}

private void RemoveGameEntity(long id, long type)
{
IEntity entity = this.GetGameEntity(id, type);

AltEntitySync.RemoveEntity(entity);
}

private void SetGameEntityPosition(long id, long type, Vector3 position)
{
IEntity entity = GetGameEntity(id, type);

if (entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::SetGameEntityPosition was called with invalid entity!");
return;
}

entity.Position = position;
}

private Vector3 GetGameEntityPosition(long id, long type)
{
IEntity entity = GetGameEntity(id, type);

if (entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::GetGameEntityPosition was called with invalid entity!");
return new Vector3();
}

return entity.Position;
}

private uint GetGameEntityRange(long id, long type)
{
IEntity entity = GetGameEntity(id, type);

if (entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::GetGameEntityRange was called with invalid entity!");
return 0;
}

return entity.Range;
}

private void SetGameEntityDimension(long id, long type, int dimension)
{
IEntity entity = GetGameEntity(id, type);

if (entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::SetGameEntityDimension was called with invalid entity!");
return;
}

entity.Dimension = dimension;
}

private int GetGameEntityDimension(long id, long type)
{
IEntity entity = GetGameEntity(id, type);

if (entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::GetGameEntityDimension was called with invalid entity!");
return 0;
}

return entity.Dimension;
}

private void SetGameEntityData(long id, long type, String key, object value)
{
IEntity entity = GetGameEntity(id, type);

if(entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::SetGameEntityData was called with invalid entity!");
return;
}

if (value == null)
entity.ResetData(key);

else
entity.SetData(key, value);
}

private object GetGameEntityData(long id, long type, String key)
{
IEntity entity = GetGameEntity(id, type);

if (entity == null)
{
Console.WriteLine("[WARN] GameEntityResource::GetGameEntityData was called with invalid entity!");
return null;
}

object result;

if(!entity.TryGetData(key, out result))
{
Console.WriteLine("[WARN] GameEntityResource::GetGameEntityData was called with invalid data key!"); ;
return null;
}

return result;
}

private void ResetGameEntityData(long id, long type, String key)
{
this.SetGameEntityData(id, type, key, null);
}

public override void OnStart()
{
this.InitEntitySync();
this.RegisterExports();
}

public override void OnStop()
{
AltEntitySync.Stop();
}
}
}
21 changes: 21 additions & 0 deletions GameEntityScript/GameEntityScript.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>C:\Users\karol\OneDrive\Stalinis kompiuteris\PaletoBay\sr\resources\game-entity-script\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>C:\Users\karol\OneDrive\Stalinis kompiuteris\PaletoBay\sr\resources\game-entity-script\</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AltV.Net" Version="1.29.0" />
<PackageReference Include="AltV.Net.EntitySync" Version="1.3.0-dev-preview" />
<PackageReference Include="AltV.Net.EntitySync.ServerEvent" Version="1.3.0-dev-preview" />
</ItemGroup>

</Project>

0 comments on commit c09020c

Please sign in to comment.