using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("FightZone", "YourName", "1.0.0")]
[Description("Plugin that adds a fight zone with rocks to the map and rewards players for getting kills inside it.")]
public class FightZone : RustPlugin
{
private bool fightZoneActive = false;
private Vector3 fightZoneCenter = new Vector3(0, 0, 0);
private float fightZoneRadius = 50;
private int rewardAmount = 100;
private int rewardInterval = 60;
private Timer timer;
private Dictionary<ulong, int> playerKills = new Dictionary<ulong, int>();
private List<GameObject> rocks = new List<GameObject>();
[PluginReference]
Plugin Economics;
private void OnServerInitialized()
{
GenerateRocks();
timer = this.timer.Repeat(rewardInterval, 0, () =>
{
if (fightZoneActive)
{
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
if (Vector3.Distance(player.transform.position, fightZoneCenter) < fightZoneRadius)
{
int kills = 0;
playerKills.TryGetValue(player.userID, out kills);
playerKills[player.userID] = kills + 1;
Economics?.Call("Deposit", player.UserIDString, rewardAmount);
}
}
}
});
}
private void OnPlayerDeath(BasePlayer player, HitInfo hitInfo)
{
if (player != null && hitInfo != null && hitInfo.Initiator != null)
{
BasePlayer killer = hitInfo.Initiator.ToPlayer();
if (killer != null && playerKills.ContainsKey(killer.userID))
{
int kills = 0;
playerKills.TryGetValue(killer.userID, out kills);
playerKills[killer.userID] = kills + 1;
}
}
}
private void OnTick()
{
if (fightZoneActive)
{
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
if (Vector3.Distance(player.transform.position, fightZoneCenter) > fightZoneRadius)
{
playerKills.Remove(player.userID);
}
}
foreach (GameObject rock in rocks)
{
if (Vector3.Distance(rock.transform.position, fightZoneCenter) > fightZoneRadius)
{
Pool.Free(ref rock);
}
}
}
}
private void GenerateRocks()
{
float xMin = fightZoneCenter.x - fightZoneRadius;
float xMax = fightZoneCenter.x + fightZoneRadius;
float zMin = fightZoneCenter.z - fightZoneRadius;
float zMax = fightZoneCenter.z + fightZoneRadius;
for (float x = xMin; x <= xMax; x = x + 2.5f)
{
for (float z = zMin; z <= zMax; z = z + 2.5f)
{
if (Vector3.Distance(new Vector3(x, 0, z), fightZoneCenter) <= 30)
{
Vector3 position = new Vector3(x, TerrainMeta.HeightMap.GetHeight(new Vector3(x, 0, z)), z);
Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
GameObject rock = GameManager.server.CreateEntity("assets/bundled/prefabs/autospawn/resource/ore/stone-01.prefab", position, rotation).gameObject;
rock.Spawn();
rocks.Add(rock);
}
}
}
}
[ChatCommand("fightzone")]
private void cmdFightZone(BasePlayer player, string command, string[] args)
{
if (player.IsAdmin)
{
if (args.Length == 1 && args[0] == "start")
{
fightZoneActive = true;
fightZoneCenter = player.transform.position;
player.ChatMessage("Fight zone started. Get kills inside the marked area to earn rewards!");
DrawFightZone(player);
}
else if (args.Length == 1 && args[0] == "stop")
{
fightZoneActive = false;
player.ChatMessage("Fight zone stopped.");
RemoveFightZone(player);
foreach (GameObject rock in rocks)
{
Pool.Free(ref rock);
}
rocks.Clear();
}
else
{
player.ChatMessage("Usage: /fightzone start|stop");
}
}
}
private void OnPlayerDisconnected(BasePlayer player, string reason)
{
playerKills.Remove(player.userID);
}
private void OnPlayerRespawned(BasePlayer player)
{
playerKills.Remove(player.userID);
}
private void OnPlayerDisconnected(Network.Connection connection)
{
ulong userID = connection.userid;
playerKills.Remove(userID);
}
private void OnPlayerRespawned(Connection connection)
{
ulong userID = connection.userid;
playerKills.Remove(userID);
}
private void DrawFightZone(BasePlayer player)
{
CuiElementContainer container = new CuiElementContainer();
string panelName = "FightZonePanel";
float posX = fightZoneCenter.x - fightZoneRadius;
float posY = fightZoneCenter.y - fightZoneRadius;
float posZ = fightZoneCenter.z - fightZoneRadius;
float size = fightZoneRadius * 2;
container.Add(new CuiPanel
{
Image = { Color = "0.2 0.2 0.2 0.5" },
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" },
CursorEnabled = false,
FadeOut = 1f,
Name = panelName
});
container.Add(new CuiElement
{
Parent = panelName,
Components =
{
new CuiRawImageComponent
{
Color = "1 0.2 0.2 0.5",
Sprite = "assets/content/ui/uibackgroundblur-ingamemenu.png"
},
new CuiRectTransformComponent
{
AnchorMin = "0 0",
AnchorMax = "1 1",
OffsetMin = "0 0",
OffsetMax = "0 0"
}
}
});
foreach (GameObject rock in rocks)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(rock.transform.position);
if (screenPos.z > 0)
{
float x = screenPos.x / Screen.width;
float y = screenPos.y / Screen.height;
container.Add(new CuiElement
{
Parent = panelName,
Name = UnityEngine.Random.Range(0, 2147483647).ToString(),
Components =
{
new CuiRawImageComponent
{
Color = "1 1 1 1",
Sprite = "assets/content/ui/cursors/cursorhandheld.prefab",
Material = "assets/content/ui/uibackgroundblur-ingamemenu.mat"
},
new CuiRectTransformComponent
{
AnchorMin = x + " " + y,
AnchorMax = x + " " + y,
OffsetMin = "-10 -10",
OffsetMax = "10 10"
}
}
});
}
}
CuiHelper.AddUi(player, container);
}
private void RemoveFightZone(BasePlayer player)
{
CuiHelper.DestroyUi(player, "FightZonePanel");
}
private void OnPlayerInit(BasePlayer player)
{
if (fightZoneActive)
{
DrawFightZone(player);
}
}
private void OnPlayerSleepEnded(BasePlayer player)
{
if (fightZoneActive)
{
DrawFightZone(player);
}
}
private void OnPlayerSleep(BasePlayer player)
{
if (fightZoneActive)
{
RemoveFightZone(player);
}
}
}
}