2026-08-10 14:57:50 +01:00
/ * This Source Code Form is subject to the terms of the Mozilla Public
2020-12-22 03:13:43 +00:00
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at https : //mozilla.org/MPL/2.0/. */
using System ;
2026-08-10 14:57:50 +01:00
using System.Collections ;
2020-03-22 19:05:26 +00:00
using System.Collections.Generic ;
using System.Linq ;
2026-08-10 14:57:50 +01:00
using Unity.Mathematics ;
using static Unity . Mathematics . math ;
using float2 = Unity . Mathematics . float2 ;
using Random = Unity . Mathematics . Random ;
2020-05-10 00:16:33 +01:00
using JM.LinqFaster ;
2020-12-01 00:16:11 +00:00
using UniRx ;
2026-08-10 14:57:50 +01:00
using float4 = Unity . Mathematics . float4 ;
2020-03-22 19:05:26 +00:00
2020-12-10 23:20:38 +00:00
public class ItemManager
2020-03-22 19:05:26 +00:00
{
2020-04-30 02:54:31 +01:00
public Random Random = new Random ( ( uint ) ( DateTime . Now . Ticks % uint . MaxValue ) ) ;
2026-08-10 14:57:50 +01:00
// public Dictionary<string, GalaxyMapLayerData> MapLayers = new Dictionary<string, GalaxyMapLayerData>();
// public SimpleCommodityData[] Resources;
// public Dictionary<Guid, List<IController>> CorporationControllers = new Dictionary<Guid, List<IController>>();
// public Dictionary<Guid, ZoneDefinition> GalaxyZones;
2026-06-16 13:11:38 +01:00
private Action < string > _logger ;
2026-06-14 10:54:17 +01:00
2026-08-10 14:57:50 +01:00
private double _time ;
private float _deltaTime ;
private Dictionary < Guid , Zone > _zones = new Dictionary < Guid , Zone > ( ) ;
2026-06-15 18:51:59 +01:00
2026-08-10 14:57:50 +01:00
// private Guid _forceLoadZone;
// public GlobalData GlobalData => _globalData ?? (_globalData = ItemData.GetAll<GlobalData>().FirstOrDefault());
public CultCache ItemData { get ; }
public GameplaySettings GameplaySettings { get ; }
2026-06-14 05:15:18 +01:00
2026-08-10 14:57:50 +01:00
// public double Time
// {
// get => _time;
// set
// {
// _deltaTime = (float) (value - _time);
// _time = value;
// //Log($"GameContext delta time: {_deltaTime}");
// }
// }
2026-06-14 05:15:18 +01:00
2026-08-10 14:57:50 +01:00
// private readonly Dictionary<CraftedItemData, int> Tier = new Dictionary<CraftedItemData, int>();
2026-06-14 22:58:02 +01:00
2026-08-10 14:57:50 +01:00
public ItemManager ( CultCache itemData , GameplaySettings settings , Action < string > logger )
2026-06-14 22:58:02 +01:00
{
2026-08-10 14:57:50 +01:00
ItemData = itemData ;
GameplaySettings = settings ;
_logger = logger ;
2026-06-14 07:27:23 +01:00
}
2020-04-16 03:08:18 +01:00
public void Log ( string s )
{
_logger ( s ) ;
}
2026-08-10 14:57:50 +01:00
public SimpleCommodityData GetData ( SimpleCommodity item )
{
return item . Data . Value as SimpleCommodityData ;
}
public CraftedItemData GetData ( CraftedItemInstance item )
{
return item . Data . Value as CraftedItemData ;
}
public EquippableItemData GetData ( EquippableItem item )
{
return item . Data . Value as EquippableItemData ;
}
2020-03-22 19:05:26 +00:00
public float GetMass ( ItemInstance item )
{
2021-04-18 05:13:20 +01:00
return item switch
2020-03-22 19:05:26 +00:00
{
2026-08-10 14:57:50 +01:00
CraftedItemInstance _ = > item . Data . Value . Mass ,
SimpleCommodity commodity = > item . Data . Value . Mass * commodity . Quantity ,
2021-04-18 05:13:20 +01:00
_ = > 0
} ;
2020-03-22 19:05:26 +00:00
}
2020-06-05 04:16:33 +01:00
public float GetThermalMass ( ItemInstance item )
2020-03-22 19:05:26 +00:00
{
2026-08-10 14:57:50 +01:00
var data = item . Data . Value ;
2021-04-18 05:13:20 +01:00
return item switch
2020-03-22 19:05:26 +00:00
{
2026-08-10 14:57:50 +01:00
CraftedItemInstance _ = > data . Mass * data . SpecificHeat ,
SimpleCommodity commodity = > data . Mass * data . SpecificHeat * commodity . Quantity ,
2021-04-18 05:13:20 +01:00
_ = > 0
} ;
2020-03-22 19:05:26 +00:00
}
// Returns stat when not equipped
2020-12-10 23:20:38 +00:00
public float Evaluate ( PerformanceStat stat , EquippableItem item )
2020-03-22 19:05:26 +00:00
{
2026-08-10 14:57:50 +01:00
var data = GetData ( item ) ;
2026-09-12 01:12:12 +02:00
var quality = pow ( item . QualityForRole ( stat . FromRole ) , stat . QualityExponent ) ;
2026-08-10 14:57:50 +01:00
var durabilityExponent = lerp (
2021-03-22 05:22:31 +00:00
GameplaySettings . DurabilityQualityMin ,
GameplaySettings . DurabilityQualityMax ,
2026-08-10 14:57:50 +01:00
pow ( item . Quality , GameplaySettings . DurabilityQualityExponent ) ) ;
var durability = pow ( item . Durability / data . Durability , durabilityExponent * stat . DurabilityExponentMultiplier ) ;
var result = lerp ( stat . Min , stat . Max , quality * durability ) ;
2021-03-27 10:03:24 +00:00
if ( float . IsNaN ( result ) )
2026-08-10 14:57:50 +01:00
throw new InvalidOperationException ( $"Performance Stat on {data.Name} evaluating as NaN: input data is invalid! Durability: {item.Durability} / {data.Durability}" ) ;
2020-03-22 19:05:26 +00:00
return result ;
2021-03-22 05:22:31 +00:00
2020-03-22 19:05:26 +00:00
}
2020-05-25 03:05:30 +01:00
2021-03-20 04:20:17 +00:00
public int GetPrice ( CraftedItemInstance item )
{
2026-08-10 14:57:50 +01:00
var data = GetData ( item ) ;
return ( int ) ( GameplaySettings . QualityPriceModifier . Evaluate ( item . Quality ) * data . Price ) ;
2026-06-14 05:56:41 +01:00
}
2026-08-10 14:57:50 +01:00
public SimpleCommodity CreateInstance ( SimpleCommodityData item , int count )
2020-03-22 19:05:26 +00:00
{
2026-08-10 14:57:50 +01:00
if ( item ! = null )
2020-06-02 07:01:03 +01:00
{
2026-08-10 14:57:50 +01:00
var newItem = new SimpleCommodity
2020-03-22 19:05:26 +00:00
{
2026-08-10 14:57:50 +01:00
Data = new DatabaseLink < ItemData > { LinkID = item . ID } ,
2020-05-25 03:05:30 +01:00
Quantity = count
2020-03-22 19:05:26 +00:00
} ;
2026-08-10 14:57:50 +01:00
//ItemData.Add(newItem);
return newItem ;
2020-06-02 07:01:03 +01:00
}
2026-08-10 14:57:50 +01:00
_logger ( "Attempted to create Simple Commodity instance using missing or incorrect item id" ) ;
2020-04-09 07:48:45 +01:00
return null ;
2020-03-22 19:05:26 +00:00
}
2021-01-19 01:22:56 +00:00
public ItemInstance Instantiate ( ItemInstance item )
{
2026-08-10 14:57:50 +01:00
var data = item . Data . Value ;
if ( data is CraftedItemData c )
2021-01-27 00:05:30 +00:00
{
2026-08-10 14:57:50 +01:00
var i = CreateInstance ( c ) ;
i . Rotation = item . Rotation ;
return i ;
2021-01-27 00:05:30 +00:00
}
2026-08-10 14:57:50 +01:00
if ( item is SimpleCommodity s )
2021-01-27 00:05:30 +00:00
{
2026-08-10 14:57:50 +01:00
var i = CreateInstance ( data as SimpleCommodityData , s . Quantity ) ;
i . Rotation = item . Rotation ;
return i ;
}
return null ;
2021-01-19 01:22:56 +00:00
}
2021-03-27 10:03:24 +00:00
2026-08-10 14:57:50 +01:00
public CraftedItemInstance CreateInstance ( CraftedItemData item , float quality )
2021-03-27 10:03:24 +00:00
{
2026-08-10 14:57:50 +01:00
if ( item is EquippableItemData equippableItemData )
2026-06-14 07:41:32 +01:00
{
2026-08-10 14:57:50 +01:00
return new EquippableItem
2021-03-27 10:03:24 +00:00
{
2026-08-10 14:57:50 +01:00
Data = new DatabaseLink < ItemData > { LinkID = item . ID } , Quality = quality , Durability = equippableItemData . Durability
2021-03-27 10:03:24 +00:00
} ;
}
2026-08-10 14:57:50 +01:00
var newCommodity = new CompoundCommodity
2021-03-27 10:03:24 +00:00
{
2026-08-10 14:57:50 +01:00
Data = new DatabaseLink < ItemData > { LinkID = item . ID } ,
2021-03-27 10:03:24 +00:00
Quality = quality
} ;
2026-08-10 14:57:50 +01:00
return newCommodity ;
2026-06-14 07:41:32 +01:00
}
2026-08-10 14:57:50 +01:00
public CraftedItemInstance CreateInstance ( CraftedItemData item )
2026-06-14 07:27:23 +01:00
{
2026-08-10 14:57:50 +01:00
if ( item = = null )
2026-06-14 07:27:23 +01:00
{
2026-08-10 14:57:50 +01:00
throw new NullReferenceException ( "Attempted to create crafted item instance using missing or incorrect item data!" ) ;
return null ;
2026-06-14 07:27:23 +01:00
}
2021-03-08 02:57:38 +00:00
var quality = Random . NextFloat ( ) ;
var tier = GameplaySettings . Tiers [ 0 ] ;
foreach ( var t in GameplaySettings . Tiers )
{
if ( t . Rarity > quality )
tier = t ;
}
2020-03-22 19:05:26 +00:00
2026-08-10 14:57:50 +01:00
return CreateInstance ( item , tier . Quality ) ;
2026-06-14 07:41:32 +01:00
}
2026-09-12 01:12:12 +02:00
// Builds one of a manufacturer's branded products: the unit's own workmanship rolled as before, and each of
// the design's roles filled with a part whose quality is drawn from that manufacturer's distribution for it.
public CraftedItemInstance CreateInstance ( FactionProductData product )
{
var design = ItemData . Get < CraftedItemData > ( product . Design ) ;
if ( design = = null )
{
_logger ( $"Product {product.Name} names a design that does not exist!" ) ;
return null ;
}
var instance = CreateInstance ( design ) ;
instance . Product = product . ID ;
if ( design . Roles = = null ) return instance ;
foreach ( var role in design . Roles )
{
var build = product . Roles ? . FirstOrDefault ( b = > b . Role = = role . Name ) ? ? new ProductRole ( ) ;
instance . Ingredients . Add ( new RoleFill
{
Role = role . Name ,
Quality = clamp ( Random . NextGaussian ( build . Mean , build . StandardDeviation ) , . 01f , 1 )
} ) ;
}
return instance ;
}
2021-03-08 02:57:38 +00:00
public ( RarityTier tier , int upgrades ) GetTier ( CraftedItemInstance item )
{
var tier = GameplaySettings . Tiers [ 0 ] ;
foreach ( var t in GameplaySettings . Tiers )
if ( item . Quality + . 001f > t . Quality )
tier = t ;
int upgrades = ( int ) ( ( item . Quality - tier . Quality ) / . 0499f ) ;
return ( tier , upgrades ) ;
}
2026-08-10 14:57:50 +01:00
}