Skip to content

Instantly share code, notes, and snippets.

@nevadascout
Created November 10, 2015 15:39
Show Gist options
  • Save nevadascout/9d902ab6e888474634f9 to your computer and use it in GitHub Desktop.
Save nevadascout/9d902ab6e888474634f9 to your computer and use it in GitHub Desktop.
Gather level modifier for Rust (Oxide plugin)
//-----------------------------------------------------------------------
// <copyright file="GatherLevels.cs" company="RustFactions">
// Player Gather Level Modifier for Rust Oxide
// Written by nevada_scout for Rust Factions (http://reddit.com/r/rustfactions)
// Copyright (c) 2015 nevada_scout
// </copyright>
//-----------------------------------------------------------------------
namespace Oxide.Plugins
{
using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Random = System.Random;
[Info("GatherLevels", "nevada_scout", "1.0")]
public class GatherLevels : RustPlugin
{
private StoredData storedData;
private string dataFile = "gatherlevels.data";
private readonly Random rand = new Random();
// If the player gather rate is less than 2x, use this rate to increase
private readonly float quickIncreaseRate = 0.001f;
// If the player gather rate is more than 2x, use this rate to increase
private readonly float slowIncreaseRate = 0.0007f;
private void Loaded()
{
this.storedData = Interface.GetMod().DataFileSystem.ReadObject<StoredData>(this.dataFile);
if (this.storedData.Players == null) this.storedData.Players = new HashSet<PlayerGatherLevel>();
}
#region Player Levelling
[ChatCommand("rates")]
private void RatesCmd(BasePlayer player, string cmd, string[] args)
{
if (player != null)
{
var rates = this.GetRateMultipliers(player);
this.SendReply(player, "Gather Rate Multipliers ---------");
this.SendReply(player, $"Tree: {Math.Round(rates.Tree, 3)}x");
this.SendReply(player, $"Ores: {Math.Round(rates.Ore, 3)}x");
this.SendReply(player, $"Flesh: {Math.Round(rates.Flesh, 3)}x");
this.SendReply(player, $"Other: {Math.Round(rates.Other, 3)}x");
}
}
/// <summary>
/// Increase gather rates from trees, ores, animals, etc
/// </summary>
private void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
{
var player = entity.ToPlayer();
if (player == null) return;
var rateMultipliers = this.GetRateMultipliers(player);
float multiplier;
switch (dispenser.gatherType)
{
case ResourceDispenser.GatherType.Tree:
multiplier = rateMultipliers.Tree;
// Increase gather rate
rateMultipliers.Tree = this.IncreaseRate(rateMultipliers.Tree);
break;
case ResourceDispenser.GatherType.Ore:
multiplier = rateMultipliers.Ore;
// Increase gather rate
rateMultipliers.Ore = this.IncreaseRate(rateMultipliers.Ore);
break;
case ResourceDispenser.GatherType.Flesh:
multiplier = rateMultipliers.Flesh;
// Increase gather rate
rateMultipliers.Flesh = this.IncreaseRate(rateMultipliers.Flesh);
break;
default:
multiplier = rateMultipliers.Other;
// Increase gather rate
rateMultipliers.Other = this.IncreaseRate(rateMultipliers.Other);
break;
}
// Apply rate multiplier
item.amount = Convert.ToInt32(item.amount * multiplier);
// Save changes made to rate multipliers
this.SaveRateMultipliers(player.userID, rateMultipliers);
}
/// <summary>
/// Increase hemp gather rate -- doesn't seem to actually work
/// </summary>
private void OnPlantGather(PlantEntity plant, Item item, BasePlayer player)
{
if (player == null) return;
var rateMultipliers = this.GetRateMultipliers(player);
// Apply rate multiplier
item.amount = Convert.ToInt32(item.amount * rateMultipliers.Plant);
// Increase gather rate
rateMultipliers.Plant = this.IncreaseRate(rateMultipliers.Plant);
this.SaveRateMultipliers(player.userID, rateMultipliers);
}
/// <summary>
/// Save player levelling progress to disk
/// </summary>
private void OnServerSave()
{
Interface.GetMod().DataFileSystem.WriteObject(this.dataFile, this.storedData);
}
/// <summary>
/// Reset player progress on death
/// </summary>
private void OnPlayerRespawned(BasePlayer player)
{
if (player == null) return;
var defaultRates = new RateMultipliers();
this.SaveRateMultipliers(player.userID, defaultRates);
this.SendReply(player, "You died! Your gather rate multiplers have been reset back to 1x");
}
private float IncreaseRate(float currentRate)
{
if (currentRate < 3f)
{
if (currentRate < 2f)
{
// We're less than 2x, so increase quickly
currentRate += this.quickIncreaseRate;
}
else
{
// We're between 2x and 3x, so increase slowly
currentRate += this.slowIncreaseRate;
}
}
return currentRate;
}
private RateMultipliers GetRateMultipliers(BasePlayer player)
{
var data = this.storedData.Players.FirstOrDefault(p => p.UserId == player.userID);
if (data != null)
{
return data.GatherMultipliers;
}
// ELSE player is not found, so create a new entry for the player
this.storedData.Players.Add(new PlayerGatherLevel(player, new RateMultipliers()));
return new RateMultipliers();
}
private void SaveRateMultipliers(ulong userId, RateMultipliers rateMultipliers)
{
var data = this.storedData.Players.FirstOrDefault(p => p.UserId == userId);
if (data != null)
{
data.GatherMultipliers = rateMultipliers;
}
}
#endregion
#region DataTable
internal class StoredData
{
public HashSet<PlayerGatherLevel> Players = new HashSet<PlayerGatherLevel>();
}
internal class PlayerGatherLevel
{
public ulong UserId;
public RateMultipliers GatherMultipliers;
public PlayerGatherLevel()
{
}
public PlayerGatherLevel(BasePlayer player, RateMultipliers gatherMultipliers)
{
this.UserId = player.userID;
this.GatherMultipliers = gatherMultipliers;
}
}
internal class RateMultipliers
{
public float Tree;
public float Ore;
public float Flesh;
public float Plant;
public float Other;
public RateMultipliers()
{
this.Tree = 1f;
this.Ore = 1f;
this.Flesh = 1f;
this.Plant = 1f;
this.Other = 1f;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment