C#:
//#define DEBUG
// Requires: PathFinding
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Oxide.Core;
using Oxide.Core.Configuration;
using Oxide.Core.Plugins;
using Oxide.Game.Rust;
using Rust;
using Facepunch;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEngine;
using Convert = System.Convert;
namespace Oxide.Plugins {
[Info("HumanNPC", "Reneb/Nogrod/Calytic/RFC1920", "0.3.27", ResourceId = 856)]
[Description("Adds interactive Human NPCs which can be modded by other plugins")]
public class HumanNPC : RustPlugin {
//////////////////////////////////////////////////////
/// Fields
//////////////////////////////////////////////////////
private static Collider[] colBuffer;
private int playerLayer;
private static int targetLayer;
private static Vector3 Vector3Down;
private static int groundLayer;
private Hash<ulong, HumanNPCInfo> humannpcs = new Hash<ulong, HumanNPCInfo>();
static int playerMask = LayerMask.GetMask("Player (Server)");
static int obstructionMask = LayerMask.GetMask(new[] {
"Player (Server)", "Construction", "Deployed", "Clutter"
}
);
static int constructionMask = LayerMask.GetMask(new[] {
"Construction", "Deployed", "Clutter"
}
);
static int terrainMask = LayerMask.GetMask(new[] {
"Terrain", "Tree"
}
);
private bool save;
private StoredData storedData;
private DynamicConfigFile data;
private Vector3 eyesPosition;
private string chat = "<color=#FA58AC>{0}:</color> ";
[PluginReference]
private Plugin Kits, Waypoints, Vanish;
private static PathFinding PathFinding;
private class StoredData {
public HashSet<HumanNPCInfo> HumanNPCs = new HashSet<HumanNPCInfo>();
}
public class WaypointInfo {
public float Speed;
public Vector3 Position;
public WaypointInfo(Vector3 position, float speed) {
Speed = speed;
Position = position;
}
}
public static bool IsLayerBlocked(Vector3 position, float radius, int mask) {
var colliders = Pool.GetList<Collider>();
Vis.Colliders<Collider>(position, radius, colliders, mask, QueryTriggerInteraction.Collide);
bool blocked = colliders.Count > 0;
Pool.FreeList<Collider>(ref colliders);
return blocked;
}
//////////////////////////////////////////////////////
/// class SpawnInfo
/// Spawn information, position & rotation
/// public => will be saved in the data file
/// non public => won't be saved in the data file
//////////////////////////////////////////////////////
public class SpawnInfo {
public Vector3 position;
public Quaternion rotation;
public SpawnInfo(Vector3 position, Quaternion rotation) {
this.position = position;
this.rotation = rotation;
}
public string String() {
return $"Pos{position} - Rot{rotation}";
}
public string ShortString() {
return $"Pos({Math.Ceiling(position.x)},{Math.Ceiling(position.y)},{Math.Ceiling(position.z)})";
}
}
//////////////////////////////////////////////////////
/// class HumanTrigger
/// MonoBehaviour: managed by UnityEngine
/// This takes care of all collisions and area management of humanNPCs
//////////////////////////////////////////////////////
public class HumanTrigger : MonoBehaviour {
private HumanPlayer npc;
private readonly HashSet<BasePlayer> triggerPlayers = new HashSet<BasePlayer>();
public float collisionRadius;
private void Awake() {
npc = GetComponent<HumanPlayer>();
collisionRadius = npc.info.collisionRadius;
InvokeRepeating("UpdateTriggerArea", 2f, 1.5f);
}
private void OnDestroy() {
#if DEBUG
//Interface.Oxide.LogInfo("Destroy child: {0}", child?.name);
#endif
CancelInvoke("UpdateTriggerArea");
}
private void UpdateTriggerArea() {
var count = Physics.OverlapSphereNonAlloc(npc.player.transform.position, collisionRadius, colBuffer, targetLayer);
var collidePlayers = new HashSet<BasePlayer>();
for (int i = 0; i < count; i++) {
var collider = colBuffer[i];
colBuffer[i] = null;
var player = collider.GetComponentInParent<BasePlayer>();
if (player != null) {
if (player == npc.player) continue;
collidePlayers.Add(player);
if (triggerPlayers.Add(player)) OnEnterCollision(player);
continue;
}
//temp fix
/*var ai = collider.GetComponentInParent<NPCAI>();
if (ai != null && ai.decider.hatesHumans)
npc.StartAttackingEntity(collider.GetComponentInParent<BaseNpc>());*/
}
var removePlayers = new HashSet<BasePlayer>();
foreach (BasePlayer player in triggerPlayers) if (!collidePlayers.Contains(player)) removePlayers.Add(player);
foreach (BasePlayer player in removePlayers) {
triggerPlayers.Remove(player);
OnLeaveCollision(player);
}
}
private void OnEnterCollision(BasePlayer player) {
Interface.Oxide.CallHook("OnEnterNPC", npc.player, player);
}
private void OnLeaveCollision(BasePlayer player) {
Interface.Oxide.CallHook("OnLeaveNPC", npc.player, player);
}
}
//////////////////////////////////////////////////////
/// class HumanLocomotion
/// MonoBehaviour: managed by UnityEngine
/// This takes care of all movements and attacks of HumanNPCs
//////////////////////////////////////////////////////
public class HumanLocomotion : MonoBehaviour {
private HumanPlayer npc;
public Vector3 StartPos = new Vector3(0f, 0f, 0f);
public Vector3 EndPos = new Vector3(0f, 0f, 0f);
public Vector3 LastPos = new Vector3(0f, 0f, 0f);
private Vector3 nextPos = new Vector3(0f, 0f, 0f);
private float waypointDone = 0f;
public float secondsTaken = 0f;
private float secondsToTake = 0f;
public List<WaypointInfo> cachedWaypoints;
private int currentWaypoint = -1;
public float followDistance = 3.5f;
private float lastHit = 0f;
public int noPath = 0;
public bool shouldMove = true;
private float startedReload = 0f;
private bool reloading = false;
public bool returning = false;
public bool sitting = false;
public BaseCombatEntity attackEntity = null;
public BaseEntity followEntity = null;
public Vector3 targetPosition = Vector3.zero;
public List<Vector3> pathFinding;
private HeldEntity firstWeapon = null;
public void Awake() {
npc = GetComponent<HumanPlayer>();
UpdateWaypoints();
npc.player.modelState.onground = true;
}
public void UpdateWaypoints() {
if (string.IsNullOrEmpty(npc.info.waypoint)) return;
var cwaypoints = Interface.Oxide.CallHook("GetWaypointsList", npc.info.waypoint);
if (cwaypoints == null) cachedWaypoints = null;
else {
cachedWaypoints = new List<WaypointInfo>();
var lastPos = npc.info.spawnInfo.position;
var speed = GetSpeed();
foreach (var cwaypoint in (List<object>)cwaypoints) {
foreach (var pair in (Dictionary<Vector3, float>)cwaypoint) {
if (HumanNPC.PathFinding == null) {
cachedWaypoints.Add(new WaypointInfo(pair.Key, pair.Value));
continue;
}
var temppathFinding = HumanNPC.PathFinding.Go(lastPos, pair.Key);
speed = pair.Value;
if (temppathFinding != null) {
lastPos = pair.Key;
foreach (var vector3 in temppathFinding) cachedWaypoints.Add(new WaypointInfo(vector3, speed));
}
else {
#if DEBUG
Interface.Oxide.LogInfo("Blocked waypoint? {0} for {1}", pair.Key, npc.player.displayName);
#endif
//cachedWaypoints.Add(new WaypointInfo(pair.Key, speed));
}
}
}
if (HumanNPC.PathFinding != null && lastPos != npc.info.spawnInfo.position) {
var temppathFinding = HumanNPC.PathFinding.Go(lastPos, npc.info.spawnInfo.position);
if (temppathFinding != null) {
foreach (var vector3 in temppathFinding) cachedWaypoints.Add(new WaypointInfo(vector3, speed));
}
else {
#if DEBUG
Interface.Oxide.LogInfo("Blocked waypoint to spawn? {0} for {1}", lastPos, npc.player.displayName);
#endif
}
}
if (cachedWaypoints.Count <= 0) cachedWaypoints = null;
#if DEBUG
Interface.Oxide.LogInfo("Waypoints: {0} for {1}", cachedWaypoints.Count, npc.player.displayName);
#endif
}
}
private void FixedUpdate() {
TryToMove();
}
public void TryToMove() {
if (npc.player.IsDead() || npc.player.IsWounded()) return;
if (targetPosition != Vector3.zero) {
#if DEBUG
Interface.Oxide.LogInfo("TryToMove: ProcessFollow(target)");
#endif
ProcessFollow(targetPosition);
}
if (attackEntity is BaseCombatEntity) {
#if DEBUG
Interface.Oxide.LogInfo("TryToMove: ProcessAttack(attackEntity)");
#endif
ProcessAttack(attackEntity);
}
else if (followEntity is BaseEntity) {
#if DEBUG
Interface.Oxide.LogInfo("TryToMove: ProcessFollow(followEntity)");
#endif
ProcessFollow(followEntity.transform.position);
}
else if (secondsTaken == 0f) GetNextPath();
if (StartPos != EndPos) Execute_Move();
if (waypointDone >= 1f) secondsTaken = 0f;
}
private void Execute_Move() {
if (!shouldMove) return;
secondsTaken += Time.deltaTime;
waypointDone = Mathf.InverseLerp(0f, secondsToTake, secondsTaken);
nextPos = Vector3.Lerp(StartPos, EndPos, waypointDone);
nextPos.y = GetMoveY(nextPos);
npc.player.MovePosition(nextPos);
//npc.player.eyes.position = nextPos + new Vector3(0, 1.6f, 0);
var newEyesPos = nextPos + new Vector3(0, 1.6f, 0);
npc.player.eyes.position.Set(newEyesPos.x, newEyesPos.y, newEyesPos.z);
npc.player.UpdatePlayerCollider(true);
npc.player.modelState.onground = !IsSwimming();
}
public void Evade() {
if(IsSwimming()) return;
if(npc.info.evade == false) return;
#if DEBUG
Interface.Oxide.LogInfo("Evading...");
#endif
// var ra = UnityEngine.Random.Range(0f, 100f);
// if(ra > 40f)
// {
// npc.player.modelState.ducked = true;
// }
// else
// {
// npc.player.modelState.ducked = false;
// }
Vector3 ev = new Vector3(UnityEngine.Random.Range(-npc.info.evdist, npc.info.evdist), UnityEngine.Random.Range(-1.5f, 1.5f));
Vector3 newpos = npc.player.transform.position + ev;
#if DEBUG
Interface.Oxide.LogInfo($" first trying new position {newpos.ToString()}");
#endif
RaycastHit hitinfo;
int i = 0;
while(Physics.OverlapSphere(newpos, npc.info.evdist, constructionMask) != null) {
newpos.x = newpos.x + UnityEngine.Random.Range(-0.2f, 0.2f);
newpos.y = newpos.y + UnityEngine.Random.Range(-0.1f, 0.1f);
newpos.z = newpos.z + UnityEngine.Random.Range(-0.2f, 0.2f);
#if DEBUG
Interface.Oxide.LogInfo($" trying new position {newpos.ToString()}");
#endif
if(Physics.Raycast(newpos, Vector3Down, out hitinfo, 0.1f, groundLayer)) {
#if DEBUG
Interface.Oxide.LogInfo($" found ground or construction at {newpos.ToString()}");
#endif
break;
}
else {
newpos.y = npc.locomotion.GetGroundY(newpos);
#if DEBUG
Interface.Oxide.LogInfo($" fell through floor, relocating to {newpos.ToString()}");
#endif
}
i++;
if(i > 100) break;
}
npc.player.MovePosition(newpos);
}
public bool IsSwimming() {
return WaterLevel.Test(npc.player.transform.position + new Vector3(0, 0.65f, 0));
}
private bool CanSit() {
if(sitting) {
return false;
}
return npc.info.allowsit;
}
public void Sit() {
npc.Invoke("AllowMove", 0);
// Find a place to sit
List<BaseChair> chairs = new List<BaseChair>();
Vis.Entities<BaseChair>(npc.player.transform.position, 15f, chairs);
foreach(var mountable in chairs.Distinct().ToList()) {
#if DEBUG
Interface.Oxide.LogInfo($"HumanNPC {npc.player.displayName} trying to sit...");
#endif
if(mountable.IsMounted()) {
#if DEBUG
Interface.Oxide.LogInfo($"Someone is sitting here.");
#endif
continue;
}
#if DEBUG
Interface.Oxide.LogInfo($"Found an empty chair.");
#endif
mountable.MountPlayer(npc.player);
//npc.player.MovePosition(mountable.mountAnchor.transform.position);
//npc.player.transform.rotation = mountable.mountAnchor.transform.rotation;
//npc.player.ServerRotation = mountable.mountAnchor.transform.rotation;
npc.player.OverrideViewAngles(mountable.mountAnchor.transform.rotation.eulerAngles);
npc.player.eyes.NetworkUpdate(mountable.mountAnchor.transform.rotation);
npc.player.ClientRPCPlayer<Vector3>(null, npc.player, "ForcePositionTo", npc.player.transform.position);
mountable.SetFlag(BaseEntity.Flags.Busy, true, false);
sitting = true;
break;
}
}
public void Stand() {
//if(CanSit() && sitting)
if(sitting) {
#if DEBUG
Interface.Oxide.LogInfo($"HumanNPC {npc.player.displayName} trying to stand...");
#endif
// npc.Invoke("AllowMove",0);
var mounted = npc.player.GetMounted();
mounted.DismountPlayer(npc.player);
mounted.SetFlag(BaseEntity.Flags.Busy, false, false);
sitting = false;
}
}
private float GetSpeed(float speed = -1) {
if (sitting) speed = 0;
if (returning) speed = 7;
else if (speed == -1) speed = npc.info.speed;
if (IsSwimming()) speed = speed / 2f;
return speed;
}
private void GetNextPath() {
if (npc == null) npc = GetComponent<HumanPlayer>();
if(CanSit() && sitting == false) {
Sit();
}
LastPos = Vector3.zero;
if (cachedWaypoints == null) {
shouldMove = false;
return;
}
shouldMove = true;
Interface.Oxide.CallHook("OnNPCPosition", npc.player, npc.player.transform.position);
if (currentWaypoint + 1 >= cachedWaypoints.Count) {
UpdateWaypoints();
currentWaypoint = -1;
}
if (cachedWaypoints == null) {
shouldMove = false;
return;
}
currentWaypoint++;
var wp = cachedWaypoints[currentWaypoint];
SetMovementPoint(npc.player.transform.position, wp.Position, GetSpeed(wp.Speed));
if (npc.player.transform.position == wp.Position) {
npc.DisableMove();
npc.Invoke("AllowMove", GetSpeed(wp.Speed));
return;
}
}
public void SetMovementPoint(Vector3 startpos, Vector3 endpos, float s) {
StartPos = startpos;
if (endpos != Vector3.zero) {
EndPos = endpos;
EndPos.y = Math.Max(EndPos.y, TerrainMeta.HeightMap.GetHeight(EndPos));
if (StartPos != EndPos) secondsToTake = Vector3.Distance(EndPos, StartPos) / s;
npc.LookTowards(EndPos);
}
else {
if (IsInvoking("PathFinding")) {
CancelInvoke("PathFinding");
}
}
secondsTaken = 0f;
waypointDone = 0f;
}
private bool HitChance(float chance = -1f) {
if (chance < 0) chance = npc.info.hitchance;
return UnityEngine.Random.Range(1, 100) < (int)(chance * 100);
}
private void Move(Vector3 position, float speed = -1) {
if (speed == -1) {
speed = npc.info.speed;
}
if (waypointDone >= 1f) {
if (pathFinding != null && pathFinding.Count > 0) pathFinding.RemoveAt(pathFinding.Count - 1);
waypointDone = 0f;
}
if (pathFinding == null || pathFinding.Count < 1) return;
shouldMove = true;
if (waypointDone == 0f) SetMovementPoint(position, pathFinding[pathFinding.Count - 1], GetSpeed(speed));
}
private void ProcessAttack(BaseCombatEntity entity) {
#if DEBUG
Interface.Oxide.LogInfo("ProcessAttack: {0} -> {1}", npc.player.displayName, entity.name);
#endif
if (entity != null && entity.IsAlive()) {
//var c_attackDistance = Vector3.Distance(entity.transform.position, npc.player.transform.position);
var c_attackDistance = Vector3.Distance(entity.transform.position + new Vector3(0, 1.6f, 0), npc.player.transform.position + new Vector3(0, 1.6f, 0));
shouldMove = false;
bool validAttack = Vector3.Distance(LastPos, npc.player.transform.position) < npc.info.maxDistance && noPath < 5;
#if DEBUG
Interface.Oxide.LogInfo(" Entity: Type {0}, alive {1}, valid {2}", entity.GetType().FullName, entity.IsAlive(), validAttack);
#endif
if (validAttack) {
bool range = false;
if(npc.info.follow) {
range = c_attackDistance < npc.info.damageDistance;
}
else {
range = c_attackDistance < npc.info.maxDistance;
}
var see = CanSee(npc, entity);
#if DEBUG
Interface.Oxide.LogInfo(" validAttack Entity: Type {0}, ranged {1}, cansee {2}", entity.GetType().FullName, range, see);
#endif
if (range && see) {
AttemptAttack(entity);
return;
}
if (GetSpeed() <= 0) {
npc.EndAttackingEntity();
}
else if(!npc.info.follow) {
}
else {
Move(npc.player.transform.position);
}
}
else {
npc.EndAttackingEntity();
}
}
else {
npc.EndAttackingEntity();
}
}
public void ProcessFollow(Vector3 target) {
#if DEBUG
Interface.Oxide.LogInfo($"ProcessFollow() called for {target.ToString()}");
#endif
var c_followDistance = Vector3.Distance(target, npc.player.transform.position);
shouldMove = false;
#if DEBUG
Interface.Oxide.LogInfo($"ProcessFollow() distance {c_followDistance.ToString()}");
#endif
//if (c_followDistance > 0)// && Vector3.Distance(LastPos, npc.player.transform.position) < followDistance)// && noPath < 5)
if (c_followDistance > followDistance && Vector3.Distance(LastPos, npc.player.transform.position) < npc.info.maxDistance && noPath < 5) {
Move(npc.player.transform.position, npc.info.speed);
}
else {
if (followEntity is BaseEntity) {
#if DEBUG
Interface.Oxide.LogInfo($"ProcessFollow() bailing out - is BaseEntity");
#endif
npc.EndFollowingEntity(noPath < 5);
}
else if (targetPosition != Vector3.zero) {
#if DEBUG
Interface.Oxide.LogInfo($"ProcessFollow() bailing out");
#endif
npc.EndGo(noPath < 5);
}
}
}
public void PathFinding() {
Vector3 target = Vector3.zero;
if (attackEntity != null) {
//Vector3 diff = new Vector3(Core.Random.Range(-npc.info.attackDistance, npc.info.attackDistance), 0, Core.Random.Range(-npc.info.attackDistance, npc.info.attackDistance));
target = attackEntity.transform.position;
// + diff;
}
else if (followEntity != null) {
target = followEntity.transform.position;
}
else if (targetPosition != Vector3.zero) {
target = targetPosition;
}
if (target != Vector3.zero) {
PathFinding(new Vector3(target.x, GetMoveY(target), target.z));
}
}
public void PathFinding(Vector3 targetPos) {
if (gameObject == null) return;
if (IsInvoking("PathFinding")) {
CancelInvoke("PathFinding");
}
if (GetSpeed() <= 0) return;
var temppathFinding = HumanNPC.PathFinding?.Go(npc.player.transform.position, targetPos);
if (temppathFinding == null) {
if(pathFinding == null || pathFinding.Count == 0) {
noPath++;
}
else {
noPath = 0;
}
if(noPath < 5) {
Invoke("PathFinding", 2);
}
else if (returning) {
returning = false;
SetMovementPoint(npc.player.transform.position, LastPos, 7f);
secondsTaken = 0.01f;
}
}
else {
noPath = 0;
pathFinding = temppathFinding;
pathFinding.Reverse();
waypointDone = 0f;
Invoke("PathFinding", pathFinding.Count / GetSpeed(npc.info.speed));
}
}
public void GetBackToLastPos() {
if (npc.player.transform.position == LastPos) return;
if (LastPos == Vector3.zero) LastPos = npc.info.spawnInfo.position;
if (Vector3.Distance(npc.player.transform.position, LastPos) < 5) {
SetMovementPoint(npc.player.transform.position, LastPos, 7f);
secondsTaken = 0.01f;
return;
}
returning = true;
npc.StartGo(LastPos);
}
public void Enable() {
//if (GetSpeed() <= 0) return;
enabled = true;
}
public void Disable() {
enabled = false;
}
public float GetMoveY(Vector3 position) {
if (IsSwimming()) {
float point = TerrainMeta.WaterMap.GetHeight(position) - 0.65f;
float groundY = GetGroundY(position);
if (groundY > point) {
return groundY;
}
return point - 0.65f;
}
return GetGroundY(position);
}
public float GetGroundY(Vector3 position) {
position = position + Vector3.up;
RaycastHit hitinfo;
if (Physics.Raycast(position, Vector3Down, out hitinfo, 100f, groundLayer)) {
return hitinfo.point.y;
}
return position.y -.5f;
}
public void CreateProjectileEffect(BaseCombatEntity target, BaseProjectile baseProjectile, float dmg, bool miss = false) {
if (baseProjectile.primaryMagazine.contents <= 0) {
#if DEBUG
Interface.Oxide.LogInfo("Attack failed(empty): {0} - {1}", npc.player.displayName, attackEntity.name);
#endif
return;
}
var component = baseProjectile.primaryMagazine.ammoType.GetComponent<ItemModProjectile>();
if (component == null) {
#if DEBUG
Interface.Oxide.LogInfo("Attack failed(Component): {0} - {1}", npc.player.displayName, attackEntity.name);
#endif
return;
}
npc.LookTowards(target.transform.position);
var source = npc.player.transform.position + npc.player.GetOffset();
if(baseProjectile.MuzzlePoint != null) {
source += Quaternion.LookRotation(target.transform.position - npc.player.transform.position) * baseProjectile.MuzzlePoint.position;
}
var dir = (target.transform.position + npc.player.GetOffset() - source).normalized;
var vector32 = dir * (component.projectileVelocity * baseProjectile.projectileVelocityScale);
Vector3 hit;
RaycastHit raycastHit;
if(Vector3.Distance(npc.player.transform.position, target.transform.position) < 0.5) {
hit = target.transform.position + npc.player.GetOffset(true);
}
else if(!Physics.SphereCast(source, .01f, vector32, out raycastHit, float.MaxValue, targetLayer)) {
#if DEBUG
Interface.Oxide.LogInfo("Attack failed: {0} - {1}", npc.player.displayName, attackEntity.name);
#endif
return;
}
else {
hit = raycastHit.point;
target = raycastHit.GetCollider().GetComponent<BaseCombatEntity>();
#if DEBUG
Interface.Oxide.LogInfo("Attack failed: {0} - {1}", raycastHit.GetCollider().name, (Rust.Layer)raycastHit.GetCollider().gameObject.layer);
#endif
miss = miss || target == null;
}
baseProjectile.primaryMagazine.contents--;
npc.ForceSignalAttack();
if (miss) {
var aimCone = baseProjectile.GetAimCone();
vector32 += Quaternion.Euler(UnityEngine.Random.Range((float)(-aimCone * 0.5), aimCone * 0.5f), UnityEngine.Random.Range((float)(-aimCone * 0.5), aimCone * 0.5f), UnityEngine.Random.Range((float)(-aimCone * 0.5), aimCone * 0.5f)) * npc.player.eyes.HeadForward();
}
Effect.server.Run(baseProjectile.attackFX.resourcePath, baseProjectile, StringPool.Get(baseProjectile.handBone), Vector3.zero, Vector3.forward);
var effect = new Effect();
effect.Init(Effect.Type.Projectile, source, vector32.normalized);
effect.scale = vector32.magnitude;
effect.pooledString = component.projectileObject.resourcePath;
effect.number = UnityEngine.Random.Range(0, 2147483647);
EffectNetwork.Send(effect);
Vector3 dest;
if (miss) {
dmg = 0;
dest = hit;
}
else {
dest = target.transform.position;
}
var hitInfo = new HitInfo(npc.player, target, DamageType.Bullet, dmg, dest) {
DidHit = !miss, HitEntity = target, PointStart = source, PointEnd = hit, HitPositionWorld = dest, HitNormalWorld = -dir, WeaponPrefab = GameManager.server.FindPrefab(StringPool.Get(baseProjectile.prefabID)).GetComponent<AttackEntity>(), Weapon = (AttackEntity)firstWeapon, HitMaterial = StringPool.Get("Flesh")
}
;
target?.OnAttacked(hitInfo);
Effect.server.ImpactEffect(hitInfo);
}
public void AttemptAttack(BaseCombatEntity entity) {
var weapon = firstWeapon as BaseProjectile;
if (weapon != null) {
if (!reloading && weapon.primaryMagazine.contents <= 0) {
reloading = true;
npc.player.SignalBroadcast(BaseEntity.Signal.Reload, string.Empty);
startedReload = Time.realtimeSinceStartup;
return;
}
if (reloading && Time.realtimeSinceStartup > startedReload + (npc.info.reloadDuration > 0 ? npc.info.reloadDuration : weapon.reloadTime)) {
reloading = false;
if (npc.info.needsAmmo) {
weapon.primaryMagazine.Reload(npc.player);
npc.player.inventory.ServerUpdate(0f);
}
else {
weapon.primaryMagazine.contents = weapon.primaryMagazine.capacity;
}
}
if (reloading) return;
}
if (!(Time.realtimeSinceStartup > lastHit + npc.info.damageInterval)) return;
lastHit = Time.realtimeSinceStartup;
DoAttack(entity, !HitChance());
}
public void DoAttack(BaseCombatEntity target, bool miss = false) {
if (npc == null) return;
var weapon = firstWeapon as BaseProjectile;
if (firstWeapon == null || (firstWeapon != null && (firstWeapon.IsDestroyed || weapon != null && weapon.primaryMagazine.contents == 0))) {
firstWeapon = npc.EquipFirstWeapon();
weapon = firstWeapon as BaseProjectile;
npc.SetActive(0);
}
var attackitem = firstWeapon?.GetItem();
if (attackitem == null) {
npc.EndAttackingEntity();
return;
}
if (attackitem.uid != npc.player.svActiveItemID) npc.SetActive(attackitem.uid);
float dmg = npc.info.damageAmount * UnityEngine.Random.Range(0.8f, 1.2f);
if (target is BaseNpc) dmg *= 1.5f;
else if (target is AutoTurret) dmg *= 3f;
if (weapon != null) {
//npc.ForceSignalGesture();
CreateProjectileEffect(target, weapon, dmg, miss);
}
else {
var hitInfo = new HitInfo(npc.player, target, DamageType.Stab, dmg, target.transform.position) {
PointStart = npc.player.transform.position, PointEnd = target.transform.position
}
;
target.SendMessage("OnAttacked", hitInfo, SendMessageOptions.DontRequireReceiver);
npc.ForceSignalAttack();
}
}
}
//////////////////////////////////////////////////////
/// class HumanPlayer : MonoBehaviour
/// MonoBehaviour: managed by UnityEngine
/// Takes care of all the sub categories of the HumanNPCs
//////////////////////////////////////////////////////
public class HumanPlayer : MonoBehaviour {
public HumanNPCInfo info;
public HumanLocomotion locomotion;
public HumanTrigger trigger;
public ProtectionProperties protection;
//public InstrumentKeyController instrument;
public BasePlayer player;
public float lastMessage;
private void Awake() {
player = GetComponent<BasePlayer>();
protection = ScriptableObject.CreateInstance<ProtectionProperties>();
}
public void SetInfo(HumanNPCInfo info, bool update = false) {
this.info = info;
if (info == null) return;
player.displayName = info.displayName;
SetViewAngle(info.spawnInfo.rotation);
player.syncPosition = true;
if (!update) {
//player.xp = ServerMgr.Xp.GetAgent(info.userid);
player.stats = new PlayerStatistics(player);
player.userID = info.userid;
player.UserIDString = player.userID.ToString();
player.MovePosition(info.spawnInfo.position);
player.eyes = player.eyes ?? player.GetComponent<PlayerEyes>();
//player.eyes.position = info.spawnInfo.position + new Vector3(0, 1.6f, 0);
var newEyes = info.spawnInfo.position + new Vector3(0, 1.6f, 0);
player.eyes.position.Set(newEyes.x, newEyes.y, newEyes.z);
player.EndSleeping();
protection.Clear();
foreach (var pro in info.protections) protection.Add(pro.Key, pro.Value);
}
if (locomotion != null) Destroy(locomotion);
locomotion = player.gameObject.AddComponent<HumanLocomotion>();
if (trigger != null) Destroy(trigger);
trigger = player.gameObject.AddComponent<HumanTrigger>();
lastMessage = Time.realtimeSinceStartup;
DisableMove();
AllowMove();
}
public void UpdateHealth(HumanNPCInfo info) {
player.InitializeHealth(info.health, info.health);
player.health = info.health;
}
public void Evade() {
this.locomotion.Evade();
}
public void AllowMove() {
locomotion?.Enable();
}
public void DisableMove() {
locomotion?.Disable();
}
public void TemporaryDisableMove(float thetime = -1f) {
if (thetime == -1f) thetime = info.stopandtalkSeconds;
DisableMove();
if (gameObject == null) return;
if (IsInvoking("AllowMove")) CancelInvoke("AllowMove");
Invoke("AllowMove", thetime);
}
public void EndAttackingEntity(bool trigger = true) {
if (locomotion.gameObject != null && locomotion.IsInvoking("PathFinding")) locomotion.CancelInvoke("PathFinding");
locomotion.noPath = 0;
locomotion.shouldMove = true;
if (trigger) {
Interface.Oxide.CallHook("OnNPCStopTarget", player, locomotion.attackEntity);
}
locomotion.attackEntity = null;
player.health = info.health;
locomotion.GetBackToLastPos();
SetActive(0);
}
public void EndFollowingEntity(bool trigger = true) {
if (locomotion.IsInvoking("PathFinding")) locomotion.CancelInvoke("PathFinding");
locomotion.noPath = 0;
locomotion.shouldMove = true;
if (trigger) {
Interface.Oxide.CallHook("OnNPCStopTarget", player, locomotion.followEntity);
}
locomotion.followEntity = null;
}
public void EndGo(bool trigger = true) {
if (locomotion.IsInvoking("PathFinding")) locomotion.CancelInvoke("PathFinding");
locomotion.noPath = 0;
locomotion.shouldMove = true;
if (trigger) {
Interface.Oxide.CallHook("OnNPCStopGo", player, locomotion.targetPosition);
}
if (locomotion.returning) {
locomotion.returning = false;
locomotion.SetMovementPoint(player.transform.position, locomotion.LastPos, 7f);
locomotion.secondsTaken = 0.01f;
}
locomotion.targetPosition = Vector3.zero;
}
public void StartAttackingEntity(BaseCombatEntity entity) {
if (locomotion.attackEntity != null && UnityEngine.Random.Range(0f, 1f) < 0.75f) return;
if (Interface.Oxide.CallHook("OnNPCStartTarget", player, entity) == null) {
var item = GetFirstWeaponItem();
if (item != null) SetActive(item.uid);
locomotion.attackEntity = entity;
locomotion.pathFinding = null;
if (locomotion.LastPos == Vector3.zero) locomotion.LastPos = player.transform.position;
if (gameObject != null && IsInvoking("AllowMove")) {
CancelInvoke("AllowMove");
AllowMove();
}
locomotion.Invoke("PathFinding", 0);
}
}
public void StartFollowingEntity(BaseEntity entity, string pname = "player") {
#if DEBUG
Interface.Oxide.LogInfo($"StartFollowingEntity() called for {pname}");
#endif
if (locomotion.targetPosition != Vector3.zero) {
EndGo(false);
}
player.SendNetworkUpdate();
locomotion.followEntity = entity;
locomotion.pathFinding = null;
if (locomotion.LastPos == Vector3.zero) locomotion.LastPos = player.transform.position;
// if (IsInvoking("AllowMove")) { CancelInvoke("AllowMove"); AllowMove(); }
locomotion.Invoke("PathFinding", 0);
}
public void StartGo(Vector3 position) {
if (locomotion.followEntity != null) {
EndFollowingEntity(false);
}
player.SendNetworkUpdate();
locomotion.targetPosition = position;
locomotion.pathFinding = null;
if (locomotion.LastPos == Vector3.zero) locomotion.LastPos = player.transform.position;
if (IsInvoking("AllowMove")) {
CancelInvoke("AllowMove");
AllowMove();
}
locomotion.Invoke("PathFinding", 0);
}
public HeldEntity GetCurrentWeapon() {
foreach (Item item in player.inventory.containerBelt.itemList) {
BaseEntity heldEntity = item.GetHeldEntity();
if (heldEntity is HeldEntity && !heldEntity.HasFlag(BaseEntity.Flags.Disabled)) return (HeldEntity)heldEntity;
}
return null;
}
public Item GetFirstWeaponItem() {
return GetFirstWeapon()?.GetItem();
}
public HeldEntity GetFirstWeapon() {
foreach (Item item in player.inventory.containerBelt.itemList) {
if(item.CanBeHeld() && HasAmmo(item) && (item.info.category == ItemCategory.Weapon)) {
return item.GetHeldEntity() as HeldEntity;
}
}
return null;
}
public HeldEntity GetFirstTool() {
foreach (Item item in player.inventory.containerBelt.itemList) {
if(item.CanBeHeld() && item.info.category == ItemCategory.Tool) {
return item.GetHeldEntity() as HeldEntity;
}
}
return null;
}
public HeldEntity GetFirstMisc() {
foreach (Item item in player.inventory.containerBelt.itemList) {
if(item.CanBeHeld() && item.info.category != ItemCategory.Tool && item.info.category != ItemCategory.Weapon) {
return item.GetHeldEntity() as HeldEntity;
}
}
return null;
}
public HeldEntity GetFirstInstrument() {
foreach (Item item in player.inventory.containerBelt.itemList) {
if(item.CanBeHeld() && item.info.category == ItemCategory.Fun) {
return item.GetHeldEntity() as HeldEntity;
}
}
return null;
}
public List<Item> GetAmmo(Item item) {
var ammos = new List<Item>();
AmmoTypes ammoType;
if (!ammoTypes.TryGetValue(item.info.shortname, out ammoType)) return ammos;
player.inventory.FindAmmo(ammos, ammoType);
return ammos;
}
public bool HasAmmo(Item item) {
if (!info.needsAmmo) return true;
var weapon = item.GetHeldEntity() as BaseProjectile;
if (weapon == null) return true;
return weapon.primaryMagazine.contents > 0 || weapon.primaryMagazine.CanReload(player);
}
public void UnequipAll() {
if (player.inventory?.containerBelt == null) return;
foreach (Item item in player.inventory.containerBelt.itemList) {
if (item.CanBeHeld()) (item.GetHeldEntity() as HeldEntity)?.SetHeld(false);
}
}
public HeldEntity EquipFirstWeapon() {
HeldEntity weapon = GetFirstWeapon();
if (weapon != null) {
UnequipAll();
weapon.SetHeld(true);
}
return weapon;
}
public HeldEntity EquipFirstTool() {
HeldEntity tool = GetFirstTool();
if (tool != null) {
UnequipAll();
tool.SetHeld(true);
}
return tool;
}
public HeldEntity EquipFirstMisc() {
HeldEntity misc = GetFirstMisc();
if (misc != null) {
UnequipAll();
misc.SetHeld(true);
}
return misc;
}
public HeldEntity EquipFirstInstrument() {
HeldEntity misc = GetFirstInstrument();
if (misc != null) {
UnequipAll();
misc.SetHeld(true);
}
return misc;
}
public void SetActive(uint id) {
player.svActiveItemID = id;
player.SendNetworkUpdate();
player.SignalBroadcast(BaseEntity.Signal.Reload, string.Empty);
}
private void OnDestroy() {
Destroy(locomotion);
Destroy(trigger);
Destroy(protection);
}
public void LookTowards(Vector3 pos) {
if (pos != player.transform.position) SetViewAngle(Quaternion.LookRotation(pos - player.transform.position));
//player.transform.LookAt(pos - player.transform.position);
player.eyes.position.Set(pos.x, pos.y, pos.z);
}
public void ForceSignalGesture() {
player.SignalBroadcast(BaseEntity.Signal.Gesture, "pickup_item");
}
public void ForceSignalAttack() {
player.SignalBroadcast(BaseEntity.Signal.Attack, string.Empty);
}
public void SetViewAngle(Quaternion viewAngles) {
if (viewAngles.eulerAngles == default(Vector3)) return;
player.viewAngles = viewAngles.eulerAngles;
player.SendNetworkUpdate();
}
}
//////////////////////////////////////////////////////
/// class HumanNPCInfo
/// NPC information that will be saved inside the datafile
/// public => will be saved in the data file
/// non public
// ... [Code preview truncated due to length. Full source in attached file] ...