Reorganizes Project (#41)
This commit is contained in:
parent
08bf44af9a
commit
3614a66aee
3499 changed files with 79 additions and 55 deletions
|
|
@ -0,0 +1,64 @@
|
|||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongDealerIndicator
|
||||
{
|
||||
public MahjongDealerIndicator(MahjongGame game, Point2D position, MahjongPieceDirection direction, MahjongWind wind)
|
||||
{
|
||||
Game = game;
|
||||
Position = position;
|
||||
Direction = direction;
|
||||
Wind = wind;
|
||||
}
|
||||
|
||||
public MahjongDealerIndicator(MahjongGame game, GenericReader reader)
|
||||
{
|
||||
Game = game;
|
||||
|
||||
int version = reader.ReadInt();
|
||||
|
||||
Position = reader.ReadPoint2D();
|
||||
Direction = (MahjongPieceDirection)reader.ReadInt();
|
||||
Wind = (MahjongWind)reader.ReadInt();
|
||||
}
|
||||
|
||||
public MahjongGame Game{ get; }
|
||||
|
||||
public Point2D Position{ get; private set; }
|
||||
|
||||
public MahjongPieceDirection Direction{ get; private set; }
|
||||
|
||||
public MahjongWind Wind{ get; private set; }
|
||||
|
||||
public MahjongPieceDim Dimensions => GetDimensions(Position, Direction);
|
||||
|
||||
public static MahjongPieceDim GetDimensions(Point2D position, MahjongPieceDirection direction)
|
||||
{
|
||||
if (direction == MahjongPieceDirection.Up || direction == MahjongPieceDirection.Down)
|
||||
return new MahjongPieceDim(position, 40, 20);
|
||||
return new MahjongPieceDim(position, 20, 40);
|
||||
}
|
||||
|
||||
public void Move(Point2D position, MahjongPieceDirection direction, MahjongWind wind)
|
||||
{
|
||||
MahjongPieceDim dim = GetDimensions(position, direction);
|
||||
|
||||
if (!dim.IsValid())
|
||||
return;
|
||||
|
||||
Position = position;
|
||||
Direction = direction;
|
||||
Wind = wind;
|
||||
|
||||
Game.Players.SendGeneralPacket(true, true);
|
||||
}
|
||||
|
||||
public void Save(GenericWriter writer)
|
||||
{
|
||||
writer.Write(0); // version
|
||||
|
||||
writer.Write(Position);
|
||||
writer.Write((int)Direction);
|
||||
writer.Write((int)Wind);
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Projects/Scripts/Items/Games/Mahjong/MahjongDices.cs
Normal file
48
Projects/Scripts/Items/Games/Mahjong/MahjongDices.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongDices
|
||||
{
|
||||
public MahjongDices(MahjongGame game)
|
||||
{
|
||||
Game = game;
|
||||
First = Utility.Random(1, 6);
|
||||
Second = Utility.Random(1, 6);
|
||||
}
|
||||
|
||||
public MahjongDices(MahjongGame game, GenericReader reader)
|
||||
{
|
||||
Game = game;
|
||||
|
||||
int version = reader.ReadInt();
|
||||
|
||||
First = reader.ReadInt();
|
||||
Second = reader.ReadInt();
|
||||
}
|
||||
|
||||
public MahjongGame Game{ get; }
|
||||
|
||||
public int First{ get; private set; }
|
||||
|
||||
public int Second{ get; private set; }
|
||||
|
||||
public void RollDices(Mobile from)
|
||||
{
|
||||
First = Utility.Random(1, 6);
|
||||
Second = Utility.Random(1, 6);
|
||||
|
||||
Game.Players.SendGeneralPacket(true, true);
|
||||
|
||||
if (from != null)
|
||||
Game.Players.SendLocalizedMessage(1062695,
|
||||
$"{from.Name}\t{First}\t{Second}"); // ~1_name~ rolls the dice and gets a ~2_number~ and a ~3_number~!
|
||||
}
|
||||
|
||||
public void Save(GenericWriter writer)
|
||||
{
|
||||
writer.Write(0); // version
|
||||
|
||||
writer.Write(First);
|
||||
writer.Write(Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Projects/Scripts/Items/Games/Mahjong/MahjongEnums.cs
Normal file
56
Projects/Scripts/Items/Games/Mahjong/MahjongEnums.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public enum MahjongPieceDirection
|
||||
{
|
||||
Up,
|
||||
Left,
|
||||
Down,
|
||||
Right
|
||||
}
|
||||
|
||||
public enum MahjongWind
|
||||
{
|
||||
North,
|
||||
East,
|
||||
South,
|
||||
West
|
||||
}
|
||||
|
||||
public enum MahjongTileType
|
||||
{
|
||||
Dagger1 = 1,
|
||||
Dagger2,
|
||||
Dagger3,
|
||||
Dagger4,
|
||||
Dagger5,
|
||||
Dagger6,
|
||||
Dagger7,
|
||||
Dagger8,
|
||||
Dagger9,
|
||||
Gem1,
|
||||
Gem2,
|
||||
Gem3,
|
||||
Gem4,
|
||||
Gem5,
|
||||
Gem6,
|
||||
Gem7,
|
||||
Gem8,
|
||||
Gem9,
|
||||
Number1,
|
||||
Number2,
|
||||
Number3,
|
||||
Number4,
|
||||
Number5,
|
||||
Number6,
|
||||
Number7,
|
||||
Number8,
|
||||
Number9,
|
||||
North,
|
||||
East,
|
||||
South,
|
||||
West,
|
||||
Green,
|
||||
Red,
|
||||
White
|
||||
}
|
||||
}
|
||||
298
Projects/Scripts/Items/Games/Mahjong/MahjongGame.cs
Normal file
298
Projects/Scripts/Items/Games/Mahjong/MahjongGame.cs
Normal file
|
|
@ -0,0 +1,298 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Server.ContextMenus;
|
||||
using Server.Gumps;
|
||||
using Server.Multis;
|
||||
|
||||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongGame : Item, ISecurable
|
||||
{
|
||||
public const int MaxPlayers = 4;
|
||||
public const int BaseScore = 30000;
|
||||
private DateTime m_LastReset;
|
||||
|
||||
private bool m_ShowScores;
|
||||
private bool m_SpectatorVision;
|
||||
|
||||
[Constructible]
|
||||
public MahjongGame() : base(0xFAA)
|
||||
{
|
||||
Weight = 5.0;
|
||||
|
||||
BuildWalls();
|
||||
DealerIndicator =
|
||||
new MahjongDealerIndicator(this, new Point2D(300, 300), MahjongPieceDirection.Up, MahjongWind.North);
|
||||
WallBreakIndicator = new MahjongWallBreakIndicator(this, new Point2D(335, 335));
|
||||
Dices = new MahjongDices(this);
|
||||
Players = new MahjongPlayers(this, MaxPlayers, BaseScore);
|
||||
m_LastReset = DateTime.UtcNow;
|
||||
Level = SecureLevel.CoOwners;
|
||||
}
|
||||
|
||||
public MahjongGame(Serial serial) : base(serial)
|
||||
{
|
||||
}
|
||||
|
||||
public MahjongTile[] Tiles{ get; private set; }
|
||||
|
||||
public MahjongDealerIndicator DealerIndicator{ get; private set; }
|
||||
|
||||
public MahjongWallBreakIndicator WallBreakIndicator{ get; private set; }
|
||||
|
||||
public MahjongDices Dices{ get; private set; }
|
||||
|
||||
public MahjongPlayers Players{ get; private set; }
|
||||
|
||||
[CommandProperty(AccessLevel.GameMaster)]
|
||||
public bool ShowScores
|
||||
{
|
||||
get => m_ShowScores;
|
||||
set
|
||||
{
|
||||
if (m_ShowScores == value)
|
||||
return;
|
||||
|
||||
m_ShowScores = value;
|
||||
|
||||
if (value)
|
||||
Players.SendPlayersPacket(true, true);
|
||||
|
||||
Players.SendGeneralPacket(true, true);
|
||||
|
||||
Players.SendLocalizedMessage(value ? 1062777 : 1062778); // The dealer has enabled/disabled score display.
|
||||
}
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.GameMaster)]
|
||||
public bool SpectatorVision
|
||||
{
|
||||
get => m_SpectatorVision;
|
||||
set
|
||||
{
|
||||
if (m_SpectatorVision == value)
|
||||
return;
|
||||
|
||||
m_SpectatorVision = value;
|
||||
|
||||
if (Players.IsInGamePlayer(Players.DealerPosition))
|
||||
Players.Dealer.Send(new MahjongGeneralInfo(this));
|
||||
|
||||
Players.SendTilesPacket(false, true);
|
||||
|
||||
Players.SendLocalizedMessage(value ? 1062715 : 1062716); // The dealer has enabled/disabled Spectator Vision.
|
||||
|
||||
InvalidateProperties();
|
||||
}
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.GameMaster)]
|
||||
public SecureLevel Level{ get; set; }
|
||||
|
||||
private void BuildHorizontalWall(ref int index, int x, int y, int stackLevel, MahjongPieceDirection direction,
|
||||
MahjongTileTypeGenerator typeGenerator)
|
||||
{
|
||||
for (int i = 0; i < 17; i++)
|
||||
{
|
||||
Point2D position = new Point2D(x + i * 20, y);
|
||||
Tiles[index + i] = new MahjongTile(this, index + i, typeGenerator.Next(), position, stackLevel, direction,
|
||||
false);
|
||||
}
|
||||
|
||||
index += 17;
|
||||
}
|
||||
|
||||
private void BuildVerticalWall(ref int index, int x, int y, int stackLevel, MahjongPieceDirection direction,
|
||||
MahjongTileTypeGenerator typeGenerator)
|
||||
{
|
||||
for (int i = 0; i < 17; i++)
|
||||
{
|
||||
Point2D position = new Point2D(x, y + i * 20);
|
||||
Tiles[index + i] = new MahjongTile(this, index + i, typeGenerator.Next(), position, stackLevel, direction,
|
||||
false);
|
||||
}
|
||||
|
||||
index += 17;
|
||||
}
|
||||
|
||||
private void BuildWalls()
|
||||
{
|
||||
Tiles = new MahjongTile[136];
|
||||
|
||||
MahjongTileTypeGenerator typeGenerator = new MahjongTileTypeGenerator();
|
||||
|
||||
int i = 0;
|
||||
|
||||
BuildHorizontalWall(ref i, 165, 110, 0, MahjongPieceDirection.Up, typeGenerator);
|
||||
BuildHorizontalWall(ref i, 165, 115, 1, MahjongPieceDirection.Up, typeGenerator);
|
||||
|
||||
BuildVerticalWall(ref i, 530, 165, 0, MahjongPieceDirection.Left, typeGenerator);
|
||||
BuildVerticalWall(ref i, 525, 165, 1, MahjongPieceDirection.Left, typeGenerator);
|
||||
|
||||
BuildHorizontalWall(ref i, 165, 530, 0, MahjongPieceDirection.Down, typeGenerator);
|
||||
BuildHorizontalWall(ref i, 165, 525, 1, MahjongPieceDirection.Down, typeGenerator);
|
||||
|
||||
BuildVerticalWall(ref i, 110, 165, 0, MahjongPieceDirection.Right, typeGenerator);
|
||||
BuildVerticalWall(ref i, 115, 165, 1, MahjongPieceDirection.Right, typeGenerator);
|
||||
}
|
||||
|
||||
public override void GetProperties(ObjectPropertyList list)
|
||||
{
|
||||
base.GetProperties(list);
|
||||
|
||||
if (m_SpectatorVision)
|
||||
list.Add(1062717); // Spectator Vision Enabled
|
||||
else
|
||||
list.Add(1062718); // Spectator Vision Disabled
|
||||
}
|
||||
|
||||
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
|
||||
{
|
||||
base.GetContextMenuEntries(from, list);
|
||||
|
||||
Players.CheckPlayers();
|
||||
|
||||
if (from.Alive && IsAccessibleTo(from) && Players.GetInGameMobiles(true, false).Count == 0)
|
||||
list.Add(new ResetGameEntry(this));
|
||||
|
||||
SetSecureLevelEntry.AddTo(from, this, list);
|
||||
}
|
||||
|
||||
public override void OnDoubleClick(Mobile from)
|
||||
{
|
||||
Players.CheckPlayers();
|
||||
|
||||
Players.Join(from);
|
||||
}
|
||||
|
||||
public void ResetGame(Mobile from)
|
||||
{
|
||||
if (DateTime.UtcNow - m_LastReset < TimeSpan.FromSeconds(5.0))
|
||||
return;
|
||||
|
||||
m_LastReset = DateTime.UtcNow;
|
||||
|
||||
if (from != null)
|
||||
Players.SendLocalizedMessage(1062771, from.Name); // ~1_name~ has reset the game.
|
||||
|
||||
Players.SendRelievePacket(true, true);
|
||||
|
||||
BuildWalls();
|
||||
DealerIndicator =
|
||||
new MahjongDealerIndicator(this, new Point2D(300, 300), MahjongPieceDirection.Up, MahjongWind.North);
|
||||
WallBreakIndicator = new MahjongWallBreakIndicator(this, new Point2D(335, 335));
|
||||
Players = new MahjongPlayers(this, MaxPlayers, BaseScore);
|
||||
}
|
||||
|
||||
public void ResetWalls(Mobile from)
|
||||
{
|
||||
if (DateTime.UtcNow - m_LastReset < TimeSpan.FromSeconds(5.0))
|
||||
return;
|
||||
|
||||
m_LastReset = DateTime.UtcNow;
|
||||
|
||||
BuildWalls();
|
||||
|
||||
Players.SendTilesPacket(true, true);
|
||||
|
||||
if (from != null)
|
||||
Players.SendLocalizedMessage(1062696); // The dealer rebuilds the wall.
|
||||
}
|
||||
|
||||
public int GetStackLevel(MahjongPieceDim dim)
|
||||
{
|
||||
int level = -1;
|
||||
foreach (MahjongTile tile in Tiles)
|
||||
if (tile.StackLevel > level && dim.IsOverlapping(tile.Dimensions))
|
||||
level = tile.StackLevel;
|
||||
return level;
|
||||
}
|
||||
|
||||
public override void Serialize(GenericWriter writer)
|
||||
{
|
||||
base.Serialize(writer);
|
||||
|
||||
writer.Write(1); // version
|
||||
|
||||
writer.Write((int)Level);
|
||||
|
||||
writer.Write(Tiles.Length);
|
||||
|
||||
for (int i = 0; i < Tiles.Length; i++)
|
||||
Tiles[i].Save(writer);
|
||||
|
||||
DealerIndicator.Save(writer);
|
||||
|
||||
WallBreakIndicator.Save(writer);
|
||||
|
||||
Dices.Save(writer);
|
||||
|
||||
Players.Save(writer);
|
||||
|
||||
writer.Write(m_ShowScores);
|
||||
writer.Write(m_SpectatorVision);
|
||||
}
|
||||
|
||||
public override void Deserialize(GenericReader reader)
|
||||
{
|
||||
base.Deserialize(reader);
|
||||
|
||||
int version = reader.ReadInt();
|
||||
|
||||
switch (version)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
Level = (SecureLevel)reader.ReadInt();
|
||||
|
||||
goto case 0;
|
||||
}
|
||||
case 0:
|
||||
{
|
||||
if (version < 1)
|
||||
Level = SecureLevel.CoOwners;
|
||||
|
||||
int length = reader.ReadInt();
|
||||
Tiles = new MahjongTile[length];
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
Tiles[i] = new MahjongTile(this, reader);
|
||||
|
||||
DealerIndicator = new MahjongDealerIndicator(this, reader);
|
||||
|
||||
WallBreakIndicator = new MahjongWallBreakIndicator(this, reader);
|
||||
|
||||
Dices = new MahjongDices(this, reader);
|
||||
|
||||
Players = new MahjongPlayers(this, reader);
|
||||
|
||||
m_ShowScores = reader.ReadBool();
|
||||
m_SpectatorVision = reader.ReadBool();
|
||||
|
||||
m_LastReset = DateTime.UtcNow;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ResetGameEntry : ContextMenuEntry
|
||||
{
|
||||
private MahjongGame m_Game;
|
||||
|
||||
public ResetGameEntry(MahjongGame game) : base(6162)
|
||||
{
|
||||
m_Game = game;
|
||||
}
|
||||
|
||||
public override void OnClick()
|
||||
{
|
||||
Mobile from = Owner.From;
|
||||
|
||||
if (from.CheckAlive() && !m_Game.Deleted && m_Game.IsAccessibleTo(from) &&
|
||||
m_Game.Players.GetInGameMobiles(true, false).Count == 0)
|
||||
m_Game.ResetGame(from);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
233
Projects/Scripts/Items/Games/Mahjong/MahjongPacketHandlers.cs
Normal file
233
Projects/Scripts/Items/Games/Mahjong/MahjongPacketHandlers.cs
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
using Server.Network;
|
||||
|
||||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public delegate void OnMahjongPacketReceive(MahjongGame game, NetState state, PacketReader pvSrc);
|
||||
|
||||
public sealed class MahjongPacketHandlers
|
||||
{
|
||||
private static OnMahjongPacketReceive[] m_SubCommandDelegates = new OnMahjongPacketReceive[0x100];
|
||||
|
||||
public static void RegisterSubCommand(int subCmd, OnMahjongPacketReceive onReceive)
|
||||
{
|
||||
m_SubCommandDelegates[subCmd] = onReceive;
|
||||
}
|
||||
|
||||
public static OnMahjongPacketReceive GetSubCommandDelegate(int cmd)
|
||||
{
|
||||
if (cmd >= 0 && cmd < 0x100) return m_SubCommandDelegates[cmd];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
PacketHandlers.Register(0xDA, 0, true, OnPacket);
|
||||
|
||||
RegisterSubCommand(0x6, ExitGame);
|
||||
RegisterSubCommand(0xA, GivePoints);
|
||||
RegisterSubCommand(0xB, RollDice);
|
||||
RegisterSubCommand(0xC, BuildWalls);
|
||||
RegisterSubCommand(0xD, ResetScores);
|
||||
RegisterSubCommand(0xF, AssignDealer);
|
||||
RegisterSubCommand(0x10, OpenSeat);
|
||||
RegisterSubCommand(0x11, ChangeOption);
|
||||
RegisterSubCommand(0x15, MoveWallBreakIndicator);
|
||||
RegisterSubCommand(0x16, TogglePublicHand);
|
||||
RegisterSubCommand(0x17, MoveTile);
|
||||
RegisterSubCommand(0x18, MoveDealerIndicator);
|
||||
}
|
||||
|
||||
public static void OnPacket(NetState state, PacketReader pvSrc)
|
||||
{
|
||||
MahjongGame game = World.FindItem(pvSrc.ReadUInt32()) as MahjongGame;
|
||||
|
||||
game?.Players.CheckPlayers();
|
||||
|
||||
pvSrc.ReadByte();
|
||||
|
||||
int cmd = pvSrc.ReadByte();
|
||||
|
||||
OnMahjongPacketReceive onReceive = GetSubCommandDelegate(cmd);
|
||||
|
||||
if (onReceive != null)
|
||||
onReceive(game, state, pvSrc);
|
||||
else
|
||||
pvSrc.Trace(state);
|
||||
}
|
||||
|
||||
private static MahjongPieceDirection GetDirection(int value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case 0: return MahjongPieceDirection.Up;
|
||||
case 1: return MahjongPieceDirection.Left;
|
||||
case 2: return MahjongPieceDirection.Down;
|
||||
default: return MahjongPieceDirection.Right;
|
||||
}
|
||||
}
|
||||
|
||||
private static MahjongWind GetWind(int value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case 0: return MahjongWind.North;
|
||||
case 1: return MahjongWind.East;
|
||||
case 2: return MahjongWind.South;
|
||||
default: return MahjongWind.West;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ExitGame(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null)
|
||||
return;
|
||||
|
||||
Mobile from = state.Mobile;
|
||||
|
||||
game.Players.LeaveGame(from);
|
||||
}
|
||||
|
||||
public static void GivePoints(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGamePlayer(state.Mobile))
|
||||
return;
|
||||
|
||||
int to = pvSrc.ReadByte();
|
||||
int amount = pvSrc.ReadInt32();
|
||||
|
||||
game.Players.TransferScore(state.Mobile, to, amount);
|
||||
}
|
||||
|
||||
public static void RollDice(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGamePlayer(state.Mobile))
|
||||
return;
|
||||
|
||||
game.Dices.RollDices(state.Mobile);
|
||||
}
|
||||
|
||||
public static void BuildWalls(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
game.ResetWalls(state.Mobile);
|
||||
}
|
||||
|
||||
public static void ResetScores(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
game.Players.ResetScores(MahjongGame.BaseScore);
|
||||
}
|
||||
|
||||
public static void AssignDealer(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
int position = pvSrc.ReadByte();
|
||||
|
||||
game.Players.AssignDealer(position);
|
||||
}
|
||||
|
||||
public static void OpenSeat(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
int position = pvSrc.ReadByte();
|
||||
|
||||
if (game.Players.GetPlayer(position) == state.Mobile)
|
||||
return;
|
||||
|
||||
game.Players.OpenSeat(position);
|
||||
}
|
||||
|
||||
public static void ChangeOption(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
pvSrc.ReadInt16();
|
||||
pvSrc.ReadByte();
|
||||
|
||||
int options = pvSrc.ReadByte();
|
||||
|
||||
game.ShowScores = (options & 0x1) != 0;
|
||||
game.SpectatorVision = (options & 0x2) != 0;
|
||||
}
|
||||
|
||||
public static void MoveWallBreakIndicator(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
int y = pvSrc.ReadInt16();
|
||||
int x = pvSrc.ReadInt16();
|
||||
|
||||
game.WallBreakIndicator.Move(new Point2D(x, y));
|
||||
}
|
||||
|
||||
public static void TogglePublicHand(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGamePlayer(state.Mobile))
|
||||
return;
|
||||
|
||||
pvSrc.ReadInt16();
|
||||
pvSrc.ReadByte();
|
||||
|
||||
bool publicHand = pvSrc.ReadBoolean();
|
||||
|
||||
game.Players.SetPublic(game.Players.GetPlayerIndex(state.Mobile), publicHand);
|
||||
}
|
||||
|
||||
public static void MoveTile(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGamePlayer(state.Mobile))
|
||||
return;
|
||||
|
||||
int number = pvSrc.ReadByte();
|
||||
|
||||
if (number < 0 || number >= game.Tiles.Length)
|
||||
return;
|
||||
|
||||
pvSrc.ReadByte(); // Current direction
|
||||
|
||||
MahjongPieceDirection direction = GetDirection(pvSrc.ReadByte());
|
||||
|
||||
pvSrc.ReadByte();
|
||||
|
||||
bool flip = pvSrc.ReadBoolean();
|
||||
|
||||
pvSrc.ReadInt16(); // Current Y
|
||||
pvSrc.ReadInt16(); // Current X
|
||||
|
||||
pvSrc.ReadByte();
|
||||
|
||||
int y = pvSrc.ReadInt16();
|
||||
int x = pvSrc.ReadInt16();
|
||||
|
||||
pvSrc.ReadByte();
|
||||
|
||||
game.Tiles[number].Move(new Point2D(x, y), direction, flip, game.Players.GetPlayerIndex(state.Mobile));
|
||||
}
|
||||
|
||||
public static void MoveDealerIndicator(MahjongGame game, NetState state, PacketReader pvSrc)
|
||||
{
|
||||
if (game == null || !game.Players.IsInGameDealer(state.Mobile))
|
||||
return;
|
||||
|
||||
MahjongPieceDirection direction = GetDirection(pvSrc.ReadByte());
|
||||
|
||||
MahjongWind wind = GetWind(pvSrc.ReadByte());
|
||||
|
||||
int y = pvSrc.ReadInt16();
|
||||
int x = pvSrc.ReadInt16();
|
||||
|
||||
game.DealerIndicator.Move(new Point2D(x, y), direction, wind);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Projects/Scripts/Items/Games/Mahjong/MahjongPieceDim.cs
Normal file
46
Projects/Scripts/Items/Games/Mahjong/MahjongPieceDim.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public struct MahjongPieceDim
|
||||
{
|
||||
public Point2D Position{ get; }
|
||||
|
||||
public int Width{ get; }
|
||||
|
||||
public int Height{ get; }
|
||||
|
||||
public MahjongPieceDim(Point2D position, int width, int height)
|
||||
{
|
||||
Position = position;
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
|
||||
public bool IsValid()
|
||||
{
|
||||
return Position.X >= 0 && Position.Y >= 0 && Position.X + Width <= 670 && Position.Y + Height <= 670;
|
||||
}
|
||||
|
||||
public bool IsOverlapping(MahjongPieceDim dim)
|
||||
{
|
||||
return Position.X < dim.Position.X + dim.Width && Position.Y < dim.Position.Y + dim.Height &&
|
||||
Position.X + Width > dim.Position.X && Position.Y + Height > dim.Position.Y;
|
||||
}
|
||||
|
||||
public int GetHandArea()
|
||||
{
|
||||
if (Position.X + Width > 150 && Position.X < 520 && Position.Y < 35)
|
||||
return 0;
|
||||
|
||||
if (Position.X + Width > 635 && Position.Y + Height > 150 && Position.Y < 520)
|
||||
return 1;
|
||||
|
||||
if (Position.X + Width > 150 && Position.X < 520 && Position.Y + Height > 635)
|
||||
return 2;
|
||||
|
||||
if (Position.X < 35 && Position.Y + Height > 150 && Position.Y < 520)
|
||||
return 3;
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
505
Projects/Scripts/Items/Games/Mahjong/MahjongPlayers.cs
Normal file
505
Projects/Scripts/Items/Games/Mahjong/MahjongPlayers.cs
Normal file
|
|
@ -0,0 +1,505 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongPlayers
|
||||
{
|
||||
private bool[] m_InGame;
|
||||
private Mobile[] m_Players;
|
||||
private bool[] m_PublicHand;
|
||||
private int[] m_Scores;
|
||||
private List<Mobile> m_Spectators;
|
||||
|
||||
public MahjongPlayers(MahjongGame game, int maxPlayers, int baseScore)
|
||||
{
|
||||
Game = game;
|
||||
m_Spectators = new List<Mobile>();
|
||||
|
||||
m_Players = new Mobile[maxPlayers];
|
||||
m_InGame = new bool[maxPlayers];
|
||||
m_PublicHand = new bool[maxPlayers];
|
||||
m_Scores = new int[maxPlayers];
|
||||
|
||||
for (int i = 0; i < m_Scores.Length; i++)
|
||||
m_Scores[i] = baseScore;
|
||||
}
|
||||
|
||||
public MahjongPlayers(MahjongGame game, GenericReader reader)
|
||||
{
|
||||
Game = game;
|
||||
m_Spectators = new List<Mobile>();
|
||||
|
||||
int version = reader.ReadInt();
|
||||
|
||||
int seats = reader.ReadInt();
|
||||
m_Players = new Mobile[seats];
|
||||
m_InGame = new bool[seats];
|
||||
m_PublicHand = new bool[seats];
|
||||
m_Scores = new int[seats];
|
||||
|
||||
for (int i = 0; i < seats; i++)
|
||||
{
|
||||
m_Players[i] = reader.ReadMobile();
|
||||
m_PublicHand[i] = reader.ReadBool();
|
||||
m_Scores[i] = reader.ReadInt();
|
||||
}
|
||||
|
||||
DealerPosition = reader.ReadInt();
|
||||
}
|
||||
|
||||
public MahjongGame Game{ get; }
|
||||
|
||||
public int Seats => m_Players.Length;
|
||||
public Mobile Dealer => m_Players[DealerPosition];
|
||||
public int DealerPosition{ get; private set; }
|
||||
|
||||
public Mobile GetPlayer(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_Players.Length)
|
||||
return null;
|
||||
return m_Players[index];
|
||||
}
|
||||
|
||||
public int GetPlayerIndex(Mobile mobile)
|
||||
{
|
||||
for (int i = 0; i < m_Players.Length; i++)
|
||||
if (m_Players[i] == mobile)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public bool IsInGameDealer(Mobile mobile)
|
||||
{
|
||||
if (Dealer != mobile)
|
||||
return false;
|
||||
return m_InGame[DealerPosition];
|
||||
}
|
||||
|
||||
public bool IsInGamePlayer(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_Players.Length || m_Players[index] == null)
|
||||
return false;
|
||||
return m_InGame[index];
|
||||
}
|
||||
|
||||
public bool IsInGamePlayer(Mobile mobile)
|
||||
{
|
||||
int index = GetPlayerIndex(mobile);
|
||||
|
||||
return IsInGamePlayer(index);
|
||||
}
|
||||
|
||||
public bool IsSpectator(Mobile mobile)
|
||||
{
|
||||
return m_Spectators.Contains(mobile);
|
||||
}
|
||||
|
||||
public int GetScore(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_Scores.Length)
|
||||
return 0;
|
||||
return m_Scores[index];
|
||||
}
|
||||
|
||||
public bool IsPublic(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_PublicHand.Length)
|
||||
return false;
|
||||
return m_PublicHand[index];
|
||||
}
|
||||
|
||||
public void SetPublic(int index, bool value)
|
||||
{
|
||||
if (index < 0 || index >= m_PublicHand.Length || m_PublicHand[index] == value)
|
||||
return;
|
||||
|
||||
m_PublicHand[index] = value;
|
||||
|
||||
SendTilesPacket(true, !Game.SpectatorVision);
|
||||
|
||||
if (IsInGamePlayer(index))
|
||||
m_Players[index].SendLocalizedMessage(value ? 1062775 : 1062776); // Your hand is [not] publicly viewable.
|
||||
}
|
||||
|
||||
public List<Mobile> GetInGameMobiles(bool players, bool spectators)
|
||||
{
|
||||
List<Mobile> list = new List<Mobile>();
|
||||
|
||||
if (players)
|
||||
for (int i = 0; i < m_Players.Length; i++)
|
||||
if (IsInGamePlayer(i))
|
||||
list.Add(m_Players[i]);
|
||||
|
||||
if (spectators)
|
||||
list.AddRange(m_Spectators);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public void CheckPlayers()
|
||||
{
|
||||
bool removed = false;
|
||||
|
||||
for (int i = 0; i < m_Players.Length; i++)
|
||||
{
|
||||
Mobile player = m_Players[i];
|
||||
|
||||
if (player == null)
|
||||
continue;
|
||||
|
||||
if (player.Deleted)
|
||||
{
|
||||
m_Players[i] = null;
|
||||
|
||||
SendPlayerExitMessage(player);
|
||||
UpdateDealer(true);
|
||||
|
||||
removed = true;
|
||||
}
|
||||
else if (m_InGame[i])
|
||||
{
|
||||
if (player.NetState == null)
|
||||
{
|
||||
m_InGame[i] = false;
|
||||
|
||||
SendPlayerExitMessage(player);
|
||||
UpdateDealer(true);
|
||||
|
||||
removed = true;
|
||||
}
|
||||
else if (!Game.IsAccessibleTo(player) || player.Map != Game.Map ||
|
||||
!player.InRange(Game.GetWorldLocation(), 5))
|
||||
{
|
||||
m_InGame[i] = false;
|
||||
|
||||
player.Send(new MahjongRelieve(Game));
|
||||
|
||||
SendPlayerExitMessage(player);
|
||||
UpdateDealer(true);
|
||||
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_Spectators.Count;)
|
||||
{
|
||||
Mobile mobile = m_Spectators[i];
|
||||
|
||||
if (mobile.NetState == null || mobile.Deleted)
|
||||
{
|
||||
m_Spectators.RemoveAt(i);
|
||||
}
|
||||
else if (!Game.IsAccessibleTo(mobile) || mobile.Map != Game.Map ||
|
||||
!mobile.InRange(Game.GetWorldLocation(), 5))
|
||||
{
|
||||
m_Spectators.RemoveAt(i);
|
||||
|
||||
mobile.Send(new MahjongRelieve(Game));
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (removed && !UpdateSpectators())
|
||||
SendPlayersPacket(true, true);
|
||||
}
|
||||
|
||||
private void UpdateDealer(bool message)
|
||||
{
|
||||
if (IsInGamePlayer(DealerPosition))
|
||||
return;
|
||||
|
||||
for (int i = DealerPosition + 1; i < m_Players.Length; i++)
|
||||
if (IsInGamePlayer(i))
|
||||
{
|
||||
DealerPosition = i;
|
||||
|
||||
if (message)
|
||||
SendDealerChangedMessage();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < DealerPosition; i++)
|
||||
if (IsInGamePlayer(i))
|
||||
{
|
||||
DealerPosition = i;
|
||||
|
||||
if (message)
|
||||
SendDealerChangedMessage();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetNextSeat()
|
||||
{
|
||||
for (int i = DealerPosition; i < m_Players.Length; i++)
|
||||
if (m_Players[i] == null)
|
||||
return i;
|
||||
|
||||
for (int i = 0; i < DealerPosition; i++)
|
||||
if (m_Players[i] == null)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private bool UpdateSpectators()
|
||||
{
|
||||
if (m_Spectators.Count == 0)
|
||||
return false;
|
||||
|
||||
int nextSeat = GetNextSeat();
|
||||
|
||||
if (nextSeat >= 0)
|
||||
{
|
||||
Mobile newPlayer = m_Spectators[0];
|
||||
|
||||
m_Spectators.RemoveAt(0);
|
||||
|
||||
AddPlayer(newPlayer, nextSeat, false);
|
||||
|
||||
UpdateSpectators();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void AddPlayer(Mobile player, int index, bool sendJoinGame)
|
||||
{
|
||||
m_Players[index] = player;
|
||||
m_InGame[index] = true;
|
||||
|
||||
UpdateDealer(false);
|
||||
|
||||
if (sendJoinGame)
|
||||
player.Send(new MahjongJoinGame(Game));
|
||||
|
||||
SendPlayersPacket(true, true);
|
||||
|
||||
player.Send(new MahjongGeneralInfo(Game));
|
||||
player.Send(new MahjongTilesInfo(Game, player));
|
||||
|
||||
if (DealerPosition == index)
|
||||
SendLocalizedMessage(1062773, player.Name); // ~1_name~ has entered the game as the dealer.
|
||||
else
|
||||
SendLocalizedMessage(1062772, player.Name); // ~1_name~ has entered the game as a player.
|
||||
}
|
||||
|
||||
private void AddSpectator(Mobile mobile)
|
||||
{
|
||||
if (!IsSpectator(mobile)) m_Spectators.Add(mobile);
|
||||
|
||||
mobile.Send(new MahjongJoinGame(Game));
|
||||
mobile.Send(new MahjongPlayersInfo(Game, mobile));
|
||||
mobile.Send(new MahjongGeneralInfo(Game));
|
||||
mobile.Send(new MahjongTilesInfo(Game, mobile));
|
||||
}
|
||||
|
||||
public void Join(Mobile mobile)
|
||||
{
|
||||
int index = GetPlayerIndex(mobile);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
AddPlayer(mobile, index, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
int nextSeat = GetNextSeat();
|
||||
|
||||
if (nextSeat >= 0)
|
||||
AddPlayer(mobile, nextSeat, true);
|
||||
else
|
||||
AddSpectator(mobile);
|
||||
}
|
||||
}
|
||||
|
||||
public void LeaveGame(Mobile player)
|
||||
{
|
||||
int index = GetPlayerIndex(player);
|
||||
if (index >= 0)
|
||||
{
|
||||
m_InGame[index] = false;
|
||||
|
||||
SendPlayerExitMessage(player);
|
||||
UpdateDealer(true);
|
||||
|
||||
SendPlayersPacket(true, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Spectators.Remove(player);
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetScores(int value)
|
||||
{
|
||||
for (int i = 0; i < m_Scores.Length; i++) m_Scores[i] = value;
|
||||
|
||||
SendPlayersPacket(true, Game.ShowScores);
|
||||
|
||||
SendLocalizedMessage(1062697); // The dealer redistributes the score sticks evenly.
|
||||
}
|
||||
|
||||
public void TransferScore(Mobile from, int toPosition, int amount)
|
||||
{
|
||||
int fromPosition = GetPlayerIndex(from);
|
||||
Mobile to = GetPlayer(toPosition);
|
||||
|
||||
if (fromPosition < 0 || to == null || m_Scores[fromPosition] < amount)
|
||||
return;
|
||||
|
||||
m_Scores[fromPosition] -= amount;
|
||||
m_Scores[toPosition] += amount;
|
||||
|
||||
if (Game.ShowScores)
|
||||
{
|
||||
SendPlayersPacket(true, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
from.Send(new MahjongPlayersInfo(Game, from));
|
||||
to.Send(new MahjongPlayersInfo(Game, to));
|
||||
}
|
||||
|
||||
SendLocalizedMessage(1062774,
|
||||
$"{from.Name}\t{to.Name}\t{amount}"); // ~1_giver~ gives ~2_receiver~ ~3_number~ points.
|
||||
}
|
||||
|
||||
public void OpenSeat(int index)
|
||||
{
|
||||
Mobile player = GetPlayer(index);
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
if (m_InGame[index])
|
||||
player.Send(new MahjongRelieve(Game));
|
||||
|
||||
m_Players[index] = null;
|
||||
|
||||
SendLocalizedMessage(1062699, player.Name); // ~1_name~ is relieved from the game by the dealer.
|
||||
|
||||
UpdateDealer(true);
|
||||
|
||||
if (!UpdateSpectators())
|
||||
SendPlayersPacket(true, true);
|
||||
}
|
||||
|
||||
public void AssignDealer(int index)
|
||||
{
|
||||
Mobile to = GetPlayer(index);
|
||||
|
||||
if (to == null || !m_InGame[index])
|
||||
return;
|
||||
|
||||
int oldDealer = DealerPosition;
|
||||
|
||||
DealerPosition = index;
|
||||
|
||||
if (IsInGamePlayer(oldDealer))
|
||||
m_Players[oldDealer].Send(new MahjongPlayersInfo(Game, m_Players[oldDealer]));
|
||||
|
||||
to.Send(new MahjongPlayersInfo(Game, to));
|
||||
|
||||
SendDealerChangedMessage();
|
||||
}
|
||||
|
||||
private void SendDealerChangedMessage()
|
||||
{
|
||||
if (Dealer != null)
|
||||
SendLocalizedMessage(1062698, Dealer.Name); // ~1_name~ is assigned the dealer.
|
||||
}
|
||||
|
||||
private void SendPlayerExitMessage(Mobile who)
|
||||
{
|
||||
SendLocalizedMessage(1062762, who.Name); // ~1_name~ has left the game.
|
||||
}
|
||||
|
||||
public void SendPlayersPacket(bool players, bool spectators)
|
||||
{
|
||||
foreach (Mobile mobile in GetInGameMobiles(players, spectators))
|
||||
mobile.Send(new MahjongPlayersInfo(Game, mobile));
|
||||
}
|
||||
|
||||
public void SendGeneralPacket(bool players, bool spectators)
|
||||
{
|
||||
List<Mobile> mobiles = GetInGameMobiles(players, spectators);
|
||||
|
||||
if (mobiles.Count == 0)
|
||||
return;
|
||||
|
||||
MahjongGeneralInfo generalInfo = new MahjongGeneralInfo(Game);
|
||||
|
||||
generalInfo.Acquire();
|
||||
|
||||
foreach (Mobile mobile in mobiles)
|
||||
mobile.Send(generalInfo);
|
||||
|
||||
generalInfo.Release();
|
||||
}
|
||||
|
||||
public void SendTilesPacket(bool players, bool spectators)
|
||||
{
|
||||
foreach (Mobile mobile in GetInGameMobiles(players, spectators))
|
||||
mobile.Send(new MahjongTilesInfo(Game, mobile));
|
||||
}
|
||||
|
||||
public void SendTilePacket(MahjongTile tile, bool players, bool spectators)
|
||||
{
|
||||
foreach (Mobile mobile in GetInGameMobiles(players, spectators))
|
||||
mobile.Send(new MahjongTileInfo(tile, mobile));
|
||||
}
|
||||
|
||||
public void SendRelievePacket(bool players, bool spectators)
|
||||
{
|
||||
List<Mobile> mobiles = GetInGameMobiles(players, spectators);
|
||||
|
||||
if (mobiles.Count == 0)
|
||||
return;
|
||||
|
||||
MahjongRelieve relieve = new MahjongRelieve(Game);
|
||||
|
||||
relieve.Acquire();
|
||||
|
||||
foreach (Mobile mobile in mobiles)
|
||||
mobile.Send(relieve);
|
||||
|
||||
relieve.Release();
|
||||
}
|
||||
|
||||
public void SendLocalizedMessage(int number)
|
||||
{
|
||||
foreach (Mobile mobile in GetInGameMobiles(true, true))
|
||||
mobile.SendLocalizedMessage(number);
|
||||
}
|
||||
|
||||
public void SendLocalizedMessage(int number, string args)
|
||||
{
|
||||
foreach (Mobile mobile in GetInGameMobiles(true, true))
|
||||
mobile.SendLocalizedMessage(number, args);
|
||||
}
|
||||
|
||||
public void Save(GenericWriter writer)
|
||||
{
|
||||
writer.Write(0); // version
|
||||
|
||||
writer.Write(Seats);
|
||||
|
||||
for (int i = 0; i < Seats; i++)
|
||||
{
|
||||
writer.Write(m_Players[i]);
|
||||
writer.Write(m_PublicHand[i]);
|
||||
writer.Write(m_Scores[i]);
|
||||
}
|
||||
|
||||
writer.Write(DealerPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
88
Projects/Scripts/Items/Games/Mahjong/MahjongTile.cs
Normal file
88
Projects/Scripts/Items/Games/Mahjong/MahjongTile.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongTile
|
||||
{
|
||||
protected Point2D m_Position;
|
||||
|
||||
public MahjongTile(MahjongGame game, int number, MahjongTileType value, Point2D position, int stackLevel,
|
||||
MahjongPieceDirection direction, bool flipped)
|
||||
{
|
||||
Game = game;
|
||||
Number = number;
|
||||
Value = value;
|
||||
m_Position = position;
|
||||
StackLevel = stackLevel;
|
||||
Direction = direction;
|
||||
Flipped = flipped;
|
||||
}
|
||||
|
||||
public MahjongTile(MahjongGame game, GenericReader reader)
|
||||
{
|
||||
Game = game;
|
||||
|
||||
int version = reader.ReadInt();
|
||||
|
||||
Number = reader.ReadInt();
|
||||
Value = (MahjongTileType)reader.ReadInt();
|
||||
m_Position = reader.ReadPoint2D();
|
||||
StackLevel = reader.ReadInt();
|
||||
Direction = (MahjongPieceDirection)reader.ReadInt();
|
||||
Flipped = reader.ReadBool();
|
||||
}
|
||||
|
||||
public MahjongGame Game{ get; }
|
||||
|
||||
public int Number{ get; }
|
||||
|
||||
public MahjongTileType Value{ get; }
|
||||
|
||||
public Point2D Position => m_Position;
|
||||
public int StackLevel{ get; private set; }
|
||||
|
||||
public MahjongPieceDirection Direction{ get; private set; }
|
||||
|
||||
public bool Flipped{ get; private set; }
|
||||
|
||||
public MahjongPieceDim Dimensions => GetDimensions(m_Position, Direction);
|
||||
|
||||
public bool IsMovable => Game.GetStackLevel(Dimensions) <= StackLevel;
|
||||
|
||||
public static MahjongPieceDim GetDimensions(Point2D position, MahjongPieceDirection direction)
|
||||
{
|
||||
if (direction == MahjongPieceDirection.Up || direction == MahjongPieceDirection.Down)
|
||||
return new MahjongPieceDim(position, 20, 30);
|
||||
return new MahjongPieceDim(position, 30, 20);
|
||||
}
|
||||
|
||||
public void Move(Point2D position, MahjongPieceDirection direction, bool flip, int validHandArea)
|
||||
{
|
||||
MahjongPieceDim dim = GetDimensions(position, direction);
|
||||
int curHandArea = Dimensions.GetHandArea();
|
||||
int newHandArea = dim.GetHandArea();
|
||||
|
||||
if (!IsMovable || !dim.IsValid() || validHandArea >= 0 &&
|
||||
(curHandArea >= 0 && curHandArea != validHandArea || newHandArea >= 0 && newHandArea != validHandArea))
|
||||
return;
|
||||
|
||||
m_Position = position;
|
||||
Direction = direction;
|
||||
StackLevel = -1; // Avoid self interference
|
||||
StackLevel = Game.GetStackLevel(dim) + 1;
|
||||
Flipped = flip;
|
||||
|
||||
Game.Players.SendTilePacket(this, true, true);
|
||||
}
|
||||
|
||||
public void Save(GenericWriter writer)
|
||||
{
|
||||
writer.Write(0); // version
|
||||
|
||||
writer.Write(Number);
|
||||
writer.Write((int)Value);
|
||||
writer.Write(m_Position);
|
||||
writer.Write(StackLevel);
|
||||
writer.Write((int)Direction);
|
||||
writer.Write(Flipped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongTileTypeGenerator
|
||||
{
|
||||
public MahjongTileTypeGenerator()
|
||||
{
|
||||
LeftTileTypes = new List<MahjongTileType>(136);
|
||||
|
||||
for (int i = 1; i <= 34; i++)
|
||||
{
|
||||
MahjongTileType tile = (MahjongTileType)i;
|
||||
LeftTileTypes.Add(tile);
|
||||
LeftTileTypes.Add(tile);
|
||||
LeftTileTypes.Add(tile);
|
||||
LeftTileTypes.Add(tile);
|
||||
}
|
||||
}
|
||||
|
||||
public List<MahjongTileType> LeftTileTypes{ get; }
|
||||
|
||||
public MahjongTileType Next()
|
||||
{
|
||||
int random = Utility.Random(LeftTileTypes.Count);
|
||||
MahjongTileType next = LeftTileTypes[random];
|
||||
LeftTileTypes.RemoveAt(random);
|
||||
|
||||
return next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public class MahjongWallBreakIndicator
|
||||
{
|
||||
public MahjongWallBreakIndicator(MahjongGame game, Point2D position)
|
||||
{
|
||||
Game = game;
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public MahjongWallBreakIndicator(MahjongGame game, GenericReader reader)
|
||||
{
|
||||
Game = game;
|
||||
|
||||
int version = reader.ReadInt();
|
||||
|
||||
Position = reader.ReadPoint2D();
|
||||
}
|
||||
|
||||
public MahjongGame Game{ get; }
|
||||
|
||||
public Point2D Position{ get; private set; }
|
||||
|
||||
public MahjongPieceDim Dimensions => GetDimensions(Position);
|
||||
|
||||
public static MahjongPieceDim GetDimensions(Point2D position)
|
||||
{
|
||||
return new MahjongPieceDim(position, 20, 20);
|
||||
}
|
||||
|
||||
public void Move(Point2D position)
|
||||
{
|
||||
MahjongPieceDim dim = GetDimensions(position);
|
||||
|
||||
if (!dim.IsValid())
|
||||
return;
|
||||
|
||||
Position = position;
|
||||
|
||||
Game.Players.SendGeneralPacket(true, true);
|
||||
}
|
||||
|
||||
public void Save(GenericWriter writer)
|
||||
{
|
||||
writer.Write(0); // version
|
||||
|
||||
writer.Write(Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
209
Projects/Scripts/Items/Games/Mahjong/Packets.cs
Normal file
209
Projects/Scripts/Items/Games/Mahjong/Packets.cs
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
using System.IO;
|
||||
using Server.Network;
|
||||
|
||||
namespace Server.Engines.Mahjong
|
||||
{
|
||||
public sealed class MahjongJoinGame : Packet
|
||||
{
|
||||
public MahjongJoinGame(MahjongGame game) : base(0xDA)
|
||||
{
|
||||
EnsureCapacity(9);
|
||||
|
||||
m_Stream.Write(game.Serial);
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)0x19);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MahjongPlayersInfo : Packet
|
||||
{
|
||||
public MahjongPlayersInfo(MahjongGame game, Mobile to) : base(0xDA)
|
||||
{
|
||||
MahjongPlayers players = game.Players;
|
||||
|
||||
EnsureCapacity(11 + 45 * players.Seats);
|
||||
|
||||
m_Stream.Write(game.Serial);
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)0x2);
|
||||
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)players.Seats);
|
||||
|
||||
int n = 0;
|
||||
for (int i = 0; i < players.Seats; i++)
|
||||
{
|
||||
Mobile mobile = players.GetPlayer(i);
|
||||
|
||||
if (mobile != null)
|
||||
{
|
||||
m_Stream.Write(mobile.Serial);
|
||||
m_Stream.Write(players.DealerPosition == i ? (byte)0x1 : (byte)0x2);
|
||||
m_Stream.Write((byte)i);
|
||||
|
||||
if (game.ShowScores || mobile == to)
|
||||
m_Stream.Write(players.GetScore(i));
|
||||
else
|
||||
m_Stream.Write(0);
|
||||
|
||||
m_Stream.Write((short)0);
|
||||
m_Stream.Write((byte)0);
|
||||
|
||||
m_Stream.Write(players.IsPublic(i));
|
||||
|
||||
m_Stream.WriteAsciiFixed(mobile.Name, 30);
|
||||
m_Stream.Write(!players.IsInGamePlayer(i));
|
||||
|
||||
n++;
|
||||
}
|
||||
else if (game.ShowScores)
|
||||
{
|
||||
m_Stream.Write(0);
|
||||
m_Stream.Write((byte)0x2);
|
||||
m_Stream.Write((byte)i);
|
||||
|
||||
m_Stream.Write(players.GetScore(i));
|
||||
|
||||
m_Stream.Write((short)0);
|
||||
m_Stream.Write((byte)0);
|
||||
|
||||
m_Stream.Write(players.IsPublic(i));
|
||||
|
||||
m_Stream.WriteAsciiFixed("", 30);
|
||||
m_Stream.Write(true);
|
||||
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
if (n != players.Seats)
|
||||
{
|
||||
m_Stream.Seek(10, SeekOrigin.Begin);
|
||||
m_Stream.Write((byte)n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MahjongGeneralInfo : Packet
|
||||
{
|
||||
public MahjongGeneralInfo(MahjongGame game) : base(0xDA)
|
||||
{
|
||||
EnsureCapacity(13);
|
||||
|
||||
m_Stream.Write(game.Serial);
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)0x5);
|
||||
|
||||
m_Stream.Write((short)0);
|
||||
m_Stream.Write((byte)0);
|
||||
|
||||
m_Stream.Write((byte)((game.ShowScores ? 0x1 : 0x0) | (game.SpectatorVision ? 0x2 : 0x0)));
|
||||
|
||||
m_Stream.Write((byte)game.Dices.First);
|
||||
m_Stream.Write((byte)game.Dices.Second);
|
||||
|
||||
m_Stream.Write((byte)game.DealerIndicator.Wind);
|
||||
m_Stream.Write((short)game.DealerIndicator.Position.Y);
|
||||
m_Stream.Write((short)game.DealerIndicator.Position.X);
|
||||
m_Stream.Write((byte)game.DealerIndicator.Direction);
|
||||
|
||||
m_Stream.Write((short)game.WallBreakIndicator.Position.Y);
|
||||
m_Stream.Write((short)game.WallBreakIndicator.Position.X);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MahjongTilesInfo : Packet
|
||||
{
|
||||
public MahjongTilesInfo(MahjongGame game, Mobile to) : base(0xDA)
|
||||
{
|
||||
MahjongTile[] tiles = game.Tiles;
|
||||
MahjongPlayers players = game.Players;
|
||||
|
||||
EnsureCapacity(11 + 9 * tiles.Length);
|
||||
|
||||
m_Stream.Write(game.Serial);
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)0x4);
|
||||
|
||||
m_Stream.Write((short)tiles.Length);
|
||||
|
||||
foreach (MahjongTile tile in tiles)
|
||||
{
|
||||
m_Stream.Write((byte)tile.Number);
|
||||
|
||||
if (tile.Flipped)
|
||||
{
|
||||
int hand = tile.Dimensions.GetHandArea();
|
||||
|
||||
if (hand < 0 || players.IsPublic(hand) || players.GetPlayer(hand) == to ||
|
||||
game.SpectatorVision && players.IsSpectator(to))
|
||||
m_Stream.Write((byte)tile.Value);
|
||||
else
|
||||
m_Stream.Write((byte)0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Stream.Write((byte)0);
|
||||
}
|
||||
|
||||
m_Stream.Write((short)tile.Position.Y);
|
||||
m_Stream.Write((short)tile.Position.X);
|
||||
m_Stream.Write((byte)tile.StackLevel);
|
||||
m_Stream.Write((byte)tile.Direction);
|
||||
|
||||
m_Stream.Write(tile.Flipped ? (byte)0x10 : (byte)0x0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MahjongTileInfo : Packet
|
||||
{
|
||||
public MahjongTileInfo(MahjongTile tile, Mobile to) : base(0xDA)
|
||||
{
|
||||
MahjongGame game = tile.Game;
|
||||
MahjongPlayers players = game.Players;
|
||||
|
||||
EnsureCapacity(18);
|
||||
|
||||
m_Stream.Write(tile.Game.Serial);
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)0x3);
|
||||
|
||||
m_Stream.Write((byte)tile.Number);
|
||||
|
||||
if (tile.Flipped)
|
||||
{
|
||||
int hand = tile.Dimensions.GetHandArea();
|
||||
|
||||
if (hand < 0 || players.IsPublic(hand) || players.GetPlayer(hand) == to ||
|
||||
game.SpectatorVision && players.IsSpectator(to))
|
||||
m_Stream.Write((byte)tile.Value);
|
||||
else
|
||||
m_Stream.Write((byte)0);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Stream.Write((byte)0);
|
||||
}
|
||||
|
||||
m_Stream.Write((short)tile.Position.Y);
|
||||
m_Stream.Write((short)tile.Position.X);
|
||||
m_Stream.Write((byte)tile.StackLevel);
|
||||
m_Stream.Write((byte)tile.Direction);
|
||||
|
||||
m_Stream.Write(tile.Flipped ? (byte)0x10 : (byte)0x0);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MahjongRelieve : Packet
|
||||
{
|
||||
public MahjongRelieve(MahjongGame game) : base(0xDA)
|
||||
{
|
||||
EnsureCapacity(9);
|
||||
|
||||
m_Stream.Write(game.Serial);
|
||||
m_Stream.Write((byte)0);
|
||||
m_Stream.Write((byte)0x1A);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue