Skip to content

Commit

Permalink
Merge branch 'dev' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
panxuc authored Apr 9, 2024
2 parents 5edc179 + 4585913 commit bd2bc78
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 32 deletions.
3 changes: 1 addition & 2 deletions dependency/proto/Message2Server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,5 @@ message BuildShipMsg
{
ShipType ship_type = 1;
int64 team_id = 2;
int64 player_id = 3;
int32 birthpoint_index = 4;
int32 birthpoint_index = 3;
}
6 changes: 3 additions & 3 deletions logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace GameClass.GameObj.Areas;

public class Wormhole(XY initPos, List<XY> grids)
public class Wormhole(XY initPos, List<CellXY> cells)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Wormhole), IWormhole
{
public InVariableRange<long> HP = new(GameData.WormholeHP);
private readonly List<XY> grids = grids;
public List<XY> Grids => grids;
private readonly List<CellXY> cells = cells;
public List<CellXY> Cells => cells;
public override bool IsRigid => HP > GameData.WormholeHP / 2;
public override ShapeType Shape => ShapeType.Square;
public AtomicInt RepairNum { get; } = new AtomicInt(0);
Expand Down
17 changes: 11 additions & 6 deletions logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public IOutOfBound GetOutOfBound(XY pos)

public static bool WormholeInteract(Wormhole gameObj, XY Pos)
{
foreach (XY xy in gameObj.Grids)
foreach (CellXY xy in gameObj.Cells)
{
if (GameData.ApproachToInteract(xy, Pos))
return true;
Expand Down Expand Up @@ -114,6 +114,11 @@ public static bool WormholeInteract(Wormhole gameObj, XY Pos)
return GameObjDict[GameObjType.Ship].Cast<Ship>()?.FindAll(ship =>
GameData.IsInTheRange(ship.Position, Pos, range));
}
public List<Ship>? ShipInTheList(List<CellXY> PosList)
{
return GameObjDict[GameObjType.Ship].Cast<Ship>()?.FindAll(ship =>
PosList.Contains(GameData.PosGridToCellXY(ship.Position)));
}
public bool CanSee(Ship ship, GameObj gameObj)
{
XY pos1 = ship.Position;
Expand Down Expand Up @@ -215,13 +220,13 @@ public Map(MapStruct mapResource)
case PlaceType.Wormhole:
Func<Wormhole, bool> HasWormhole = (Wormhole wormhole) =>
{
if (wormhole.Grids.Contains(new XY(i, j)))
if (wormhole.Cells.Contains(new CellXY(i, j)))
return true;
foreach (XY xy in wormhole.Grids)
foreach (CellXY xy in wormhole.Cells)
{
if (Math.Abs(xy.x - i) <= 1 && Math.Abs(xy.y - j) <= 1)
{
wormhole.Grids.Add(new XY(i, j));
wormhole.Cells.Add(new CellXY(i, j));
return true;
}
}
Expand All @@ -230,8 +235,8 @@ public Map(MapStruct mapResource)

if (GameObjDict[GameObjType.Wormhole].Cast<Wormhole>()?.Find(wormhole => HasWormhole(wormhole)) == null)
{
List<XY> grids = [new XY(i, j)];
Add(new Wormhole(GameData.GetCellCenterPos(i, j), grids));
List<CellXY> cells = [new CellXY(i, j)];
Add(new Wormhole(GameData.GetCellCenterPos(i, j), cells));
}
break;
case PlaceType.Home:
Expand Down
15 changes: 15 additions & 0 deletions logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ private void BombObj(Bullet bullet, GameObj objBeingShot)
break;
case GameObjType.Wormhole:
((Wormhole)objBeingShot).BeAttacked(bullet);
if (((Wormhole)objBeingShot).HP < GameData.WormholeHP / 2)
{
var shipList = gameMap.ShipInTheList(((Wormhole)objBeingShot).Cells);
if (shipList != null)
{
foreach (Ship ship in shipList)
{
Debugger.Output(ship, " is destroyed!");
var money = ship.GetCost();
bullet.Parent.AddMoney(money);
Debugger.Output(bullet.Parent, " get " + money.ToString() + " money because of destroying " + ship);
shipManager.Remove(ship);
}
}
}
break;
case GameObjType.Home:
((Home)objBeingShot).BeAttacked(bullet);
Expand Down
43 changes: 36 additions & 7 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,42 @@ public struct PlayerInitInfo(long teamID, long playerID, ShipType shipType)
public List<Team> TeamList => teamList;
private readonly Map gameMap;
public Map GameMap => gameMap;
private Random random = new();
public long AddPlayer(PlayerInitInfo playerInitInfo)
{
if (!gameMap.TeamExists(playerInitInfo.teamID))
{
return GameObj.invalidID;
}
if (playerInitInfo.shipType != ShipType.Null)
if (playerInitInfo.playerID != 0)
{
// Add a ship
var shipType = playerInitInfo.shipType;
switch (shipType)
{
case ShipType.Null:
return GameObj.invalidID;
case ShipType.CivilShip:
if (teamList[(int)playerInitInfo.teamID].ShipPool.GetNum(ShipType.CivilShip) >= GameData.MaxCivilShipNum)
{
return GameObj.invalidID;
}
break;
case ShipType.WarShip:
if (teamList[(int)playerInitInfo.teamID].ShipPool.GetNum(ShipType.WarShip) >= GameData.MaxWarShipNum)
{
return GameObj.invalidID;
}
break;
case ShipType.FlagShip:
if (teamList[(int)playerInitInfo.teamID].ShipPool.GetNum(ShipType.FlagShip) >= GameData.MaxFlagShipNum)
{
return GameObj.invalidID;
}
break;
default:
return GameObj.invalidID;
}
Ship? newShip = shipManager.AddShip(playerInitInfo.teamID, playerInitInfo.playerID,
playerInitInfo.shipType, teamList[(int)playerInitInfo.teamID].MoneyPool);
if (newShip == null)
Expand All @@ -46,21 +73,22 @@ public long AddPlayer(PlayerInitInfo playerInitInfo)
return playerInitInfo.playerID;
}
}
public bool ActivateShip(long teamID, long playerID, ShipType shipType, int birthPointIndex = 0)
public long ActivateShip(long teamID, ShipType shipType, int birthPointIndex = 0)
{
Ship? ship = teamList[(int)teamID].ShipPool.GetObj(shipType);
if (ship == null)
return false;
else if (ship.IsRemoved == false)
return false;
return GameObj.invalidID;
if (birthPointIndex < 0)
birthPointIndex = 0;
if (birthPointIndex >= teamList[(int)teamID].BirthPointList.Count)
birthPointIndex = teamList[(int)teamID].BirthPointList.Count - 1;
XY pos = teamList[(int)teamID].BirthPointList[birthPointIndex];
Random random = new();
pos += new XY(((random.Next() & 2) - 1) * 1000, ((random.Next() & 2) - 1) * 1000);
return shipManager.ActivateShip(ship, pos);
if (shipManager.ActivateShip(ship, pos))
{
return ship.PlayerID;
}
return GameObj.invalidID;
}
public bool StartGame(int milliSeconds)
{
Expand All @@ -70,6 +98,7 @@ public bool StartGame(int milliSeconds)
foreach (var team in TeamList)
{
actionManager.AddMoneyNaturally(team);
ActivateShip(team.TeamID, ShipType.CivilShip);
}
new Thread
(
Expand Down
2 changes: 1 addition & 1 deletion logic/Preparation/Interface/IWormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Preparation.Interface
{
public interface IWormhole : IGameObj
{
public List<XY> Grids { get; }
public List<CellXY> Cells { get; }
}
}
5 changes: 5 additions & 0 deletions logic/Preparation/Utility/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static bool ApproachToInteract(XY pos1, XY pos2)
return Math.Abs(PosGridToCellX(pos1) - PosGridToCellX(pos2)) <= 1
&& Math.Abs(PosGridToCellY(pos1) - PosGridToCellY(pos2)) <= 1;
}
public static bool ApproachToInteract(CellXY pos1, XY pos2)
{
return Math.Abs(pos1.x - PosGridToCellX(pos2)) <= 1
&& Math.Abs(pos1.y - PosGridToCellY(pos2)) <= 1;
}
public static bool ApproachToInteractInACross(XY pos1, XY pos2)
{
if (pos1 == pos2) return false;
Expand Down
14 changes: 1 addition & 13 deletions logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,7 @@ public override Task<BoolRes> BuildShip(BuildShipMsg request, ServerCallContext
Console.WriteLine($"TRY BuildShip: ShipType {request.ShipType} from Team {request.TeamId}");
#endif
BoolRes boolRes = new();
var ship = game.TeamList[(int)request.TeamId].ShipPool.Find(
(ship) => ship.PlayerID == request.PlayerId);
if (ship == null)
{
boolRes.ActSuccess = false;
return Task.FromResult(boolRes);
}
else if (ship.IsRemoved == false)
{
boolRes.ActSuccess = false;
return Task.FromResult(boolRes);
}
boolRes.ActSuccess = game.ActivateShip(request.TeamId, request.PlayerId, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex);
boolRes.ActSuccess = game.ActivateShip(request.TeamId, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex) != GameObj.invalidID;
#if DEBUG
Console.WriteLine("END BuildShip");
#endif
Expand Down

0 comments on commit bd2bc78

Please sign in to comment.