Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions packages/contracts/src/libraries/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,44 +82,6 @@ library LibUtils {
return ((a + b) * (a + b + 1)) / 2 + b;
}

/**
* @dev Removes an element from an array of `bytes32` if it exists and returns the new array.
* @param array The original array of `bytes32` elements.
* @param element The `bytes32` element to be removed from the array.
* @return newArray The new array containing all elements of the original array except for the removed element.
*/
function removeFromArray(bytes32[] memory array, bytes32 element) internal pure returns (bytes32[] memory) {
// Determine if the element exists and the index of the element to be removed
bool found = false;
uint256 foundIndex = 0;
for (uint256 i = 0; i < array.length; i++) {
if (array[i] == element) {
found = true;
foundIndex = i;
break;
}
}

// If the element was not found, return the original array
if (!found) {
return array;
}

// Create a new array of length `array.length - 1` to store the result
bytes32[] memory newArray = new bytes32[](array.length - 1);

// Copy elements from the original array to the new array, skipping the removed element
uint256 j = 0;
for (uint256 i = 0; i < array.length; i++) {
if (i != foundIndex) {
newArray[j] = array[i];
j++;
}
}

return newArray;
}

function stringEq(string memory a, string memory b) internal pure returns (bool) {
if (bytes(a).length != bytes(b).length) {
return false;
Expand Down
22 changes: 21 additions & 1 deletion packages/contracts/src/systems/machine-network/DestroySystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ contract DestroySystem is System {
LibEntity.destroy(_machineEntity);

// Remove it from the list of machines
MachinesInPod.set(podEntity, LibUtils.removeFromArray(MachinesInPod.get(podEntity), _machineEntity));
_swapAndPopMachinesInPod(podEntity, _machineEntity);
}

function _swapAndPopMachinesInPod(bytes32 _podEntity, bytes32 _machineEntity) internal {
uint256 length = MachinesInPod.length(_podEntity);
if (length == 0) {
return;
} else {
uint256 lastIndex = length - 1;

for (uint256 i; i < length; i++) {
if (_machineEntity == MachinesInPod.getItem(_podEntity, i)) {
if (i != lastIndex) {
bytes32 entityToSwap = MachinesInPod.getItem(_podEntity, lastIndex);
MachinesInPod.update(_podEntity, i, entityToSwap);
}
MachinesInPod.pop(_podEntity);
return;
}
}
}
}
}