예제 #1
0
        /// <summary>
        /// Returns the load data for the load case.
        /// </summary>
        /// <exception cref="CSiException"><see cref="CSiApiBase.API_DEFAULT_ERROR_CODE" /></exception>
        public void FillLoads()
        {
            if (_responseSpectrum == null)
            {
                return;
            }
            _responseSpectrum.GetLoads(Name,
                                       out var loadDirections,
                                       out var functions,
                                       out var scaleFactor,
                                       out var coordinateSystems,
                                       out var angles);

            for (int i = 0; i < loadDirections.Length; i++)
            {
                LoadResponseSpectrum loadFunction = new LoadResponseSpectrum()
                {
                    Function         = functions[i],
                    ScaleFactor      = scaleFactor[i],
                    CoordinateSystem = coordinateSystems[i],
                    Angle            = angles[i],
                    Direction        = loadDirections[i]
                };
                Loads.Add(loadFunction);
            }
        }
예제 #2
0
    public Corner(Vector3Int index, Grid3d grid)
    {
        _grid    = grid;
        Index    = index;
        Position = grid.Corner + new Vector3(index.x, index.y, index.z) * grid.VoxelSize;

        Location    = new Point(Position.x, Position.z, Position.y);
        Constraints = index.y == 0 ? Constraint.Fixed : Constraint.RotationFixed;
        Loads.Add(new NodalLoad(new Force(0, 0, -2000000, 0, 0, 0)));
    }
예제 #3
0
        public RepositoryQuery <T> Load(params string[] navProperty)
        {
            if (navProperty == null)
            {
                throw new ArgumentNullException("navProperty");
            }
            foreach (var expression in navProperty)
            {
                Loads.Add(expression);
            }

            return(this);
        }
예제 #4
0
 internal void Parse(string pattern, Func <string, string> stripFunc)
 {
     foreach (var line in Content.Where(l => l.StartsWith(pattern)).ToList())
     {
         var dirPath = new FileInfo(FilePath).Directory ?? new DirectoryInfo(Directory.GetCurrentDirectory());
         var path    = Path.Combine(dirPath.FullName, stripFunc.Invoke(line));
         Loads.Add(Content.IndexOf(line), new ScriptContent(path));
     }
     foreach (var load in Loads)
     {
         load.Value.Parse(pattern, stripFunc);
     }
 }
예제 #5
0
        public static LoadSaveResult LoadLoadouts(string filename = "loads.json")
        {
            try
            {
                string text   = File.ReadAllText(filename);
                var    lloads = JsonConvert.DeserializeObject <Loadout[]>(text);
                Loads.Clear();

                foreach (var load in lloads)
                {
                    if (load.RuneIDs != null)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            var ids = load.RuneIDs[i];
                            load.Runes[i] = Program.Data.Runes.FirstOrDefault(r => r.Id == ids);
                            if (load.Runes[i] != null)
                            {
                                load.Runes[i].UsedInBuild = true;
                                if (load.HasManageStats)
                                {
                                    foreach (var ms in load.ManageStats[i])
                                    {
                                        load.Runes[i].ManageStats.AddOrUpdate(ms.Key, ms.Value, (s, d) => ms.Value);
                                    }
                                }
                            }
                        }
                    }
                    load.Shrines = Data.Shrines;
                    Loads.Add(load);
                }
                return(LoadSaveResult.Success);
            }
            catch (Exception e)
            {
                LineLog.Error($"Error while loading loads {e.GetType()}", e);
                //MessageBox.Show("Error occurred loading Save JSON.\r\n" + e.GetType() + "\r\nInformation is saved to error_save.txt");
                File.WriteAllText("error_loads.txt", e.ToString());
                throw;
            }
        }
예제 #6
0
 /// <summary>
 /// Add the specified object to the selection, if possible
 /// (i.e. if it is of a selectable type and does not already exist within the current selection)
 /// </summary>
 /// <param name="mObject">The object to select</param>
 /// <returns>True if the item was successfully added to the selection</returns>
 public bool Select(ModelObject mObject)
 {
     if (mObject == null)
     {
         return(false);
     }
     else if (mObject is Element)
     {
         return(Elements.Add((Element)mObject));
     }
     else if (mObject is Node)
     {
         return(Nodes.Add((Node)mObject));
     }
     else if (mObject is Load)
     {
         return(Loads.Add((Load)mObject));
     }
     else if (mObject is SectionFamily)
     {
         return(SectionFamilies.Add((SectionFamily)mObject));
     }
     else if (mObject is BuildUpFamily)
     {
         return(BuildUpFamilies.Add((BuildUpFamily)mObject));
     }
     else if (mObject is Material)
     {
         return(Materials.Add((Material)mObject));
     }
     else if (mObject is ModelObjectSetBase)
     {
         return(Sets.Add((ModelObjectSetBase)mObject));
     }
     else
     {
         return(false);
     }
 }
