Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPC Component (WIP) #916

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
da8f8b2
continue onReceive event calls if Recording component has failure
AmyrAhmady Apr 17, 2024
0edf498
create NPCs component
AmyrAhmady Apr 17, 2024
7784525
create pawn scripting api for current NPC functions
AmyrAhmady Apr 17, 2024
c4b958a
force internal updates on position and rotation change
AmyrAhmady Apr 17, 2024
8b54323
calculate travelled distance once
AmyrAhmady Apr 17, 2024
dc4ac7f
add NPC_StopMove
AmyrAhmady Apr 17, 2024
857dff5
restart npc move when pos or rot is reset
AmyrAhmady Apr 17, 2024
d27e088
only start npc move if move type is valid
AmyrAhmady Apr 17, 2024
6a517e5
set internal rot and pos values during move to avoid recursion
AmyrAhmady Apr 17, 2024
3f3f1bc
use stopMove when npc move is finished
AmyrAhmady Apr 17, 2024
f7539c8
only send NPC foot sync if player is spawned
AmyrAhmady Apr 17, 2024
de76383
use underscore suffixed class variables for NPC class
AmyrAhmady Apr 17, 2024
9d09b6c
fix formatting
AmyrAhmady Apr 17, 2024
0ec6e6b
rename npc connect events to entity create and destroy
AmyrAhmady Apr 20, 2024
1c065f4
mark to be destroyed npcs to be process next tick
AmyrAhmady Apr 20, 2024
a8afa3e
destroy NPCs safely
AmyrAhmady Apr 20, 2024
9ca235f
use NPC ref instead of ptr in emulate functions
AmyrAhmady Apr 20, 2024
26abc98
store footSyncRate config value in npc component
AmyrAhmady Apr 23, 2024
612341a
calculate estimated arrival time and use it to detect finish move
AmyrAhmady Apr 23, 2024
51030b7
fix velocity calculation
AmyrAhmady Apr 23, 2024
7af68ca
add NPC_SetFacingAngle and GetFacingAngle
AmyrAhmady Apr 23, 2024
aeda9d6
another attempt to fix npc movements
AmyrAhmady Apr 24, 2024
b7e6b75
add NPC_SetSkin
AmyrAhmady May 8, 2024
8ef1e38
add streamed in check natives for NPCs
AmyrAhmady May 13, 2024
75f5ce1
add NPC_GetAll native
AmyrAhmady May 13, 2024
8b868ac
npc interior natives
AmyrAhmady May 18, 2024
4160bce
proper class variable initialization in constructor
AmyrAhmady May 18, 2024
ccec7a7
add set/get velocity for NPCs
AmyrAhmady May 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions SDK/include/Server/Components/NPCs/npcs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <sdk.hpp>

enum NPCMoveType
{
NPCMoveType_None,
NPCMoveType_Walk,
NPCMoveType_Jog,
NPCMoveType_Sprint
};

struct INPC : public IExtensible, public IEntity
{
/// Get player instance of NPC.
virtual IPlayer* getPlayer() = 0;

/// Spawn NPC.
virtual void spawn() = 0;

/// Move NPC to a specified location.
virtual bool move(Vector3 position, NPCMoveType moveType) = 0;

/// Stop NPC from moving.
virtual void stopMove() = 0;

/// Set NPC skin
virtual void setSkin(int model) = 0;

/// Check if a player is streamed in for the current NPC
virtual bool isStreamedInForPlayer(const IPlayer& other) const = 0;

/// Get the NPC which are streamed in for this player
virtual const FlatPtrHashSet<IPlayer>& streamedForPlayers() const = 0;

/// Set NPC interior, it doesn't do much but keeps a record internally to use getter functions later
virtual void setInterior(unsigned int interior);

/// Get NPC interior, just the value that is stored internally
virtual unsigned int getInterior() const;

/// Get NPC velocity
virtual Vector3 getVelocity() const = 0;

/// Set NPC velocity
virtual void setVelocity(Vector3 position, bool update = false) = 0;
};

struct NPCEventHandler
{
virtual void onNPCFinishMove(INPC& npc) {};
virtual void onNPCCreate(INPC& npc) {};
virtual void onNPCDestroy(INPC& npc) {};
};

