ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Domestic/PatrolRules.cs

267 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using UnityEngine;
namespace TKS.OutGame
{
/// <summary>순찰 수확 1회(미리보기 · 실제 지급 공용).</summary>
public class PatrolYield
{
public float hours;
public int gold;
public int exp;
public string gearGrade;
public int gear;
public int chests;
public float gearF;
public float chestF;
public float mult = 1f;
public List<string> gearIds = new List<string>();
public List<string> expTo = new List<string>();
public List<string> chestIds = new List<string>();
}
/// <summary>
/// 내정 순찰 규칙(기획서 §7 · S10 · PD #1093 C) — patrol.csv 행(골드 · 경험 = 마지막 클리어 스테이지 기준 선형 · 장비 등급 · 상자 수 = 마지막 클리어 챕터 chapterMax) × 누적 시간(오프라인 포함 · 상한 patrol_cap_hours + 농업 patrol_cap_min) ·
/// 골드(× 순찰 수확·순찰 골드 개발) · 병사 경험(출전 편성 전원 · × 순찰 수확·경험 개발) · 승급 장비(patrol_gear_per_hour × 장비 확률 개발 · 등급 = gear_grade + 둔전제) ·
/// 보물 상자(chest_bronze_per_hour 개/시간 · 등급 = chest_drop 순찰 줄) — 장비·상자 소수점은 다음 수확으로 이월(결정적) · 광고 2배(patrol_ad_daily/일 · 스텁) · 즉시 수확(보옥 · patrol_instant_hours 분 · 타이머 그대로).
/// </summary>
public static class PatrolRules
{
public static float CapHours(OutGameData db, SaveData d)
{
return db.Economy.PatrolCapHours + DevRules.Mod(db, d, DevRules.PatrolCap) / 60f;
}
public static DateTime StartAt(SaveData d, DateTime nowUtc)
{
var t = SaveStore.ParseIso(d.patrol.startAt, nowUtc);
return t > nowUtc ? nowUtc : t;
}
/// <summary>지금까지 쌓인 시간(상한에서 멈춤).</summary>
public static float Hours(OutGameData db, SaveData d, DateTime nowUtc)
{
float h = (float)(nowUtc - StartAt(d, nowUtc)).TotalHours;
return Mathf.Clamp(h, 0f, CapHours(db, d));
}
public static PatrolTable.Row Row(OutGameData db, SaveData d)
{
return db.Patrol.For(Mathf.Max(1, d.chapterMax));
}
/// <summary>순찰 장비의 가장 높은 등급 순서 = 순찰 표 전체에서 가장 높은 등급(지금 전설) — 내정 「둔전제」 로 올려도 이 위(불멸 · 신화)는 나오지 않는다.</summary>
public static int TopGearOrder(OutGameData db)
{
int top = 0;
foreach (var r in db.Patrol.rows)
foreach (var g in r.gearGrades) top = Mathf.Max(top, db.Gear.OrderOf(g));
return top;
}
/// <summary>등급 순서(1 = 일반 …) → 그 순서 이하에서 가장 높은 장비 등급 이름.</summary>
static string GradeAt(OutGameData db, int order, string fallback)
{
string best = fallback;
int bestOrder = 0;
foreach (var it in db.Gear.items)
if (it.order <= order && it.order > bestOrder)
{
bestOrder = it.order;
best = it.grade;
}
return best;
}
/// <summary>
/// 순찰 장비 1개의 등급 = 그 장의 가중치(patrol l_gear_grade · l_gear_weight)로 굴림 + 둔전제 Lv(순찰 표의 최고 등급에서 멈춤) —
/// PD 2026-11-24 「10장 기준 확률에 따라 최대 전설까지 · 영웅 · 전설 등급은 확률이 낮아 많이 얻지는 못하더라도」.
/// </summary>
public static string RollGearGrade(OutGameData db, SaveData d, System.Random rng)
{
var row = Row(db, d);
if (row == null || row.gearGrades.Length == 0) return GearGrade(db, d);
float sum = 0f;
foreach (var w in row.gearWeights) sum += w;
float at = (float)(rng.NextDouble() * sum);
string g = row.gearGrades[row.gearGrades.Length - 1];
for (int i = 0; i < row.gearGrades.Length && i < row.gearWeights.Length; i++)
{
if (at < row.gearWeights[i])
{
g = row.gearGrades[i];
break;
}
at -= row.gearWeights[i];
}
int order = Mathf.Min(TopGearOrder(db), db.Gear.OrderOf(g) + Mathf.RoundToInt(DevRules.Mod(db, d, DevRules.PatrolGearGrade)));
return GradeAt(db, order, g);
}
/// <summary>순찰 장비의 대표 등급(가장 잘 나오는 것 · 화면 그림) = 표의 가장 큰 가중치 등급 + 둔전제 Lv(순찰 표의 최고 등급에서 멈춤).</summary>
public static string GearGrade(OutGameData db, SaveData d)
{
var row = Row(db, d);
if (row == null) return "";
int order = Mathf.Min(TopGearOrder(db), db.Gear.OrderOf(row.gearGrade) + Mathf.RoundToInt(DevRules.Mod(db, d, DevRules.PatrolGearGrade)));
return GradeAt(db, order, row.gearGrade);
}
/// <summary>마지막으로 클리어한 스테이지 = 클리어한 것 가운데 가장 뒤(장 → 판) · 없으면 1-1 로 본다(하드 난이도 기록은 세지 않는다).</summary>
public static void LastCleared(SaveData d, out int chapter, out int stage)
{
chapter = 1;
stage = 1;
bool any = false;
foreach (var s in d.stages)
{
int ch, st;
if (s.clears <= 0 || !ArenaRules.ParseStageKey(s.key, out ch, out st)) continue;
if (any && (ch < chapter || (ch == chapter && st <= stage))) continue;
chapter = ch;
stage = st;
any = true;
}
}
/// <summary>그 장의 스테이지 수(stage_drop 줄 수).</summary>
static int StageCount(OutGameData db, int chapter)
{
int n = 0;
if (db.Gear != null)
foreach (var s in db.Gear.stageDrops)
if (s.chapter == chapter) n++;
return n;
}
/// <summary>
/// 시간당 표 값 = 마지막 클리어 스테이지 기준(PD 2026-11-24 재화 배치 §2-1-8) — 그 장 값과 다음 장 값 사이를 그 장 스테이지 수로 나눠 선형
/// (그 장 첫 판 = 그 장 값 · 판이 오를 때마다 차이 ÷ 스테이지 수 · 다음 장 줄이 없으면 그 장 값).
/// </summary>
public static float StageValue(OutGameData db, SaveData d, Func<PatrolTable.Row, float> v)
{
int ch, st;
LastCleared(d, out ch, out st);
var a = db.Patrol.For(ch);
if (a == null) return 0f;
var b = db.Patrol.For(ch + 1);
int n = StageCount(db, ch);
if (b == null || a.chapter != ch || b.chapter != ch + 1 || n <= 0) return v(a);
return Mathf.Lerp(v(a), v(b), Mathf.Clamp01((st - 1) / (float)n));
}
public static float GoldPerHour(OutGameData db, SaveData d)
{
return StageValue(db, d, r => r.goldPerHour) * (1f + (DevRules.Mod(db, d, DevRules.PatrolYield) + DevRules.Mod(db, d, DevRules.PatrolGold)) / 100f);
}
public static float ExpPerHour(OutGameData db, SaveData d)
{
return StageValue(db, d, r => r.expPerHour) * (1f + (DevRules.Mod(db, d, DevRules.PatrolYield) + DevRules.Mod(db, d, DevRules.ExpGain)) / 100f);
}
public static float GearPerHour(OutGameData db, SaveData d)
{
return db.Economy.PatrolGearPerHour * DevRules.Mult(db, d, DevRules.PatrolGearChance) * DevRules.Mult(db, d, DevRules.PatrolYield);
}
public static float ChestPerHour(OutGameData db, SaveData d)
{
var row = Row(db, d);
return row == null ? 0f : row.chestPerHour * DevRules.Mult(db, d, DevRules.PatrolYield);
}
/// <summary>hours 시간분 수확량(mult = 광고 2배 등 · 이월 소수점 포함).</summary>
public static PatrolYield Preview(OutGameData db, SaveData d, float hours, float mult)
{
var y = new PatrolYield { hours = hours, mult = mult, gearGrade = GearGrade(db, d) };
y.gold = Mathf.RoundToInt(GoldPerHour(db, d) * hours * mult);
y.exp = Mathf.RoundToInt(ExpPerHour(db, d) * hours * mult);
y.gearF = GearPerHour(db, d) * hours * mult + d.patrol.carryGear;
y.chestF = ChestPerHour(db, d) * hours * mult + d.patrol.carryChest;
y.gear = Mathf.FloorToInt(y.gearF + 1e-4f);
y.chests = Mathf.FloorToInt(y.chestF + 1e-4f);
return y;
}
static void Pay(OutGameData db, SaveData d, PatrolYield y, IList<string> expTo, System.Random rng)
{
d.gold += y.gold;
if (expTo != null)
foreach (var id in expTo)
{
var s = d.Level(id);
if (s == null || y.expTo.Contains(id)) continue;
HeroRules.GrantExp(s, y.exp);
y.expTo.Add(id);
}
for (int i = 0; i < y.gear; i++)
{
// 장비마다 등급을 굴린다(장별 가중치 · 10장 = 전설까지) → 그 등급의 장비 가운데 무작위
string grade = RollGearGrade(db, d, rng);
var pool = new List<string>();
foreach (var it in db.Gear.items)
if (it.grade == grade) pool.Add(it.id);
if (pool.Count == 0) continue;
var g = pool[rng.Next(pool.Count)];
d.inv.AddGear(g, 1);
y.gearIds.Add(g);
}
int lastCh, lastSt;
LastCleared(d, out lastCh, out lastSt);
for (int i = 0; i < y.chests; i++)
{
// 등급 = chest_drop(순찰 · 마지막 클리어 장 구간) 가중치 → 내정 「상자 등급」(PD 2026-11-24 ⑤)
var got = GoodsRules.ChestGrade(db, d, GoodsDb.SourcePatrol, lastCh, db.Economy.PatrolChest, rng);
if (got == null) continue;
Counts.Add(d.inv.chests, got, 1);
y.chestIds.Add(got);
}
d.patrol.carryGear = Mathf.Max(0f, y.gearF - y.gear);
d.patrol.carryChest = Mathf.Max(0f, y.chestF - y.chests);
}
/// <summary>수확(쌓인 시간 전부 · 타이머 리셋 · 순찰 카운터 +1). ad = 광고 2배(하루 patrol_ad_daily 회 · 스텁).</summary>
public static PatrolYield Harvest(OutGameData db, SaveData d, DateTime nowUtc, IList<string> expTo, bool ad, System.Random rng, out string why)
{
why = null;
float h = Hours(db, d, nowUtc);
var e = db.Economy;
EconomyRules.RollDaily(d, nowUtc);
if (ad && d.adsUsed.patrol >= e.PatrolAdDaily)
{
why = "오늘 광고 2배 " + e.PatrolAdDaily + "회를 모두 썼습니다";
return null;
}
if (h <= 0f)
{
why = "쌓인 수확 없음";
return null;
}
var y = Preview(db, d, h, ad ? e.PatrolAdMult : 1f);
Pay(db, d, y, expTo, rng);
if (ad) d.adsUsed.patrol++;
d.patrol.startAt = SaveStore.Iso(nowUtc);
TreasureRules.AddCount(d, BattleEvents.Patrols, 1);
return y;
}
/// <summary>즉시 수확 = 보옥 patrol_instant_gem → patrol_instant_hours 시간분(쌓인 타이머는 그대로).</summary>
public static PatrolYield Instant(OutGameData db, SaveData d, IList<string> expTo, System.Random rng, out string why)
{
why = null;
var e = db.Economy;
if (d.gem < e.PatrolInstantGem)
{
why = "보옥 부족 " + d.gem + " / " + e.PatrolInstantGem;
return null;
}
d.gem -= e.PatrolInstantGem;
var y = Preview(db, d, e.PatrolInstantHours, 1f);
Pay(db, d, y, expTo, rng);
return y;
}
}
}