예제 #7
0
파일: FeModel.cs 프로젝트: scudelari/EMASA
        /// <summary>
        /// Creates the abstraction of the FeModel.
        /// </summary>
        /// <param name="inSolPoint">The solution point that contain the Gh Geometry that will define the model. Usually the model is then saved into the its FeModel parameter</param>
        public FeModel([NotNull] NlOpt_Point inSolPoint)
        {
            try
            {
                if (inSolPoint == null)
                {
                    throw new ArgumentNullException(nameof(inSolPoint));
                }

                Owner = inSolPoint;

                // Generates the model
                //////////////////////////

                // For each point list GH Geometry Parameter - Adds the joint
                foreach (PointList_GhGeom_ParamDef pointList_Output_ParamDef in inSolPoint.GhGeom_Values.Keys.OfType <PointList_GhGeom_ParamDef>())
                {
                    FeGroup grp = AddNewOrGet_Group(pointList_Output_ParamDef.FeGroupNameHelper);

                    List <Point3d> pPoints = (List <Point3d>)inSolPoint.GhGeom_Values[pointList_Output_ParamDef];

                    foreach (Point3d p in pPoints)
                    {
                        FeJoint j = AddNewOrGet_JointByCoordinate(p);
                        j.Restraint.IncorporateRestraint(pointList_Output_ParamDef.Restraint);
                        grp.AddElement(j);
                    }
                }

                // For each line list GH Geometry Parameter - Adds the joints and a frame
                foreach (LineList_GhGeom_ParamDef lineList_Output_ParamDef in inSolPoint.GhGeom_Values.Keys.OfType <LineList_GhGeom_ParamDef>())
                {
                    FeGroup grp = AddNewOrGet_Group(lineList_Output_ParamDef.FeGroupNameHelper);

                    List <Line> pLines = (List <Line>)inSolPoint.GhGeom_Values[lineList_Output_ParamDef];

                    foreach (Line l in pLines)
                    {
                        // Adds the From joint
                        FeJoint jFrom = AddNewOrGet_JointByCoordinate(l.From);
                        jFrom.Restraint.IncorporateRestraint(lineList_Output_ParamDef.Restraint);
                        grp.AddElement(jFrom);

                        // Adds the To joint
                        FeJoint jTo = AddNewOrGet_JointByCoordinate(l.To);
                        jTo.Restraint.IncorporateRestraint(lineList_Output_ParamDef.Restraint);
                        grp.AddElement(jTo);

                        // Adds the Frame
                        FeSection s = Owner.Owner.GetGhLineListSection(lineList_Output_ParamDef);
                        FeFrame   f = AddNewOrGet_LineByCoordinate(jFrom, jTo, s);
                        grp.AddElement(f);
                    }
                }

                // Adds the gravity loads
                if (AppSS.I.FeOpt.Gravity_IsLoad)
                {
                    FeLoad_Inertial gravity = FeLoad_Inertial.GetStandardGravity(AppSS.I.FeOpt.Gravity_Multiplier);
                    // Sets the direction based on the options
                    switch (AppSS.I.FeOpt.Gravity_DirectionEnum_Selected)
                    {
                    case MainAxisDirectionEnum.xPos:
                        gravity.Direction = Vector3d.XAxis;
                        break;

                    case MainAxisDirectionEnum.xNeg:
                        gravity.Direction = -Vector3d.XAxis;
                        break;

                    case MainAxisDirectionEnum.yPos:
                        gravity.Direction = Vector3d.YAxis;
                        break;

                    case MainAxisDirectionEnum.yNeg:
                        gravity.Direction = -Vector3d.YAxis;
                        break;

                    case MainAxisDirectionEnum.zPos:
                        gravity.Direction = Vector3d.ZAxis;
                        break;

                    case MainAxisDirectionEnum.zNeg:
                        gravity.Direction = -Vector3d.ZAxis;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    Loads.Add(gravity);
                }

                // Adds the point loads
                if (AppSS.I.FeOpt.PointLoads.Count > 0)
                {
                    Loads.AddRange(AppSS.I.FeOpt.PointLoads);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"Error defining the FeModel internal class.", e);
            }
        }
예제 #8
0
		public BuildResult GenBuilds(string prefix = "") {
			if (Type == BuildType.Lock) {
				Best = new Monster(Mon, true);
				return BuildResult.Success;
			}
			else if (Type == BuildType.Link) {
				if (LinkBuild == null) {
					for (int i = 0; i < 6; i++)
						runes[i] = new Rune[0];
					return BuildResult.Failure;
				}
				else {
					CopyFrom(LinkBuild);
				}
			}

			if (runes.Any(r => r == null)) {
				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Null rune"));
				return BuildResult.BadRune;
			}
			if (!BuildSaveStats)
				BuildGoodRunes = false;

			if (!BuildGoodRunes) {
				RuneUsage = new RuneUsage();
				BuildUsage = new BuildUsage();
			}
			try {
				// if to get awakened
				if (DownloadAwake && !Mon.downloaded) {
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Downloading Awake def"));
					var mref = MonsterStat.FindMon(Mon);
					if (mref != null) {
						// download the current (unawakened monster)
						var mstat = mref.Download();
						BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Reading stats"));
						// if the retrieved mon is unawakened, get the awakened
						if (!mstat.Awakened && mstat.AwakenTo != null) {
							BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Awakening"));
							Mon = mstat.AwakenTo.Download().GetMon(Mon);
						}
					}
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Downloaded"));
				}
				// getting awakened also gets level 40, so...
				// only get lvl 40 stats if the monster isn't 40, wants to download AND isn't already downloaded (first and last are about the same)
				else if (Mon.level < 40 && DownloadStats && !Mon.downloaded) {
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Downloading 40 def"));
					var mref = MonsterStat.FindMon(Mon);
					if (mref != null)
						Mon = mref.Download().GetMon(Mon);
				}
			}
			catch (Exception e) {
				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Failed downloading def: " + e.Message + Environment.NewLine + e.StackTrace));
			}

			if (!getRunningHandle())
				throw new InvalidOperationException("The build is locked with another action.");
			
			Loads.Clear();

			if (!Sort[Attr.Speed].EqualTo(0) && Sort[Attr.Speed] <= 1 // 1 SPD is too good to pass
				|| Mon.Current.Runes.Any(r => r == null)
				|| !Mon.Current.Runes.All(r => runes[r.Slot - 1].Contains(r)) // only IgnoreLess5 if I have my own runes
				|| Sort.NonZeroStats.HasCount(1)) // if there is only 1 sorting, must be too important to drop???
				IgnoreLess5 = false;

			Thread timeThread = null;

			if (!string.IsNullOrWhiteSpace(BuildStrategy)) {

				var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetTypes().Where(t => typeof(IBuildStrategyDefinition).IsAssignableFrom(t)));

				var type = types.FirstOrDefault(t => t.AssemblyQualifiedName.Contains(BuildStrategy));
				if (type != null) {
					RunStrategy();
				}
			}

			tcs.TrySetResult(null);

			try {
				Best = null;
				Stats bstats = null;
				count = 0;
				actual = 0;
				total = runes[0].Length;
				total *= runes[1].Length;
				total *= runes[2].Length;
				total *= runes[3].Length;
				total *= runes[4].Length;
				total *= runes[5].Length;
				complete = total;

				Mon.ExtraCritRate = extraCritRate;
				Mon.GetStats();
				Mon.DamageFormula?.Invoke(Mon);

				int?[] slotFakesTemp = new int?[6];
				bool[] slotPred = new bool[6];
				getPrediction(slotFakesTemp, slotPred);

				int[] slotFakes = slotFakesTemp.Select(i => i ?? 0).ToArray();

				var currentLoad = new Monster(Mon, true);
				currentLoad.Current.TempLoad = true;
				currentLoad.Current.Buffs = Buffs;
				currentLoad.Current.Shrines = Shrines;
				currentLoad.Current.Leader = Leader;

				currentLoad.Current.FakeLevel = slotFakes;
				currentLoad.Current.PredictSubs = slotPred;

				double currentScore = CalcScore(currentLoad.GetStats(true));

				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "cooking"));

				if (total == 0) {
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "0 perms"));
					RuneLog.Info("Zero permuations");
					return BuildResult.NoPermutations;
				}

				bool hasSort = Sort.IsNonZero;
				if (BuildTake == 0 && !hasSort) {
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "No sort"));
					RuneLog.Info("No method of determining best");
					return BuildResult.NoSorting;
				}

				DateTime begin = DateTime.Now;

				RuneLog.Debug(count + "/" + total + "  " + string.Format("{0:P2}", (count + complete - total) / (double)complete));


				// set to running
				IsRunning = true;

