Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Assets.Resources.Scripts;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public enum MagicPropertyType
- {
- None,
- ExtraDamage_Fire,
- ExtraDamage_Cold,
- ExtraDamage_Shock,
- ExtraDamage_Poison,
- ExtraDamage_Arcane,
- ExtraDamage_Shadow,
- Resist_All,
- Resist_Fire,
- Resist_Cold,
- Resist_Shock,
- Resist_Poison,
- Resist_Arcane,
- Resist_Shadow,
- CriticalRate,
- CarryWeight,
- DamageVs_Humans,
- DamageVs_Undead,
- DamageVs_Demons,
- DamageVs_Beasts,
- Steal_Mana,
- Steal_Stamina,
- Steal_Life,
- StatMod_Strength, // Damage with melee weapons
- StatMod_Dexterity, // Damage with ranged weapons
- StatMod_Endurance, // Carry Weight, Stamina Regen
- StatMod_Vitality, // Health
- StatMod_Intellect, // Damage with spells
- StatMod_Wisdom, // Mana Regen
- StatMod_Spirit, // Void Timer, all stats in small way.
- }
- public class LootGeneratorArgs
- {
- public BasicRange commonChance;
- public BasicRange uncommonChance;
- public BasicRange rareChance;
- public BasicRange epicChance;
- public BasicRange legendaryChance;
- public GameItemType[] targets;
- public LootGeneratorArgs()
- {
- }
- public LootGeneratorArgs(BasicRange commonChance, BasicRange uncommonChance, BasicRange rareChance,
- BasicRange epicChance, BasicRange legendaryChance, GameItemType[] targets)
- {
- this.commonChance = commonChance;
- this.uncommonChance = uncommonChance;
- this.rareChance = rareChance;
- this.epicChance = epicChance;
- this.legendaryChance = legendaryChance;
- this.targets = targets;
- }
- }
- public class GameItemGenerator
- {
- private GameItemNameGenerator _nameGenerator;
- private VoidwalkerRandom _random;
- private List<Enchantment> _enchantments;
- private Dictionary<string, MagicPropertyType> _magicPropertyTypeConverter;
- private Dictionary<string, GameItemType> _gameItemTypeConverter;
- //public Dictionary<GameItemType, Sprite[]> _gameItemSprites;
- public GameItemGenerator()
- {
- _nameGenerator = new GameItemNameGenerator();
- _random = new VoidwalkerRandom();
- InitializeMagicProperties();
- //InitializeGameItemSprites();
- }
- //private void InitializeGameItemSprites()
- //{
- // object[] swordSprites = Resources.LoadAll("Graphics/UI/GameItems/Weapons/Swords", typeof(Sprite));
- // _gameItemSprites = new Dictionary<GameItemType, Sprite[]>
- // {
- // { GameItemType.Weapon_Sword, ConvertResourceToSpriteArray(swordSprites) }
- // };
- //}
- private Sprite[] ConvertResourceToSpriteArray(object[] resource)
- {
- List<Sprite> sprites = new List<Sprite>();
- foreach (object item in resource)
- {
- sprites.Add((Sprite)item);
- }
- return sprites.ToArray();
- }
- private void InitializeMagicProperties()
- {
- _magicPropertyTypeConverter = new Dictionary<string, MagicPropertyType>
- {
- {"ExtraDamage_Fire",MagicPropertyType.ExtraDamage_Fire },
- {"ExtraDamage_Cold",MagicPropertyType.ExtraDamage_Cold },
- {"ExtraDamage_Shock",MagicPropertyType.ExtraDamage_Shock },
- {"ExtraDamage_Poison",MagicPropertyType.ExtraDamage_Poison },
- {"ExtraDamage_Arcane",MagicPropertyType.ExtraDamage_Arcane },
- {"ExtraDamage_Shadow",MagicPropertyType.ExtraDamage_Shadow },
- {"Resist_All",MagicPropertyType.Resist_All },
- {"Resist_Fire",MagicPropertyType.Resist_Fire },
- {"Resist_Cold",MagicPropertyType.Resist_Cold },
- {"Resist_Shock",MagicPropertyType.Resist_Shock },
- {"Resist_Poison",MagicPropertyType.Resist_Poison },
- {"Resist_Arcane",MagicPropertyType.Resist_Arcane },
- {"Resist_Shadow",MagicPropertyType.Resist_Shadow },
- {"CriticalRate",MagicPropertyType.CriticalRate },
- {"CarryWeight",MagicPropertyType.CarryWeight },
- {"DamageVs_Humans",MagicPropertyType.DamageVs_Humans },
- {"DamageVs_Undead",MagicPropertyType.DamageVs_Undead },
- {"DamageVs_Demons",MagicPropertyType.DamageVs_Demons },
- {"DamageVs_Beasts",MagicPropertyType.DamageVs_Beasts },
- {"Steal_Mana",MagicPropertyType.Steal_Mana },
- {"Steal_Stamina",MagicPropertyType.Steal_Stamina },
- {"Steal_Health",MagicPropertyType.Steal_Life },
- {"StatMod_Strength",MagicPropertyType.StatMod_Strength },
- {"StatMod_Dexterity",MagicPropertyType.StatMod_Strength },
- {"StatMod_Endurance",MagicPropertyType.StatMod_Strength },
- {"StatMod_Vitality",MagicPropertyType.StatMod_Strength },
- {"StatMod_Intellect",MagicPropertyType.StatMod_Strength },
- {"StatMod_Wisdom",MagicPropertyType.StatMod_Strength },
- {"StatMod_Spirit",MagicPropertyType.StatMod_Strength },
- };
- _gameItemTypeConverter = new Dictionary<string, GameItemType>
- {
- { "Daggers", GameItemType.Weapon_Dagger },
- { "Bows", GameItemType.Weapon_Bow },
- { "Knuckles", GameItemType.Weapon_Knuckle },
- { "Staves", GameItemType.Weapon_Staff },
- { "Wands", GameItemType.Weapon_Wand },
- { "Swords", GameItemType.Weapon_Sword },
- { "Greatswords", GameItemType.Weapon_Greatsword },
- { "Axes", GameItemType.Weapon_Axe },
- { "Greataxes", GameItemType.Weapon_Greataxe },
- { "Maces", GameItemType.Weapon_Mace },
- { "Greatmaces", GameItemType.Weapon_Greatmace },
- { "Shields", GameItemType.Shield },
- { "Light_Headwear", GameItemType.Light_Headwear },
- { "Light_Chestwear", GameItemType.Light_Chestwear },
- { "Light_Handwear", GameItemType.Light_Handwear },
- { "Light_Footwear", GameItemType.Light_Footwear },
- { "Medium_Headwear", GameItemType.Medium_Headwear },
- { "Medium_Chestwear", GameItemType.Medium_Chestwear },
- { "Medium_Handwear", GameItemType.Medium_Handwear },
- { "Medium_Footwear", GameItemType.Medium_Footwear },
- { "Heavy_Headwear", GameItemType.Heavy_Headwear },
- { "Heavy_Chestwear", GameItemType.Heavy_Chestwear },
- { "Heavy_Handwear", GameItemType.Heavy_Handwear },
- { "Heavy_Footwear", GameItemType.Heavy_Footwear },
- { "Amulets", GameItemType.Jewelry_Amulet },
- { "Rings", GameItemType.Jewelry_Ring },
- };
- /**
- * Load Enchantments.txt
- */
- this._enchantments = new List<Enchantment>();
- TextAsset mytxtData = Resources.Load<TextAsset>("Data/Enchantments");
- string[] magicPropertyData = mytxtData.text.Split("\n");
- foreach (string line in magicPropertyData)
- {
- string[] lineData = line.Split("\t");
- string[] constraints = RemoveFormatting(lineData[7]).Split(",");
- Enchantment template = new Enchantment()
- {
- identifier = lineData[0],
- name = lineData[1],
- prefix = lineData[2],
- suffix = lineData[3],
- family = lineData[4],
- level = Int32.Parse(lineData[5]),
- weight = Int32.Parse(lineData[6]),
- constraints = ParseGameItemConstraints(constraints),
- description = lineData[8],
- };
- /**
- * Load Magic Properties
- */
- List<EnchantmentProperty> effects = new List<EnchantmentProperty>();
- string[] properties = RemoveFormatting(lineData[9]).Split(";"); // ; is the delimiter for multiple properties
- foreach (string property in properties)
- {
- EnchantmentProperty enchantmentProperty = new EnchantmentProperty();
- string[] fields = property.Split(",");
- enchantmentProperty.property = ParseMagicPropertyType(fields[0]);
- enchantmentProperty.chance = Int32.Parse(fields[1]);
- enchantmentProperty.value = Int32.Parse(fields[2]);
- effects.Add(enchantmentProperty);
- }
- template.effects = effects.ToArray();
- this._enchantments.Add(template);
- }
- }
- public static string RemoveFormatting(string pString)
- {
- return pString.Replace("\n", "").Replace("\r", "");
- }
- private MagicPropertyType ParseMagicPropertyType(string property)
- {
- return _magicPropertyTypeConverter[property];
- }
- private GameItemType[] ParseGameItemConstraints(string[] properties)
- {
- List<GameItemType> propertyTypes = new List<GameItemType>();
- foreach (string propertyType in properties)
- {
- propertyTypes.Add(ParseGameItemConstraint(propertyType));
- }
- return propertyTypes.ToArray();
- }
- private GameItemType ParseGameItemConstraint(string constraint)
- {
- return _gameItemTypeConverter[constraint];
- }
- /// <summary>
- /// TODO: Finish adding this functionality.
- /// </summary>
- /// <param name="level"></param>
- /// <returns></returns>
- public GameItem Generate(int level, LootGeneratorArgs args)
- {
- if (args != null)
- {
- GameItemRarity rarity = GenerateItemRarity(args);
- if (args.targets != null && args.targets.Length > 0)
- {
- GameItemType target = _random.Choose(args.targets);
- if (target.IsLootGeneratorTarget())
- {
- GameItem template = Generate(target, level, rarity);
- return template;
- }
- }
- }
- return null;
- }
- public GameItemRarity GenerateItemRarity(LootGeneratorArgs args)
- {
- if (args != null)
- {
- if (_random.NextDouble() <= args.legendaryChance.ToRatio())
- {
- return GameItemRarity.Legendary;
- }
- if (_random.NextDouble() <= args.epicChance.ToRatio())
- {
- return GameItemRarity.Epic;
- }
- if (_random.NextDouble() <= args.rareChance.ToRatio())
- {
- return GameItemRarity.Rare;
- }
- if (_random.NextDouble() <= args.uncommonChance.ToRatio())
- {
- return GameItemRarity.Uncommon;
- }
- if (_random.NextDouble() <= args.commonChance.ToRatio())
- {
- return GameItemRarity.Common;
- }
- }
- return GameItemRarity.None;
- }
- public GameItem Generate(GameItemType itemType, int level, GameItemRarity rarity)
- {
- GameItem template = new GameItem();
- template.baseItemQuality = rarity;
- template.itemType = itemType;
- template.itemLevel = level;
- //template.itemIcon = _random.Choose(_gameItemSprites[itemType]);
- template.identifier = "ProcGen_" + level + "_" + itemType;
- template.itemName = _nameGenerator.Generate(itemType, rarity);
- //if (rarity == GameItemRarity.Uncommon || rarity == GameItemRarity.Rare || rarity == GameItemRarity.Epic)
- //{
- // int affixCount = GenerateAffixCount(rarity);
- // List<Enchantment> properties = new List<Enchantment>();
- // properties.AddRange(GenerateEnchantments(itemType, level, affixCount));
- // //properties = properties.OrderBy(property => property.propertyType).ToList();
- // template.enchantments = properties;
- //}
- return template;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="constraint"></param>
- /// <param name="level"></param>
- /// <param name="count"></param>
- /// <param name="allowDuplicates"></param>
- /// <returns></returns>
- //private List<Enchantment> GenerateEnchantments(GameItemType constraint, int level, int count)
- //{
- // List<Enchantment> results = new List<Enchantment>();
- // if (count == 0)
- // {
- // return results;
- // }
- // int totalFitness = 0;
- // List<Enchantment> filteredTemplates = new List<Enchantment>();
- // foreach (Enchantment candidate in this._enchantments)
- // {
- // if (candidate.constraints.Contains(constraint))
- // {
- // if (candidate.level <= level)
- // {
- // filteredTemplates.Add(candidate);
- // totalFitness += candidate.weight;
- // }
- // }
- // }
- // if (filteredTemplates.Count < count)
- // {
- // count = filteredTemplates.Count;
- // if (count == 0)
- // {
- // return results;
- // }
- // }
- // filteredTemplates.OrderBy(enchantment => enchantment.weight).Reverse().ToList();
- // int templatesGenerated = 0;
- // bool isSearching = true;
- // int attempts = 0;
- // const int maximumAttempts = 32; // Prevent Infinite Loops
- // while (isSearching)
- // {
- // Enchantment selectedTemplate = null;
- // int randomSample = _random.NextInteger(totalFitness);
- // foreach (Enchantment attempt in filteredTemplates)
- // {
- // if (randomSample < attempt.weight)
- // {
- // selectedTemplate = attempt;
- // break;
- // }
- // randomSample -= attempt.weight;
- // }
- // if (results.Any(e => e.family == selectedTemplate.family) == false)
- // {
- // attempts = 0;
- // results.Add(selectedTemplate);
- // templatesGenerated++;
- // }
- // attempts++;
- // if (templatesGenerated == count || attempts >= maximumAttempts)
- // {
- // isSearching = false;
- // }
- // }
- // return results;
- //}
- private List<Enchantment> GenerateEnchantments(GameItemType constraint, int level, int count)
- {
- List<Enchantment> results = new List<Enchantment>();
- if (count == 0)
- {
- return results;
- }
- // Filtered templates directly
- List<Enchantment> filteredTemplates = this._enchantments
- .Where(candidate => candidate.constraints.Contains(constraint) && candidate.level <= level)
- .OrderByDescending(candidate => candidate.weight)
- .ToList();
- int totalFitness = filteredTemplates.Sum(candidate => candidate.weight);
- // Limit count if filtered templates count is less
- count = Math.Min(count, filteredTemplates.Count);
- // Randomize selection
- int templatesGenerated = 0;
- while (templatesGenerated < count && results.Count < filteredTemplates.Count && filteredTemplates.Any())
- {
- int randomSample = _random.NextInteger(totalFitness);
- foreach (Enchantment attempt in filteredTemplates.ToList())
- {
- if (randomSample < attempt.weight && !results.Any(e => e.family == attempt.family))
- {
- results.Add(attempt);
- templatesGenerated++;
- filteredTemplates.Remove(attempt);
- break;
- }
- randomSample -= attempt.weight;
- }
- }
- return results;
- }
- private int GenerateAffixCount(GameItemRarity quality)
- {
- switch (quality)
- {
- case GameItemRarity.Uncommon:
- return 1;
- case GameItemRarity.Rare:
- return _random.NextRange(1, 2);
- case GameItemRarity.Epic:
- return _random.NextRange(2, 3);
- default:
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement