Files
tbd-station-14/Content.Server/GameTicking/GameTicker.Spawning.cs
collinlunn f2816e8081 Moves Hands to shared, some prediction (#3829)
* HandsGuiState

* Gui state setting methods

* code cleanup

* Removes TryGetHands

* ClientHand

* Gui Hands

* Refactor WIP 1

* Hand index

* refactors 2

* wip 3

* wip 4

* wiip 4

* wip 5

* wip 6

* wip 7

* wip 8

* wip 9

* wip 11

* Hand ui mostly looks fine

* hands gui cleanup 1

* cleanup 2

* wip 13

* hand enabled

* stuff

* Hands gui gap fix

* onpressed test

* hand gui buttons events work

* bag activation works

* fix item use

* todo comment

* hands activate fix

* Moves Client Hands back to using strings to identify active hand

* fixes action hand highlighting

* diff fix

* serverhand

* SharedHand

* SharedHand, IReadOnlyHand

* Client Hands only stores SharedHand

* cleanup server hands

* server hand container shutdown

* misc renames, refactors of serverhand

* stuff 1

* stuff 3

* server hand refactor 1

* Undo API changes to remove massive diff

* More API name fixes

* server hands cleanup 2

* cleanup 3

* dropping cleanup

* Cleanup 4

* MoveItemFromHand

* Stuff

* region sorting

* Hand Putter methods cleanup

* stuff 2

* Merges all of serverhand and clienthand into sharedhand

* Other hands systems, hack to make inhands update (gui state set every frame, visualzier updated every frame)

* GetFinalDropCoordinates cleanup

* SwapHands cleanup

* Moves server hands code to shared hands

* Fixed hand selected and deselected

* Naming fixes

* Server hands system cleanup

* Hands privacy fixes

* Client hand updates when containers are modified

* HeldItemVisualizer

* Fixes hand gui item status panel

* method name fix

* Swap hands prediction

* Dropping prediction

* Fixes pickup entity animation

* Fixes HeldItemsVisualizer

* moves item pickup to shared

* PR cleanup

* fixes hand enabling/disabling

* build fix

* Conflict fixes

* Fixes pickup animation

* Uses component directed message subscriptions

* event unsubscriptions in hand system

* unsubscribe fix

* CanInsertEntityIntoHand checks if hand is enabled

* Moving items from one hand to another checks if the hands can pick up and drop

* Fixes stop pulling not re-enabling hand

* Fixes pickup animation for entities containers on the floor

* Fixes using held items

* Fixes multiple hands guis appearing

* test fix

* removes obsolete system sunsubscribes

* Checks IsFirstTimePredicted before playing drop animation

* fixes hand item deleted crash

* Uses Get to get other system

* Replaces AppearanceComponent with SharedAppearanceComponent

* Replaces EnsureComponent with TryGetComponent

* Improves event class names

* Moves property up to top of class

* Moves code for determining the hand visualizer rsi state into the visualizer instead of being determined on hand component

* Eventbus todo comment

* Yaml fix for changed visualizer name

* Makes HandsVisuals a byte

* Removes state from HandsVisualizer

* Fixes hand using interaction method name

* Namespace changes fixes

* Fix for changed hand interaction method

* missing }

* conflict build fix

* Moves cleint HandsSystem to correct folder

* Moved namespace fix for interaction test

* Moves Handsvisualizer ot correct folder

* Moves SharedHandsSystem to correct folder

* Fixes errors from moving namespace of hand systems

* Fixes PDA component changes

* Fixes ActionsComponent diff

* Fixes inventory component diff

* fixes null ref

* Replaces obsolete Loc.GetString usage with fluent translations

* Fluent for hands disarming

* SwapHands and Drop user input specify to the server which hand

* Pickup animation WorldPosiiton todo

* Cleans up hands gui subscription handling

* Fixes change in ActionBlockerSystem access

* Namespace references fixes

* HandsComponent PlayerAttached/Detached messages are handled through eventbus

* Fixes GasCanisterSystem drop method usage

* Fix gameticker equipping method at new location

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2021-06-21 19:21:20 +10:00

