This commit is contained in:
kawagiri 2025-09-05 16:21:33 +08:00
commit 3057212a6c
6 changed files with 103 additions and 13 deletions

View File

@ -77,6 +77,8 @@ namespace RuntimeData
public CityMapData CityMap;
// 小兵数据
public UnitMapData UnitMap;
// 网络数据
public NetData Net;
// 城市 -> 玩家, 将引用关系转化为ID关系
public Dictionary<uint, uint> CityToPlayerDict;
@ -849,6 +851,7 @@ namespace RuntimeData
PlayerMap = new PlayerMapData(mapCfg, _idGenerator);
CityMap = new CityMapData();
UnitMap = new UnitMapData();
Net = new NetData();
CityToPlayerDict = new Dictionary<uint, uint>();
UnitToCityDict = new Dictionary<uint, uint>();
@ -866,6 +869,7 @@ namespace RuntimeData
PlayerMap = new PlayerMapData(copyMap.PlayerMap);
CityMap = new CityMapData(copyMap.CityMap);
UnitMap = new UnitMapData(copyMap.UnitMap);
Net = new NetData(copyMap.Net);
CityToPlayerDict = new Dictionary<uint, uint>();
UnitToCityDict = new Dictionary<uint, uint>();
@ -896,6 +900,7 @@ namespace RuntimeData
PlayerMap.DeepCopy(copyMap.PlayerMap);
CityMap.DeepCopy(copyMap.CityMap);
UnitMap.DeepCopy(copyMap.UnitMap);
Net.DeepCopy(copyMap.Net);
CityToPlayerDict.Clear();
UnitToCityDict.Clear();

View File

@ -0,0 +1,70 @@
/*
* @Author:
* @Description:
* @Date: 20250403 11:04:31
* @Modify:
*/
using System.Collections.Generic;
using Logic.AI;
using MemoryPack;
using UnityEngine;
namespace RuntimeData
{
// 网络信息
[MemoryPackable]
public partial class NetData
{
// PlayerId => SteamId 的索引
public Dictionary<uint, ulong> Players;
// 所有玩家行为的序列
public List<ActionNetData> Actions;
// 随机数种子 开始游戏时由房主进行初始化,整局游戏不可变
public int RandomSeed;
private System.Random _random;
[MemoryPackConstructor]
public NetData()
{
Players = new Dictionary<uint, ulong>();
Actions = new List<ActionNetData>();
// 生成确定性种子(由房主生成并广播)
RandomSeed = System.Environment.TickCount;
}
// 这里单纯是因为深拷贝目前只用于 AI 演算,故不对 Players 和 Actions 赋值
public NetData(NetData copyData)
{
RandomSeed = copyData.RandomSeed;
Players = new Dictionary<uint, ulong>();
Actions = new List<ActionNetData>();
}
// 这里单纯是因为深拷贝目前只用于 AI 演算,故不对 Players 和 Actions 赋值
public void DeepCopy(NetData copyData)
{
RandomSeed = copyData.RandomSeed;
Players = new Dictionary<uint, ulong>();
Actions = new List<ActionNetData>();
}
public System.Random GetRandom()
{
if (_random == null) _random = new System.Random(RandomSeed);
return _random;
}
public static string GetMapDataHash(MapData mapData)
{
byte[] bytes = MemoryPackSerializer.Serialize(mapData);
// 使用 Unity 的 Hash128性能很好且稳定
var hash128 = new Hash128();
hash128.Append(bytes);
return hash128.ToString();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dd39d72c31f64e9a9606dd2ce1ec1ee4
timeCreated: 1756988430

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using Logic.Action;
using Logic.AI;
using MemoryPack;
using Steamworks;
using UnityEngine;
@ -274,6 +275,9 @@ namespace RuntimeData
//用于记录当前是否正在playing主要是视觉那边使用
public bool IsPlaying;
// 用以绑定开始游戏时的 Steam ID
public ulong SteamID;
// 无参数初始化
[MemoryPackConstructor]
@ -286,6 +290,7 @@ namespace RuntimeData
MeetPlayers = new List<uint>();
CurAttackPlayers = new List<uint>();
LastAttackPlayers = new List<uint>();
SteamID = 0;
//InitData();
}
@ -307,7 +312,7 @@ namespace RuntimeData
MeetPlayers = new List<uint>();
MeetPlayers.Add(Id);
InitData(civId,forceId);
SteamID = 0;
}
public PlayerData(PlayerData copyData)
@ -345,7 +350,7 @@ namespace RuntimeData
giantExp[i] = copyData.giantExp[i];
giantPenalty[i] = copyData.giantPenalty[i];
}
SteamID = copyData.SteamID;
}
public void DeepCopy(PlayerData copyData)
@ -391,7 +396,7 @@ namespace RuntimeData
giantExp[i] = copyData.giantExp[i];
giantPenalty[i] = copyData.giantPenalty[i];
}
SteamID = copyData.SteamID;
}
private void InitData(uint civId,uint forceId)

View File

@ -1947,6 +1947,10 @@ namespace Logic.AI
[MemoryPackable]
public partial class ActionNetData
{
// 行为版本号
public uint Version;
// 地图校验哈希
public string MapHash;
public CommonActionParams Param;
public CommonActionId ActionId;

View File

@ -9,6 +9,8 @@
using System.Collections.Generic;
using System.IO;
using Logic.AI;
using Logic.CrashSight;
using MemoryPack;
using UnityEngine;
@ -36,26 +38,27 @@ namespace RuntimeData
{
RefreshGameRecord();
_gameRecord.Records.Add(record);
string json = JsonUtility.ToJson(_gameRecord);
File.WriteAllText(Application.persistentDataPath + "/game_record.json", json);
byte[] bytes = MemoryPackSerializer.Serialize(_gameRecord);
File.WriteAllBytes(Application.persistentDataPath + "/game_record.dat", bytes);
}
public void RefreshGameRecord()
{
if (_gameRecord != null) return;
string path = Application.persistentDataPath + "/game_record.json";
string path = Application.persistentDataPath + "/game_record.dat";
if (File.Exists(path))
{
string json = File.ReadAllText(path);
_gameRecord = JsonUtility.FromJson<GameRecordData>(json);
byte[] bytes = File.ReadAllBytes(path);
_gameRecord = MemoryPackSerializer.Deserialize<GameRecordData>(bytes);
}
if (_gameRecord == null) _gameRecord = new GameRecordData();
_gameRecord ??= new GameRecordData();
}
}
[System.Serializable]
public class GameRecordData
[MemoryPackable]
public partial class GameRecordData
{
public List<GameRecord> Records;
@ -67,8 +70,8 @@ namespace RuntimeData
}
[System.Serializable]
public class GameRecord
[MemoryPackable]
public partial class GameRecord
{
public GameMode Mode;
public AIDifficult AIDiff;