You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

347 lines
11 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using ET.EventType;
using UnityEngine;
namespace ET
{
[FriendClass(typeof(Unit))]
[FriendClass(typeof(People))]
public static class UnitOperate
{
public static void InitProsperity(Unit unit)
{
var prosperity = 0;
BuildingComponent bc = unit.GetComponent<BuildingComponent>();
foreach (var v in bc.Children)
{
var building = (Building) v.Value;
if (building.Config.Boom > 0)
{
prosperity += building.Config.Boom;
}
}
// TODO 村民繁荣值
unit.Prosperity = prosperity;
}
public static void UpgradeScale(Unit unit)
{
var nextScale = (unit.Scale > 0? unit.Scale : 1) + 1;
ScaleConfig sc = ScaleConfigCategory.Instance.Get(nextScale);
if (unit.Prosperity >= sc.Prosperity)
{
unit.Scale = nextScale;
RewardGroupConfig rewardGroupConfig = RewardGroupConfigCategory.Instance.Get(sc.RewardGroup);
for (var i = 0; i < rewardGroupConfig.ItemId.Length; i++)
{
StoreOperate.AddItem(unit, rewardGroupConfig.ItemId[i], rewardGroupConfig.Number[i]);
}
}
}
public static int PeopleNum(Unit unit)
{
var peopleNum = unit.GetComponent<PeopleComponent>().Children.Count;
return peopleNum;
}
public static int TotalMinutesPerDay(Unit unit)
{
// TODO 一天的分钟数
var total = 5*12;
return total;
}
public static long rectify(long origin = 0, long delta = 0,float min = float.NegativeInfinity, float max = float.PositiveInfinity)
{
var result = origin + delta;
if (result < min) {
result = (long)min;
}
if (result > max) {
result = (long)max;
}
return result;
}
public static int FoodConsumptionPerMinute(Unit unit)
{
var peopleCount = PeopleNum(unit);
WorldParametersConfig wc = WorldParametersConfigCategory.Instance.Get(WorldParam.FoodConsumption);
return peopleCount * wc.Value[0];
}
public static void UpdateFood(Unit unit)
{
var consume = FoodConsumptionPerMinute(unit);
var previous = unit.Food ;
unit.Food = rectify(unit.Food, -consume, -consume*TotalMinutesPerDay(unit));
if (unit.Food <= 0) {
if (previous > 0) {
// todo debuff
}
} else if (previous <= 0) {
// todo buff
}
#if SERVER
UnitHelper.NofityUpdateValley(unit, new List<int> { NumericType.Food }, new List<long> { unit.Food });
#endif
}
public static int WaterConsumptionPerMinute(Unit unit)
{
var peopleCount = PeopleNum(unit);
WorldParametersConfig wc = WorldParametersConfigCategory.Instance.Get(WorldParam.WaterConsumption);
return peopleCount * wc.Value[0];
}
public static void UpdateWater(Unit unit)
{
var consume = WaterConsumptionPerMinute(unit);
var previous = unit.Water ;
unit.Water = rectify(unit.Water, -consume, -consume*TotalMinutesPerDay(unit));
if (unit.Water <= 0)
{
unit.Water = 0;
if (previous > 0)
{
// todo debuff
}
} else if (previous <= 0)
{
// todo buff
}
#if SERVER
UnitHelper.NofityUpdateValley(unit, new List<int> { NumericType.Water }, new List<long> { unit.Water });
#endif
}
public static EventGroupConfig GetNightEventConfig()
{
EventGroupConfig eventGroupConfig= null;
List<EventGroupConfig> list = EventGroupConfigCategory.Instance.GetList();
foreach (var v in list)
{
if (v.Type == 2)
{
eventGroupConfig = v;
break;
}
}
return eventGroupConfig;
}
public static int[] AddArray(int[] probs, int[] overlay, int index)
{
if (probs == null || overlay == null)
{
return probs;
}
for (var i=0; i<probs.Length; i++)
{
if (i == index)
{
continue;
}
probs[i] += overlay[i];
}
return probs;
}
public static int CheckMoonNightTime(int day, int nightTime)
{
if (day % 15 == 0)
{
nightTime += 400;
}
return nightTime;
}
public static int[] CopyArray(int[] array)
{
int[] copy = new int[array.Length];
for (int i = 0; i < array.Length; i++)
{
copy[i] = array[i];
}
return copy;
}
public static int TimePass(int fromTime, int passTime) {
int hours;
int minutes;
ParseTime(fromTime,out hours, out minutes);
return TimePassWithHm(hours, minutes, passTime);
}
public static void ParseTime(int time, out int hours, out int minutes)
{
hours = time / 100;
minutes = time % 100;
}
public static int TimePassWithHm(int hour,int minute,int passTime)
{
minute += passTime;
if(minute < 0)
{
int hourDelta = (Math.Abs(minute/60) + 1);
minute += 60 * hourDelta;
hour -= hourDelta;
if(hour < 0)
{
int dayDelta = (Math.Abs(hour/24) + 1);
hour += 24 * dayDelta;
}
}
else
{
hour += (minute/60);
minute = minute % 60;
hour = hour % 24;
}
return hour* 100 + minute;
}
public static List<NightEvent> NightEvent(Unit unit, int nightTime, int time)
{
EventGroupConfig egc = GetNightEventConfig();
var nightEvents = new List<NightEvent>();
Log.Info($"EventGroupConfig: {egc.ToString()}");
int[] eventWeight = CopyArray(egc.EventWeight);
int[] overlayWeight = CopyArray(egc.OverlayWeight);
int[] eventList = CopyArray(egc.Event);
Log.Info($"eventWeight: {string.Join(",", eventWeight)} :{egc.EventWeight.Clone()}");
Log.Info($"overlayWeight: {string.Join(",", overlayWeight)}");
Log.Info($"eventList: {string.Join(",", eventList)}");
int eventCount = 0;
int h = 23 - nightTime / 100;
int m = 60 - (nightTime % 100);
h += time / 100;
m += time % 100;
int topM = m + h * 60;
int curM = 0;
List<int> timeList = new List<int>();
var rand = new ValleyRandom();
rand.SetSeed(unit.EventSeed);
while (topM > curM)
{
var range = rand.RandomNumber(80, 120);
curM += range;
if (curM > topM)
{
break;
}
eventCount++;
timeList.Add(TimePass(nightTime, curM));
}
for (var i = 0; i < eventCount; i++)
{
int index =RandomHelper.RandomByWeight(eventWeight);
Log.Info($"eventWeight: {string.Join(",",eventWeight)}, eventList: {string.Join(",",eventList)}, index: {string.Join(",",index)}");
if (index == -1)
{
continue;
}
int id = eventList[index];
NightEvent nightEvent = new NightEvent();
nightEvent.EventId = id;
nightEvent.Time = timeList[i];
nightEvents.Add(nightEvent);
eventWeight = AddArray(eventWeight, overlayWeight, index);
}
foreach (var ne in nightEvents)
{
NightEvent nightEvent = ne;
EventConfig eventConfig = EventConfigCategory.Instance.Get(nightEvent.EventId);
// todo 掉落组数量
if (eventConfig.DropGroup[0] > 0)
{
nightEvent.Drops = new Dictionary<int, int>();
foreach (var v in eventConfig.DropGroup)
{
(int dropId,int num) = DropOperate.DropItem(unit, v);
if (!nightEvent.Drops.ContainsKey(dropId))
{
nightEvent.Drops[dropId] = 0;
}
nightEvent.Drops[dropId] += num;
}
}
}
#if SERVER
List<string> names = new List<string>();
foreach (var v in unit.GetComponent<PeopleComponent>().Children.Values)
{
names.Add(((People)v).Name);
}
unit.EventNames = new List<string>();
foreach (var ne in nightEvents)
{
var index = RandomHelper.RandomNumber(0, names.Count - 1);
unit.EventNames.Add(names[index]);
ne.Name = names[index];
}
MessageHelper.SendToClient(unit,new M2C_NotifyNightEvent(){EventSeed = unit.EventSeed, NightTime = nightTime, Time = time, EventNames = unit.EventNames});
#else
for (var i=0; i<nightEvents.Count; i++)
{
Log.Console("night======================================{0},{1}",nightEvents.Count,unit.EventNames.Count);
nightEvents[i].Name = unit.EventNames[i];
}
#endif
Log.Info($"nightEvents: {nightEvents.Count}, topM: {topM}, nightTime: {nightTime}, time: {time}");
foreach (var v in nightEvents)
{
Log.Info($"nightEvent: name: {v.Name}, EventId: {v.EventId}, Time: {v.Time}, Drops: {v.Drops}");
}
return nightEvents;
}
public static SeasonConfig GetSeasonConfigByDay(int day)
{
var totalDay = 0;
List<SeasonConfig> listConfig = SeasonConfigCategory.Instance.GetList();
foreach (var v in listConfig)
{
totalDay += v.Duration;
}
SeasonConfig res = null;
var delta = day % totalDay;
for (var i = 1; i <= listConfig.Count; i++)
{
SeasonConfig seasonConfig = SeasonConfigCategory.Instance.Get(i);
if (delta >= seasonConfig.Duration)
{
delta -= seasonConfig.Duration;
}
else
{
res = seasonConfig;
}
}
return res;
}
}
}