ModernUO/Projects/Scripts/Mobiles/Guards/ArcherGuard.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

378 lines
9.4 KiB
C#

using System;
using Server.Items;
namespace Server.Mobiles
{
public class ArcherGuard : BaseGuard
{
private Timer m_AttackTimer, m_IdleTimer;
private Mobile m_Focus;
public ArcherGuard(Mobile target = null) : base(target)
{
InitStats(100, 125, 25);
Title = "the guard";
SpeechHue = Utility.RandomDyedHue();
Hue = Race.Human.RandomSkinHue();
if (Female = Utility.RandomBool())
{
Body = 0x191;
Name = NameList.RandomName("female");
}
else
{
Body = 0x190;
Name = NameList.RandomName("male");
}
new Horse().Rider = this;
AddItem(new StuddedChest());
AddItem(new StuddedArms());
AddItem(new StuddedGloves());
AddItem(new StuddedGorget());
AddItem(new StuddedLegs());
AddItem(new Boots());
AddItem(new SkullCap());
Bow bow = new Bow();
bow.Movable = false;
bow.Crafter = this;
bow.Quality = WeaponQuality.Exceptional;
AddItem(bow);
Container pack = new Backpack();
pack.Movable = false;
Arrow arrows = new Arrow(250);
arrows.LootType = LootType.Newbied;
pack.DropItem(arrows);
pack.DropItem(new Gold(10, 25));
AddItem(pack);
Skills.Anatomy.Base = 120.0;
Skills.Tactics.Base = 120.0;
Skills.Archery.Base = 120.0;
Skills.MagicResist.Base = 120.0;
Skills.DetectHidden.Base = 100.0;
NextCombatTime = Core.TickCount + 500;
Focus = target;
}
public ArcherGuard(Serial serial) : base(serial)
{
}
[CommandProperty(AccessLevel.GameMaster)]
public override Mobile Focus
{
get => m_Focus;
set
{
if (Deleted)
return;
Mobile oldFocus = m_Focus;
if (oldFocus != value)
{
m_Focus = value;
if (value != null)
AggressiveAction(value);
Combatant = value;
if (oldFocus?.Alive == false)
Say("Thou hast suffered thy punishment, scoundrel.");
if (value != null)
Say(500131); // Thou wilt regret thine actions, swine!
if (m_AttackTimer != null)
{
m_AttackTimer.Stop();
m_AttackTimer = null;
}
if (m_IdleTimer != null)
{
m_IdleTimer.Stop();
m_IdleTimer = null;
}
if (m_Focus != null)
{
m_AttackTimer = new AttackTimer(this);
m_AttackTimer.Start();
((AttackTimer)m_AttackTimer).DoOnTick();
}
else
{
m_IdleTimer = new IdleTimer(this);
m_IdleTimer.Start();
}
}
else if (m_Focus == null && m_IdleTimer == null)
{
m_IdleTimer = new IdleTimer(this);
m_IdleTimer.Start();
}
}
}
public override bool OnBeforeDeath()
{
if (m_Focus?.Alive == true)
new AvengeTimer(m_Focus).Start(); // If a guard dies, three more guards will spawn
return base.OnBeforeDeath();
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write(0); // version
writer.Write(m_Focus);
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
switch (version)
{
case 0:
{
m_Focus = reader.ReadMobile();
if (m_Focus != null)
{
m_AttackTimer = new AttackTimer(this);
m_AttackTimer.Start();
}
else
{
m_IdleTimer = new IdleTimer(this);
m_IdleTimer.Start();
}
break;
}
}
}
public override void OnAfterDelete()
{
if (m_AttackTimer != null)
{
m_AttackTimer.Stop();
m_AttackTimer = null;
}
if (m_IdleTimer != null)
{
m_IdleTimer.Stop();
m_IdleTimer = null;
}
base.OnAfterDelete();
}
private class AvengeTimer : Timer
{
private Mobile m_Focus;
public AvengeTimer(Mobile focus) :
base(TimeSpan.FromSeconds(2.5), TimeSpan.FromSeconds(1.0),
3) // After 2.5 seconds, one guard will spawn every 1.0 second, three times
=>
m_Focus = focus;
protected override void OnTick()
{
Spawn(m_Focus, m_Focus, 1, true);
}
}
private class AttackTimer : Timer
{
private ArcherGuard m_Owner;
// private bool m_Shooting;
public AttackTimer(ArcherGuard owner) : base(TimeSpan.FromSeconds(0.25), TimeSpan.FromSeconds(0.1)) => m_Owner = owner;
public void DoOnTick()
{
OnTick();
}
protected override void OnTick()
{
if (m_Owner.Deleted)
{
Stop();
return;
}
m_Owner.Criminal = false;
m_Owner.Kills = 0;
m_Owner.Stam = m_Owner.StamMax;
Mobile target = m_Owner.Focus;
if (target != null && (target.Deleted || !target.Alive || !m_Owner.CanBeHarmful(target)))
{
m_Owner.Focus = null;
Stop();
return;
}
if (m_Owner.Weapon is Fists)
{
m_Owner.Kill();
Stop();
return;
}
if (target != null && m_Owner.Combatant != target)
m_Owner.Combatant = target;
if (target == null)
{
Stop();
}
else
{
// <instakill>
TeleportTo(target);
target.BoltEffect();
if (target is BaseCreature creature)
creature.NoKillAwards = true;
target.Damage(target.HitsMax, m_Owner);
target.Kill(); // just in case, maybe Damage is overridden on some shard
if (target.Corpse != null && !target.Player)
target.Corpse.Delete();
m_Owner.Focus = null;
Stop();
} // </instakill>
/*else if ( !m_Owner.InRange( target, 20 ) )
{
m_Shooting = false;
m_Owner.Focus = null;
}
else if ( !m_Owner.InLOS( target ) )
{
m_Shooting = false;
TeleportTo( target );
}
else if ( !m_Owner.CanSee( target ) )
{
m_Shooting = false;
if ( !m_Owner.InRange( target, 2 ) )
{
if ( !m_Owner.Move( m_Owner.GetDirectionTo( target ) | Direction.Running ) && OutOfMaxDistance( target ) )
TeleportTo( target );
}
else
{
if ( !m_Owner.UseSkill( SkillName.DetectHidden ) && Utility.Random( 50 ) == 0 )
m_Owner.Say( "Reveal!" );
}
}
else
{
if ( m_Shooting && (TimeToSpare() || OutOfMaxDistance( target )) )
m_Shooting = false;
else if ( !m_Shooting && InMinDistance( target ) )
m_Shooting = true;
if ( !m_Shooting )
{
if ( m_Owner.InRange( target, 1 ) )
{
if ( !m_Owner.Move( (Direction)(m_Owner.GetDirectionTo( target ) - 4) | Direction.Running ) && OutOfMaxDistance( target ) ) // Too close, move away
TeleportTo( target );
}
else if ( !m_Owner.InRange( target, 2 ) )
{
if ( !m_Owner.Move( m_Owner.GetDirectionTo( target ) | Direction.Running ) && OutOfMaxDistance( target ) )
TeleportTo( target );
}
}
}*/
}
private bool TimeToSpare() => m_Owner.NextCombatTime - Core.TickCount > 1000;
private bool OutOfMaxDistance(Mobile target) => !m_Owner.InRange(target, m_Owner.Weapon.MaxRange);
private bool InMinDistance(Mobile target) => m_Owner.InRange(target, 4);
private void TeleportTo(Mobile target)
{
Point3D from = m_Owner.Location;
Point3D to = target.Location;
m_Owner.Location = to;
Effects.SendLocationParticles(EffectItem.Create(from, m_Owner.Map, EffectItem.DefaultDuration), 0x3728, 10,
10, 2023);
Effects.SendLocationParticles(EffectItem.Create(to, m_Owner.Map, EffectItem.DefaultDuration), 0x3728, 10, 10,
5023);
m_Owner.PlaySound(0x1FE);
}
}
private class IdleTimer : Timer
{
private ArcherGuard m_Owner;
private int m_Stage;
public IdleTimer(ArcherGuard owner) : base(TimeSpan.FromSeconds(2.0), TimeSpan.FromSeconds(2.5)) => m_Owner = owner;
protected override void OnTick()
{
if (m_Owner.Deleted)
{
Stop();
return;
}
if (m_Stage++ % 4 == 0 || !m_Owner.Move(m_Owner.Direction))
m_Owner.Direction = (Direction)Utility.Random(8);
if (m_Stage > 16)
{
Effects.SendLocationParticles(
EffectItem.Create(m_Owner.Location, m_Owner.Map, EffectItem.DefaultDuration), 0x3728, 10, 10, 2023);
m_Owner.PlaySound(0x1FE);
m_Owner.Delete();
}
}
}
}
}