static const UID NPCComponent_UID = UID(0x3D0E59E87F4E90BC);
struct INPCComponent : public IPool<INPC>, public INetworkComponent
{
PROVIDE_UID(NPCComponent_UID)

/// Get the npc event dispatcher
virtual IEventDispatcher<NPCEventHandler>& getEventDispatcher() = 0;

/// Create a controllable NPC
virtual INPC* create(StringView name) = 0;

/// Destroy an NPC. We need this because it's more than just an entity removal from a pool
virtual void destroy(INPC& npc) = 0;
};
1 change: 1 addition & 0 deletions SDK/include/Server/Components/Pawn/Impl/pawn_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void setAmxLookups()
lookups_.vars = components_->queryComponent<IVariablesComponent>();
lookups_.vehicles = components_->queryComponent<IVehiclesComponent>();
lookups_.models = components_->queryComponent<ICustomModelsComponent>();
lookups_.npcs = components_->queryComponent<INPCComponent>();
}
}

Expand Down
1 change: 1 addition & 0 deletions SDK/include/Server/Components/Pawn/Impl/pawn_natives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ POOL_PARAM(IObject, objects);
POOL_PARAM(ITextDraw, textdraws);
POOL_PARAM(ITextLabel, textlabels);
POOL_PARAM(IVehicle, vehicles);
POOL_PARAM(INPC, npcs);

PLAYER_POOL_PARAM(IPlayerObject, IPlayerObjectData);
PLAYER_POOL_PARAM(IPlayerTextDraw, IPlayerTextDrawData);
Expand Down
2 changes: 2 additions & 0 deletions SDK/include/Server/Components/Pawn/pawn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <Server/Components/Variables/variables.hpp>
#include <Server/Components/Vehicles/vehicles.hpp>
#include <Server/Components/CustomModels/custommodels.hpp>
#include <Server/Components/NPCs/npcs.hpp>

#define SCRIPT_API(name, prototype) PAWN_NATIVE(openmp_scripting, name, prototype)
#define SCRIPT_API_FAILRET(name, failret, prototype) PAWN_NATIVE_FAILRET(openmp_scripting, failret, name, prototype)
Expand Down Expand Up @@ -64,6 +65,7 @@ struct PawnLookup
IVariablesComponent* vars = nullptr;
IVehiclesComponent* vehicles = nullptr;
ICustomModelsComponent* models = nullptr;
INPCComponent* npcs = nullptr;
};

PawnLookup* getAmxLookups();
Expand Down
1 change: 1 addition & 0 deletions SDK/include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum ENetworkType
{
ENetworkType_RakNetLegacy,
ENetworkType_ENet,
ENetworkType_NPCs,

ENetworkType_End
};
Expand Down
8 changes: 7 additions & 1 deletion SDK/include/values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

constexpr int MAX_SEATS = 4;
constexpr int PLAYER_POOL_SIZE = 1000;
constexpr int NPC_POOL_SIZE = 1000;
constexpr int VEHICLE_POOL_SIZE = 2000;
constexpr int CLASS_POOL_SIZE = 320;
constexpr int OBJECT_POOL_SIZE = 2000;
Expand Down Expand Up @@ -95,4 +96,9 @@ enum Key
};

constexpr uint16_t INVALID_MODEL_ID = 65535u;
constexpr int32_t QUESTION_MARK_MODEL_ID = 18631;
constexpr int32_t QUESTION_MARK_MODEL_ID = 18631;

constexpr float NPC_MOVE_SPEED_AUTO = -1.0f;
constexpr float NPC_MOVE_SPEED_WALK = 0.1552086f;
constexpr float NPC_MOVE_SPEED_JOG = 0.56444f;
constexpr float NPC_MOVE_SPEED_SPRINT = 0.926784f;
1 change: 1 addition & 0 deletions Server/Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_subdirectory(TextLabels)
add_subdirectory(Timers)
add_subdirectory(Variables)
add_subdirectory(Vehicles)
add_subdirectory(NPCs)

# Pawn
if(BUILD_PAWN_COMPONENT)
Expand Down
2 changes: 2 additions & 0 deletions Server/Components/NPCs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
add_server_component(${ProjectId})
Loading
Loading