forked from Shardstone/trail-into-darkness
updated linq calls to zero allocation zlinq
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
|
||||
namespace Nox.Game {
|
||||
public interface ICharacterAttributesFactory {
|
||||
@@ -16,12 +16,12 @@ namespace Nox.Game {
|
||||
public EntityAttributes Create(IEntityDefinition entityDefinition) {
|
||||
var attributes = entityDefinition.Attributes;
|
||||
|
||||
if(attributes.attributes.Any(a => a.value <= 0)) {
|
||||
if(attributes.attributes.AsValueEnumerable().Any(a => a.value <= 0)) {
|
||||
throw new ArgumentOutOfRangeException( "attributes cannot be zero or negative.", new ArgumentException() );
|
||||
}
|
||||
|
||||
return new EntityAttributes {
|
||||
attributes = attributes.attributes
|
||||
attributes = attributes.attributes.AsValueEnumerable()
|
||||
.Select(a => {
|
||||
var modifiers = modifierResolver.CollectModifiers(entityDefinition, a.attribute);
|
||||
return new Attribute(a.attribute, modifierResolver.Resolve(a.value, modifiers));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
|
||||
namespace Nox.Game {
|
||||
public interface ICharacterFactory {
|
||||
@@ -118,7 +118,7 @@ namespace Nox.Game {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var perkId in perkData.perks.Distinct()) {
|
||||
foreach(var perkId in perkData.perks.AsValueEnumerable().Distinct()) {
|
||||
perkFactory.TryAddPerk(character, perkId.Id);
|
||||
}
|
||||
}
|
||||
@@ -128,7 +128,7 @@ namespace Nox.Game {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var modifierId in modifiersData.modifiers.Distinct()) {
|
||||
foreach(var modifierId in modifiersData.modifiers.AsValueEnumerable().Distinct()) {
|
||||
modifiersFactory.TryAddModifier(character, modifierId.Id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
|
||||
namespace Nox.Game {
|
||||
public interface ICharacterStatsFactory {
|
||||
@@ -18,7 +18,7 @@ namespace Nox.Game {
|
||||
public EntityStats Create(IEntityDefinition entityDefinition) {
|
||||
var attributes = entityDefinition.Attributes;
|
||||
|
||||
if(attributes.attributes.Any(a => a.value <= 0)) {
|
||||
if(attributes.attributes.AsValueEnumerable().Any(a => a.value <= 0)) {
|
||||
throw new ArgumentOutOfRangeException( "attributes cannot be zero or negative.", new ArgumentException() );
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Nox.Game {
|
||||
@@ -32,8 +32,8 @@ namespace Nox.Game {
|
||||
}
|
||||
}
|
||||
|
||||
if(partyDefinitionSets.FirstOrDefault(pds => pds.id == partyDefinitionSet.id && pds.isTestingSet) != null) {
|
||||
var testingSet = partyDefinitionSets.FirstOrDefault(pds => pds.id == partyDefinitionSet.id && pds.isTestingSet);
|
||||
if(partyDefinitionSets.AsValueEnumerable().FirstOrDefault(pds => pds.id == partyDefinitionSet.id && pds.isTestingSet) != null) {
|
||||
var testingSet = partyDefinitionSets.AsValueEnumerable().FirstOrDefault(pds => pds.id == partyDefinitionSet.id && pds.isTestingSet);
|
||||
ApplyClassAndRacialBonuses(testingSet);
|
||||
}
|
||||
|
||||
@@ -48,8 +48,8 @@ namespace Nox.Game {
|
||||
var partyDefinition = testingSet.partyDefinition;
|
||||
foreach(var member in partyDefinition.members) {
|
||||
var baseSettings = starterCharacterSettings.defaultEntityAttributes;
|
||||
var classAttributes = starterCharacterSettings.classBonuses.FirstOrDefault(c => c.@class == member.Class)?.bonusAttributes;
|
||||
var racialAttributes = starterCharacterSettings.racialBonuses.FirstOrDefault(rb => rb.race == member.Race)?.bonusAttributes;
|
||||
var classAttributes = starterCharacterSettings.classBonuses.AsValueEnumerable().FirstOrDefault(c => c.@class == member.Class)?.bonusAttributes;
|
||||
var racialAttributes = starterCharacterSettings.racialBonuses.AsValueEnumerable().FirstOrDefault(rb => rb.race == member.Race)?.bonusAttributes;
|
||||
if (classAttributes != null && racialAttributes != null) {
|
||||
member.Attributes += baseSettings + classAttributes + racialAttributes;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Nox.Game {
|
||||
@@ -81,9 +81,9 @@ namespace Nox.Game {
|
||||
|
||||
public static EntityAttributes operator +(EntityAttributes a, EntityAttributes b) {
|
||||
return new EntityAttributes {
|
||||
attributes = a.attributes
|
||||
attributes = a.attributes.AsValueEnumerable()
|
||||
.Select(attr => {
|
||||
var match = b.attributes?.FirstOrDefault(attr2 => attr2.attribute == attr.attribute);
|
||||
var match = b.attributes?.AsValueEnumerable().FirstOrDefault(attr2 => attr2.attribute == attr.attribute);
|
||||
return new Attribute(attr.attribute, attr.value + (match?.value ?? 0));
|
||||
})
|
||||
.ToArray()
|
||||
@@ -91,7 +91,7 @@ namespace Nox.Game {
|
||||
}
|
||||
|
||||
public int GetValue(AttributeType attributeType) {
|
||||
return attributes.First(attr => attr.attribute == attributeType).value;
|
||||
return attributes.AsValueEnumerable().First(attr => attr.attribute == attributeType).value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,10 @@ namespace Nox.Game {
|
||||
public Stat[] stats;
|
||||
|
||||
public int GetValue(StatType statType) {
|
||||
var match = stats?.FirstOrDefault(stat => stat.stat == statType);
|
||||
if(stats == null) {
|
||||
return 0;
|
||||
}
|
||||
var match = stats.AsValueEnumerable().FirstOrDefault(stat => stat.stat == statType);
|
||||
return match?.value ?? 0;
|
||||
}
|
||||
}
|
||||
@@ -125,20 +128,20 @@ namespace Nox.Game {
|
||||
Race = Race,
|
||||
Class = Class,
|
||||
Attributes = new EntityAttributes {
|
||||
attributes = Attributes?.attributes?.Select(a => new Attribute(a.attribute, a.value)).ToArray()
|
||||
attributes = Attributes?.attributes?.AsValueEnumerable().Select(a => new Attribute(a.attribute, a.value)).ToArray()
|
||||
},
|
||||
Stats = new EntityStats {
|
||||
stats = Stats?.stats?.Select(s => new Stat(s.stat, s.value)).ToArray()
|
||||
stats = Stats?.stats?.AsValueEnumerable().Select(s => new Stat(s.stat, s.value)).ToArray()
|
||||
},
|
||||
Perks = new PerksData {
|
||||
perks = Perks?.perks?.Select(p => new PerkDefinition {
|
||||
perks = Perks?.perks?.AsValueEnumerable().Select(p => new PerkDefinition {
|
||||
Id = p.Id,
|
||||
Name = p.Name,
|
||||
Modifiers = p.Modifiers
|
||||
}).ToList() ?? new()
|
||||
},
|
||||
Modifiers = new ModifiersData {
|
||||
modifiers = Modifiers?.modifiers?.Select(m => new ModifierDefinition {
|
||||
modifiers = Modifiers?.modifiers?.AsValueEnumerable().Select(m => new ModifierDefinition {
|
||||
Id = m.Id,
|
||||
Name = m.Name,
|
||||
StatType = m.StatType,
|
||||
@@ -159,9 +162,9 @@ namespace Nox.Game {
|
||||
public List<CharacterDefinition> members = new();
|
||||
|
||||
[JsonIgnore]
|
||||
public CharacterDefinition Protagonist => members.FirstOrDefault(m => m.Role == CharacterRole.Protagonist);
|
||||
public CharacterDefinition Protagonist => members.AsValueEnumerable().FirstOrDefault(m => m.Role == CharacterRole.Protagonist);
|
||||
|
||||
[JsonIgnore]
|
||||
public IReadOnlyList<CharacterDefinition> Companions => members.Where(m => m.Role == CharacterRole.Companion).ToList();
|
||||
public IReadOnlyList<CharacterDefinition> Companions => members.AsValueEnumerable().Where(m => m.Role == CharacterRole.Companion).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ using Jovian.InspectorTools;
|
||||
using Jovian.Logger;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Nox.Game {
|
||||
@@ -61,7 +61,7 @@ namespace Nox.Game {
|
||||
return modifiersRegistry.modifiersData.modifiers;
|
||||
}
|
||||
public IModifier GetById(Guid modifierId) {
|
||||
return modifiersRegistry.modifiersData.modifiers.FirstOrDefault(m => m.Id == modifierId);
|
||||
return modifiersRegistry.modifiersData.modifiers.AsValueEnumerable().FirstOrDefault(m => m.Id == modifierId);
|
||||
}
|
||||
public IReadOnlyCollection<IModifier> GetModifiersFor(IEntityDefinition character) {
|
||||
return character.Modifiers.modifiers;
|
||||
@@ -76,7 +76,7 @@ namespace Nox.Game {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(character.Modifiers.modifiers.Any(p => p != null && p.Id == modifierId)) {
|
||||
if(character.Modifiers.modifiers.AsValueEnumerable().Any(p => p != null && p.Id == modifierId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
|
||||
namespace Nox.Game {
|
||||
|
||||
@@ -26,34 +26,40 @@ namespace Nox.Game {
|
||||
return baseValue;
|
||||
}
|
||||
|
||||
var grouped = modifiers
|
||||
.Where(m => m != null && m.Operation != ModifierOperation.None)
|
||||
.GroupBy(m => m.Operation)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
float flatSum = 0f;
|
||||
float addSum = 0f;
|
||||
float pctSum = 0f;
|
||||
var mulValues = new List<float>();
|
||||
var hasFlat = false;
|
||||
|
||||
float result = baseValue;
|
||||
|
||||
// 1. Flat — if any flat modifiers exist, they replace the base entirely
|
||||
if(grouped.TryGetValue(ModifierOperation.Flat, out var flatMods)) {
|
||||
result = flatMods.Sum(m => m.Value);
|
||||
}
|
||||
|
||||
// 2. Addition — sum all additive bonuses
|
||||
if(grouped.TryGetValue(ModifierOperation.Addition, out var addMods)) {
|
||||
result += addMods.Sum(m => m.Value);
|
||||
}
|
||||
|
||||
// 3. Percentage — summed into one multiplier: result * (1 + totalPercent / 100)
|
||||
if(grouped.TryGetValue(ModifierOperation.Percentage, out var pctMods)) {
|
||||
var totalPercent = pctMods.Sum(m => m.Value);
|
||||
result *= 1f + (totalPercent / 100f);
|
||||
}
|
||||
|
||||
// 4. Multiplication — each factor applied sequentially
|
||||
if(grouped.TryGetValue(ModifierOperation.Multiplication, out var mulMods)) {
|
||||
foreach(var mod in mulMods) {
|
||||
result *= mod.Value;
|
||||
foreach(var m in modifiers) {
|
||||
if(m == null || m.Operation == ModifierOperation.None) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(m.Operation) {
|
||||
case ModifierOperation.Flat:
|
||||
flatSum += m.Value;
|
||||
hasFlat = true;
|
||||
break;
|
||||
case ModifierOperation.Addition:
|
||||
addSum += m.Value;
|
||||
break;
|
||||
case ModifierOperation.Percentage:
|
||||
pctSum += m.Value;
|
||||
break;
|
||||
case ModifierOperation.Multiplication:
|
||||
mulValues.Add(m.Value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
float result = hasFlat ? flatSum : baseValue;
|
||||
result += addSum;
|
||||
result *= 1f + (pctSum / 100f);
|
||||
|
||||
foreach(var mul in mulValues) {
|
||||
result *= mul;
|
||||
}
|
||||
|
||||
return (int)Math.Round(result);
|
||||
@@ -61,38 +67,64 @@ namespace Nox.Game {
|
||||
|
||||
public IEnumerable<IModifier> CollectModifiers(IEntityDefinition entity, StatType statType) {
|
||||
if(entity == null) {
|
||||
return Enumerable.Empty<IModifier>();
|
||||
return Array.Empty<IModifier>();
|
||||
}
|
||||
|
||||
var direct = entity.Modifiers?.modifiers?
|
||||
.Where(m => m != null && m.StatType == statType)
|
||||
?? Enumerable.Empty<IModifier>();
|
||||
var result = new List<IModifier>();
|
||||
|
||||
var fromPerks = entity.Perks?.perks?
|
||||
.Where(p => p?.Modifiers?.modifiers != null)
|
||||
.SelectMany(p => p.Modifiers.modifiers)
|
||||
.Where(m => m != null && m.StatType == statType)
|
||||
?? Enumerable.Empty<IModifier>();
|
||||
if(entity.Modifiers?.modifiers != null) {
|
||||
foreach(var m in entity.Modifiers.modifiers) {
|
||||
if(m != null && m.StatType == statType) {
|
||||
result.Add(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return direct.Concat(fromPerks);
|
||||
if(entity.Perks?.perks != null) {
|
||||
foreach(var p in entity.Perks.perks) {
|
||||
if(p?.Modifiers?.modifiers == null) {
|
||||
continue;
|
||||
}
|
||||
foreach(var m in p.Modifiers.modifiers) {
|
||||
if(m != null && m.StatType == statType) {
|
||||
result.Add(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public IEnumerable<IModifier> CollectModifiers(IEntityDefinition entity, AttributeType attributeType) {
|
||||
if(entity == null) {
|
||||
return Enumerable.Empty<IModifier>();
|
||||
return Array.Empty<IModifier>();
|
||||
}
|
||||
|
||||
var direct = entity.Modifiers?.modifiers?
|
||||
.Where(m => m != null && m.AttributeType == attributeType)
|
||||
?? Enumerable.Empty<IModifier>();
|
||||
var result = new List<IModifier>();
|
||||
|
||||
var fromPerks = entity.Perks?.perks?
|
||||
.Where(p => p?.Modifiers?.modifiers != null)
|
||||
.SelectMany(p => p.Modifiers.modifiers)
|
||||
.Where(m => m != null && m.AttributeType == attributeType)
|
||||
?? Enumerable.Empty<IModifier>();
|
||||
if(entity.Modifiers?.modifiers != null) {
|
||||
foreach(var m in entity.Modifiers.modifiers) {
|
||||
if(m != null && m.AttributeType == attributeType) {
|
||||
result.Add(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return direct.Concat(fromPerks);
|
||||
if(entity.Perks?.perks != null) {
|
||||
foreach(var p in entity.Perks.perks) {
|
||||
if(p?.Modifiers?.modifiers == null) {
|
||||
continue;
|
||||
}
|
||||
foreach(var m in p.Modifiers.modifiers) {
|
||||
if(m != null && m.AttributeType == attributeType) {
|
||||
result.Add(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
|
||||
namespace Nox.Game {
|
||||
public interface IPartyFactory {
|
||||
@@ -28,7 +28,7 @@ namespace Nox.Game {
|
||||
party.members.Add(protagonistClone);
|
||||
|
||||
if(companions != null) {
|
||||
foreach(var companion in companions.Where(c => c != null)) {
|
||||
foreach(var companion in companions.AsValueEnumerable().Where(c => c != null)) {
|
||||
var companionClone = companion.Clone();
|
||||
companionClone.Role = CharacterRole.Companion;
|
||||
party.members.Add(companionClone);
|
||||
@@ -44,12 +44,12 @@ namespace Nox.Game {
|
||||
throw new ArgumentException($"Party size {party.members.Count} exceeds max {party.maxPartySize}.");
|
||||
}
|
||||
|
||||
var protagonistCount = party.members.Count(m => m.Role == CharacterRole.Protagonist);
|
||||
var protagonistCount = party.members.AsValueEnumerable().Count(m => m.Role == CharacterRole.Protagonist);
|
||||
if(protagonistCount != 1) {
|
||||
throw new ArgumentException($"Party must contain exactly one protagonist, found {protagonistCount}.");
|
||||
}
|
||||
|
||||
var uniqueIds = party.members
|
||||
var uniqueIds = party.members.AsValueEnumerable()
|
||||
.Where(m => m.Id != Guid.Empty)
|
||||
.Select(m => m.Id)
|
||||
.Distinct()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using Jovian.Logger;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZLinq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Nox.Game {
|
||||
@@ -45,7 +45,7 @@ namespace Nox.Game {
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<IPerk> GetAll() {
|
||||
return perkPool.Values.ToList();
|
||||
return perkPool.Values.AsValueEnumerable().ToList();
|
||||
}
|
||||
|
||||
public IPerk GetById(Guid perkId) {
|
||||
@@ -55,14 +55,14 @@ namespace Nox.Game {
|
||||
|
||||
public IReadOnlyCollection<IPerk> GetPerksFor(IEntityDefinition character) {
|
||||
if(character == null) {
|
||||
return perkPool.Values.ToList();
|
||||
return perkPool.Values.AsValueEnumerable().ToList();
|
||||
}
|
||||
|
||||
var ownedPerkIds = character.Perks.perks
|
||||
var ownedPerkIds = character.Perks.perks.AsValueEnumerable()
|
||||
.Select(p => p.Id)
|
||||
.ToHashSet();
|
||||
|
||||
return perkPool.Values.Where(p => !ownedPerkIds.Contains(p.Id)).ToList();
|
||||
return perkPool.Values.AsValueEnumerable().Where(p => !ownedPerkIds.Contains(p.Id)).ToList();
|
||||
}
|
||||
|
||||
public bool TryAddPerk(IEntityDefinition character, Guid perkId) {
|
||||
@@ -74,7 +74,7 @@ namespace Nox.Game {
|
||||
GlobalLogger.LogException("Cannot add a perk with an empty Id!", LogCategory.GameLogic);
|
||||
}
|
||||
|
||||
if(character.Perks.perks.Any(p => p != null && p.Id == perkId)) {
|
||||
if(character.Perks.perks.AsValueEnumerable().Any(p => p != null && p.Id == perkId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user