//Generate the correct mapping private void btnGenerate_Click(object sender, EventArgs e) { //Check if ship and destination is selected otherwise stop if(cbTypeShip.SelectedIndex == -1 || cbDestination.SelectedIndex == -1) { return; } Container container = new Container(); //Get select freighter and destination from combobox freighter = freighters[cbTypeShip.SelectedIndex]; destination = destinations[cbDestination.SelectedIndex]; //Clear incase of multiple runs availableContainers.Clear(); placedContainers.Clear(); //Get all available containers availableContainers = container.GetNonShippedContainerList(); //Get the max amount of spots on a ship int totalSpots = freighter.maxHeight * freighter.containerRows * freighter.containersEachRow; //Get the max amount of power supply on a ship int availablePowerSupply = freighter.powerSupply; //Sort the containers so the chilled containers are first availableContainers = availableContainers.OrderByDescending(o => o.containerType.chilled).ThenBy(o => o.containerType.chilled).ThenBy(o => o.weight).ToList(); //Remember count value int countX = 1; int countY = 1; int countZ = 1; int y; int x; int z; bool excists; //For every container in available containers foreach(Container con in availableContainers) { countZ = 1; //Continue to check while(true) { //If all spots have been filled close if (placedContainers.Count == totalSpots) break; //If a container is valuable make sure it can never be placed on the sides if (con.containerType.valuable == 1 && countX == freighter.containerRows) { countX = 2; if (countY == freighter.containersEachRow) { countZ++; countY = 0; } countY++; Container priority = new Container(); priority.positionX = countX; priority.positionY = countY; priority.positionZ = countZ + 1; prioritys.Add(priority); } //If there is a priority position fill that one first otherwise normal position if (con.containerType.valuable == 0 && con.containerType.valuable == 0 && prioritys.Count > 0) { y = prioritys[0].positionY; x = prioritys[0].positionX; z = prioritys[0].positionZ; prioritys.RemoveAt(0); } else { y = countY; x = countX; z = countZ; } //Check if the given container position is valid excists = CheckIfPositionIsValid(x, y, z); //if its valid add the container to to the placed list if (!excists) { con.positionX = x; con.positionY = y; con.positionZ = z; placedContainers.Add(con); if (con.containerType.chilled == 1) availablePowerSupply--; break; } else { //if container is chilled make sure its always on the front row if (con.containerType.chilled == 1 && countX == freighter.containerRows) { countX = 0; countZ++; } //if the X is at its end, reset and add Y else if (countX == freighter.containerRows) { //if the Y is at its end, reset and add Z if (countY == freighter.containersEachRow) { //if the Z is at its end, break the loop if (countZ == freighter.maxHeight) { break; } countZ++; countY = 0; } countX = 0; countY++; } countX++; } } } //Check all the valuables if the have a container on top of them foreach (Container cont in placedContainers) { bool hasTopContainer = false; if (cont.containerType.valuable == 1) { foreach (Container contain in placedContainers) { if (contain.positionX == cont.positionX && contain.positionY == cont.positionY && contain.positionZ == (cont.positionZ + 1)) { hasTopContainer = true; } } if(!hasTopContainer) { //Add to delete later so the function doesn't break toDelete.Add(cont); } } } //Delete found containers foreach (Container cont in toDelete) { placedContainers.Remove(cont); } FillTextBox(); btnExport.Enabled = true; btnMarkContainers.Enabled = true; }