#if BUILD_PRECHECK_BUILDS_DEBUG
				SynchronizedCollection<string> outstrs = new SynchronizedCollection<string>();
#endif
				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "..."));

				List<Monster> tests = new List<Monster>();

				timeThread = new Thread(() => {
					while (IsRunning) {
						if (RunesOnlyFillEmpty)
							Thread.Sleep(30 / ((Mon?.Current?.RuneCount ?? 1) + 1));
						else
							Thread.Sleep(400);
						// every second, give a bit of feedback to those watching
						RuneLog.Debug(count + "/" + total + "  " + string.Format("{0:P2}", (count + complete - total) / (double)complete));
						if (tests != null)
							BuildProgTo?.Invoke(this, ProgToEventArgs.GetEvent(this, (count + complete - total) / (double)complete, tests.Count));

						if (BuildTimeout > 0 && DateTime.Now > begin.AddSeconds(BuildTimeout)) {
							RuneLog.Info("Timeout");
							BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + "Timeout"));
							BuildProgTo?.Invoke(this, ProgToEventArgs.GetEvent(this, 1, tests.Count));

							IsRunning = false;
						}
					}
				});
				timeThread.Start();

				double bestScore = double.MinValue;

				var opts = new ParallelOptions() {
					MaxDegreeOfParallelism = Environment.ProcessorCount - 1
				};

				var mmm = Maximum.NonZeroCached;

				// Parallel the outer loop
				// TODO: setup the begin/finish Actions with syncList.
				void body (Rune r0, ParallelLoopState loopState) {
					var tempReq = RequiredSets.ToList();
					var tempMax = Maximum == null || !Maximum.IsNonZero ? null : new Stats(Maximum, true);
					int tempCheck = 0;

					Monster myBest = null;
					List<Monster> syncList = new List<Monster>();

					void syncMyList() {
						lock (bestLock) {
#if DEBUG_SYNC_BUILDS
							foreach (var s in syncList) {
								if (s.Current.Runes.All(r => r.Assigned == mon)) {
									Console.WriteLine("!");
								}
							}
#endif
							tests.AddRange(syncList);

						}
						//syncList.ForEach(_ => tests.Add(_));
						syncList.Clear();
						if (tests.Count > Math.Max(BuildGenerate, 250000)) {
#if DEBUG_SYNC_BUILDS
								var rems = tests.OrderByDescending(b => b.score).Skip(75000).ToList();
								foreach (var bbb in rems) {
									if (bbb.Current.Runes.All(r => r.Assigned == mon)) {
										Console.WriteLine("!");
									}
								}
#endif
							lock (bestLock) {
								tests = tests.OrderByDescending(b => b.score).Take(75000).ToList();
							}
						}

						if (tests.Count > MaxBuilds32)
							IsRunning = false;
					}

					if (!IsRunning_Unsafe) {
						syncMyList();
						loopState.Break();
					}

					// number of builds ruled out since last sync
					int kill = 0;
					// number of builds added since last sync
					int plus = 0;
					// number of builds skipped
					int skip = 0;


					bool isBad;
					double myBestScore = double.MinValue, curScore, lastBest = double.MinValue;
					Stats cstats, myStats;


					Monster test = new Monster(Mon);
					test.Current.TempLoad = true;
					test.Current.Buffs = Buffs;
					test.Current.Shrines = Shrines;
					test.Current.Leader = Leader;

					test.Current.FakeLevel = slotFakes;
					test.Current.PredictSubs = slotPred;
					test.ApplyRune(r0, 7);

					RuneSet set4 = r0.SetIs4 ? r0.Set : RuneSet.Null;
					RuneSet set2 = r0.SetIs4 ? RuneSet.Null : r0.Set;
					int pop4 = 0;
					int pop2 = 0;

					foreach (Rune r1 in runes[1]) {
						// TODO: refactor to local method
						if (!IsRunning_Unsafe) // Can't break to a label, don't want to goto
							break;
						// TODO: break into multiple implementations that have less branching
#if BUILD_PRECHECK_BUILDS
						if (!AllowBroken && !RunesOnlyFillEmpty) {
							if (r1.SetIs4) {
								if (pop2 == 2)
									pop2 = 7;
								if (set4 == RuneSet.Null || pop4 >= 2) {
									set4 = r1.Set;
									pop4 = 2;
								}
								else if (set4 != r1.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
									outstrs.Add($"bad4@2 {set4} {set2} | {r0.Set} {r1.Set}");
#endif
									skip += runes[2].Length * runes[3].Length * runes[4].Length * runes[5].Length;
									continue;
								}
							}
							else {
								if (pop4 == 2)
									pop4 = 7;
								if (set2 == RuneSet.Null || pop2 >= 2) {
									set2 = r1.Set;
									pop2 = 2;
								}
							}
						}
#endif
						test.ApplyRune(r1, 7);

						foreach (Rune r2 in runes[2]) {
							if (!IsRunning_Unsafe)
								break;
#if BUILD_PRECHECK_BUILDS
							if (!AllowBroken && !RunesOnlyFillEmpty) {
								if (r2.SetIs4) {
									if (pop2 == 3)
										pop2 = 7;
									if (set4 == RuneSet.Null || pop4 >= 3) {
										set4 = r2.Set;
										pop4 = 3;
									}
									else if (set4 != r2.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
										outstrs.Add($"bad4@3 {set4} {set2} | {r0.Set} {r1.Set} {r2.Set}");
#endif
										skip += runes[3].Length * runes[4].Length * runes[5].Length;
										continue;
									}
								}
								else {
									if (pop4 == 3)
										pop4 = 7;
									if (set2 == RuneSet.Null || pop2 >= 3) {
										set2 = r2.Set;
										pop2 = 3;
									}
									else if (set4 != RuneSet.Null && set2 != r2.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
										outstrs.Add($"bad2@3 {set4} {set2} | {r0.Set} {r1.Set} {r2.Set}");
#endif
										skip += runes[3].Length * runes[4].Length * runes[5].Length;
										continue;
									}
								}
							}
#endif
							test.ApplyRune(r2, 7);

							foreach (Rune r3 in runes[3]) {
								if (!IsRunning_Unsafe)
									break;
#if BUILD_PRECHECK_BUILDS
								if (!AllowBroken && !RunesOnlyFillEmpty) {
									if (r3.SetIs4) {
										if (pop2 == 4)
											pop2 = 7;
										if (set4 == RuneSet.Null || pop4 >= 4) {
											set4 = r3.Set;
											pop4 = 4;
										}
										else if (set4 != r3.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
											outstrs.Add($"bad4@4 {set4} {set2} | {r0.Set} {r1.Set} {r2.Set} {r3.Set}");
#endif
											skip += runes[4].Length * runes[5].Length;
											continue;
										}
									}
									else {
										if (pop4 == 4)
											pop4 = 7;
										if (set2 == RuneSet.Null || pop2 >= 4) {
											set2 = r3.Set;
											pop2 = 4;
										}
										else if (set4 != RuneSet.Null && set2 != r3.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
											outstrs.Add($"bad2@4 {set4} {set2} | {r0.Set} {r1.Set} {r2.Set} {r3.Set}");
#endif
											skip += runes[4].Length * runes[5].Length;
											continue;
										}
									}
								}
#endif
								test.ApplyRune(r3, 7);

								foreach (Rune r4 in runes[4]) {
									if (!IsRunning_Unsafe) {
										break;
									}
#if BUILD_PRECHECK_BUILDS
									if (!AllowBroken && !RunesOnlyFillEmpty) {
										if (r4.SetIs4) {
											if (pop2 == 5)
												pop2 = 7;
											if (set4 == RuneSet.Null || pop4 >= 5) {
												set4 = r4.Set;
												pop4 = 5;
											}
											else if (set4 != r4.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
												outstrs.Add($"bad4@5 {set4} {set2} | {r0.Set} {r1.Set} {r2.Set} {r3.Set} {r4.Set}");
#endif
												skip += runes[5].Length;
												continue;
											}
										}
										else {
											if (pop4 == 5)
												pop4 = 7;
											if (set2 == RuneSet.Null || pop2 >= 5) {
												set2 = r4.Set;
												pop2 = 5;

											}
											else if (set4 != RuneSet.Null && set2 != r4.Set) {
#if BUILD_PRECHECK_BUILDS_DEBUG
												outstrs.Add($"bad2@5 {set4} {set2} | {r0.Set} {r1.Set} {r2.Set} {r3.Set} {r4.Set}");
#endif
												skip += runes[5].Length;
												continue;
											}
										}
									}
#endif
									test.ApplyRune(r4, 7);

									foreach (Rune r5 in runes[5]) {
										if (!IsRunning_Unsafe)
											break;

										test.ApplyRune(r5, 7);
										test.Current.CheckSets();
#if BUILD_PRECHECK_BUILDS_DEBUG
										outstrs.Add($"fine {set4} {set2} | {r0.Set} {r1.Set} {r2.Set} {r3.Set} {r4.Set} {r5.Set}");
#endif
										isBad = false;

										cstats = test.GetStats();

										// check if build meets minimum
										isBad |= !RunesOnlyFillEmpty && !AllowBroken && !test.Current.SetsFull;

										isBad |= tempMax != null && cstats.AnyExceedCached(tempMax);

										if (!isBad && GrindLoads) {
											var mahGrinds = grinds.ToList();
											for (int rg = 0; rg < 6; rg++) {
												var lgrinds = test.Runes[rg].FilterGrinds(mahGrinds);
												foreach (var g in lgrinds) {
													var tr = test.Runes[rg].Grind(g);
												}
												// TODO: 
											}
										}

										isBad |= !RunesOnlyFillEmpty && Minimum != null && !cstats.GreaterEqual(Minimum, true);
										// if no broken sets, check for broken sets
										// if there are required sets, ensure we have them
										/*isBad |= (tempReq != null && tempReq.Count > 0
											// this Linq adds no overhead compared to GetStats() and ApplyRune()
											//&& !tempReq.All(s => test.Current.Sets.Count(q => q == s) >= tempReq.Count(q => q == s))
											//&& !tempReq.GroupBy(s => s).All(s => test.Current.Sets.Count(q => q == s.Key) >= s.Count())
											);*/
										// TODO: recheck this code is correct
										if (tempReq != null && tempReq.Count > 0) {
											tempCheck = 0;
											foreach (var r in tempReq) {
												int i;
												for (i = 0; i < 3; i++) {
													if (test.Current.Sets[i] == r && (tempCheck & 1 << i) != 1 << i) {
														tempCheck |= 1 << i;
														break;
													}
												}
												if (i >= 3) {
													isBad |= true;
													break;
												}
											}
										}

										if (isBad) {
											kill++;
											curScore = 0;
										}
										else {
											// try to reduce CalcScore hits
											curScore = CalcScore(cstats);
											isBad |= IgnoreLess5 && curScore < currentScore * 1.05;
											if (isBad)
												kill++;
										}

										if (!isBad) {
											// we found an okay build!
											plus++;
											test.score = curScore;

											if (BuildSaveStats) {
												foreach (Rune r in test.Current.Runes) {
													if (!BuildGoodRunes) {
														r.manageStats.AddOrUpdate("LoadFilt", 1, (s, d) => { return d + 1; });
														RuneUsage.runesGood.AddOrUpdate(r, (byte)r.Slot, (key, ov) => (byte)r.Slot);
														r.manageStats.AddOrUpdate("currentBuildPoints", curScore, (k, v) => Math.Max(v, curScore));
														r.manageStats.AddOrUpdate("cbp" + ID, curScore, (k, v) => Math.Max(v, curScore));
													}
													else {
														r.manageStats.AddOrUpdate("LoadFilt", 0.001, (s, d) => { return d + 0.001; });
														RuneUsage.runesOkay.AddOrUpdate(r, (byte)r.Slot, (key, ov) => (byte)r.Slot);
														r.manageStats.AddOrUpdate("cbp" + ID, curScore, (k, v) => Math.Max(v, curScore * 0.9));
													}
												}
											}

											if (syncList.Count >= 500) {
												syncMyList();
											}

											// if we are to track all good builds, keep it
											if (!BuildDumpBads) {

												syncList.Add(new Monster(test, true));

												// locally track my best
												if (myBest == null || curScore > myBestScore) {
													myBest = new Monster(test, true);
													myStats = myBest.GetStats();
													myBestScore = CalcScore(myStats);
													myBest.score = myBestScore;
												}

												// if mine is better than what I last saw
												if (myBestScore > lastBest) {
													lock (bestLock) {
														if (Best == null) {
															Best = new Monster(myBest, true);
															bstats = Best.GetStats();
															bestScore = CalcScore(bstats);
															Best.score = bestScore;
														}
														else {
															// sync best score
															lastBest = bestScore;
															// double check
															if (myBestScore > lastBest) {
																Best = new Monster(myBest, true);
																bestScore = CalcScore(bstats);
																Best.score = bestScore;
																bstats = Best.GetStats();
															}
														}
													}
												}

											}
											// if we only want to track really good builds
											else {
												// if there are currently no good builds, keep it
												// or if this build is better than the best, keep it

												// locally track my best
												if (myBest == null || curScore > myBestScore) {
													myBest = new Monster(test, true);
													myStats = myBest.GetStats();
													myBestScore = CalcScore(myStats);
													myBest.score = myBestScore;
													syncList.Add(myBest);
												}
												else if (BuildSaveStats) {
													// keep it for spreadsheeting
													syncList.Add(new Monster(test, true) {
														score = curScore
													});
												}
											}
										}
									}
									// sum up what work we've done
									Interlocked.Add(ref count, kill);
									Interlocked.Add(ref count, skip);
									Interlocked.Add(ref skipped, skip);
									Interlocked.Add(ref actual, kill);
									Interlocked.Add(ref BuildUsage.failed, kill);
									kill = 0;
									skip = 0;
									Interlocked.Add(ref count, plus);
									Interlocked.Add(ref actual, plus);
									Interlocked.Add(ref BuildUsage.passed, plus);
									plus = 0;

									// if we've got enough, stop
									if (BuildGenerate > 0 && BuildUsage.passed >= BuildGenerate) {
										IsRunning = false;
										break;
									}
								}
							}
						}
					}
					// just before dying
					syncMyList();
				}
				Parallel.ForEach(runes[0], opts, body);


				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + "finalizing..."));
				BuildProgTo?.Invoke(this, ProgToEventArgs.GetEvent(this, 0.99, -1));

