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
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { getUniqueEntity } from "@latticexyz/world-modules/src/modules/uniqueentity/getUniqueEntity.sol";
import { EntityType, MachineType, BuildIndex, IncomingConnections, OutgoingConnections } from "../codegen/index.sol";
import { EntityType, MachineType, BuildIndex, IncomingConnections, OutgoingConnections, TutorialLevel, GameConfig, SpawnIndex } from "../codegen/index.sol";
import { ENTITY_TYPE, MACHINE_TYPE } from "../codegen/common.sol";

library LibEntity {
library LibMachineBuild {
/**
* @notice Creates a new machine entity of the specified type.
* @notice Creates a new machine entity of the specified buildable type.
* @param _machineType The type of machine to create, specified by the MACHINE_TYPE enum.
* @return entity The identifier for the newly created machine entity.
*/
function create(MACHINE_TYPE _machineType) internal returns (bytes32) {
bytes32 entity = getUniqueEntity();
EntityType.set(entity, ENTITY_TYPE.MACHINE);
MachineType.set(entity, _machineType);
function build(MACHINE_TYPE _machineType) internal returns (bytes32 entity) {
require(isBuildableMachineType(_machineType), "not buildable");
entity = getUniqueEntity();
create(_machineType, entity);
}

/**
* @notice Creates records for a new machine entity of the specified type.
* @param _machineType The type of machine to create, specified by the MACHINE_TYPE enum.
* @return entity The identifier for the newly created machine entity.
*/
function create(MACHINE_TYPE _machineType) internal returns (bytes32 entity) {
entity = getUniqueEntity();
create(_machineType, entity);
}

/**
* @notice Creates records for a new machine of the specified type, using the provided entity.
* @param _machineType The type of machine to create, specified by the MACHINE_TYPE enum.
* @param _entity The identifier to be used for the machine entity creation.
*/
function create(MACHINE_TYPE _machineType, bytes32 _entity) internal {
EntityType.set(_entity, ENTITY_TYPE.MACHINE);
MachineType.set(_entity, _machineType);

// Set ports on machine
// - - - - - - - - - - - -
// NONE: 0 IN, 0 OUT
// INLET: 0 IN, 1 OUT
// OUTLET: 1 IN, 0 OUT
// PLAYER: 1 IN, 2 OUT (created at spawn)
// PLAYER: 1 IN, 2 OUT
// SPLITTER: 1 IN, 2 OUT
// MIXER: 2 IN, 1 OUT
// WETTER: 1 IN, 1 OUT
Expand All @@ -29,23 +49,21 @@ library LibEntity {
// COOLER: 1 IN, 1 OUT

if (_machineType == MACHINE_TYPE.INLET) {
IncomingConnections.set(entity, new bytes32[](0));
OutgoingConnections.set(entity, new bytes32[](1));
IncomingConnections.set(_entity, new bytes32[](0));
OutgoingConnections.set(_entity, new bytes32[](1));
} else if (_machineType == MACHINE_TYPE.OUTLET) {
IncomingConnections.set(entity, new bytes32[](1));
OutgoingConnections.set(entity, new bytes32[](0));
} else if (_machineType == MACHINE_TYPE.SPLITTER) {
IncomingConnections.set(entity, new bytes32[](1));
OutgoingConnections.set(entity, new bytes32[](2));
IncomingConnections.set(_entity, new bytes32[](1));
OutgoingConnections.set(_entity, new bytes32[](0));
} else if (_machineType == MACHINE_TYPE.SPLITTER || _machineType == MACHINE_TYPE.PLAYER) {
IncomingConnections.set(_entity, new bytes32[](1));
OutgoingConnections.set(_entity, new bytes32[](2));
} else if (_machineType == MACHINE_TYPE.MIXER) {
IncomingConnections.set(entity, new bytes32[](2));
OutgoingConnections.set(entity, new bytes32[](1));
IncomingConnections.set(_entity, new bytes32[](2));
OutgoingConnections.set(_entity, new bytes32[](1));
} else {
IncomingConnections.set(entity, new bytes32[](1));
OutgoingConnections.set(entity, new bytes32[](1));
IncomingConnections.set(_entity, new bytes32[](1));
OutgoingConnections.set(_entity, new bytes32[](1));
}

return entity;
}

/**
Expand Down
45 changes: 36 additions & 9 deletions packages/contracts/src/libraries/LibPlayer.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { WorldContextConsumerLib } from "@latticexyz/world/src/WorldContext.sol";
import { TutorialLevel, EntityType, MachineType, OutgoingConnections, IncomingConnections, GameConfig, SpawnIndex } from "../codegen/index.sol";
import { ENTITY_TYPE, MACHINE_TYPE } from "../codegen/common.sol";
import { LibMachineBuild } from "./LibMachineBuild.sol";
import { LibUtils } from "./LibUtils.sol";

library LibPlayer {
function spawn(bytes32 _playerEntity) internal {
EntityType.set(_playerEntity, ENTITY_TYPE.MACHINE);
MachineType.set(_playerEntity, MACHINE_TYPE.PLAYER);
TutorialLevel.set(_playerEntity, 0);
/**
* @notice Returns the current player entity (based on World's _msgSender)
*/
function _getPlayerEntity() private view returns (bytes32) {
return LibUtils.addressToEntityKey(WorldContextConsumerLib._msgSender());
}

/**
* @notice Returns whether the records for the player entity have been spawned
*/
function _isPlayerSpawned(bytes32 _playerEntity) private view returns (bool) {
return EntityType.get(_playerEntity) != ENTITY_TYPE.NONE;
}

/**
* @notice Returns the current player entity (based on World's _msgSender), reverts if plater is not spawned
*/
function getSpawnedPlayerEntity() internal view returns (bytes32 playerEntity) {
playerEntity = LibUtils.addressToEntityKey(WorldContextConsumerLib._msgSender());
require(_isPlayerSpawned(playerEntity), "player not spawned");
}

/**
* @notice Spawns records for the current player entity (based on World's msgSender).
* @return playerEntity The identifier for the newly created player entity.
*/
function spawn() internal returns (bytes32 playerEntity) {
playerEntity = _getPlayerEntity();
require(!_isPlayerSpawned(playerEntity), "player already spawned");
LibMachineBuild.create(MACHINE_TYPE.PLAYER, playerEntity);

TutorialLevel.set(playerEntity, 0);

uint32 newSpawnIndex = GameConfig.getGlobalSpawnIndex() + 1;
GameConfig.setGlobalSpawnIndex(newSpawnIndex);
SpawnIndex.set(_playerEntity, newSpawnIndex);

// Player has 1 input and 2 outputs
IncomingConnections.set(_playerEntity, new bytes32[](1));
OutgoingConnections.set(_playerEntity, new bytes32[](2));
SpawnIndex.set(playerEntity, newSpawnIndex);
}
}
2 changes: 1 addition & 1 deletion packages/contracts/src/libraries/Libraries.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.24;
import { LibUtils } from "./LibUtils.sol";
import { LibPod } from "./LibPod.sol";
import { LibPlayer } from "./LibPlayer.sol";
import { LibEntity } from "./LibEntity.sol";
import { LibMachineBuild } from "./LibMachineBuild.sol";
import { LibNetwork } from "./LibNetwork.sol";
import { LibMachine } from "./LibMachine.sol";
import { LibDepot } from "./LibDepot.sol";
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/src/systems/dev/WarpSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { TutorialLevel, CurrentOrder, TutorialOrders, CarriedBy, DepotsInPod, Amount, MaterialType, Order, Tutorial, Name } from "../../codegen/index.sol";
import { LibUtils, LibToken } from "../../libraries/Libraries.sol";
import { LibPlayer, LibToken } from "../../libraries/Libraries.sol";

contract WarpSystem is System {
/**
* @dev Used in testing to fast forward the tutorial level. NOTE: disable for production.
*/
function warp(uint32 _level) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);
// Set level
TutorialLevel.set(playerEntity, _level);
Expand All @@ -24,7 +24,7 @@ contract WarpSystem is System {
* @dev Used in testing to skip tutorial levels. NOTE: disable for production.
*/
function graduate() public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

TutorialLevel.deleteRecord(playerEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { CarriedBy, BuildIndex, MachinesInPod } from "../../codegen/index.sol";
import { MACHINE_TYPE, ENTITY_TYPE } from "../../codegen/common.sol";
import { LibUtils, LibEntity, LibPod } from "../../libraries/Libraries.sol";
import { LibPlayer, LibMachineBuild, LibPod } from "../../libraries/Libraries.sol";

contract BuildSystem is System {
/**
Expand All @@ -12,14 +12,11 @@ contract BuildSystem is System {
* @return machineEntity The identifier for the newly created machine entity.
*/
function build(MACHINE_TYPE _machineType) public returns (bytes32) {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
require(LibEntity.isBuildableMachineType(_machineType), "not buildable");

// Create machine entity
bytes32 machineEntity = LibEntity.create(_machineType);
bytes32 machineEntity = LibMachineBuild.build(_machineType);

// Get player's pod entity
bytes32 podEntity = CarriedBy.get(playerEntity);
bytes32 podEntity = CarriedBy.get(LibPlayer.getSpawnedPlayerEntity());

// Place in same pod as the player
CarriedBy.set(machineEntity, podEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { EntityType, GameConfig, GameConfigData, IncomingConnections, OutgoingConnections, CarriedBy } from "../../codegen/index.sol";
import { ENTITY_TYPE, PORT_INDEX } from "../../codegen/common.sol";
import { LibUtils, LibNetwork } from "../../libraries/Libraries.sol";
import { LibPlayer, LibNetwork } from "../../libraries/Libraries.sol";

contract ConnectSystem is System {
/**
Expand All @@ -13,7 +13,7 @@ contract ConnectSystem is System {
* @param _portIndex The port index on the source machine which determines the position (FIRST or SECOND) to write in the outgoing connections array.
*/
function connect(bytes32 _sourceMachine, bytes32 _targetMachine, PORT_INDEX _portIndex) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CarriedBy.get(_sourceMachine) == podEntity && CarriedBy.get(_targetMachine) == podEntity, "not in pod");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { GameConfig, GameConfigData, CarriedBy, EntityType, MachinesInPod, IncomingConnections, OutgoingConnections } from "../../codegen/index.sol";
import { MACHINE_TYPE, ENTITY_TYPE } from "../../codegen/common.sol";
import { LibUtils, LibEntity, LibNetwork } from "../../libraries/Libraries.sol";
import { LibUtils, LibPlayer, LibMachineBuild, LibNetwork } from "../../libraries/Libraries.sol";

contract DestroySystem is System {
/**
* @notice Destroys the specified machine entity and cleans up all its network connections.
* @param _machineEntity The identifier for the machine entity to be destroyed.
*/
function destroy(bytes32 _machineEntity) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CarriedBy.get(_machineEntity) == podEntity, "not in pod");
Expand Down Expand Up @@ -69,7 +69,7 @@ contract DestroySystem is System {
}

// Destroy machine entity
LibEntity.destroy(_machineEntity);
LibMachineBuild.destroy(_machineEntity);

// Remove it from the list of machines
MachinesInPod.set(podEntity, LibUtils.removeFromArray(MachinesInPod.get(podEntity), _machineEntity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { EntityType, GameConfig, GameConfigData, IncomingConnections, OutgoingConnections, CarriedBy } from "../../codegen/index.sol";
import { ENTITY_TYPE, PORT_INDEX } from "../../codegen/common.sol";
import { LibUtils, LibNetwork } from "../../libraries/Libraries.sol";
import { LibPlayer, LibNetwork } from "../../libraries/Libraries.sol";

contract DisconnectSystem is System {
/**
Expand All @@ -12,7 +12,7 @@ contract DisconnectSystem is System {
* @param _portIndex The port index on the source machine which determines which connection to disconnect.
*/
function disconnect(bytes32 _sourceMachine, PORT_INDEX _portIndex) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CarriedBy.get(_sourceMachine) == podEntity, "not in pod");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { CarriedBy } from "../../codegen/index.sol";
import { LibUtils, LibNetwork } from "../../libraries/Libraries.sol";
import { LibPlayer, LibNetwork } from "../../libraries/Libraries.sol";

contract ResolveSystem is System {
function resolve() public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);
LibNetwork.resolve(podEntity);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/src/systems/order/DepotSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { EntityType, CarriedBy, MaterialType, MachineType, Amount, DepotConnection, FixedEntities } from "../../codegen/index.sol";
import { ENTITY_TYPE, MATERIAL_TYPE, MACHINE_TYPE } from "../../codegen/common.sol";
import { LibUtils, LibPod, LibNetwork } from "../../libraries/Libraries.sol";
import { LibPlayer, LibPod, LibNetwork } from "../../libraries/Libraries.sol";

contract DepotSystem is System {
function attachDepot(bytes32 _depotEntity, MACHINE_TYPE _machineType) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CarriedBy.get(_depotEntity) == podEntity, "not in pod");
Expand All @@ -32,7 +32,7 @@ contract DepotSystem is System {
}

function detachDepot(MACHINE_TYPE _machineType) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(_machineType == MACHINE_TYPE.INLET || _machineType == MACHINE_TYPE.OUTLET, "not inlet/outlet");
Expand All @@ -53,7 +53,7 @@ contract DepotSystem is System {
}

function clearDepot(bytes32 _depotEntity) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CarriedBy.get(_depotEntity) == podEntity, "not in pod");
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/src/systems/order/OrderSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { console } from "forge-std/console.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { EntityType, CarriedBy, MaterialType, Order, Amount, CurrentOrder, DepotConnection, Tutorial, TutorialLevel, TutorialOrders, CompletedPlayers, FixedEntities, DepotsInPod } from "../../codegen/index.sol";
import { MACHINE_TYPE, ENTITY_TYPE, MATERIAL_TYPE } from "../../codegen/common.sol";
import { LibUtils, LibOrder, LibToken } from "../../libraries/Libraries.sol";
import { LibPlayer, LibOrder, LibToken } from "../../libraries/Libraries.sol";

contract OrderSystem is System {
function create(
Expand Down Expand Up @@ -33,7 +33,7 @@ contract OrderSystem is System {
}

function fill(bytes32 _depotEntity) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CarriedBy.get(_depotEntity) == podEntity, "not in pod");
Expand Down Expand Up @@ -91,7 +91,7 @@ contract OrderSystem is System {
}

function accept(bytes32 _orderEntity) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
bytes32 podEntity = CarriedBy.get(playerEntity);

require(CurrentOrder.get(podEntity) == bytes32(0), "order in progress");
Expand All @@ -113,7 +113,7 @@ contract OrderSystem is System {
function buy() public {
require(LibToken.getTokenBalance(_msgSender()) >= 100, "insufficient balance");

bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
// Get player's pod entity
bytes32 podEntity = CarriedBy.get(playerEntity);

Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/src/systems/progression/NameSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { Tutorial, Name } from "../../codegen/index.sol";
import { LibUtils, LibNetwork } from "../../libraries/Libraries.sol";
import { LibUtils, LibNetwork, LibPlayer } from "../../libraries/Libraries.sol";

contract NameSystem is System {
function name(string memory _name) public {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
bytes32 playerEntity = LibPlayer.getSpawnedPlayerEntity();
// Only allow naming when tutorial is completed
require(Tutorial.get(playerEntity) != false, "not completed");
require(!LibUtils.stringEq(_name, ""), "name empty");
Expand Down
7 changes: 1 addition & 6 deletions packages/contracts/src/systems/progression/SpawnSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { LibUtils, LibPlayer } from "../../libraries/Libraries.sol";

contract SpawnSystem is System {
function spawn() public returns (bytes32) {
bytes32 playerEntity = LibUtils.addressToEntityKey(_msgSender());
// @todo: check if already spawned

// Create player entity
LibPlayer.spawn(playerEntity);

return playerEntity;
return LibPlayer.spawn();
}
}
Loading