Untitled
plain_text
a month ago
7.3 kB
8
Indexable
Never
using System; using System.Collections.Generic; using System.Threading.Tasks; using TMPro; using Unity.Burst; using Unity.Collections; using Unity.Jobs; using UnityEngine; using UnityEngine.UI; using Random = UnityEngine.Random; public class TestMono : MonoBehaviour { [NonSerialized] private NativeArray<CombatPairJob> jobArr; [NonSerialized] private CombatPair[] normalTaskArr; private List<Task> taskListToWait; public TextMeshProUGUI labelTMP; public Button nexButton; public int counterForLoop;// 7mil public DoItLikeThis myCurrSetting; public float importantInt; public enum DoItLikeThis { tasks, jobs, //tasksButStupid, } private void Start() { jobArr = new NativeArray<CombatPairJob>(counterForLoop, Allocator.Persistent); normalTaskArr = new CombatPair[counterForLoop]; taskListToWait = new List<Task>(); var r = Unity.Mathematics.Random.CreateFromIndex(0).NextFloat(0,100); for (int i = 0; i < counterForLoop; i++) { Defender d = new Defender(); d.armor = Random.Range(0, 100); d.health = Random.Range(1, 100000); d.weaknessToTypeOfAttack = (AttackType) Mathf.FloorToInt(Random.Range(0, 2)); normalTaskArr[i].defender = d; Attacker a = new Attacker(); a.damage = Random.Range(0, 200); a.critMultiplier = Random.Range(0, 3); a.hitChance = Random.Range(0, 100); a.damageType = (AttackType) Mathf.FloorToInt(Random.Range(0, 2)); normalTaskArr[i].attacker = a; CombatPairJob pair = new CombatPairJob(); pair.defender = d; pair.attacker = a; jobArr[i] = pair; } nexButton.onClick.AddListener(ButtonWasPressedGoNext); labelTMP.SetText(myCurrSetting.ToString()); } private void ButtonWasPressedGoNext() { //just switches current enum for which method to run in update myCurrSetting++; if ((int) myCurrSetting > (Enum.GetValues(typeof(DoItLikeThis)).Length - 1)) { myCurrSetting = 0; } labelTMP.SetText(myCurrSetting.ToString()); } private void Update() { switch (myCurrSetting) { case DoItLikeThis.tasks: DoTasks(); break; case DoItLikeThis.jobs: DoJobs(); break; } } private void SimpleTaskExecution(Span<CombatPair> span) { for (int i = 0; i < span.Length; i++) { ref var pair = ref span[i]; pair.DoAttackCalculation(); } } private void StupidTask(int i) { ref var pair = ref normalTaskArr[i]; pair.DoAttackCalculation(); } private void DoTasksStupid() { taskListToWait.Clear(); for (int i = 0; i < counterForLoop; i++) { int iterator = i; Task t = new Task(()=>StupidTask(iterator)); taskListToWait.Add(t); t.Start(); } Task.WaitAll(taskListToWait.ToArray()); float totalHp = 0; for (var i = 0; i < normalTaskArr.Length; i++) { ref var pair = ref normalTaskArr[i]; totalHp += pair.defender.health; } Debug.Log(totalHp); } private void DoTasks() { taskListToWait.Clear(); int counter = counterForLoop; while (counter >= 0) { int start = counter; counter -= 10000; int end = counter; if (end < 0) { end = 0; } var trueLen = start - end; Task t = new Task(()=>SimpleTaskExecution(normalTaskArr.AsSpan(end, trueLen))); t.Start(); taskListToWait.Add(t); } Task.WaitAll(taskListToWait.ToArray()); float totalHp = 0; for (var i = 0; i < normalTaskArr.Length; i++) { ref var pair = ref normalTaskArr[i]; totalHp += pair.defender.health; } importantInt = totalHp; Debug.Log(importantInt); } public void DoJobs() { ParalelJobThing paralelJob = new ParalelJobThing(); paralelJob.nativeArray = jobArr; JobHandle handle = paralelJob.Schedule(counterForLoop, 10000); handle.Complete(); float totalHealt = 0; for (int i = 0; i < counterForLoop; i++) { var pair = jobArr[i]; totalHealt += pair.defender.health; } importantInt = totalHealt; Debug.Log(importantInt); } public struct CombatPair { public Defender defender; public Attacker attacker; [BurstCompile] public void DoAttackCalculation() { var r = Unity.Mathematics.Random.CreateFromIndex(0).NextFloat(0,100); if (r < attacker.hitChance) { float critMultiplier = 1; if (defender.weaknessToTypeOfAttack == attacker.damageType) { critMultiplier = attacker.critMultiplier; } var dmg = attacker.damage * critMultiplier; float armorReduction = 0.006f * defender.armor / 1 + 0.06f * Mathf.Abs(defender.armor); var totalDamage = dmg * armorReduction; defender.health -= totalDamage; } } } public struct ParalelJobThing : IJobParallelFor { public NativeArray<CombatPairJob> nativeArray; public void Execute(int index) { var structurino = nativeArray[index]; structurino.Execute(); nativeArray[index]=structurino; } } public struct CombatPairJob : IJob { public Defender defender; public Attacker attacker; [BurstCompile] public void DoAttackCalculation() { var r = Unity.Mathematics.Random.CreateFromIndex(0).NextFloat(0,100); if (r < attacker.hitChance) { float critMultiplier = 1; if (defender.weaknessToTypeOfAttack == attacker.damageType) { critMultiplier = attacker.critMultiplier; } var dmg = attacker.damage * critMultiplier; float armorReduction = 0.006f * defender.armor / 1 + 0.06f * Mathf.Abs(defender.armor); var totalDamage = dmg * armorReduction; defender.health -= totalDamage; } } public void Execute() { DoAttackCalculation(); } } public struct Defender { public float armor; public float health; public AttackType weaknessToTypeOfAttack; } public struct Attacker { public float damage; public AttackType damageType; public float hitChance; public float critMultiplier; } public enum AttackType { fire, ice, pierce } }