public BlockData(IStructure structure, VectorInt3 pos) { _structure = structure; _block = structure?.GetBlock(pos); _device = structure?.GetDevice <IDevice>(pos); Position = pos; if (_device is IContainer c) { Device = new ContainerData(c); } }
private Tuple <ItemsData[], ConcurrentDictionary <string, IContainerSource> > CollectAllItems(IStructure structure) { var allItems = new ConcurrentDictionary <int, ItemsData>(); var containerSource = new ConcurrentDictionary <string, IContainerSource>(); Parallel.ForEach(AllCustomDeviceNames, N => { GetCurrent().GetDevicePositions(N) .ForEach(P => { var container = structure.GetDevice <IContainer>(P); var block = structure.GetBlock(P); if (container == null || block == null) { return; } containerSource.TryAdd(block.CustomName, new ContainerSource() { E = E, Container = container, CustomName = block.CustomName, Position = P }); container.GetContent() .ForEach(I => { EmpyrionScripting.ItemInfos.ItemInfo.TryGetValue(I.id, out ItemInfo details); IItemsSource source = new ItemsSource() { E = E, Id = I.id, Count = I.count, Container = container, CustomName = block.CustomName, Position = P }; allItems.AddOrUpdate(I.id, new ItemsData() { Source = new[] { source }.ToList(), Id = I.id, Count = I.count, Key = details == null ? I.id.ToString() : details.Key, Name = details == null ? I.id.ToString() : details.Name, }, (K, U) => U.AddCount(I.count, source)); }); }); }); return(new Tuple <ItemsData[], ConcurrentDictionary <string, IContainerSource> >(allItems.Values.OrderBy(I => I.Id).ToArray(), containerSource)); }
/// <summary> /// Examines an IEntity instance to see if any feature will be interested in it. /// </summary> /// <param name="entity">The IEntity instance to be examined.</param> private void ProcessEntity(IEntity entity) { if (entity == null) { return; } EntityTokenizer entityTokens = null; // Process structure data of the entity. IStructure structure = entity.Structure; if (structure != null) { //DarkCity.LogDebug($"Processing structure {structure.Entity.Name}"); if (EmpyrionExtension.LiveLcd && ((EmpyrionExtension.Application.Mode == ApplicationMode.PlayfieldServer) || (EmpyrionExtension.Application.Mode == ApplicationMode.SinglePlayer))) { // Process LCD devices. IDevicePosList list = structure.GetDevices(DeviceTypeName.LCD); if (list != null) { for (int i = 0; i < list.Count; i++) { Eleon.Modding.VectorInt3 position = list.GetAt(i); ILcd lcd = structure.GetDevice <ILcd>(position); if (lcd != null) { // Prep structure tokens if not available yet. if (entityTokens == null) { entityTokens = new EntityTokenizer(entity); } // Hand off ILcd device to any potentially interested processors. LiveLcd.Process(structure, lcd, position, entityTokens, this.PlayfieldTokens); } } } } } }
public void Add(IStructure structure) { if (structure == null) { return; } IDevicePosList containers = structure.GetDevices(DeviceTypeName.Container); if (containers != null) { for (int i = 0; i < containers.Count; i++) { Eleon.Modding.VectorInt3 position = containers.GetAt(i); IContainer container = structure.GetDevice <IContainer>(position); if (container == null) { continue; } this.Add(container.GetContent()); } } }
void CheckLCD(IModApi api, IDevicePosList ammo, IDevicePosList container, IDevicePosList fridge, IDevicePosList harvest, float blockScanDist) { IDevicePosList idpl = mStruct.GetDevices("LCD"); for (int i = 0; i < idpl.Count; i++) { VectorInt3 pos = idpl.GetAt(i); ILcd lcd = mStruct.GetDevice <ILcd>(pos); if (mAmmoLCD.ContainsValue(lcd)) { continue; //already in use } if (mContainerLCD.ContainsValue(lcd)) { continue; //already in use } if (mFridgeLCD.ContainsValue(lcd)) { continue; //already in use } if (mHarvestLCD.ContainsValue(lcd)) { continue; //already in use } api.Log("Unattached LCD Device at pos: " + pos); //find the closest device within BlockScanDistance float blockDist = blockScanDist; float bestDist = float.MaxValue; VectorInt3 bestPos = new VectorInt3(-1, -1, -1); int bestType = -1; IDevice assoc = null; for (int j = 0; j < ammo.Count; j++) { VectorInt3 pos2 = ammo.GetAt(j); float dist = VecDistance(pos, pos2); if (dist < blockDist && dist < bestDist) { assoc = mStruct.GetDevice <IContainer>(pos2); bestDist = dist; bestType = 0; bestPos = pos2; } } for (int j = 0; j < container.Count; j++) { VectorInt3 pos2 = container.GetAt(j); float dist = VecDistance(pos, pos2); if (dist < blockDist && dist < bestDist) { assoc = mStruct.GetDevice <IContainer>(pos2); bestDist = dist; bestType = 1; bestPos = pos2; } } for (int j = 0; j < fridge.Count; j++) { VectorInt3 pos2 = fridge.GetAt(j); float dist = VecDistance(pos, pos2); if (dist < blockDist && dist < bestDist) { assoc = mStruct.GetDevice <IContainer>(pos2); bestDist = dist; bestType = 2; bestPos = pos2; } } for (int j = 0; j < harvest.Count; j++) { VectorInt3 pos2 = harvest.GetAt(j); float dist = VecDistance(pos, pos2); if (dist < blockDist && dist < bestDist) { assoc = mStruct.GetDevice <IContainer>(pos2); bestDist = dist; bestType = 3; bestPos = pos2; } } IBlock lcdBlock = mStruct.GetBlock(pos); if (assoc == null) { api.Log("Null Assoc"); //maybe since this isn't near a device //it can be used for fuel or something CheckForSpecialLCD(lcdBlock, lcd); continue; } if (!mDevicePositions.ContainsKey(lcd)) { mDevicePositions.Add(lcd, pos); } if (!mDevicePositions.ContainsKey(assoc)) { mDevicePositions.Add(assoc, bestPos); } IBlock devBlock = mStruct.GetBlock(bestPos); if (lcdBlock == null) { api.Log("Null block for lcd!"); } else { if (mDeviceBlocks.ContainsKey(lcd)) { api.Log("BadCleanup! Device blocks already has lcd: " + lcd + "!!"); } else { mDeviceBlocks.Add(lcd, lcdBlock); } } if (devBlock == null) { api.Log("Null block for device!"); } else { if (mDeviceBlocks.ContainsKey(assoc)) { api.Log("BadCleanup! Device blocks already has assoc: " + assoc + "!!"); } else { mDeviceBlocks.Add(assoc, devBlock); } } if (bestType == 0) { api.Log("Ammo Assoc"); mAmmoLCD.Add(assoc as IContainer, lcd); } else if (bestType == 1) { api.Log("Con Assoc"); mContainerLCD.Add(assoc as IContainer, lcd); } else if (bestType == 2) { api.Log("Fridge Assoc"); mFridgeLCD.Add(assoc as IContainer, lcd); } else if (bestType == 3) { api.Log("Harv Assoc"); mHarvestLCD.Add(assoc as IContainer, lcd); } } }
/// <summary> /// Examines an ILcd device to see if it contains LiveLCD code. ILcd devices without code are ignored. /// </summary> /// <param name="structure">The IStructure instance that the ILcd device resides in.</param> /// <param name="source">The ILcd device to be processed for LiveLCD code.</param> /// <param name="position">VectorInt3 containing the position of the ILcd device within the structure.</param> /// <param name="tokens">Token collection representing data from the parent structure.</param> public static void Process(IStructure structure, ILcd source, VectorInt3 position, Tokenizer tokens, PlayfieldTokenizer playfieldTokens) { try { // Confirm that LCD has been marked as a LiveLCD. StringReader config = new StringReader(source.GetText()); string header = config.ReadLine(); if ((header == null) || (header.Trim() != liveLcdPhrase)) { return; } //DarkCity.LogDebug($"Processing LiveLCD in {structure.Entity.Name} ({structure.Entity.Id}) @ {position}."); List <ILcd> targets = new List <ILcd>(); // Process source LCD configuration. string line = config.ReadLine(); while (line != null) { // If the format phrase is found then configuration has finished. line = line.Trim(); if (line == formatPhrase) { break; } // Split the configuration line into a key/value pair. string[] kvp = line.Split(kvpSeparator, 2); if (kvp.Length == 2) { string key = kvp[0].Trim().ToLower(); string value = kvp[1].Trim(); switch (key) { // Gives the custom name of the LCD device that will display the results of formatted string from this LCD. case "target": ILcd target = structure.GetDevice <ILcd>(value); if (target == null) { Log.Debug($"Could not find LiveLCD target {value}."); } else { //DarkCity.LogDebug($"Found LiveLCD target {value}."); targets.Add(target); } break; // Ignore unknown config directives. default: Log.Debug($"Encountered unknown key {key}; Ignoring."); break; } } line = config.ReadLine(); } // Validate configuration. if (targets.Count < 1) { Log.Debug("Live LCD did not specify a target LCD or it could not be found."); return; } // Read the rest of the source LCD text as a string format specifier and apply it to the target LCDs. string data = tokens.Tokenize(config.ReadToEnd()); if (playfieldTokens != null) { playfieldTokens.UpdateWithPosition(structure.Entity.Position); playfieldTokens.UpdateWithFaction(structure.Entity.Faction.Id); data = playfieldTokens.Tokenize(data); } foreach (ILcd target in targets) { target?.SetText(data); } } catch (Exception ex) { Log.Warn($"Failed to process LCD due to exception ({ex.GetType()}) : {ex.Message}"); } }