ModernUO/Projects/Scripts/Misc/SkillCheck.cs
Kamron Batman 179cb50557 Updates Packets & Randomizer (#43)
* Fixes a bug with the main loop. Removes unnecessary optimizations for RDRand.

* WIP - Rewriting packets.

* Cleanup and updates README

* Converts more packets

* Fixes header

* Converts Effects packets.

* Forgot the send command

* Adds the start of containers packets.

* Adds message packets with caching

* Extends the send methods

* Starts to add Acquire methods

* Converted the packets

* Cleanup

* Cleanup

* Adds more packets

* Adds more packets

* Fixes clearing arrays from pool. Adds Mobile Incoming

* Converts more packets.

* Moves more packets

* Moves more packets

* Test compression

* Merges

* Migrating to an idiomatic syntax that also supports compression, and proxying.

* Optimize the stack alloc. Changes attributes

* Converted container packets

* Visual Studio doesn't auto save files. I am still getting used to subpar IDEs.

* Adds static packet caching. Will profile later. Converts more packets

* Fixes packets and changes how compression is configured

* WIP - Adds basics for Gumps. Not finished though.

* Fix file

* Fixes formatting

* Changed SpanWriter to be more idiomatic.

* Fixes Span vs RawSpan and missing stackallocs

* Converts over some more gumps

* WIP - Deletes 32bit support.

* Drops RDRand32 support.

* Fixes gump compilation

* Converting gump components

* Removes old huffman compression function

* Revert signature for backwards compatibility

* WIP - Converting more gump components

* Creates ArraySet for the strings. Updates AppendTo to reference that.

* Converts the maining gump components

* Cleans up gump components

* Cleans up directives

* Cleans up ArraySet and moves it. Adds null-coalescing-assignment

* Cleanup, Target Packets, and C# 8 changes.

* Removed OPL Packet

* World packets

* Fixing packet uses

* Cleans up code. Fixes packet uses in various places.

* More cleanup for packets

* Code cleanup

* Converts more packet uses and cleans up more code

* More code cleanup

* Finishes fixing the packets in Item

* Updates secure trade packets

* Updates core and gets it to compile.

* Moved packets to scripts. Fixed account handler use of packets

* Code cleanup

* Updates chat packets

* Code cleanuo

* Adds party packets, but need to implement them.

* Party packets WIP

* Finishes party packets

* Rearrange movement namespaces and classes

* Finishes plant packets

* Code Formatting

* Fixes moving effects

* Code cleanup, eliminates equipinfo

* Adds more packets. Fixes bugs with various packets.

* Finishes mahjon packets

* Fixes mahjong packet

* Code cleanup

* Finishes mahjong packets

* Adds Map packets

* Add multifacet maps and charts

* Cleans up some packets with UTF8

* Optimizes packets

* Cleans up more packets. Moves the MessageHelper

* Removes assistant support. Removes extended protocol. Incorporates MapUO packets as normal packets.

* Updates protocol extensions packet receiver

* Fixes a few bugs. Fixes a few more packets.

* Code cleanup and fixing more packets

* Fixes packet effects

* Cleaned up more code

* Buff Icon cleanup

* Removed unused constructors

* Code Cleanup. Adds BoatHS Packets

* Moves house files. Updates house foundation packets.

* Deployment cleanup

* Fixes:

* Code cleanup

* More code cleanup

* More code cleanup

* Cleaned up BaseHouse

* Enforces styling

* Converts foreach to linq where possible.

* Dont need that

* Goals/Readme updates

* Removes 32bit support at the highest level. Turns on HRT by default.

* Code cleanup. Fixes extended features packet.

* Fixes various bugs

* Code cleanup

* Code cleanup

* Code cleanup. Fixes gump X/Y assignment.

* Code cleanup using |= operator

* More code cleanup

* Cleanup

* Fixes spacing issues. Thanks Visual Studio. You suck.

* Compiler error

* Fixes NPE from RunUO 2.7

* Renames ScriptCompiler to AssemblyHandler. Fixes packets. Updates README

* Fixes more packets. Stupid trailing nulls.

* Fixes various bugs.

* Fixes for gumps

* Fixes more gump stuff. Going to split it out later since it is getting insane

* Recoded the gump writing

* WIP

* *Added output path of scripts project to dev branch
*Activated debugging in code
2019-11-11 12:40:16 +01:00

400 lines
12 KiB
C#

using System;
using Server.Factions;
using Server.Mobiles;
using Server.Regions;
namespace Server.Misc
{
public class SkillCheck
{
public enum Stat
{
Str,
Dex,
Int
}
public const int Allowance = 3; //How many times may we use the same location/target for gain
private const int
LocationSize = 5; //The size of eeach location, make this smaller so players dont have to move as far
private static readonly bool AntiMacroCode = !Core.ML; //Change this to false to disable anti-macro code
public static TimeSpan AntiMacroExpire = TimeSpan.FromMinutes(5.0); //How long do we remember targets/locations?
private static bool[] UseAntiMacro =
{
// true if this skill uses the anti-macro code, false if it does not
false, // Alchemy = 0,
true, // Anatomy = 1,
true, // AnimalLore = 2,
true, // ItemID = 3,
true, // ArmsLore = 4,
false, // Parry = 5,
true, // Begging = 6,
false, // Blacksmith = 7,
false, // Fletching = 8,
true, // Peacemaking = 9,
true, // Camping = 10,
false, // Carpentry = 11,
false, // Cartography = 12,
false, // Cooking = 13,
true, // DetectHidden = 14,
true, // Discordance = 15,
true, // EvalInt = 16,
true, // Healing = 17,
true, // Fishing = 18,
true, // Forensics = 19,
true, // Herding = 20,
true, // Hiding = 21,
true, // Provocation = 22,
false, // Inscribe = 23,
true, // Lockpicking = 24,
true, // Magery = 25,
true, // MagicResist = 26,
false, // Tactics = 27,
true, // Snooping = 28,
true, // Musicianship = 29,
true, // Poisoning = 30,
false, // Archery = 31,
true, // SpiritSpeak = 32,
true, // Stealing = 33,
false, // Tailoring = 34,
true, // AnimalTaming = 35,
true, // TasteID = 36,
false, // Tinkering = 37,
true, // Tracking = 38,
true, // Veterinary = 39,
false, // Swords = 40,
false, // Macing = 41,
false, // Fencing = 42,
false, // Wrestling = 43,
true, // Lumberjacking = 44,
true, // Mining = 45,
true, // Meditation = 46,
true, // Stealth = 47,
true, // RemoveTrap = 48,
true, // Necromancy = 49,
false, // Focus = 50,
true, // Chivalry = 51
true, // Bushido = 52
true, //Ninjitsu = 53
true // Spellweaving
};
private static TimeSpan m_StatGainDelay = TimeSpan.FromMinutes(Core.ML ? 0.05 : 15);
private static TimeSpan m_PetStatGainDelay = TimeSpan.FromMinutes(5.0);
public static void Initialize()
{
Mobile.SkillCheckLocationHandler = Mobile_SkillCheckLocation;
Mobile.SkillCheckDirectLocationHandler = Mobile_SkillCheckDirectLocation;
Mobile.SkillCheckTargetHandler = Mobile_SkillCheckTarget;
Mobile.SkillCheckDirectTargetHandler = Mobile_SkillCheckDirectTarget;
}
public static bool Mobile_SkillCheckLocation(Mobile from, SkillName skillName, double minSkill, double maxSkill)
{
Skill skill = from.Skills[skillName];
if (skill == null)
return false;
double value = skill.Value;
if (value < minSkill)
return false; // Too difficult
if (value >= maxSkill)
return true; // No challenge
double chance = (value - minSkill) / (maxSkill - minSkill);
Point2D loc = new Point2D(from.Location.X / LocationSize, from.Location.Y / LocationSize);
return CheckSkill(from, skill, loc, chance);
}
public static bool Mobile_SkillCheckDirectLocation(Mobile from, SkillName skillName, double chance)
{
Skill skill = from.Skills[skillName];
if (skill == null)
return false;
if (chance < 0.0)
return false; // Too difficult
if (chance >= 1.0)
return true; // No challenge
Point2D loc = new Point2D(from.Location.X / LocationSize, from.Location.Y / LocationSize);
return CheckSkill(from, skill, loc, chance);
}
public static bool CheckSkill(Mobile from, Skill skill, object amObj, double chance)
{
if (from.Skills.Cap == 0)
return false;
bool success = chance >= Utility.RandomDouble();
double gc = (double)(from.Skills.Cap - from.Skills.Total) / from.Skills.Cap;
gc += (skill.Cap - skill.Base) / skill.Cap;
gc /= 2;
gc += (1.0 - chance) * (success ? 0.5 : Core.AOS ? 0.0 : 0.2);
gc /= 2;
gc *= skill.Info.GainFactor;
if (gc < 0.01)
gc = 0.01;
if (from is BaseCreature creature && creature.Controlled)
gc *= 2;
if (from.Alive && (gc >= Utility.RandomDouble() && AllowGain(from, skill, amObj) || skill.Base < 10.0))
Gain(from, skill);
return success;
}
public static bool Mobile_SkillCheckTarget(Mobile from, SkillName skillName, object target, double minSkill,
double maxSkill)
{
Skill skill = from.Skills[skillName];
if (skill == null)
return false;
double value = skill.Value;
if (value < minSkill)
return false; // Too difficult
if (value >= maxSkill)
return true; // No challenge
double chance = (value - minSkill) / (maxSkill - minSkill);
return CheckSkill(from, skill, target, chance);
}
public static bool Mobile_SkillCheckDirectTarget(Mobile from, SkillName skillName, object target, double chance)
{
Skill skill = from.Skills[skillName];
if (skill == null)
return false;
if (chance < 0.0)
return false; // Too difficult
if (chance >= 1.0)
return true; // No challenge
return CheckSkill(from, skill, target, chance);
}
private static bool AllowGain(Mobile from, Skill skill, object obj)
{
if (Core.AOS && Faction.InSkillLoss(from)) //Changed some time between the introduction of AoS and SE.
return false;
if (AntiMacroCode && from is PlayerMobile mobile && UseAntiMacro[skill.Info.SkillID])
return mobile.AntiMacroCheck(skill, obj);
return true;
}
public static void Gain(Mobile from, Skill skill)
{
if (from.Region.IsPartOf<Jail>())
return;
if (from is BaseCreature creature && creature.IsDeadPet)
return;
if (skill.SkillName == SkillName.Focus && from is BaseCreature)
return;
if (skill.Base < skill.Cap && skill.Lock == SkillLock.Up)
{
int toGain = 1;
if (skill.Base <= 10.0)
toGain = Utility.Random(4) + 1;
Skills skills = from.Skills;
if (from.Player && skills.Total / (double)skills.Cap >= Utility.RandomDouble()) //( skills.Total >= skills.Cap )
for (int i = 0; i < skills.Length; ++i)
{
Skill toLower = skills[i];
if (toLower != skill && toLower.Lock == SkillLock.Down && toLower.BaseFixedPoint >= toGain)
{
toLower.BaseFixedPoint -= toGain;
break;
}
}
#region Scroll of Alacrity
if (from is PlayerMobile pm && skill.SkillName == pm.AcceleratedSkill &&
pm.AcceleratedStart > DateTime.UtcNow)
toGain *= Utility.RandomMinMax(2, 5);
#endregion
if (!from.Player || skills.Total + toGain <= skills.Cap) skill.BaseFixedPoint += toGain;
}
if (skill.Lock == SkillLock.Up)
{
SkillInfo info = skill.Info;
if (from.StrLock == StatLockType.Up && info.StrGain / 33.3 > Utility.RandomDouble())
GainStat(from, Stat.Str);
else if (from.DexLock == StatLockType.Up && info.DexGain / 33.3 > Utility.RandomDouble())
GainStat(from, Stat.Dex);
else if (from.IntLock == StatLockType.Up && info.IntGain / 33.3 > Utility.RandomDouble())
GainStat(from, Stat.Int);
}
}
public static bool CanLower(Mobile from, Stat stat)
{
return stat switch
{
Stat.Str => (from.StrLock == StatLockType.Down && from.RawStr > 10),
Stat.Dex => (from.DexLock == StatLockType.Down && from.RawDex > 10),
Stat.Int => (from.IntLock == StatLockType.Down && from.RawInt > 10),
_ => false
};
}
public static bool CanRaise(Mobile from, Stat stat)
{
if (!(from is BaseCreature creature && creature.Controlled))
if (from.RawStatTotal >= from.StatCap)
return false;
return stat switch
{
Stat.Str => (from.StrLock == StatLockType.Up && from.RawStr < 125),
Stat.Dex => (from.DexLock == StatLockType.Up && from.RawDex < 125),
Stat.Int => (from.IntLock == StatLockType.Up && from.RawInt < 125),
_ => false
};
}
public static void IncreaseStat(Mobile from, Stat stat, bool atrophy)
{
atrophy = atrophy || from.RawStatTotal >= from.StatCap;
switch (stat)
{
case Stat.Str:
{
if (atrophy)
{
if (CanLower(from, Stat.Dex) && (from.RawDex < from.RawInt || !CanLower(from, Stat.Int)))
--from.RawDex;
else if (CanLower(from, Stat.Int))
--from.RawInt;
}
if (CanRaise(from, Stat.Str))
++from.RawStr;
break;
}
case Stat.Dex:
{
if (atrophy)
{
if (CanLower(from, Stat.Str) && (from.RawStr < from.RawInt || !CanLower(from, Stat.Int)))
--from.RawStr;
else if (CanLower(from, Stat.Int))
--from.RawInt;
}
if (CanRaise(from, Stat.Dex))
++from.RawDex;
break;
}
case Stat.Int:
{
if (atrophy)
{
if (CanLower(from, Stat.Str) && (from.RawStr < from.RawDex || !CanLower(from, Stat.Dex)))
--from.RawStr;
else if (CanLower(from, Stat.Dex))
--from.RawDex;
}
if (CanRaise(from, Stat.Int))
++from.RawInt;
break;
}
}
}
public static void GainStat(Mobile from, Stat stat)
{
switch (stat)
{
case Stat.Str:
{
if (from is BaseCreature creature && creature.Controlled)
{
if (creature.LastStrGain + m_PetStatGainDelay >= DateTime.UtcNow)
return;
}
else if (from.LastStrGain + m_StatGainDelay >= DateTime.UtcNow)
{
return;
}
from.LastStrGain = DateTime.UtcNow;
break;
}
case Stat.Dex:
{
if (from is BaseCreature creature && creature.Controlled)
{
if (creature.LastDexGain + m_PetStatGainDelay >= DateTime.UtcNow)
return;
}
else if (from.LastDexGain + m_StatGainDelay >= DateTime.UtcNow)
{
return;
}
from.LastDexGain = DateTime.UtcNow;
break;
}
case Stat.Int:
{
if (from is BaseCreature creature && creature.Controlled)
{
if (creature.LastIntGain + m_PetStatGainDelay >= DateTime.UtcNow)
return;
}
else if (from.LastIntGain + m_StatGainDelay >= DateTime.UtcNow)
{
return;
}
from.LastIntGain = DateTime.UtcNow;
break;
}
}
bool atrophy = from.RawStatTotal / (double)from.StatCap >= Utility.RandomDouble();
IncreaseStat(from, stat, atrophy);
}
}
}