#if BUILD_PRECHECK_BUILDS_DEBUG
				System.IO.File.WriteAllLines("_into_the_bridge.txt", outstrs.ToArray());
#endif
				if (BuildSaveStats) {
					foreach (var ra in runes) {
						foreach (var r in ra) {
							if (!BuildGoodRunes) {
								r.manageStats.AddOrUpdate("buildScoreTotal", CalcScore(Best), (k, v) => v + CalcScore(Best));
								RuneUsage.runesUsed.AddOrUpdate(r, (byte)r.Slot, (key, ov) => (byte)r.Slot);
								r.manageStats.AddOrUpdate("LoadGen", total, (s, d) => { return d + total; });

							}
							else {
								RuneUsage.runesBetter.AddOrUpdate(r, (byte)r.Slot, (key, ov) => (byte)r.Slot);
								r.manageStats.AddOrUpdate("LoadGen", total * 0.001, (s, d) => { return d + total * 0.001; });
							}
						}
					}
				}

				// write out completion
				RuneLog.Debug(IsRunning + " " + count + "/" + total + "  " + string.Format("{0:P2}", (count + complete - total) / (double)complete));
				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + " completed"));
				BuildProgTo?.Invoke(this, ProgToEventArgs.GetEvent(this, 1, tests.Count));

				// sort *all* the builds
				int takeAmount = 1;
				if (BuildSaveStats)
					takeAmount = 10;
				if (BuildTake > 0)
					takeAmount = BuildTake;

				if (IgnoreLess5)
					tests.Add(new Monster(Mon, true));

				foreach (var ll in tests.Where(t => t != null).OrderByDescending(r => CalcScore(r.GetStats())).Take(takeAmount))
					Loads.Add(ll);

				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, "Found a load " + Loads.Count()));

				if (!BuildGoodRunes)
					BuildUsage.loads = tests.ToList();

				// dump everything to console, if nothing to print to
				if (BuildPrintTo == null)
					foreach (var l in Loads) {
						RuneLog.Debug(l.GetStats().Health + "  " + l.GetStats().Attack + "  " + l.GetStats().Defense + "  " + l.GetStats().Speed
							+ "  " + l.GetStats().CritRate + "%" + "  " + l.GetStats().CritDamage + "%" + "  " + l.GetStats().Resistance + "%" + "  " + l.GetStats().Accuracy + "%");
					}

				// sadface if no builds
				if (!Loads.Any()) {
					RuneLog.Info("No builds :(");
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + "Zero :("));
				}
				else {
					// remember the good one
					Best = Loads.First();
					Best.Current.TempLoad = false;
					Best.score = CalcScore(Best.GetStats());
					BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + "best " + (Best?.score ?? -1)));
					Best.Current.ActualTests = actual;
					foreach (var bb in Loads) {
						foreach (Rune r in bb.Current.Runes) {
							double val = Best.score;
							if (BuildGoodRunes) {
								val *= 0.25;
								if (bb == Best)
									RuneUsage.runesSecond.AddOrUpdate(r, (byte)r.Slot, (key, ov) => (byte)r.Slot);
							}

							if (bb != Best)
								val *= 0.1;
							else
								r.manageStats.AddOrUpdate("In", BuildGoodRunes ? 2 : 1, (s, e) => BuildGoodRunes ? 2 : 1);

							r.manageStats.AddOrUpdate("buildScoreIn", val, (k, v) => v + val);
						}
					}
					for (int i = 0; i < 6; i++) {
						if (!BuildGoodRunes && Mon.Current.Runes[i] != null && Mon.Current.Runes[i].Id != Best.Current.Runes[i].Id)
							Mon.Current.Runes[i].Swapped = true;
					}
					foreach (var ra in runes) {
						foreach (var r in ra) {
							var cbp = r.manageStats.GetOrAdd("currentBuildPoints", 0);
							if (cbp / Best.score < 1)
								r.manageStats.AddOrUpdate("bestBuildPercent", cbp / Best.score, (k, v) => Math.Max(v, cbp / Best.score));
						}
					}
				}

				tests.Clear();
				tests = null;
				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + "Test cleared"));
				return BuildResult.Success;
			}
			catch (Exception e) {
				RuneLog.Error("Error " + e);
				BuildPrintTo?.Invoke(this, PrintToEventArgs.GetEvent(this, prefix + e.ToString()));
				return BuildResult.Failure;
			}
			finally {
				tcs = new TaskCompletionSource<IBuildRunner>();
				IsRunning = false;
				if (timeThread != null)
					timeThread.Join();
			}
		}
