Skip to content

Commit

Permalink
* HS-1430: fix Lua issues with certain enum types
Browse files Browse the repository at this point in the history
* Add `Direction`, `GetTileInDirection`, `GetMonstersInDirection`, `GetusersInDirection
  to Lua API

* Fix other long-standing stupid
  • Loading branch information
baughj committed Apr 27, 2024
1 parent ea54335 commit e01a3e5
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 30 deletions.
2 changes: 1 addition & 1 deletion hybrasyl/CreatureStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private void ProcessStatModifiers(bool remove = false)
{
dmg = NumberCruncher.CalculateDamage(castable, effect, Target, source, Name);
// If the tick itself defines an element, use it, along with the damage type
if (effect.Damage.Element != Element.None)
if (effect.Damage.Element != ElementType.None)
dmg.Element = effect.Damage.Element;
dmg.Type = effect.Damage.Type;
}
Expand Down
26 changes: 0 additions & 26 deletions hybrasyl/Scripting/ElementType.cs

This file was deleted.

2 changes: 1 addition & 1 deletion hybrasyl/Scripting/HybrasylMonster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public HybrasylMonster(Monster monster) : base(monster)
Map = new HybrasylMap(monster.Map);
}


internal Monster Monster => WorldObject as Monster;

public Direction Direction => Monster.Direction;
internal HybrasylWorld World { get; set; }
internal HybrasylMap Map { get; set; }

Expand Down
56 changes: 56 additions & 0 deletions hybrasyl/Scripting/HybrasylUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public HybrasylUser(User user) : base(user)
internal HybrasylWorld World { get; set; }
public HybrasylMap Map { get; set; }
public new string Guid => User.Guid.ToString();
public Direction Direction => User.Direction;


/// <summary>
/// The item in the first inventory slot of the player.
Expand Down Expand Up @@ -221,6 +223,60 @@ public List<HybrasylUser> GetFacingUsers(int distance = 1)
.ToList();
}

/// <summary>
/// Return a list of monsters in a specified direction.
/// </summary>
/// <param name="direction">The <see cref="Direction"/> to examine.</param>
/// <param name="radius">The number of tiles to examine in the given direction.</param>
/// <returns></returns>
public List<HybrasylMonster> GetMonstersInDirection(Direction direction, int radius = 1) =>
User.GetDirectionalTargets(direction).Where(x => x is Monster).Cast<Monster>()
.Select(y => new HybrasylMonster(y)).ToList();

/// <summary>
/// Return a list of players in a specified direction.
/// </summary>
/// <param name="direction">The <see cref="Direction"/> to examine.</param>
/// <param name="radius">The number of tiles to examine in the given direction.</param>
/// <returns>List of <see cref="HybrasylPlayer"/>. </returns>
public List<HybrasylUser> GetUsersInDirection(Direction direction, int radius = 1) =>
User.GetDirectionalTargets(direction).Where(x => x is User).Cast<User>()
.Select(y => new HybrasylUser(y)).ToList();

/// <summary>
/// Get the coordinates of a tile in a specific direction and number of tiles away from current location.
/// </summary>
/// <param name="direction">The <see cref="Direction"/> to use.</param>
/// <param name="tiles">The number of tiles away from the current location in the specified direction.</param>
/// <returns>A <see cref="Coordinate"/> representing the X,Y of the calculated tile.</returns>
public Coordinate GetTileInDirection(Direction direction, int tiles = 1)
{
int x = X;
int y = Y;
switch (direction)
{
case Direction.North:
x = X;
y = Y - tiles;
break;
case Direction.South:
x = X;
y = Y + tiles;
break;
case Direction.West:
x = X - tiles;
y = Y;
break;
case Direction.East:
x = X + tiles;
y = Y;
break;
}

return Coordinate.FromInt(x, y);

}

/// <summary>
/// Set the users facing direction.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions hybrasyl/Scripting/HybrasylUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,40 @@ public static void CreateMonster(int mapId, byte x, byte y, string creatureName,

World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcode.MonolithSpawn, monster, map));
}

public static Direction DirectionFromString(string e)
{
return e.ToLower() switch
{
"east" => Direction.East,
"north" => Direction.North,
"south" => Direction.South,
"west" => Direction.West,
_ => Direction.North
};
}

public static Direction OppositeDirection(Direction d)
{
return d switch
{
Direction.West => Direction.East,
Direction.South => Direction.North,
Direction.North => Direction.South,
Direction.East => Direction.West,
_ => Direction.North,
};
}

public static Direction OppositeDirection(string d)
{
return d.ToLower() switch
{
"east" => Direction.West,
"north" => Direction.South,
"south" => Direction.North,
"west" => Direction.East,
_ => Direction.North
};
}
}
11 changes: 10 additions & 1 deletion hybrasyl/Scripting/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
using MoonSharp.Interpreter;
using Serilog;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using MoonSharp.Interpreter.Compatibility;
using Path = System.IO.Path;
using Reactor = Hybrasyl.Objects.Reactor;
using MoonSharp.Interpreter.Interop.UserDataRegistries;

using MoonSharp.Interpreter.Compatibility.Frameworks;
using MoonSharp.Interpreter.Interop;

namespace Hybrasyl.Scripting;

Expand Down Expand Up @@ -223,10 +230,12 @@ private ScriptExecutionError HumanizeException(Exception ex)

public void SetGlobals()
{

Compiled.Globals["Gender"] = UserData.CreateStatic<Gender>();
Compiled.Globals["LegendIcon"] = UserData.CreateStatic<LegendIcon>();
Compiled.Globals["LegendColor"] = UserData.CreateStatic<LegendColor>();
Compiled.Globals["Element"] = UserData.CreateStatic(typeof(Element));
Compiled.Globals["Element"] = UserData.CreateStatic(typeof(ElementType));
Compiled.Globals["Direction"] = UserData.CreateStatic<Direction>();
Compiled.Globals["Class"] = UserData.CreateStatic<Class>();
Compiled.Globals["utility"] = typeof(HybrasylUtility);
Compiled.Globals.Set("world", UserData.Create(Processor.World));
Expand Down
2 changes: 1 addition & 1 deletion hybrasyl/Scripting/ScriptProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public ScriptProcessor(World world)
// NB registering assemblies is required for RegisterType of
// any type in that assembly to work correctly
UserData.RegisterAssembly(typeof(Game).Assembly);
UserData.RegisterAssembly(typeof(ElementType).Assembly);
UserData.RegisterType<Gender>();
UserData.RegisterType<LegendIcon>();
UserData.RegisterType<LegendColor>();
UserData.RegisterType<LegendMark>();
UserData.RegisterType<DateTime>();
UserData.RegisterType<TimeSpan>();
UserData.RegisterType<ElementType>();
UserData.RegisterType<Direction>();
_scripts = new Dictionary<string, List<Script>>();
}

Expand Down
26 changes: 26 additions & 0 deletions hybrasyl/Scripting/Tile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MoonSharp.Interpreter;

namespace Hybrasyl.Scripting
{
[MoonSharpUserData]
public class Coordinate
{
public byte X;
public byte Y;

public Coordinate(byte x, byte y)
{
X = x;
Y = y;
}

public static Coordinate FromInt(int x, int y) => new Coordinate((byte) Math.Clamp(x, byte.MinValue, byte.MaxValue),
(byte)Math.Clamp(y, byte.MinValue, byte.MaxValue));

}
}

0 comments on commit e01a3e5

Please sign in to comment.