297 lines
9.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using Content.Server.Access.Components;
using Content.Server.CharacterAppearance.Components;
using Content.Server.Ghost.Components;
using Content.Server.Hands.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Server.PDA;
using Content.Server.Players;
using Content.Server.Roles;
using Content.Server.Spawners.Components;
using Content.Server.Speech.Components;
using Content.Shared.GameTicking;
using Content.Shared.Inventory;
using Content.Shared.Preferences;
using Content.Shared.Roles;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Random;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameTicking
{
public partial class GameTicker
{
private const string PlayerPrototypeName = "HumanMob_Content";
private const string ObserverPrototypeName = "MobObserver";
[ViewVariables(VVAccess.ReadWrite)]
private EntityCoordinates _spawnPoint;
// Mainly to avoid allocations.
private readonly List<EntityCoordinates> _possiblePositions = new();
private void SpawnPlayer(IPlayerSession player, string? jobId = null, bool lateJoin = true)
{
var character = GetPlayerProfile(player);
SpawnPlayer(player, character, jobId, lateJoin);
UpdateJobsAvailable();
}
private void SpawnPlayer(IPlayerSession player, HumanoidCharacterProfile character, string? jobId = null, bool lateJoin = true)
{
// Can't spawn players with a dummy ticker!
if (DummyTicker)
return;
if (lateJoin && DisallowLateJoin)
{
MakeObserve(player);
return;
}
PlayerJoinGame(player);
var data = player.ContentData();
DebugTools.AssertNotNull(data);
data!.WipeMind();
data.Mind = new Mind.Mind(player.UserId)
{
CharacterName = character.Name
};
// Pick best job best on prefs.
jobId ??= PickBestAvailableJob(character);
var jobPrototype = _prototypeManager.Index<JobPrototype>(jobId);
var job = new Job(data.Mind, jobPrototype);
data.Mind.AddRole(job);
if (lateJoin)
{
_chatManager.DispatchStationAnnouncement(Loc.GetString(
"latejoin-arrival-announcement",
("character", character.Name),
("job", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(job.Name))
), Loc.GetString("latejoin-arrival-sender"));
}
var mob = SpawnPlayerMob(job, character, lateJoin);
data.Mind.TransferTo(mob);
if (player.UserId == new Guid("{e887eb93-f503-4b65-95b6-2f282c014192}"))
{
mob.AddComponent<OwOAccentComponent>();
}
AddManifestEntry(character.Name, jobId);
AddSpawnedPosition(jobId);
EquipIdCard(mob, character.Name, jobPrototype);
jobPrototype.Special?.AfterEquip(mob);
Preset?.OnSpawnPlayerCompleted(player, mob, lateJoin);
}
public void Respawn(IPlayerSession player)
{
player.ContentData()?.WipeMind();
if (LobbyEnabled)
PlayerJoinLobby(player);
else
SpawnPlayer(player);
}
public void MakeJoinGame(IPlayerSession player, string? jobId = null)
{
if (!_playersInLobby.ContainsKey(player)) return;
if (!_prefsManager.HavePreferencesLoaded(player))
{
return;
}
SpawnPlayer(player, jobId);
}
public void MakeObserve(IPlayerSession player)
{
// Can't spawn players with a dummy ticker!
if (DummyTicker)
return;
if (!_playersInLobby.ContainsKey(player)) return;
PlayerJoinGame(player);
var name = GetPlayerProfile(player).Name;
var data = player.ContentData();
DebugTools.AssertNotNull(data);
data!.WipeMind();
data.Mind = new Mind.Mind(player.UserId);
var mob = SpawnObserverMob();
mob.Name = name;
mob.GetComponent<GhostComponent>().CanReturnToBody = false;
data.Mind.TransferTo(mob);
_playersInLobby[player] = LobbyPlayerStatus.Observer;
RaiseNetworkEvent(GetStatusSingle(player, LobbyPlayerStatus.Observer));
}
#region Mob Spawning Helpers
private IEntity SpawnPlayerMob(Job job, HumanoidCharacterProfile? profile, bool lateJoin = true)
{
var coordinates = lateJoin ? GetLateJoinSpawnPoint() : GetJobSpawnPoint(job.Prototype.ID);
var entity = _entityManager.SpawnEntity(PlayerPrototypeName, coordinates);
if (job.StartingGear != null)
{
var startingGear = _prototypeManager.Index<StartingGearPrototype>(job.StartingGear);
EquipStartingGear(entity, startingGear, profile);
}
if (profile != null)
{
entity.GetComponent<HumanoidAppearanceComponent>().UpdateFromProfile(profile);
entity.Name = profile.Name;
}
return entity;
}
private IEntity SpawnObserverMob()
{
var coordinates = GetObserverSpawnPoint();
return _entityManager.SpawnEntity(ObserverPrototypeName, coordinates);
}
#endregion
#region Equip Helpers
public void EquipStartingGear(IEntity entity, StartingGearPrototype startingGear, HumanoidCharacterProfile? profile)
{
if (entity.TryGetComponent(out InventoryComponent? inventory))
{
foreach (var slot in EquipmentSlotDefines.AllSlots)
{
var equipmentStr = startingGear.GetGear(slot, profile);
if (equipmentStr != string.Empty)
{
var equipmentEntity = _entityManager.SpawnEntity(equipmentStr, entity.Transform.Coordinates);
inventory.Equip(slot, equipmentEntity.GetComponent<ItemComponent>());
}
}
}
if (entity.TryGetComponent(out HandsComponent? handsComponent))
{
var inhand = startingGear.Inhand;
foreach (var (hand, prototype) in inhand)
{
var inhandEntity = _entityManager.SpawnEntity(prototype, entity.Transform.Coordinates);
handsComponent.TryPickupEntity(hand, inhandEntity, checkActionBlocker: false);
}
}
}
public void EquipIdCard(IEntity entity, string characterName, JobPrototype jobPrototype)
{
if (!entity.TryGetComponent(out InventoryComponent? inventory))
return;
if (!inventory.TryGetSlotItem(EquipmentSlotDefines.Slots.IDCARD, out ItemComponent? item))
{
return;
}
var itemEntity = item.Owner;
if (!itemEntity.TryGetComponent(out PDAComponent? pdaComponent) || pdaComponent.ContainedID == null)
return;
var card = pdaComponent.ContainedID;
card.FullName = characterName;
card.JobTitle = jobPrototype.Name;
var access = card.Owner.GetComponent<AccessComponent>();
var accessTags = access.Tags;
accessTags.UnionWith(jobPrototype.Access);
pdaComponent.SetPDAOwner(characterName);
}
#endregion
private void AddManifestEntry(string characterName, string jobId)
{
_manifest.Add(new ManifestEntry(characterName, jobId));
}
#region Spawn Points
public EntityCoordinates GetJobSpawnPoint(string jobId)
{
var location = _spawnPoint;
_possiblePositions.Clear();
foreach (var (point, transform) in ComponentManager.EntityQuery<SpawnPointComponent, ITransformComponent>())
{
if (point.SpawnType == SpawnPointType.Job && point.Job?.ID == jobId)
_possiblePositions.Add(transform.Coordinates);
}
if (_possiblePositions.Count != 0)
location = _robustRandom.Pick(_possiblePositions);
return location;
}
public EntityCoordinates GetLateJoinSpawnPoint()
{
var location = _spawnPoint;
_possiblePositions.Clear();
foreach (var (point, transform) in ComponentManager.EntityQuery<SpawnPointComponent, ITransformComponent>())
{
if (point.SpawnType == SpawnPointType.LateJoin) _possiblePositions.Add(transform.Coordinates);
}
if (_possiblePositions.Count != 0)
location = _robustRandom.Pick(_possiblePositions);
return location;
}
public EntityCoordinates GetObserverSpawnPoint()
{
var location = _spawnPoint;
_possiblePositions.Clear();
foreach (var (point, transform) in ComponentManager.EntityQuery<SpawnPointComponent, ITransformComponent>())
{
if (point.SpawnType == SpawnPointType.Observer)
_possiblePositions.Add(transform.Coordinates);
}
if (_possiblePositions.Count != 0)
location = _robustRandom.Pick(_possiblePositions);
return location;
}
#endregion
}
}