ModernUO/Projects/Scripts/Items/Games/Mahjong/MahjongPlayers.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

458 lines
12 KiB
C#

using System.Collections.Generic;
using System.Linq;
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) => IsInGamePlayer(GetPlayerIndex(mobile));
public bool IsSpectator(Mobile mobile) => m_Spectators.Contains(mobile);
public bool IsSpectator(Mobile mobile) => m_Spectators.Contains(mobile);
public bool IsPublic(int index) => index >= 0 && index < m_PublicHand.Length && 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) list.AddRange(m_Players.Where((t, i) => IsInGamePlayer(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;
MahjongPackets.SendMahjongRelieve(player.NetState, 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);
MahjongPackets.SendMahjongRelieve(mobile.NetState, 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)
MahjongPackets.SendMahjongJoinGame(player.NetState, Game);
SendPlayersPacket(true, true);
MahjongPackets.SendMahjongGeneralInfo(player.NetState, Game);
MahjongPackets.SendMahjongTilesInfo(player.NetState, 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 m)
{
if (!IsSpectator(m)) m_Spectators.Add(m);
MahjongPackets.SendMahjongJoinGame(m.NetState, Game);
MahjongPackets.SendMahjongPlayersInfo(m.NetState, Game, m);
MahjongPackets.SendMahjongGeneralInfo(m.NetState, Game);
MahjongPackets.SendMahjongTilesInfo(m.NetState, Game, m);
}
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
{
MahjongPackets.SendMahjongPlayersInfo(from.NetState, Game, from);
MahjongPackets.SendMahjongPlayersInfo(to.NetState, 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])
MahjongPackets.SendMahjongRelieve(player.NetState, 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))
MahjongPackets.SendMahjongPlayersInfo(m_Players[oldDealer].NetState, Game, m_Players[oldDealer]);
MahjongPackets.SendMahjongPlayersInfo(to.NetState, 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))
MahjongPackets.SendMahjongPlayersInfo(mobile.NetState, Game, mobile);
}
public void SendGeneralPacket(bool players, bool spectators)
{
foreach (Mobile mobile in GetInGameMobiles(players, spectators))
MahjongPackets.SendMahjongGeneralInfo(mobile.NetState, Game);
}
public void SendTilesPacket(bool players, bool spectators)
{
foreach (Mobile mobile in GetInGameMobiles(players, spectators))
MahjongPackets.SendMahjongTilesInfo(mobile.NetState, Game, mobile);
}
public void SendTilePacket(MahjongTile tile, bool players, bool spectators)
{
foreach (Mobile mobile in GetInGameMobiles(players, spectators))
MahjongPackets.SendMahjongTileInfo(mobile.NetState, tile, mobile);
}
public void SendRelievePacket(bool players, bool spectators)
{
List<Mobile> mobiles = GetInGameMobiles(players, spectators);
if (mobiles.Count == 0)
return;
foreach (Mobile mobile in mobiles)
MahjongPackets.SendMahjongRelieve(mobile.NetState, Game);
}
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);
}
}
}