public static float SellExistingCell(CACell celCurrCell, Direction dirDirection) { int nNextCellX = celCurrCell.x + dirDirection.xDir; int nNextCellY = celCurrCell.y + dirDirection.yDir; // Test if the location is valid if (!(celCurrCell.matParentMatrix.IsLocationValid(nNextCellX, nNextCellY))) { return(2); } CACell celNeighbourCell = celCurrCell.matParentMatrix.GetCell(nNextCellX, nNextCellY); // Find out if there's a cell there if (celNeighbourCell == null) { return(0); } // Assign an energy transfer from deleted cell to the new cell EnergyTransfer trnTransfer = new EnergyTransfer(); trnTransfer.celFrom = celNeighbourCell; trnTransfer.celTo = celCurrCell; trnTransfer.dEnergy = ENERGY_GIVEN_BY_SELLING; celCurrCell.matParentMatrix.lstCellEnergyTransfersOne.Add(trnTransfer); // Assign a cell delete celCurrCell.matParentMatrix.lstCellDeletesForNextTime.Add( new Point(nNextCellX, nNextCellY)); return(1); }
public static void RunTimeStep(CAMatrix matMatrix) { int nWidth = matMatrix.Width; int nHeight = matMatrix.Height; for (int x = 0; x < nWidth; x++) { for (int y = 0; y < nHeight; y++) { CACell celCurrCell = matMatrix.GetCell(x, y); // If the cell has nothing in it, skip it.. if (celCurrCell == null) { continue; } // The cell has a program in it. run it. celCurrCell.progProgram.Run(celCurrCell); } } // Deleting cells for (int i = 0; i < matMatrix.lstCellDeletesForNextTime.Count; i++) { matMatrix.DestroyCell(matMatrix.lstCellDeletesForNextTime[i].X, matMatrix.lstCellDeletesForNextTime[i].Y); } matMatrix.lstCellDeletesForNextTime.Clear(); // Applying new cells waiting to creation for (int i = 0; i < matMatrix.lstNewCellsForNextTime.Count; i++) { matMatrix.PutCell(matMatrix.lstNewCellsForNextTime[i]); } matMatrix.lstNewCellsForNextTime.Clear(); // Transferring energies for (int i = 0; i < matMatrix.lstCellEnergyTransfersOne.Count; i++) { EnergyTransfer trnTransfer = matMatrix.lstCellEnergyTransfersOne[i]; if (trnTransfer.celFrom.fAvaliableEnergy >= trnTransfer.dEnergy) { trnTransfer.celFrom.fAvaliableEnergy -= trnTransfer.dEnergy; trnTransfer.celTo.fAvaliableEnergy += trnTransfer.dEnergy; } } matMatrix.lstCellEnergyTransfersOne.Clear(); }
public static float SendEnergy(CACell celCurrCell, Direction dirDirection, float fWantedEnergy) { float fTransferringEnergy = fWantedEnergy; // Check that the energy to transmit is above 0. if (fWantedEnergy <= 0) { return(3); } // If there is no energy left, then spend all the energy you can to send // The wanted energy. if (celCurrCell.fAvaliableEnergy < fWantedEnergy) { fTransferringEnergy = celCurrCell.fAvaliableEnergy; } int nNextCellX = celCurrCell.x + dirDirection.xDir; int nNextCellY = celCurrCell.y + dirDirection.yDir; // Test if the location is valid if (!(celCurrCell.matParentMatrix.IsLocationValid(nNextCellX, nNextCellY))) { return(2); } CACell celNeighbourCell = celCurrCell.matParentMatrix.GetCell(nNextCellX, nNextCellY); // Test if there's a cell there. if (celNeighbourCell == null) { return(0); } // Assign an energy transfer from cell one to the new cell EnergyTransfer trnTransfer = new EnergyTransfer(); trnTransfer.celFrom = celCurrCell; trnTransfer.celTo = celNeighbourCell; trnTransfer.dEnergy = fTransferringEnergy; celCurrCell.matParentMatrix.lstCellEnergyTransfersOne.Add(trnTransfer); return(1); }