// The clipping audit, header: counts enemies passing through geometry, in seconds on screen.
// Log-only; it never changes the game.
// Shared by BYOK Games under the MIT License
// Every 0.1 s of game time, for every live enemy (not the Wurst's own franks): each visible mesh's bounds, shrunk 15 percent,
// against world static and dynamic geometry (physics overlap plus the engine's penetration depth). Floor and ceiling contact
// (the push out is vertical) and intended contacts (a spider clinging, the Glaze on its ceiling, a Hanger on its hook, trays)
// are ignored. A penetration deeper than 8 cm lasting over 0.2 s, inside the camera's view within 15 m, logs
//   SPL NOCLIP pen area=... t0=... dur=... onscreen=... enemy=... uid=... through=<actor>/<component>/<mesh> depth=...
// Also SPL NOCLIP cam (the camera's near plane inside a mesh) and SPL NOCLIP spawn_in_view (a new enemy inside the view within
// 12 m with nothing between). SPL NOCLIP world checks that the level has collision at all.
#pragma once

#include "CoreMinimal.h"
#include "Subsystems/WorldSubsystem.h"
#include "SPNoclip.generated.h"

class ASPDirector;

// eng17 (-SPChunkLeap, the chunks' hops home): what lies under the ground line A..B at chunk height (FloorZ + 35): the highest
// top of any world mesh box it crosses (OutTop, world z; FloorZ when clear) and whether a tall one (over 2.8 m: a column, a wall,
// a machine) stands across it (bOutWall; meshes holding Goal never count as a wall). Mesh boxes gathered once per area.
// a spot near P (within 4 m) whose chunk-sized box (60 cm) is inside no world mesh box; false when P itself is clear
SPOILED_API bool SPHopFreeSpot(UWorld* World, const FString& Area, const FVector& P, float FloorZ, FVector& Out);
// the top (world z) of the highest world mesh box holding a chunk-sized box at P; P.Z when none does
SPOILED_API float SPHopTopAt(UWorld* World, const FString& Area, const FVector& P, float FloorZ);
SPOILED_API bool SPHopLine(UWorld* World, const FString& Area, const FVector& A, const FVector& B, float FloorZ, const FVector& Goal, float& OutTop, bool& bOutWall);

UCLASS()
class SPOILED_API USPNoclip : public UTickableWorldSubsystem
{
	GENERATED_BODY()
public:
	virtual bool ShouldCreateSubsystem(UObject* Outer) const override;
	virtual void Tick(float Dt) override;
	virtual TStatId GetStatId() const override;

private:
	struct FPen { float T0 = 0.f, Last = 0.f, Depth = 0.f, OnScreen = 0.f; FString Enemy, Through, Area; int32 Uid = 0; bool bLogged = false; };
	UPROPERTY() TObjectPtr<ASPDirector> Director;
	TMap<FString, FPen> Open;
	TSet<int32> Seen;
	float Next = 0.f, CamT0 = -1.f, CamDepth = 0.f, WorldNext = 0.f;
	FString CamWhat;
	struct FMesh { class UStaticMeshComponent* C = nullptr; FTransform X; FBox Lb; float R = 0.f; FVector O; };
	TArray<FMesh> Meshes;
	FString MeshArea;
	float MeshNext = 0.f;
	int32 Events = 0;
	void Close(FPen& P);
};

// eng17: -SPKeepOut. The Wurst's shed chunks crawl home in straight lines and went through the grinder hall's platform,
// columns and machines (the audit's top offender, about 80 s on screen in one ride). Each tick after the director's sim, a
// crawling chunk that is inside a world mesh's box climbs over it when it is low (under 1.2 m) and slides along its face when
// it is tall. No physics. A chunk pushed for over 2 s is let go, so none is ever stuck short of the body. Meshes are gathered
// once per area. SPL KEEPOUT logs the area's mesh count and, every 10 s, how many pushes it made.
UCLASS()
class SPOILED_API USPKeepOut : public UTickableWorldSubsystem
{
	GENERATED_BODY()
public:
	virtual bool ShouldCreateSubsystem(UObject* Outer) const override;
	virtual void Tick(float Dt) override;
	virtual TStatId GetStatId() const override;
private:
	struct FMesh { FTransform X; FBox Lb; FVector O; float R = 0.f; float Top = 0.f; float Height = 0.f; };
	UPROPERTY() TObjectPtr<ASPDirector> Director;
	TArray<FMesh> Meshes;
	TMultiMap<FIntPoint, int32> Grid;
	FString MeshArea;
	TMap<int32, float> Pushed;   // uid -> seconds pushed
	int32 Pushes = 0;
	float LogNext = 0.f;
	void Gather();
};

