DNF90 服务端与启动器完整源码(含 GM 工具功能增强)
服务端(Go): - GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面 - GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新 - 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新) - 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍 - 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0) - 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算 启动器(Python/JS): - 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭 - GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐) - 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
This commit is contained in:
commit
645b64fe21
4388 files changed
+1405457
No files matched your search
@@ -0,0 +1,286 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
dnfpvf "longheng.io/server/internal/modules/dnf/pvf"
|
||||
)
|
||||
|
||||
const ConfigPath = "etc/hellparty.etc"
|
||||
|
||||
var (
|
||||
ErrSourceRequired = errors.New("dnf hell party PVF source is required")
|
||||
ErrCatalogInvalid = errors.New("dnf hell party catalog is invalid")
|
||||
)
|
||||
|
||||
type TextSource interface {
|
||||
ReadText(string) (string, error)
|
||||
}
|
||||
|
||||
// DifficultyRule preserves the five current-PVF values without assigning
|
||||
// names to fields whose business meaning has not been confirmed in 90CN.
|
||||
// Value0 is separately exposed as RewardRollCount because the current PVF and
|
||||
// reference runtime agree on that use; the other values remain opaque.
|
||||
type DifficultyRule struct {
|
||||
Key string
|
||||
RewardRollCount uint32
|
||||
Values [5]int64
|
||||
}
|
||||
|
||||
type ActorDefinition struct {
|
||||
Code uint32
|
||||
EntityType uint8
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
ID uint32
|
||||
Actors map[string][]ActorDefinition
|
||||
}
|
||||
|
||||
type Catalog struct {
|
||||
difficulties map[string]DifficultyRule
|
||||
groups map[uint32]Group
|
||||
}
|
||||
|
||||
func Load(source TextSource) (*Catalog, error) {
|
||||
if source == nil {
|
||||
return nil, ErrSourceRequired
|
||||
}
|
||||
text, err := source.ReadText(ConfigPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", ConfigPath, err)
|
||||
}
|
||||
return Parse(text)
|
||||
}
|
||||
|
||||
func Parse(text string) (*Catalog, error) {
|
||||
document, err := dnfpvf.Parse(ConfigPath, text)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: parse %s: %v", ErrCatalogInvalid, ConfigPath, err)
|
||||
}
|
||||
return ParseDocument(document)
|
||||
}
|
||||
|
||||
func ParseDocument(document *dnfpvf.Document) (*Catalog, error) {
|
||||
if document == nil {
|
||||
return nil, fmt.Errorf("%w: document is nil", ErrCatalogInvalid)
|
||||
}
|
||||
difficulties, err := parseDifficulties(document)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups, err := parseGroups(document)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(difficulties) == 0 || len(groups) == 0 {
|
||||
return nil, fmt.Errorf("%w: difficulties=%d groups=%d", ErrCatalogInvalid, len(difficulties), len(groups))
|
||||
}
|
||||
return &Catalog{difficulties: difficulties, groups: groups}, nil
|
||||
}
|
||||
|
||||
func (c *Catalog) Difficulty(key string) (DifficultyRule, bool) {
|
||||
if c == nil {
|
||||
return DifficultyRule{}, false
|
||||
}
|
||||
rule, ok := c.difficulties[normalizeKey(key)]
|
||||
return rule, ok
|
||||
}
|
||||
|
||||
func (c *Catalog) Group(id uint32) (Group, bool) {
|
||||
if c == nil {
|
||||
return Group{}, false
|
||||
}
|
||||
group, ok := c.groups[id]
|
||||
return cloneGroup(group), ok
|
||||
}
|
||||
|
||||
func (c *Catalog) GroupIDs() []uint32 {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uint32, 0, len(c.groups))
|
||||
for id := range c.groups {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
|
||||
return ids
|
||||
}
|
||||
|
||||
func (c *Catalog) Snapshot() (difficultyRules, groups, actors int) {
|
||||
if c == nil {
|
||||
return 0, 0, 0
|
||||
}
|
||||
for _, group := range c.groups {
|
||||
for _, entries := range group.Actors {
|
||||
actors += len(entries)
|
||||
}
|
||||
}
|
||||
return len(c.difficulties), len(c.groups), actors
|
||||
}
|
||||
|
||||
func parseDifficulties(document *dnfpvf.Document) (map[string]DifficultyRule, error) {
|
||||
tokens, ok := uniqueSection(document, "difficulty")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: missing or duplicate [difficulty]", ErrCatalogInvalid)
|
||||
}
|
||||
out := make(map[string]DifficultyRule)
|
||||
for index := 0; index < len(tokens); {
|
||||
token := tokens[index]
|
||||
if token.Kind == dnfpvf.TokenSection {
|
||||
break
|
||||
}
|
||||
key, isKey := tokenKey(token)
|
||||
if !isKey {
|
||||
// The live 90CN table begins with one row-count integer. Preserve
|
||||
// forward compatibility by ignoring numeric metadata before a key.
|
||||
if token.Kind != dnfpvf.TokenInt {
|
||||
return nil, fmt.Errorf("%w: [difficulty] token=%q before key", ErrCatalogInvalid, token.Raw)
|
||||
}
|
||||
index++
|
||||
continue
|
||||
}
|
||||
index++
|
||||
if _, exists := out[key]; exists {
|
||||
return nil, fmt.Errorf("%w: duplicate difficulty=%s", ErrCatalogInvalid, key)
|
||||
}
|
||||
var values [5]int64
|
||||
for valueIndex := range values {
|
||||
if index >= len(tokens) || tokens[index].Kind != dnfpvf.TokenInt {
|
||||
return nil, fmt.Errorf("%w: difficulty=%s value_index=%d", ErrCatalogInvalid, key, valueIndex)
|
||||
}
|
||||
values[valueIndex] = tokens[index].Int
|
||||
index++
|
||||
}
|
||||
if values[0] < 0 || values[0] > math.MaxUint32 {
|
||||
return nil, fmt.Errorf("%w: difficulty=%s reward_roll_count=%d", ErrCatalogInvalid, key, values[0])
|
||||
}
|
||||
out[key] = DifficultyRule{Key: key, RewardRollCount: uint32(values[0]), Values: values}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseGroups(document *dnfpvf.Document) (map[uint32]Group, error) {
|
||||
out := make(map[uint32]Group)
|
||||
var pendingID uint32
|
||||
for _, section := range document.Sections {
|
||||
switch normalizeSection(section.Name) {
|
||||
case "group index":
|
||||
if pendingID != 0 {
|
||||
return nil, fmt.Errorf("%w: group=%d has no [group]", ErrCatalogInvalid, pendingID)
|
||||
}
|
||||
tokens := sectionTokens(document, section)
|
||||
if len(tokens) != 1 || tokens[0].Kind != dnfpvf.TokenInt || tokens[0].Int <= 0 || tokens[0].Int > math.MaxUint32 {
|
||||
return nil, fmt.Errorf("%w: invalid [group index]", ErrCatalogInvalid)
|
||||
}
|
||||
pendingID = uint32(tokens[0].Int)
|
||||
case "group":
|
||||
if pendingID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := out[pendingID]; exists {
|
||||
return nil, fmt.Errorf("%w: duplicate group=%d", ErrCatalogInvalid, pendingID)
|
||||
}
|
||||
actors, err := parseGroupActors(sectionTokens(document, section), pendingID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[pendingID] = Group{ID: pendingID, Actors: actors}
|
||||
pendingID = 0
|
||||
}
|
||||
}
|
||||
if pendingID != 0 {
|
||||
return nil, fmt.Errorf("%w: group=%d has no [group]", ErrCatalogInvalid, pendingID)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseGroupActors(tokens []dnfpvf.Token, groupID uint32) (map[string][]ActorDefinition, error) {
|
||||
out := make(map[string][]ActorDefinition)
|
||||
currentKey := ""
|
||||
for index := 0; index < len(tokens); {
|
||||
if tokens[index].Kind == dnfpvf.TokenSection {
|
||||
break
|
||||
}
|
||||
if key, ok := tokenKey(tokens[index]); ok {
|
||||
currentKey = key
|
||||
if _, exists := out[currentKey]; exists {
|
||||
return nil, fmt.Errorf("%w: group=%d duplicate mode=%s", ErrCatalogInvalid, groupID, currentKey)
|
||||
}
|
||||
out[currentKey] = nil
|
||||
index++
|
||||
continue
|
||||
}
|
||||
if currentKey == "" || index+1 >= len(tokens) || tokens[index].Kind != dnfpvf.TokenInt || tokens[index+1].Kind != dnfpvf.TokenInt {
|
||||
return nil, fmt.Errorf("%w: group=%d actor pair at token=%d", ErrCatalogInvalid, groupID, index)
|
||||
}
|
||||
code, entityType := tokens[index].Int, tokens[index+1].Int
|
||||
if code <= 0 || code > math.MaxUint32 || (entityType != 0 && entityType != 1) {
|
||||
return nil, fmt.Errorf("%w: group=%d code=%d entity_type=%d", ErrCatalogInvalid, groupID, code, entityType)
|
||||
}
|
||||
out[currentKey] = append(out[currentKey], ActorDefinition{Code: uint32(code), EntityType: uint8(entityType)})
|
||||
index += 2
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("%w: group=%d has no modes", ErrCatalogInvalid, groupID)
|
||||
}
|
||||
for key, actors := range out {
|
||||
if len(actors) == 0 {
|
||||
return nil, fmt.Errorf("%w: group=%d mode=%s has no actors", ErrCatalogInvalid, groupID, key)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func uniqueSection(document *dnfpvf.Document, name string) ([]dnfpvf.Token, bool) {
|
||||
key := normalizeSection(name)
|
||||
var result []dnfpvf.Token
|
||||
count := 0
|
||||
for _, section := range document.Sections {
|
||||
if normalizeSection(section.Name) != key {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
result = sectionTokens(document, section)
|
||||
}
|
||||
return result, count == 1
|
||||
}
|
||||
|
||||
func sectionTokens(document *dnfpvf.Document, section dnfpvf.Section) []dnfpvf.Token {
|
||||
if section.Start < 0 || section.End < section.Start || section.End > len(document.Tokens) {
|
||||
return nil
|
||||
}
|
||||
return document.Tokens[section.Start:section.End]
|
||||
}
|
||||
|
||||
func tokenKey(token dnfpvf.Token) (string, bool) {
|
||||
if token.Kind != dnfpvf.TokenString && token.Kind != dnfpvf.TokenIdent {
|
||||
return "", false
|
||||
}
|
||||
key := normalizeKey(token.Value)
|
||||
return key, len(key) == 1 && key[0] >= 'A' && key[0] <= 'Z'
|
||||
}
|
||||
|
||||
func normalizeKey(value string) string {
|
||||
return strings.ToUpper(strings.TrimSpace(value))
|
||||
}
|
||||
|
||||
func normalizeSection(value string) string {
|
||||
return strings.ToLower(strings.Join(strings.Fields(strings.TrimSpace(value)), " "))
|
||||
}
|
||||
|
||||
func cloneGroup(group Group) Group {
|
||||
if group.Actors == nil {
|
||||
return group
|
||||
}
|
||||
source := group.Actors
|
||||
group.Actors = make(map[string][]ActorDefinition, len(source))
|
||||
for key, actors := range source {
|
||||
group.Actors[key] = append([]ActorDefinition(nil), actors...)
|
||||
}
|
||||
return group
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const catalogFixture = `[difficulty]
|
||||
4 ` + "`A`" + ` 8 5 100 100 0 ` + "`B`" + ` 6 4 0 0 0
|
||||
[/difficulty]
|
||||
[hellparty monster group]
|
||||
[group index]
|
||||
125
|
||||
[group]
|
||||
` + "`A`" + ` 1050 0
|
||||
[/group]
|
||||
[group index]
|
||||
155
|
||||
[group]
|
||||
` + "`A`" + ` 20438 1 36404 1 ` + "`B`" + ` 30410 1
|
||||
[/group]
|
||||
[/hellparty monster group]`
|
||||
|
||||
func TestParseCatalog(t *testing.T) {
|
||||
catalog, err := Parse(catalogFixture)
|
||||
if err != nil {
|
||||
t.Fatalf("Parse() error = %v", err)
|
||||
}
|
||||
rule, ok := catalog.Difficulty("a")
|
||||
if !ok || rule.RewardRollCount != 8 || rule.Values != [5]int64{8, 5, 100, 100, 0} {
|
||||
t.Fatalf("Difficulty(A) = %+v, %v", rule, ok)
|
||||
}
|
||||
group, ok := catalog.Group(155)
|
||||
if !ok || len(group.Actors["A"]) != 2 || group.Actors["A"][0] != (ActorDefinition{Code: 20438, EntityType: 1}) {
|
||||
t.Fatalf("Group(155) = %+v, %v", group, ok)
|
||||
}
|
||||
group.Actors["A"][0].Code = 1
|
||||
again, _ := catalog.Group(155)
|
||||
if again.Actors["A"][0].Code != 20438 {
|
||||
t.Fatal("Group() returned mutable catalog storage")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCatalogRejectsMalformedActorPair(t *testing.T) {
|
||||
_, err := Parse(`[difficulty] 1 ` + "`A`" + ` 1 2 3 4 5 [/difficulty]
|
||||
[group index] 1 [group] ` + "`A`" + ` 100 [/group]`)
|
||||
if !errors.Is(err, ErrCatalogInvalid) {
|
||||
t.Fatalf("Parse() error = %v, want ErrCatalogInvalid", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPlanSelectsOneStableGroupPerOrder(t *testing.T) {
|
||||
catalog, err := Parse(catalogFixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
options := PlanOptions{
|
||||
GroupKey: "A",
|
||||
Seed: 0x12345678,
|
||||
Entries: []MapEntry{
|
||||
{GroupID: 125, Rate: 25, Order: 1},
|
||||
{GroupID: 155, Rate: 75, Order: 1},
|
||||
{GroupID: 155, Rate: 100, Order: 2},
|
||||
},
|
||||
}
|
||||
first, err := BuildPlan(catalog, options)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildPlan() error = %v", err)
|
||||
}
|
||||
second, err := BuildPlan(catalog, options)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildPlan() second error = %v", err)
|
||||
}
|
||||
if len(first.Waves) != 2 || first.Waves[0].Order != 1 || first.Waves[1].Order != 2 {
|
||||
t.Fatalf("waves = %+v", first.Waves)
|
||||
}
|
||||
if first.Waves[0].GroupID != second.Waves[0].GroupID {
|
||||
t.Fatalf("selection is not stable: %d != %d", first.Waves[0].GroupID, second.Waves[0].GroupID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeDeathIsIdempotentAndReleasesPerGroup(t *testing.T) {
|
||||
catalog, err := Parse(catalogFixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan, err := BuildPlan(catalog, PlanOptions{
|
||||
GroupKey: "A",
|
||||
Seed: 1,
|
||||
Entries: []MapEntry{{GroupID: 155, Rate: 100, Order: 1}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewRuntime(plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := runtime.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first := plan.Waves[0].Actors[0].Identity
|
||||
second := plan.Waves[0].Actors[1].Identity
|
||||
result, err := runtime.RecordDeath(first)
|
||||
if err != nil || result.GroupCompleted || result.RewardRollCount != 0 {
|
||||
t.Fatalf("first death = %+v, %v", result, err)
|
||||
}
|
||||
duplicate, err := runtime.RecordDeath(first)
|
||||
if err != nil || !duplicate.Duplicate {
|
||||
t.Fatalf("duplicate death = %+v, %v", duplicate, err)
|
||||
}
|
||||
result, err = runtime.RecordDeath(second)
|
||||
if err != nil || !result.GroupCompleted || !result.AllComplete || result.RewardRollCount != 8 {
|
||||
t.Fatalf("final death = %+v, %v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeReleasesEachOrderIndependently(t *testing.T) {
|
||||
catalog, err := Parse(catalogFixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan, err := BuildPlan(catalog, PlanOptions{
|
||||
GroupKey: "A",
|
||||
Seed: 2,
|
||||
Entries: []MapEntry{
|
||||
{GroupID: 155, Rate: 100, Order: 1},
|
||||
{GroupID: 155, Rate: 100, Order: 2},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewRuntime(plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := runtime.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for waveIndex, wave := range plan.Waves {
|
||||
for actorIndex, actor := range wave.Actors {
|
||||
result, recordErr := runtime.RecordDeath(actor.Identity)
|
||||
if recordErr != nil {
|
||||
t.Fatalf("wave=%d actor=%d: %v", waveIndex, actorIndex, recordErr)
|
||||
}
|
||||
lastActor := actorIndex == len(wave.Actors)-1
|
||||
if result.GroupCompleted != lastActor {
|
||||
t.Fatalf("wave=%d actor=%d result=%+v", waveIndex, actorIndex, result)
|
||||
}
|
||||
if lastActor && result.RewardRollCount != 8 {
|
||||
t.Fatalf("wave=%d reward rolls=%d, want 8", waveIndex, result.RewardRollCount)
|
||||
}
|
||||
lastWave := waveIndex == len(plan.Waves)-1
|
||||
if result.AllComplete != (lastWave && lastActor) {
|
||||
t.Fatalf("wave=%d actor=%d all_complete=%t", waveIndex, actorIndex, result.AllComplete)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeSuppressesRewardWhenHellMonsterDies(t *testing.T) {
|
||||
catalog, err := Parse(catalogFixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan, err := BuildPlan(catalog, PlanOptions{
|
||||
GroupKey: "A",
|
||||
Seed: 3,
|
||||
Entries: []MapEntry{{GroupID: 125, Rate: 100, Order: 1}},
|
||||
SuppressGroupReward: func(code uint32, entityType uint8) bool {
|
||||
return code == 1050 && entityType == 0
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewRuntime(plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := runtime.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := runtime.RecordDeath(plan.Waves[0].Actors[0].Identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !result.GroupCompleted || !result.AllComplete || !result.RewardSuppressed || result.RewardRollCount != 0 {
|
||||
t.Fatalf("result=%+v", result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
dnfpvf "longheng.io/server/internal/modules/dnf/pvf"
|
||||
)
|
||||
|
||||
const (
|
||||
DropConfigPath = "etc/itemdropinfo_monster_hell.etc"
|
||||
RarityRollDenominator = uint32(1_000_000)
|
||||
)
|
||||
|
||||
var ErrDropCatalogInvalid = errors.New("dnf hell party drop catalog is invalid")
|
||||
|
||||
type DropProbabilityRow struct {
|
||||
MinimumLevel int
|
||||
MaximumLevel int
|
||||
DifficultyRate [5]uint32
|
||||
}
|
||||
|
||||
type GradeWindow struct {
|
||||
Down int64
|
||||
Up int64
|
||||
}
|
||||
|
||||
type DropCatalog struct {
|
||||
probabilities []DropProbabilityRow
|
||||
rarity [][7]uint32
|
||||
gradeWindows map[int]GradeWindow
|
||||
}
|
||||
|
||||
func LoadDropCatalog(source TextSource) (*DropCatalog, error) {
|
||||
if source == nil {
|
||||
return nil, ErrSourceRequired
|
||||
}
|
||||
text, err := source.ReadText(DropConfigPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", DropConfigPath, err)
|
||||
}
|
||||
return ParseDropCatalog(text)
|
||||
}
|
||||
|
||||
func ParseDropCatalog(text string) (*DropCatalog, error) {
|
||||
document, err := dnfpvf.Parse(DropConfigPath, text)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: parse %s: %v", ErrDropCatalogInvalid, DropConfigPath, err)
|
||||
}
|
||||
countValues, ok := uniqueSection(document, "drop prob count")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: missing or duplicate [drop prob count]", ErrDropCatalogInvalid)
|
||||
}
|
||||
countInts, err := strictSectionInts(countValues, "drop prob count")
|
||||
if err != nil || len(countInts) != 1 || countInts[0] <= 0 || countInts[0] > 200 {
|
||||
return nil, fmt.Errorf("%w: [drop prob count] values=%v", ErrDropCatalogInvalid, countInts)
|
||||
}
|
||||
rowCount := int(countInts[0])
|
||||
|
||||
probabilityTokens, ok := uniqueSection(document, "dungeon difficulty drop prob")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: missing or duplicate [dungeon difficulty drop prob]", ErrDropCatalogInvalid)
|
||||
}
|
||||
probabilityInts, err := strictSectionInts(probabilityTokens, "dungeon difficulty drop prob")
|
||||
if err != nil || len(probabilityInts) != rowCount*7 {
|
||||
return nil, fmt.Errorf("%w: [dungeon difficulty drop prob] values=%d want=%d", ErrDropCatalogInvalid, len(probabilityInts), rowCount*7)
|
||||
}
|
||||
probabilities := make([]DropProbabilityRow, 0, rowCount)
|
||||
previousMaximum := 0
|
||||
for rowIndex := 0; rowIndex < rowCount; rowIndex++ {
|
||||
base := rowIndex * 7
|
||||
minimum, maximum := probabilityInts[base], probabilityInts[base+1]
|
||||
if minimum <= 0 || maximum < minimum || maximum > 200 || int(minimum) <= previousMaximum {
|
||||
return nil, fmt.Errorf("%w: probability row=%d level=%d-%d previous_max=%d", ErrDropCatalogInvalid, rowIndex, minimum, maximum, previousMaximum)
|
||||
}
|
||||
row := DropProbabilityRow{MinimumLevel: int(minimum), MaximumLevel: int(maximum)}
|
||||
for difficulty := range row.DifficultyRate {
|
||||
value := probabilityInts[base+2+difficulty]
|
||||
if value < 0 || value > math.MaxUint32 {
|
||||
return nil, fmt.Errorf("%w: probability row=%d difficulty=%d rate=%d", ErrDropCatalogInvalid, rowIndex, difficulty, value)
|
||||
}
|
||||
row.DifficultyRate[difficulty] = uint32(value)
|
||||
}
|
||||
probabilities = append(probabilities, row)
|
||||
previousMaximum = int(maximum)
|
||||
}
|
||||
|
||||
rarityTokens, ok := uniqueSection(document, "basis of rarity dicision")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: missing or duplicate [basis of rarity dicision]", ErrDropCatalogInvalid)
|
||||
}
|
||||
rarityInts, err := strictSectionInts(rarityTokens, "basis of rarity dicision")
|
||||
if err != nil || len(rarityInts) == 0 || rarityInts[0] <= 0 || rarityInts[0] > 32 {
|
||||
return nil, fmt.Errorf("%w: [basis of rarity dicision] values=%v", ErrDropCatalogInvalid, rarityInts)
|
||||
}
|
||||
rarityRows := int(rarityInts[0])
|
||||
if len(rarityInts) != 1+rarityRows*7 {
|
||||
return nil, fmt.Errorf("%w: rarity values=%d want=%d", ErrDropCatalogInvalid, len(rarityInts), 1+rarityRows*7)
|
||||
}
|
||||
rarity := make([][7]uint32, rarityRows)
|
||||
for rowIndex := range rarity {
|
||||
var previousEffective uint32
|
||||
for rarityIndex := range rarity[rowIndex] {
|
||||
value := rarityInts[1+rowIndex*7+rarityIndex]
|
||||
if value < 0 || value > math.MaxUint32 {
|
||||
return nil, fmt.Errorf("%w: rarity row=%d index=%d value=%d", ErrDropCatalogInvalid, rowIndex, rarityIndex, value)
|
||||
}
|
||||
effective := uint32(value)
|
||||
if effective > RarityRollDenominator {
|
||||
effective = RarityRollDenominator
|
||||
}
|
||||
if effective < previousEffective {
|
||||
return nil, fmt.Errorf("%w: rarity row=%d index=%d value=%d effective=%d previous_effective=%d", ErrDropCatalogInvalid, rowIndex, rarityIndex, value, effective, previousEffective)
|
||||
}
|
||||
rarity[rowIndex][rarityIndex] = uint32(value)
|
||||
previousEffective = effective
|
||||
}
|
||||
}
|
||||
|
||||
gradeTokens, ok := uniqueSection(document, "item drop ref table")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: missing or duplicate [item drop ref table]", ErrDropCatalogInvalid)
|
||||
}
|
||||
gradeInts, err := strictSectionInts(gradeTokens, "item drop ref table")
|
||||
if err != nil || len(gradeInts) == 0 || len(gradeInts)%3 != 0 {
|
||||
return nil, fmt.Errorf("%w: [item drop ref table] values=%d", ErrDropCatalogInvalid, len(gradeInts))
|
||||
}
|
||||
gradeWindows := make(map[int]GradeWindow, len(gradeInts)/3)
|
||||
for index := 0; index < len(gradeInts); index += 3 {
|
||||
level, down, up := gradeInts[index], gradeInts[index+1], gradeInts[index+2]
|
||||
if level <= 0 || level > 200 || down < 0 || up < 0 || (down == 0 && up == 0) {
|
||||
return nil, fmt.Errorf("%w: grade row=%d level=%d down=%d up=%d", ErrDropCatalogInvalid, index/3, level, down, up)
|
||||
}
|
||||
if _, exists := gradeWindows[int(level)]; exists {
|
||||
return nil, fmt.Errorf("%w: duplicate grade level=%d", ErrDropCatalogInvalid, level)
|
||||
}
|
||||
gradeWindows[int(level)] = GradeWindow{Down: down, Up: up}
|
||||
}
|
||||
|
||||
return &DropCatalog{
|
||||
probabilities: probabilities,
|
||||
rarity: rarity,
|
||||
gradeWindows: gradeWindows,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func strictSectionInts(tokens []dnfpvf.Token, section string) ([]int64, error) {
|
||||
values := make([]int64, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
if token.Kind == dnfpvf.TokenSection {
|
||||
break
|
||||
}
|
||||
if token.Kind != dnfpvf.TokenInt {
|
||||
return nil, fmt.Errorf("%w: [%s] token=%q is not integer", ErrDropCatalogInvalid, section, token.Raw)
|
||||
}
|
||||
values = append(values, token.Int)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func (catalog *DropCatalog) Snapshot() (probabilityRows, rarityRows, gradeRows int) {
|
||||
if catalog == nil {
|
||||
return 0, 0, 0
|
||||
}
|
||||
return len(catalog.probabilities), len(catalog.rarity), len(catalog.gradeWindows)
|
||||
}
|
||||
|
||||
func (catalog *DropCatalog) DropRate(itemBasisLevel, dungeonDifficulty int) (uint32, bool) {
|
||||
if catalog == nil {
|
||||
return 0, false
|
||||
}
|
||||
difficulty := dungeonDifficulty
|
||||
if difficulty < 0 {
|
||||
difficulty = 0
|
||||
}
|
||||
if difficulty > 4 {
|
||||
difficulty = 4
|
||||
}
|
||||
for _, row := range catalog.probabilities {
|
||||
if itemBasisLevel >= row.MinimumLevel && itemBasisLevel <= row.MaximumLevel {
|
||||
return row.DifficultyRate[difficulty], true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (catalog *DropCatalog) GradeWindow(itemBasisLevel int) (GradeWindow, bool) {
|
||||
if catalog == nil {
|
||||
return GradeWindow{}, false
|
||||
}
|
||||
window, ok := catalog.gradeWindows[itemBasisLevel]
|
||||
return window, ok
|
||||
}
|
||||
|
||||
func (catalog *DropCatalog) RarityThresholds(mode int) ([7]uint32, bool) {
|
||||
if catalog == nil || len(catalog.rarity) == 0 {
|
||||
return [7]uint32{}, false
|
||||
}
|
||||
index := mode - 1
|
||||
if index < 0 {
|
||||
index = 0
|
||||
}
|
||||
if index >= len(catalog.rarity) {
|
||||
index = len(catalog.rarity) - 1
|
||||
}
|
||||
return catalog.rarity[index], true
|
||||
}
|
||||
|
||||
func (catalog *DropCatalog) RarityWeights(mode int) ([7]uint32, bool) {
|
||||
thresholds, ok := catalog.RarityThresholds(mode)
|
||||
if !ok {
|
||||
return [7]uint32{}, false
|
||||
}
|
||||
var weights [7]uint32
|
||||
var previous uint32
|
||||
for index, threshold := range thresholds {
|
||||
if threshold > RarityRollDenominator {
|
||||
threshold = RarityRollDenominator
|
||||
}
|
||||
if threshold < previous {
|
||||
threshold = previous
|
||||
}
|
||||
weights[index] = threshold - previous
|
||||
previous = threshold
|
||||
}
|
||||
return weights, true
|
||||
}
|
||||
|
||||
func (catalog *DropCatalog) RarityForRoll(mode int, roll uint32) (int64, bool) {
|
||||
if roll >= RarityRollDenominator {
|
||||
return 0, false
|
||||
}
|
||||
thresholds, ok := catalog.RarityThresholds(mode)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
value := roll + 1
|
||||
for rarity, threshold := range thresholds {
|
||||
if threshold > RarityRollDenominator {
|
||||
threshold = RarityRollDenominator
|
||||
}
|
||||
if value <= threshold {
|
||||
return int64(rarity), true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testDropCatalog = `
|
||||
[drop prob count]
|
||||
2
|
||||
[dungeon difficulty drop prob]
|
||||
1 45 100 200 300 400 500
|
||||
46 90 600 700 800 900 1000
|
||||
[basis of rarity dicision]
|
||||
2
|
||||
700000 900000 980000 995000 1000000 1000000 1000000
|
||||
600000 850000 950000 990000 1000000 1000000 1000000
|
||||
[item drop ref table]
|
||||
45 2 3
|
||||
46 3 4
|
||||
`
|
||||
|
||||
func TestParseDropCatalog(t *testing.T) {
|
||||
catalog, err := ParseDropCatalog(testDropCatalog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if probabilityRows, rarityRows, gradeRows := catalog.Snapshot(); probabilityRows != 2 || rarityRows != 2 || gradeRows != 2 {
|
||||
t.Fatalf("snapshot=%d/%d/%d", probabilityRows, rarityRows, gradeRows)
|
||||
}
|
||||
if rate, ok := catalog.DropRate(46, 4); !ok || rate != 1000 {
|
||||
t.Fatalf("rate=%d found=%t", rate, ok)
|
||||
}
|
||||
if rate, ok := catalog.DropRate(45, -1); !ok || rate != 100 {
|
||||
t.Fatalf("clamped rate=%d found=%t", rate, ok)
|
||||
}
|
||||
if window, ok := catalog.GradeWindow(45); !ok || window != (GradeWindow{Down: 2, Up: 3}) {
|
||||
t.Fatalf("grade window=%+v found=%t", window, ok)
|
||||
}
|
||||
weights, ok := catalog.RarityWeights(1)
|
||||
if !ok || !reflect.DeepEqual(weights, [7]uint32{700000, 200000, 80000, 15000, 5000, 0, 0}) {
|
||||
t.Fatalf("rarity weights=%v found=%t", weights, ok)
|
||||
}
|
||||
if rarity, ok := catalog.RarityForRoll(1, 699999); !ok || rarity != 0 {
|
||||
t.Fatalf("rarity=%d found=%t", rarity, ok)
|
||||
}
|
||||
if rarity, ok := catalog.RarityForRoll(1, 700000); !ok || rarity != 1 {
|
||||
t.Fatalf("rarity=%d found=%t", rarity, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDropCatalogRejectsMalformedShapes(t *testing.T) {
|
||||
tests := []string{
|
||||
"[drop prob count]\n1\n[dungeon difficulty drop prob]\n1 90 1\n[basis of rarity dicision]\n1 1 2 3 4 5 6 7\n[item drop ref table]\n1 0 1\n",
|
||||
"[drop prob count]\n1\n[dungeon difficulty drop prob]\n1 90 1 2 3 4 5\n[basis of rarity dicision]\n1 2 1 3 4 5 6 7\n[item drop ref table]\n1 0 1\n",
|
||||
"[drop prob count]\n1\n[dungeon difficulty drop prob]\n1 90 1 2 3 4 5\n[basis of rarity dicision]\n1 1 2 3 4 5 6 7\n[item drop ref table]\n1 0 0\n",
|
||||
}
|
||||
for index, input := range tests {
|
||||
if _, err := ParseDropCatalog(input); !errors.Is(err, ErrDropCatalogInvalid) {
|
||||
t.Fatalf("case=%d error=%v", index, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var ErrPlanInvalid = errors.New("dnf hell party plan is invalid")
|
||||
|
||||
type MapEntry struct {
|
||||
GroupID uint32
|
||||
Rate uint32
|
||||
Order uint32
|
||||
}
|
||||
|
||||
type ActorIdentity struct {
|
||||
Order uint32
|
||||
Group uint32
|
||||
Index uint16
|
||||
}
|
||||
|
||||
type PlannedActor struct {
|
||||
Identity ActorIdentity
|
||||
Code uint32
|
||||
EntityType uint8
|
||||
SuppressGroupReward bool
|
||||
}
|
||||
|
||||
type Wave struct {
|
||||
Order uint32
|
||||
GroupID uint32
|
||||
Rate uint32
|
||||
RewardRollCount uint32
|
||||
Actors []PlannedActor
|
||||
}
|
||||
|
||||
type Plan struct {
|
||||
GroupKey string
|
||||
Seed uint64
|
||||
Waves []Wave
|
||||
}
|
||||
|
||||
type PlanOptions struct {
|
||||
GroupKey string
|
||||
Seed uint64
|
||||
Entries []MapEntry
|
||||
// SuppressGroupReward is resolved from the current actor script's
|
||||
// [hell monster] flag by the caller. A nil resolver means false.
|
||||
SuppressGroupReward func(code uint32, entityType uint8) bool
|
||||
}
|
||||
|
||||
func BuildPlan(catalog *Catalog, options PlanOptions) (Plan, error) {
|
||||
if catalog == nil {
|
||||
return Plan{}, fmt.Errorf("%w: catalog is nil", ErrPlanInvalid)
|
||||
}
|
||||
key := normalizeKey(options.GroupKey)
|
||||
rule, ok := catalog.Difficulty(key)
|
||||
if !ok {
|
||||
return Plan{}, fmt.Errorf("%w: difficulty=%q", ErrPlanInvalid, key)
|
||||
}
|
||||
byOrder := make(map[uint32][]MapEntry)
|
||||
for _, entry := range options.Entries {
|
||||
if entry.GroupID == 0 || entry.Order == 0 {
|
||||
return Plan{}, fmt.Errorf("%w: entry=%+v", ErrPlanInvalid, entry)
|
||||
}
|
||||
group, exists := catalog.Group(entry.GroupID)
|
||||
if !exists || len(group.Actors[key]) == 0 {
|
||||
continue
|
||||
}
|
||||
byOrder[entry.Order] = append(byOrder[entry.Order], entry)
|
||||
}
|
||||
orders := make([]uint32, 0, len(byOrder))
|
||||
for order := range byOrder {
|
||||
orders = append(orders, order)
|
||||
}
|
||||
sort.Slice(orders, func(i, j int) bool { return orders[i] < orders[j] })
|
||||
plan := Plan{GroupKey: key, Seed: options.Seed}
|
||||
for _, order := range orders {
|
||||
entry := selectWeighted(byOrder[order], options.Seed, order)
|
||||
group, _ := catalog.Group(entry.GroupID)
|
||||
definitions := group.Actors[key]
|
||||
wave := Wave{Order: order, GroupID: entry.GroupID, Rate: entry.Rate, RewardRollCount: rule.RewardRollCount}
|
||||
for index, actor := range definitions {
|
||||
planned := PlannedActor{
|
||||
Identity: ActorIdentity{Order: order, Group: entry.GroupID, Index: uint16(index)},
|
||||
Code: actor.Code,
|
||||
EntityType: actor.EntityType,
|
||||
}
|
||||
if options.SuppressGroupReward != nil {
|
||||
planned.SuppressGroupReward = options.SuppressGroupReward(actor.Code, actor.EntityType)
|
||||
}
|
||||
wave.Actors = append(wave.Actors, planned)
|
||||
}
|
||||
if len(wave.Actors) > 0 {
|
||||
plan.Waves = append(plan.Waves, wave)
|
||||
}
|
||||
}
|
||||
if len(plan.Waves) == 0 {
|
||||
return Plan{}, fmt.Errorf("%w: no eligible waves for difficulty=%s", ErrPlanInvalid, key)
|
||||
}
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
func selectWeighted(entries []MapEntry, seed uint64, order uint32) MapEntry {
|
||||
if len(entries) == 1 {
|
||||
return entries[0]
|
||||
}
|
||||
var total uint64
|
||||
for _, entry := range entries {
|
||||
total += uint64(entry.Rate)
|
||||
}
|
||||
if total == 0 {
|
||||
return entries[0]
|
||||
}
|
||||
roll := mix64(seed^uint64(order)*0x9e3779b97f4a7c15) % total
|
||||
for _, entry := range entries {
|
||||
if roll < uint64(entry.Rate) {
|
||||
return entry
|
||||
}
|
||||
roll -= uint64(entry.Rate)
|
||||
}
|
||||
return entries[0]
|
||||
}
|
||||
|
||||
func mix64(value uint64) uint64 {
|
||||
value ^= value >> 30
|
||||
value *= 0xbf58476d1ce4e5b9
|
||||
value ^= value >> 27
|
||||
value *= 0x94d049bb133111eb
|
||||
return value ^ (value >> 31)
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"longheng.io/server/internal/modules/dnf/worldmap"
|
||||
platformpvf "longheng.io/server/internal/platform/pvf"
|
||||
)
|
||||
|
||||
func TestRealPVFCatalog(t *testing.T) {
|
||||
path := os.Getenv("DNFBRIDGE_REAL_PVF_SMOKE")
|
||||
if path == "" {
|
||||
t.Skip("set DNFBRIDGE_REAL_PVF_SMOKE to verify the runtime Hell Party catalog")
|
||||
}
|
||||
archive, err := platformpvf.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open(%q) error = %v", path, err)
|
||||
}
|
||||
catalog, err := Load(archive)
|
||||
if err != nil {
|
||||
t.Fatalf("Load(real PVF) error = %v", err)
|
||||
}
|
||||
rules, groups, actors := catalog.Snapshot()
|
||||
if rules != 4 || groups < 160 || actors < 500 {
|
||||
t.Fatalf("real catalog snapshot rules=%d groups=%d actors=%d", rules, groups, actors)
|
||||
}
|
||||
rule, ok := catalog.Difficulty("A")
|
||||
if !ok || rule.RewardRollCount != 8 {
|
||||
t.Fatalf("real difficulty A = %+v, %v", rule, ok)
|
||||
}
|
||||
group, ok := catalog.Group(155)
|
||||
if !ok || len(group.Actors["A"]) != 2 {
|
||||
t.Fatalf("real group 155 = %+v, %v", group, ok)
|
||||
}
|
||||
t.Logf("real Hell Party catalog: rules=%d groups=%d actors=%d difficultyA=%v", rules, groups, actors, rule.Values)
|
||||
}
|
||||
|
||||
func TestRealPVFDropCatalog(t *testing.T) {
|
||||
path := os.Getenv("DNFBRIDGE_REAL_PVF_SMOKE")
|
||||
if path == "" {
|
||||
t.Skip("set DNFBRIDGE_REAL_PVF_SMOKE to verify the runtime Hell Party drop catalog")
|
||||
}
|
||||
archive, err := platformpvf.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open(%q) error = %v", path, err)
|
||||
}
|
||||
catalog, err := LoadDropCatalog(archive)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDropCatalog(real PVF) error = %v", err)
|
||||
}
|
||||
probabilityRows, rarityRows, gradeRows := catalog.Snapshot()
|
||||
if probabilityRows == 0 || rarityRows < 2 || gradeRows < 90 {
|
||||
t.Fatalf("real drop catalog snapshot probability=%d rarity=%d grade=%d", probabilityRows, rarityRows, gradeRows)
|
||||
}
|
||||
rate, rateFound := catalog.DropRate(90, 0)
|
||||
window, windowFound := catalog.GradeWindow(90)
|
||||
weights, weightsFound := catalog.RarityWeights(1)
|
||||
if !rateFound || !windowFound || !weightsFound {
|
||||
t.Fatalf("real level 90 fields rate=%d/%t window=%+v/%t weights=%v/%t", rate, rateFound, window, windowFound, weights, weightsFound)
|
||||
}
|
||||
t.Logf("real Hell Party drop catalog: probability=%d rarity=%d grade=%d level90_rate=%d level90_window=%+v modeA_weights=%v",
|
||||
probabilityRows, rarityRows, gradeRows, rate, window, weights)
|
||||
}
|
||||
|
||||
func TestRealPVFDungeon86Room(t *testing.T) {
|
||||
path := os.Getenv("DNFBRIDGE_REAL_PVF_SMOKE")
|
||||
if path == "" {
|
||||
t.Skip("set DNFBRIDGE_REAL_PVF_SMOKE to verify the runtime Hell Party room")
|
||||
}
|
||||
archive, err := platformpvf.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open(%q) error = %v", path, err)
|
||||
}
|
||||
table, err := worldmap.LoadSource(context.Background(), archive, worldmap.Options{SkipAreas: true})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadSource(real PVF) error = %v", err)
|
||||
}
|
||||
dungeon, found := table.FindDungeon(86)
|
||||
if !found || len(dungeon.Mazes) == 0 {
|
||||
t.Fatalf("real dungeon 86 missing: found=%t mazes=%d", found, len(dungeon.Mazes))
|
||||
}
|
||||
maze := dungeon.Mazes[0]
|
||||
if !maze.SealDoorMapIndex.Set || len(maze.SealDoorPosition) != 2 {
|
||||
t.Fatalf("real dungeon 86 seal-door metadata=%+v position=%v", maze.SealDoorMapIndex, maze.SealDoorPosition)
|
||||
}
|
||||
sealMap, found := table.FindMap(maze.SealDoorMapIndex.Value)
|
||||
if !found {
|
||||
t.Fatalf("real dungeon 86 seal-door map %d missing", maze.SealDoorMapIndex.Value)
|
||||
}
|
||||
resolver, err := worldmap.NewResolver(table)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
topology, err := worldmap.BuildDungeonTopology(resolver, 86, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
coordinate := worldmap.RoomCoordinate{X: maze.SealDoorPosition[0], Y: maze.SealDoorPosition[1]}
|
||||
normalRoom, found := topology.Room(coordinate)
|
||||
if !found || normalRoom.Map == nil {
|
||||
t.Fatalf("real dungeon 86 normal seal room %s missing", coordinate)
|
||||
}
|
||||
room, err := ResolveRoom(table, topology, 86, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveRoom(real dungeon 86) error = %v", err)
|
||||
}
|
||||
if room.Coordinate != coordinate || room.HellMap.ID != 60050 || room.NormalMap.ID != 20304 ||
|
||||
room.Pillar.ObjectID != 30519 || len(room.Entries) != 18 {
|
||||
t.Fatalf("real dungeon 86 Hell Party room = %+v", room)
|
||||
}
|
||||
t.Logf("dungeon=86 seal_map=%d path=%s coordinate=%s normal_map=%d pillar=%d entries=%d",
|
||||
sealMap.ID, sealMap.Path, coordinate, normalRoom.Map.Map.ID, room.Pillar.ObjectID, len(room.Entries))
|
||||
}
|
||||
|
||||
func TestRealPVFDungeon310Room(t *testing.T) {
|
||||
path := os.Getenv("DNFBRIDGE_REAL_PVF_SMOKE")
|
||||
if path == "" {
|
||||
t.Skip("set DNFBRIDGE_REAL_PVF_SMOKE to inspect the current dungeon 310 Hell Party room")
|
||||
}
|
||||
archive, err := platformpvf.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open(%q) error = %v", path, err)
|
||||
}
|
||||
table, err := worldmap.LoadSource(context.Background(), archive, worldmap.Options{SkipAreas: true})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadSource(real PVF) error = %v", err)
|
||||
}
|
||||
dungeon, found := table.FindDungeon(310)
|
||||
if !found || len(dungeon.Mazes) == 0 {
|
||||
t.Fatalf("real dungeon 310 missing: found=%t mazes=%d", found, len(dungeon.Mazes))
|
||||
}
|
||||
maze := dungeon.Mazes[0]
|
||||
if !maze.SealDoorMapIndex.Set || len(maze.SealDoorPosition) != 2 {
|
||||
t.Fatalf("real dungeon 310 seal-door metadata=%+v position=%v", maze.SealDoorMapIndex, maze.SealDoorPosition)
|
||||
}
|
||||
sealMap, found := table.FindMap(maze.SealDoorMapIndex.Value)
|
||||
if !found {
|
||||
t.Fatalf("real dungeon 310 seal-door map %d missing", maze.SealDoorMapIndex.Value)
|
||||
}
|
||||
if sealMap.ID != 60083 || sealMap.Path != "map/MetroCenter/Time_Plaza/Hell_Time_Plaza.map" || len(sealMap.Diagnostics) != 0 {
|
||||
t.Fatalf("real dungeon 310 seal-door map id=%d path=%s diagnostics=%+v", sealMap.ID, sealMap.Path, sealMap.Diagnostics)
|
||||
}
|
||||
if len(sealMap.SpecialPassiveObjects) != 1 {
|
||||
t.Fatalf("real dungeon 310 special objects=%+v", sealMap.SpecialPassiveObjects)
|
||||
}
|
||||
pillar := sealMap.SpecialPassiveObjects[0]
|
||||
if pillar.ObjectID != 30528 || pillar.X != 594 || pillar.Y != 301 || len(pillar.HellParty) != 13 {
|
||||
t.Fatalf("real dungeon 310 Hell Party pillar=%+v", pillar)
|
||||
}
|
||||
resolver, err := worldmap.NewResolver(table)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
topology, err := worldmap.BuildDungeonTopology(resolver, 310, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
room, err := ResolveRoom(table, topology, 310, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveRoom(real dungeon 310) error = %v", err)
|
||||
}
|
||||
if room.HellMap.ID != 60083 || room.NormalMap.ID == 60083 || room.Pillar.ObjectID != 30528 || len(room.Entries) != 13 {
|
||||
t.Fatalf("real dungeon 310 Hell Party room=%+v", room)
|
||||
}
|
||||
t.Logf("dungeon=310 seal_map=%d path=%s coordinate=%s normal_map=%d pillar=%d entries=%d",
|
||||
sealMap.ID, sealMap.Path, room.Coordinate, room.NormalMap.ID, room.Pillar.ObjectID, len(room.Entries))
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"longheng.io/server/internal/modules/dnf/worldmap"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRoomUnavailable = errors.New("dnf hell party room is unavailable")
|
||||
ErrRoomInvalid = errors.New("dnf hell party room is invalid")
|
||||
)
|
||||
|
||||
type WorldSource interface {
|
||||
FindDungeon(id int64) (worldmap.Dungeon, bool)
|
||||
FindMap(id int64) (worldmap.Map, bool)
|
||||
}
|
||||
|
||||
type Pillar struct {
|
||||
ObjectID int64
|
||||
X int64
|
||||
Y int64
|
||||
Flags int64
|
||||
}
|
||||
|
||||
type Room struct {
|
||||
Coordinate worldmap.RoomCoordinate
|
||||
HellMap worldmap.Map
|
||||
NormalMap worldmap.Map
|
||||
Pillar Pillar
|
||||
Entries []MapEntry
|
||||
}
|
||||
|
||||
func ResolveRoom(source WorldSource, topology *worldmap.DungeonTopology, dungeonID int64, mazeIndex int) (Room, error) {
|
||||
if source == nil || topology == nil {
|
||||
return Room{}, fmt.Errorf("%w: source or topology is nil", ErrRoomUnavailable)
|
||||
}
|
||||
dungeon, ok := source.FindDungeon(dungeonID)
|
||||
if !ok || mazeIndex < 0 || mazeIndex >= len(dungeon.Mazes) {
|
||||
return Room{}, fmt.Errorf("%w: dungeon=%d maze=%d", ErrRoomUnavailable, dungeonID, mazeIndex)
|
||||
}
|
||||
maze := dungeon.Mazes[mazeIndex]
|
||||
if !maze.SealDoorMapIndex.Set || maze.SealDoorMapIndex.Value <= 0 || len(maze.SealDoorPosition) != 2 {
|
||||
return Room{}, fmt.Errorf("%w: dungeon=%d maze=%d has no exact seal-door map/position", ErrRoomUnavailable, dungeonID, mazeIndex)
|
||||
}
|
||||
coordinate := worldmap.RoomCoordinate{X: maze.SealDoorPosition[0], Y: maze.SealDoorPosition[1]}
|
||||
topologyRoom, ok := topology.Room(coordinate)
|
||||
if !ok || topologyRoom.Map == nil {
|
||||
return Room{}, fmt.Errorf("%w: seal-door room=%s is absent or unresolved", ErrRoomInvalid, coordinate)
|
||||
}
|
||||
if topologyRoom.Boss {
|
||||
return Room{}, fmt.Errorf("%w: seal-door room=%s is a boss room", ErrRoomInvalid, coordinate)
|
||||
}
|
||||
hellMap, ok := source.FindMap(maze.SealDoorMapIndex.Value)
|
||||
if !ok {
|
||||
return Room{}, fmt.Errorf("%w: seal-door map=%d is absent", ErrRoomInvalid, maze.SealDoorMapIndex.Value)
|
||||
}
|
||||
room := Room{Coordinate: coordinate, HellMap: hellMap, NormalMap: topologyRoom.Map.Map}
|
||||
for _, object := range hellMap.SpecialPassiveObjects {
|
||||
if len(object.HellParty) == 0 {
|
||||
continue
|
||||
}
|
||||
if room.Pillar.ObjectID == 0 {
|
||||
room.Pillar = Pillar{
|
||||
ObjectID: object.ObjectID,
|
||||
X: object.X,
|
||||
Y: object.Y,
|
||||
Flags: object.Flags,
|
||||
}
|
||||
}
|
||||
for _, entry := range object.HellParty {
|
||||
if entry.GroupID <= 0 || entry.GroupID > int64(^uint32(0)) ||
|
||||
entry.Rate < 0 || entry.Rate > int64(^uint32(0)) ||
|
||||
entry.Order <= 0 || entry.Order > int64(^uint32(0)) {
|
||||
return Room{}, fmt.Errorf("%w: map=%d hellparty entry=%+v", ErrRoomInvalid, hellMap.ID, entry)
|
||||
}
|
||||
room.Entries = append(room.Entries, MapEntry{
|
||||
GroupID: uint32(entry.GroupID),
|
||||
Rate: uint32(entry.Rate),
|
||||
Order: uint32(entry.Order),
|
||||
})
|
||||
}
|
||||
}
|
||||
if room.Pillar.ObjectID == 0 || len(room.Entries) == 0 {
|
||||
return Room{}, fmt.Errorf("%w: seal-door map=%d has no [hellparty] pillar", ErrRoomInvalid, hellMap.ID)
|
||||
}
|
||||
return room, nil
|
||||
}
|
||||
|
||||
func ApplyRoom(topology *worldmap.DungeonTopology, room Room) error {
|
||||
if topology == nil || room.HellMap.ID <= 0 {
|
||||
return fmt.Errorf("%w: topology or Hell map is empty", ErrRoomInvalid)
|
||||
}
|
||||
return topology.ReplaceRoomMap(room.Coordinate, worldmap.ResolvedMap{
|
||||
Map: room.HellMap,
|
||||
Source: worldmap.ResolutionHellParty,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
dnfpvf "longheng.io/server/internal/modules/dnf/pvf"
|
||||
"longheng.io/server/internal/modules/dnf/worldmap"
|
||||
)
|
||||
|
||||
type roomTextSource map[string]string
|
||||
|
||||
func (source roomTextSource) ReadText(path string) (string, error) {
|
||||
return source[path], nil
|
||||
}
|
||||
|
||||
func TestResolveAndApplySealDoorRoom(t *testing.T) {
|
||||
source := roomTextSource{
|
||||
"map/map.lst": "100 `normal.map` 200 `hell.map`",
|
||||
"map/normal.map": "[dungeon]\n10\n[/dungeon]",
|
||||
"map/hell.map": "[special passive object]\n30528 550 254 0 1 `[hellparty]`\n[hellparty]\n125 25 1 155 75 1\n[/hellparty]",
|
||||
"dungeon/dungeon.lst": "10 `test.dgn`",
|
||||
"dungeon/test.dgn": `[maze info]
|
||||
[size]
|
||||
2 1
|
||||
[greed]
|
||||
` + "`FF`" + `
|
||||
[map specification]
|
||||
` + "`map`" + ` 0 0 100 ` + "`boss`" + ` 1 0 100
|
||||
[/map specification]
|
||||
[start map]
|
||||
0 0
|
||||
[/start map]
|
||||
[boss map]
|
||||
1 0
|
||||
[/boss map]
|
||||
[seal door map index]
|
||||
200
|
||||
[seal door pos]
|
||||
0 0`,
|
||||
"worldmap/worldmap.lst": "1 `test.map`",
|
||||
"worldmap/test.map": "[dungeon]\n10 -1\n[/dungeon]",
|
||||
}
|
||||
index, err := dnfpvf.Build(context.Background(), source, dnfpvf.BuildOptions{
|
||||
Paths: []string{"map/normal.map", "map/hell.map", "dungeon/test.dgn", "worldmap/test.map"},
|
||||
Lists: []string{worldmap.DefaultMapList, worldmap.DefaultDungeonList, worldmap.DefaultWorldMapList},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
table, err := worldmap.Load(context.Background(), index, worldmap.Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolver, err := worldmap.NewResolver(table)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
topology, err := worldmap.BuildDungeonTopology(resolver, 10, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
room, err := ResolveRoom(table, topology, 10, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveRoom() error = %v", err)
|
||||
}
|
||||
if room.HellMap.ID != 200 || room.NormalMap.ID != 100 || room.Pillar.ObjectID != 30528 || len(room.Entries) != 2 {
|
||||
t.Fatalf("room = %+v", room)
|
||||
}
|
||||
if err := ApplyRoom(topology, room); err != nil {
|
||||
t.Fatalf("ApplyRoom() error = %v", err)
|
||||
}
|
||||
applied, ok := topology.Room(room.Coordinate)
|
||||
if !ok || applied.Map == nil || applied.Map.Map.ID != 200 || applied.Map.Source != worldmap.ResolutionHellParty {
|
||||
t.Fatalf("applied room = %+v, %v", applied, ok)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package hellparty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRuntimeInvalid = errors.New("dnf hell party runtime is invalid")
|
||||
ErrPhaseInvalid = errors.New("dnf hell party phase transition is invalid")
|
||||
ErrActorUnknown = errors.New("dnf hell party actor is unknown")
|
||||
)
|
||||
|
||||
type Phase uint8
|
||||
|
||||
const (
|
||||
PhaseWaitingStart Phase = iota
|
||||
PhaseStarted
|
||||
PhaseComplete
|
||||
)
|
||||
|
||||
type DeathResult struct {
|
||||
Duplicate bool
|
||||
GroupCompleted bool
|
||||
AllComplete bool
|
||||
RewardRollCount uint32
|
||||
RewardSuppressed bool
|
||||
}
|
||||
|
||||
type Runtime struct {
|
||||
plan Plan
|
||||
phase Phase
|
||||
actors map[ActorIdentity]PlannedActor
|
||||
killed map[ActorIdentity]struct{}
|
||||
remaining map[waveIdentity]int
|
||||
rewardReleased map[waveIdentity]struct{}
|
||||
}
|
||||
|
||||
type waveIdentity struct {
|
||||
Order uint32
|
||||
Group uint32
|
||||
}
|
||||
|
||||
func NewRuntime(plan Plan) (*Runtime, error) {
|
||||
if len(plan.Waves) == 0 {
|
||||
return nil, fmt.Errorf("%w: no waves", ErrRuntimeInvalid)
|
||||
}
|
||||
runtime := &Runtime{
|
||||
plan: clonePlan(plan),
|
||||
phase: PhaseWaitingStart,
|
||||
actors: make(map[ActorIdentity]PlannedActor),
|
||||
killed: make(map[ActorIdentity]struct{}),
|
||||
remaining: make(map[waveIdentity]int),
|
||||
rewardReleased: make(map[waveIdentity]struct{}),
|
||||
}
|
||||
for _, wave := range plan.Waves {
|
||||
identity := waveIdentity{Order: wave.Order, Group: wave.GroupID}
|
||||
if _, exists := runtime.remaining[identity]; exists || len(wave.Actors) == 0 {
|
||||
return nil, fmt.Errorf("%w: duplicate or empty wave order=%d group=%d", ErrRuntimeInvalid, wave.Order, wave.GroupID)
|
||||
}
|
||||
runtime.remaining[identity] = len(wave.Actors)
|
||||
for _, actor := range wave.Actors {
|
||||
if _, exists := runtime.actors[actor.Identity]; exists {
|
||||
return nil, fmt.Errorf("%w: duplicate actor=%+v", ErrRuntimeInvalid, actor.Identity)
|
||||
}
|
||||
runtime.actors[actor.Identity] = actor
|
||||
}
|
||||
}
|
||||
return runtime, nil
|
||||
}
|
||||
|
||||
func (r *Runtime) Start() error {
|
||||
if r == nil || r.phase != PhaseWaitingStart {
|
||||
return ErrPhaseInvalid
|
||||
}
|
||||
r.phase = PhaseStarted
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runtime) RecordDeath(identity ActorIdentity) (DeathResult, error) {
|
||||
if r == nil {
|
||||
return DeathResult{}, ErrPhaseInvalid
|
||||
}
|
||||
actor, exists := r.actors[identity]
|
||||
if !exists {
|
||||
return DeathResult{}, ErrActorUnknown
|
||||
}
|
||||
if _, duplicate := r.killed[identity]; duplicate {
|
||||
return DeathResult{Duplicate: true, AllComplete: r.phase == PhaseComplete}, nil
|
||||
}
|
||||
if r.phase != PhaseStarted {
|
||||
return DeathResult{}, ErrPhaseInvalid
|
||||
}
|
||||
r.killed[identity] = struct{}{}
|
||||
waveID := waveIdentity{Order: identity.Order, Group: identity.Group}
|
||||
r.remaining[waveID]--
|
||||
result := DeathResult{}
|
||||
if r.remaining[waveID] == 0 {
|
||||
result.GroupCompleted = true
|
||||
if _, released := r.rewardReleased[waveID]; !released {
|
||||
r.rewardReleased[waveID] = struct{}{}
|
||||
result.RewardSuppressed = actor.SuppressGroupReward
|
||||
if !result.RewardSuppressed {
|
||||
result.RewardRollCount = r.waveRewardRollCount(waveID)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(r.killed) == len(r.actors) {
|
||||
r.phase = PhaseComplete
|
||||
result.AllComplete = true
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *Runtime) Phase() Phase {
|
||||
if r == nil {
|
||||
return PhaseWaitingStart
|
||||
}
|
||||
return r.phase
|
||||
}
|
||||
|
||||
func (r *Runtime) Plan() Plan {
|
||||
if r == nil {
|
||||
return Plan{}
|
||||
}
|
||||
return clonePlan(r.plan)
|
||||
}
|
||||
|
||||
func (r *Runtime) waveRewardRollCount(identity waveIdentity) uint32 {
|
||||
for _, wave := range r.plan.Waves {
|
||||
if wave.Order == identity.Order && wave.GroupID == identity.Group {
|
||||
return wave.RewardRollCount
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func clonePlan(plan Plan) Plan {
|
||||
plan.Waves = append([]Wave(nil), plan.Waves...)
|
||||
for index := range plan.Waves {
|
||||
plan.Waves[index].Actors = append([]PlannedActor(nil), plan.Waves[index].Actors...)
|
||||
}
|
||||
return plan
|
||||
}
|
||||
Reference in new issue
Block a user