Felladrin

Member
Felladrin submitted a new resource:

AutoDefend - Makes PlayerMobiles automatically counter-attack their closest aggressor.

AutoDefend

This script makes playermobiles to auto defend themselves when attacked. They will always counter-attack the closest aggressor.

It comes enabled for all accounts by default, but players can disable the feature for their accounts by using the command [AutoDefend to toggle it.

To install, just drop this script anywhere inside your Scripts folder.

Read more about this resource...
 
You need to fix the toggle. In the script replace oncommand with this:

static void OnCommand(CommandEventArgs e)
{
PlayerMobile pm = e.Mobile as PlayerMobile;
Account acc = pm.Account as Account;
if (acc == null && pm == null)
{
return;
}

if (acc.GetTag("AutoDefend") == null || acc.GetTag("AutoDefend") == "Enabled")
{
DisabledPlayers.Add(pm.Serial.Value);
acc.SetTag("AutoDefend", "Disabled");
pm.SendMessage(38, "You have disabled the auto-defend feature for your account.");
}

/*
if (acc != null && pm != null)
{
return;
}
*/
else
{
DisabledPlayers.Remove(pm.Serial.Value);
acc.SetTag("AutoDefend", "Enabled");
pm.SendMessage(68, "You have enabled the auto-defend feature for your account.");
}
}
 
Wanted to play around with this, however getting the following error on Pub 57:
C#:
+ Custom/Custom Commands/AutoDefend.cs:
    CS1061: Line 46: 'IDamageable' does not contain a definition for 'GetDistanceToSqrt' and no accessible extension method 'GetDistanceToSqrt' accepting a first argument of type 'IDamageable' could be found (are you missing a using directive or an assembly reference?)
 
I tried using this in version 57 and it wouldnt compile. I fixed it.

// AutoDefend v1.1.2 Updated for Servuo 57
// Author: Felladrin (Updated by DR Death)
// Started: 2013-10-14
// Updated: 2025-02-26
using System;
using System.Collections.Generic;
using Server;
using Server.Accounting;
using Server.Commands;
using Server.Mobiles;
namespace Felladrin.Automations
{
// Added extension method to compute distance between two Point3D objects.
public static class Point3DExtensions
{
public static double GetDistanceToSqrt(this Point3D p, Point3D other)
{
int dx = p.X - other.X;
int dy = p.Y - other.Y;
int dz = p.Z - other.Z;
return Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
}
public static class AutoDefend
{
public static class Config
{
public static bool Enabled = true; // Is this system enabled?
public static bool CommandEnabled = true; // Should we enable the command for players to enable/disable this system?
}
public static void Initialize()
{
if (Config.Enabled)
{
EventSink.AggressiveAction += OnAggressiveAction;
EventSink.Login += OnLogin;
EventSink.Logout += OnLogout;
if (Config.CommandEnabled)
CommandSystem.Register("AutoDefend", AccessLevel.Player, new CommandEventHandler(OnCommand));
}
}
static HashSet<int> DisabledPlayers = new HashSet<int>();
public static void OnAggressiveAction(AggressiveActionEventArgs e)
{
// Only auto-defend if the aggressed is a player, the aggressor isn't already the combatant,
// and auto-defend hasn't been disabled for this player.
if (e.Aggressed.Player && e.Aggressor != e.Aggressed.Combatant && !DisabledPlayers.Contains(e.Aggressed.Serial.Value))
{
if (e.Aggressed.Combatant == null)
{
e.Aggressed.Warmode = true;
e.Aggressed.Combatant = e.Aggressor;
}
// Compare distances using our extension method
else if (e.Aggressor.Location.GetDistanceToSqrt(e.Aggressed.Location) <
e.Aggressed.Combatant.Location.GetDistanceToSqrt(e.Aggressed.Location))
{
e.Aggressed.Warmode = true;
e.Aggressed.Combatant = e.Aggressor;
}
}
}
public static void OnLogin(LoginEventArgs e)
{
PlayerMobile pm = e.Mobile as PlayerMobile;
Account acc = pm.Account as Account;
if (acc.GetTag("AutoDefend") == "Disabled")
{
DisabledPlayers.Add(pm.Serial.Value);
pm.SendMessage("Auto-Defend is Disabled for your account.");
}
else
{
pm.SendMessage("Auto-Defend is Enabled for your account.");
}
}
public static void OnLogout(LogoutEventArgs e)
{
DisabledPlayers.Remove(e.Mobile.Serial.Value);
}
[Usage("AutoDefend")]
[Description("Enables or disables the auto-defend feature.")]
static void OnCommand(CommandEventArgs e)
{
PlayerMobile pm = e.Mobile as PlayerMobile;
Account acc = pm.Account as Account;
if (acc.GetTag("AutoDefend") == null || acc.GetTag("AutoDefend") == "Enabled")
{
DisabledPlayers.Add(pm.Serial.Value);
acc.SetTag("AutoDefend", "Disabled");
pm.SendMessage(38, "You have disabled the auto-defend feature for your account.");
}
else
{
DisabledPlayers.Remove(pm.Serial.Value);
acc.SetTag("AutoDefend", "Enabled");
pm.SendMessage(68, "You have enabled the auto-defend feature for your account.");
}
}
}
}
Wanted to play around with this, however getting the following error on Pub 57:
C#:
+ Custom/Custom Commands/AutoDefend.cs:
    CS1061: Line 46: 'IDamageable' does not contain a definition for 'GetDistanceToSqrt' and no accessible extension method 'GetDistanceToSqrt' accepting a first argument of type 'IDamageable' could be found (are you missing a using directive or an assembly reference?)
I just uploaded working code for 57
 

Active Shards

Donations

Total amount
$0.00
Goal
$500.00
Back