예제 #9
0
        private void button_Click(object sender, EventArgs e)
        {
            Button   btn = sender as Button;
            LoadData ld  = new LoadData();


            //dgv.Rows.Add("AAAAAAAAAAAAAAA\n\rAAAAAAAAAAAAAAA\n\rAAAAAAAAAAAAAAA\n\rAAAAAAAAAAAAAAA\n\r");

            //dgv.Rows[0].Height = 100;

            ld.TypeNo = "TYPE " + txt_type.Text;

            //ld.TypeNo = "TYPE " + (dgv.RowCount + 1);
            ld.Code         = txt_lm.Text;
            ld.Distances    = new MyList(txt_distances.Text.Replace(",", " "), ' ');
            ld.Loads        = new MyList(txt_loads.Text.Replace(",", " "), ' ');
            ld.LoadWidth    = MyList.StringToDouble(txt_load_width.Text, 0.0);
            ld.ImpactFactor = MyList.StringToDouble(txt_imf.Text, 1.0);

            if (btn.Name == btn_save.Name)
            {
                try
                {
                    Loads.Save_LL_TXT(System.IO.Path.GetDirectoryName(Live_Load_File), true);
                    //Loads.Save_LL_TXT(System.IO.Path.GetDirectoryName(iapp.LL_TXT_Path), true);
                    //Loads.Save_LL_TXT(Application.StartupPath, true);
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (Exception ex) { }
            }
            else if (btn.Name == btn_cancel.Name)
            {
                DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (btn.Name == btn_restore.Name)
            {
                try
                {
                    if (MessageBox.Show("Do you want to restore default Moving Load Data ?", "ASTRA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        iapp.Write_Default_LiveLoad_Data();
                        Read_Loads();
                    }
                }
                catch (Exception ex) { }
            }
            else if (ld.Distances.Count != (ld.Loads.Count - 1))
            {
                string s = string.Format("Number of Distances = {0}    and     Number of Loads = {1}", ld.Distances.Count, ld.Loads.Count);
                MessageBox.Show(this, s + "\n\nNumber of Distances must be equal to " + (ld.Loads.Count - 1) + " (Number of Loads - 1).", "ASTRA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (btn.Name == btn_new.Name)
            {
                txt_type.Text = (dgv.RowCount + 1).ToString();
                ld.TypeNo     = "TYPE " + txt_type.Text;
                Loads.Add(ld);
                dgv.Rows.Add(ld.Data);
            }
            else if (btn.Name == btn_update.Name)
            {
                try
                {
                    Loads[dgv.CurrentCell.RowIndex]        = ld;
                    dgv[0, dgv.CurrentCell.RowIndex].Value = ld.Data;

                    Select_Loads(dgv.CurrentCell.RowIndex);
                }
                catch (Exception ex) { }
            }
            else if (btn.Name == btn_delete.Name)
            {
                try
                {
                    Loads.RemoveAt(dgv.CurrentCell.RowIndex);
                    dgv.Rows.RemoveAt(dgv.CurrentCell.RowIndex);

                    //dgv.Rows[0].Selected = true;
                }
                catch (Exception ex) { }
            }
        }
예제 #10
0
        // TODO: pull more of the App-dependant stuff out
        private static void runBuild(Build build, Save data, BuildSettings bSettings)
        {
            try
            {
                if (build == null)
                {
                    LineLog.Info("Build is null");
                    return;
                }
                // TODO: show this somewhere
                if (currentBuild != null)
                {
                    throw new InvalidOperationException("Already running a build");
                }
                if (build.IsRunning)
                {
                    throw new InvalidOperationException("This build is already running");
                }
                currentBuild = build;

                LineLog.Info("Starting watch " + build.ID + " " + build.MonName);

                Stopwatch buildTime = Stopwatch.StartNew();

                // TODO: get builds to use the settings directly
                build.RunesUseEquipped     = bSettings.RunesUseEquipped;
                build.RunesUseLocked       = bSettings.RunesUseLocked;
                build.BuildGenerate        = bSettings.BuildGenerate;
                build.BuildTake            = bSettings.BuildTake;
                build.BuildTimeout         = bSettings.BuildTimeout;
                build.Shrines              = bSettings.Shrines;
                build.BuildDumpBads        = bSettings.BuildDumpBads;
                build.BuildSaveStats       = bSettings.BuildSaveStats;
                build.BuildGoodRunes       = bSettings.BuildGoodRunes;
                build.RunesOnlyFillEmpty   = bSettings.RunesOnlyFillEmpty;
                build.RunesDropHalfSetStat = bSettings.RunesDropHalfSetStat;
                build.IgnoreLess5          = bSettings.IgnoreLess5;


                BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, "Runes..."));
                if (build.Type == BuildType.Link)
                {
                    build.CopyFrom(build.LinkBuild);
                }

                // unlock runes on current loadout (if present)
                var load = Loads.FirstOrDefault(l => l.BuildID == build.ID);
                if (load != null)
                {
                    load.Unlock();
                }

                build.GenRunes(data);

                #region Check enough runes
                string nR = "";
                for (int i = 0; i < build.Runes.Length; i++)
                {
                    if (build.Runes[i] != null && build.Runes[i].Length == 0)
                    {
                        nR += (i + 1) + " ";
                    }
                }

                if (nR != "")
                {
                    BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, ":( " + nR + "Runes"));
                    return;
                }
                #endregion

                build.BuildPrintTo += BuildsPrintTo;
                build.BuildProgTo  += BuildsProgressTo;

                EventHandler <ProgToEventArgs> qw = (bq, s) => {
                    if (runToken.IsCancellationRequested)
                    {
                        build.Cancel();
                    }
                };
                build.BuildProgTo += qw;

                var result = build.GenBuilds();

                buildTime.Stop();
                build.Time = buildTime.ElapsedMilliseconds;
                LineLog.Info("Stopping watch " + build.ID + " " + build.MonName + " @ " + buildTime.ElapsedMilliseconds);

                if (build.Best != null)
                {
                    BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, "Best"));

                    build.Best.Current.BuildID = build.ID;

                    #region Get the rune diff
                    build.Best.Current.Lock();
                    build.Best.Current.RecountDiff(build.Mon.Id);
                    #endregion

                    //currentBuild = null;
                    build.Best.Current.Time = build.Time;

                    var dmon = Program.Data.GetMonster(build.Best.Id);

                    var dmonld = dmon.Current.Leader;
                    var dmonsh = dmon.Current.Shrines;
                    dmon.Current.Leader  = build.Best.Current.Leader;
                    dmon.Current.Shrines = build.Best.Current.Shrines;
                    var dmonfl = dmon.Current.FakeLevel;
                    var dmonps = dmon.Current.PredictSubs;
                    dmon.Current.FakeLevel   = build.Best.Current.FakeLevel;
                    dmon.Current.PredictSubs = build.Best.Current.PredictSubs;
                    var dmonbf = dmon.Current.Buffs;
                    dmon.Current.Buffs = build.Best.Current.Buffs;

                    var ds = build.CalcScore(dmon.GetStats());
                    var cs = build.CalcScore(build.Best.Current.GetStats(build.Best));
                    build.Best.Current.DeltaPoints = cs - ds;

                    dmon.Current.Leader      = dmonld;
                    dmon.Current.Shrines     = dmonsh;
                    dmon.Current.FakeLevel   = dmonfl;
                    dmon.Current.PredictSubs = dmonps;
                    dmon.Current.Buffs       = dmonbf;

                    Loads.Add(build.Best.Current);

                    // if we are on the hunt of good runes.
                    if (GoodRunes && bSettings.BuildSaveStats && build.Type != BuildType.Lock)
                    {
                        var theBest = build.Best;
                        int count   = 0;
                        // we must progressively ban more runes from the build to find second-place runes.
                        //GenDeep(b, 0, printTo, ref count);
                        RunBanned(build, ++count, theBest.Current.Runes.Where(r => r.Slot % 2 != 0).Select(r => r.Id).ToArray());
                        RunBanned(build, ++count, theBest.Current.Runes.Where(r => r.Slot % 2 == 0).Select(r => r.Id).ToArray());
                        RunBanned(build, ++count, theBest.Current.Runes.Select(r => r.Id).ToArray());

                        // after messing all that shit up
                        build.Best = theBest;
                    }

                    #region Save Build stats

                    /* TODO: put Excel on Program */
                    if (bSettings.BuildSaveStats && build.Type != BuildType.Lock)
                    {
                        BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, "Excel"));
                        RuneSheet.StatsExcelBuild(build, build.Mon, build.Best.Current, true);
                    }

                    BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, "Clean"));
                    // clean up for GC
                    if (build.BuildUsage != null)
                    {
                        build.BuildUsage.loads.Clear();
                    }
                    if (build.RuneUsage != null)
                    {
                        build.RuneUsage.runesGood.Clear();
                        build.RuneUsage.runesUsed.Clear();
                    }
                    build.RuneUsage  = null;
                    build.BuildUsage = null;
                    /**/
                    #endregion
                }

                build.BuildPrintTo -= BuildsPrintTo;
                build.BuildProgTo  -= qw;
                build.BuildProgTo  -= BuildsProgressTo;

                //if (plsDie)
                //    printTo?.Invoke("Canned");
                //else
                if (build.Best != null)
                {
                    BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, "Done"));
                }
                else
                {
                    BuildsPrintTo?.Invoke(null, PrintToEventArgs.GetEvent(build, result + " :("));
                }

                LineLog.Info("Cleaning up");
                //b.isRun = false;
                //currentBuild = null;
            }
            catch (Exception e)
            {
                LineLog.Error("Error during build " + build.ID + " " + e.Message + Environment.NewLine + e.StackTrace);
            }
            finally
            {
                currentBuild = null;
                LineLog.Info("Cleaned");
            }
        }

        private static void RunBanned(Build b, int c, params ulong[] doneIds)
        {
            LineLog.Info("Running ban");
            try
            {
                b.BanEmTemp(doneIds);

                b.RunesUseLocked       = false;
                b.RunesUseEquipped     = Program.Settings.UseEquipped;
                b.BuildSaveStats       = true;
                b.RunesOnlyFillEmpty   = Program.FillRunes;
                b.BuildGoodRunes       = GoodRunes;
                b.RunesDropHalfSetStat = Program.GoFast;
                b.IgnoreLess5          = Program.Settings.IgnoreLess5;
                b.GenRunes(Program.Data);

                b.BuildTimeout  = 0;
                b.BuildTake     = 0;
                b.BuildGenerate = 0;
                b.BuildDumpBads = true;
                var result = b.GenBuilds($"{c} ");
                b.BuildGoodRunes = false;
                LineLog.Info("ran ban with result: " + result);
            }
            catch (Exception ex)
            {
                LineLog.Error("Running ban failed ", ex);
            }
            finally
            {
                b.BanEmTemp(new ulong[] { });
                b.BuildSaveStats = false;
                b.GenRunes(Program.Data);
                LineLog.Info("Ban finished");
            }
        }

        public static void RunBuilds(bool skipLoaded, int runTo = -1)
        {
            if (Program.Data == null)
            {
                return;
            }

            if (isRunning)
            {
                if (runTask != null && runTask.Status != TaskStatus.Running)
                {
                    throw new Exception("Already running builds!");
                }
                else
                {
                    runSource.Cancel();
                    return;
                }
            }
            isRunning = true;

            try
            {
                if (runTask != null && runTask.Status == TaskStatus.Running)
                {
                    runSource.Cancel();
                    //if (currentBuild != null)
                    //   currentBuild.isRun = false;
                    //plsDie = true;
                    isRunning = false;
                    return;
                }
                //plsDie = false;

                List <int> loady = new List <int>();

                if (!skipLoaded)
                {
                    ClearLoadouts();
                    foreach (var r in Program.Data.Runes)
                    {
                        r.ManageStats.AddOrUpdate("buildScoreIn", 0, (k, v) => 0);
                        r.ManageStats.AddOrUpdate("buildScoreTotal", 0, (k, v) => 0);
                    }
                }

                List <Build> toRun = new List <Build>();
                foreach (var build in Builds.OrderBy(b => b.Priority))
                {
                    if ((!skipLoaded || !Loads.Any(l => l.BuildID == build.ID)) && (runTo == -1 || build.Priority <= runTo))
                    {
                        toRun.Add(build);
                    }
                }

                /*
                 * bool collect = true;
                 * int newPri = 1;
                 * // collect the builds
                 * List<ListViewItem> list5 = new List<ListViewItem>();
                 * foreach (ListViewItem li in buildList.Items)
                 * {
                 *  li.SubItems[0].Text = newPri.ToString();
                 *  (li.Tag as Build).priority = newPri++;
                 *
                 *  if (loady.Contains((li.Tag as Build).ID))
                 *      continue;
                 *
                 *  if ((li.Tag as Build).ID == runTo)
                 *      collect = false;
                 *
                 *  if (collect)
                 *      list5.Add(li);
                 *
                 *  li.SubItems[3].Text = "";
                 * }
                 */

                runSource = new CancellationTokenSource();
                runToken  = runSource.Token;
                runTask   = Task.Factory.StartNew(() => {
                    if (Program.Data.Runes != null && !skipLoaded)
                    {
                        foreach (Rune r in Program.Data.Runes)
                        {
                            r.Swapped = false;
                            r.ResetStats();
                        }
                    }

                    foreach (Build bbb in toRun)
                    {
                        runBuild(bbb, Program.Settings.MakeStats);
                        if (runToken.IsCancellationRequested || bbb.Best == null)
                        {
                            break;
                        }
                    }

                    if (!runToken.IsCancellationRequested && Program.Settings.MakeStats)
                    {
                        if (!skipLoaded)
                        {
                            Program.RuneSheet.StatsExcelRunes(true);
                        }
                        try
                        {
                            Program.RuneSheet.StatsExcelSave(true);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }
                    isRunning = false;
                }, runSource.Token);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + Environment.NewLine + e.StackTrace, e.GetType().ToString());
            }
        }