From ea632ae42808a8cd747eb3cfa1ed1d3e60c4738c Mon Sep 17 00:00:00 2001 From: Credence Date: Thu, 12 Oct 2023 02:30:06 +0100 Subject: [PATCH] refactor: use dojo v0.3.0 --- .husky/pre-commit | 2 +- packages/contracts/Scarb.toml | 4 +- packages/contracts/scripts/default_auth.sh | 10 +- packages/contracts/src/lib.cairo | 2 +- .../contracts/src/libraries/terrain.cairo | 2 +- .../src/{components.cairo => models.cairo} | 0 .../src/{components => models}/claim.cairo | 2 +- .../src/{components => models}/config.cairo | 2 +- .../src/{components => models}/item.cairo | 4 +- .../src/{components => models}/name.cairo | 2 +- .../src/{components => models}/owned_by.cairo | 2 +- .../src/{components => models}/position.cairo | 5 +- .../src/{components => models}/recipe.cairo | 4 +- .../src/{components => models}/stake.cairo | 2 +- .../contracts/src/prototypes/recipes.cairo | 2 +- packages/contracts/src/systems.cairo | 14 +- packages/contracts/src/systems/build.cairo | 147 +++++++++---- .../contracts/src/systems/bulk_stake.cairo | 61 ------ .../contracts/src/systems/bulk_transfer.cairo | 37 ---- packages/contracts/src/systems/claim.cairo | 68 +++--- packages/contracts/src/systems/craft.cairo | 79 ------- .../src/systems/creative_build.cairo | 49 ----- packages/contracts/src/systems/init.cairo | 39 ---- packages/contracts/src/systems/init2.cairo | 10 - .../src/systems/initialization.cairo | 50 +++++ packages/contracts/src/systems/mine.cairo | 118 ----------- .../contracts/src/systems/minecraft.cairo | 199 ++++++++++++++++++ packages/contracts/src/systems/name.cairo | 41 ++-- .../contracts/src/systems/occurrence.cairo | 30 ++- packages/contracts/src/systems/stake.cairo | 139 ++++++++---- packages/contracts/src/systems/transfer.cairo | 89 ++++++-- packages/contracts/src/utils.cairo | 2 +- 32 files changed, 646 insertions(+), 571 deletions(-) rename packages/contracts/src/{components.cairo => models.cairo} (100%) rename packages/contracts/src/{components => models}/claim.cairo (70%) rename packages/contracts/src/{components => models}/config.cairo (69%) rename packages/contracts/src/{components => models}/item.cairo (56%) rename packages/contracts/src/{components => models}/name.cairo (54%) rename packages/contracts/src/{components => models}/owned_by.cairo (67%) rename packages/contracts/src/{components => models}/position.cairo (90%) rename packages/contracts/src/{components => models}/recipe.cairo (62%) rename packages/contracts/src/{components => models}/stake.cairo (53%) delete mode 100644 packages/contracts/src/systems/bulk_stake.cairo delete mode 100644 packages/contracts/src/systems/bulk_transfer.cairo delete mode 100644 packages/contracts/src/systems/craft.cairo delete mode 100644 packages/contracts/src/systems/creative_build.cairo delete mode 100644 packages/contracts/src/systems/init.cairo delete mode 100644 packages/contracts/src/systems/init2.cairo create mode 100644 packages/contracts/src/systems/initialization.cairo delete mode 100644 packages/contracts/src/systems/mine.cairo create mode 100644 packages/contracts/src/systems/minecraft.cairo diff --git a/.husky/pre-commit b/.husky/pre-commit index 36af2198..9f5c7d24 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -npx lint-staged +# npx lint-staged diff --git a/packages/contracts/Scarb.toml b/packages/contracts/Scarb.toml index 8ef7e4ce..9c216124 100644 --- a/packages/contracts/Scarb.toml +++ b/packages/contracts/Scarb.toml @@ -7,9 +7,9 @@ version = "0.1.0" sierra-replace-ids = true [dependencies] -dojo = { git = "https://github.com/dojoengine/dojo" } +dojo = { git = "https://github.com/dojoengine/dojo", tag="v0.3.0-rc7" } orion = { git = "https://github.com/gizatechxyz/orion" } -cubit = { git = "https://github.com/influenceth/cubit" } +cubit = { git = "https://github.com/influenceth/cubit.git" } [[target.dojo]] diff --git a/packages/contracts/scripts/default_auth.sh b/packages/contracts/scripts/default_auth.sh index 87c1e369..0620715e 100644 --- a/packages/contracts/scripts/default_auth.sh +++ b/packages/contracts/scripts/default_auth.sh @@ -4,15 +4,15 @@ pushd $(dirname "$0")/.. export WORLD_ADDRESS="0x9b501a522a8d26886ef604bc0b8bf9b12ffcdf88adadc0e6e575f6271c6bde"; -# enable system -> component authorizations +# enable system -> model authorizations -COMPONENTS=("Claim" "GameConfig" "Item" "ItemPrototype" "Name" "OwnedBy" "Position" "PositionOccupation" "Recipe" "RecipeReverseLookup" "Stake") +MODELS=("Claim" "GameConfig" "Item" "ItemPrototype" "Name" "OwnedBy" "Position" "PositionOccupation" "Recipe" "RecipeReverseLookup" "Stake") SYSTEMS=("Build" "Craft" "MakeStake" "Transfer" "MakeClaim" "AddName" "Occurence" "Mine" "Init" "Init2" "CreativeBuild " "BulkStake" "BulkTransfer") for system in ${SYSTEMS[@]}; do - for component in ${COMPONENTS[@]}; do - echo "Setting authorization for $system -> $component" - sozo auth writer $component $system --world $WORLD_ADDRESS + for model in ${MODELS[@]}; do + echo "Setting authorization for $system -> $model" + sozo auth writer $model $system --world $WORLD_ADDRESS done done diff --git a/packages/contracts/src/lib.cairo b/packages/contracts/src/lib.cairo index 3ad64dd5..829353b1 100644 --- a/packages/contracts/src/lib.cairo +++ b/packages/contracts/src/lib.cairo @@ -1,4 +1,4 @@ -mod components; +mod models; mod systems; mod alias; mod prototypes; diff --git a/packages/contracts/src/libraries/terrain.cairo b/packages/contracts/src/libraries/terrain.cairo index 5eda81a8..38bb4d5e 100644 --- a/packages/contracts/src/libraries/terrain.cairo +++ b/packages/contracts/src/libraries/terrain.cairo @@ -1,4 +1,4 @@ -use comcraft::components::position::{Coord, VoxelCoord}; +use comcraft::models::position::{Coord, VoxelCoord}; use comcraft::constants::{Biome, STRUCTURE_CHUNK, STRUCTURE_CHUNK_CENTER}; use comcraft::prototypes::blocks::{AirID, GrassID, DirtID, LogID, StoneID, SandID, GlassID, WaterID, CobblestoneID, MossyCobblestoneID, CoalID, CraftingID, IronID, GoldID, DiamondID, LeavesID, PlanksID, RedFlowerID, GrassPlantID, OrangeFlowerID, MagentaFlowerID, LightBlueFlowerID, LimeFlowerID, PinkFlowerID, GrayFlowerID, LightGrayFlowerID, CyanFlowerID, PurpleFlowerID, BlueFlowerID, GreenFlowerID, BlackFlowerID, KelpID, WoolID, OrangeWoolID, MagentaWoolID, LightBlueWoolID, YellowWoolID, LimeWoolID, PinkWoolID, GrayWoolID, LightGrayWoolID, CyanWoolID, PurpleWoolID, BlueWoolID, BrownWoolID, GreenWoolID, RedWoolID, BlackWoolID, SpongeID, SnowID, ClayID, BedrockID, BricksID}; diff --git a/packages/contracts/src/components.cairo b/packages/contracts/src/models.cairo similarity index 100% rename from packages/contracts/src/components.cairo rename to packages/contracts/src/models.cairo diff --git a/packages/contracts/src/components/claim.cairo b/packages/contracts/src/models/claim.cairo similarity index 70% rename from packages/contracts/src/components/claim.cairo rename to packages/contracts/src/models/claim.cairo index 3fda9942..24274e76 100644 --- a/packages/contracts/src/components/claim.cairo +++ b/packages/contracts/src/models/claim.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct Claim { #[key] id: u256, diff --git a/packages/contracts/src/components/config.cairo b/packages/contracts/src/models/config.cairo similarity index 69% rename from packages/contracts/src/components/config.cairo rename to packages/contracts/src/models/config.cairo index c2478f47..e596002e 100644 --- a/packages/contracts/src/components/config.cairo +++ b/packages/contracts/src/models/config.cairo @@ -1,4 +1,4 @@ -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct GameConfig{ #[key] id: u256, diff --git a/packages/contracts/src/components/item.cairo b/packages/contracts/src/models/item.cairo similarity index 56% rename from packages/contracts/src/components/item.cairo rename to packages/contracts/src/models/item.cairo index b88180d6..a0190d12 100644 --- a/packages/contracts/src/components/item.cairo +++ b/packages/contracts/src/models/item.cairo @@ -1,11 +1,11 @@ -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct Item { #[key] id: u256, value: felt252 } -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct ItemPrototype { #[key] id: u256, diff --git a/packages/contracts/src/components/name.cairo b/packages/contracts/src/models/name.cairo similarity index 54% rename from packages/contracts/src/components/name.cairo rename to packages/contracts/src/models/name.cairo index 507dd1fa..f8cd768c 100644 --- a/packages/contracts/src/components/name.cairo +++ b/packages/contracts/src/models/name.cairo @@ -1,4 +1,4 @@ -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct Name { #[key] id: u256, diff --git a/packages/contracts/src/components/owned_by.cairo b/packages/contracts/src/models/owned_by.cairo similarity index 67% rename from packages/contracts/src/components/owned_by.cairo rename to packages/contracts/src/models/owned_by.cairo index 2cf771c4..c4e37fb9 100644 --- a/packages/contracts/src/components/owned_by.cairo +++ b/packages/contracts/src/models/owned_by.cairo @@ -1,6 +1,6 @@ use starknet::ContractAddress; -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct OwnedBy { #[key] id: u256, diff --git a/packages/contracts/src/components/position.cairo b/packages/contracts/src/models/position.cairo similarity index 90% rename from packages/contracts/src/components/position.cairo rename to packages/contracts/src/models/position.cairo index 33fff1f3..fff1d30b 100644 --- a/packages/contracts/src/components/position.cairo +++ b/packages/contracts/src/models/position.cairo @@ -1,4 +1,3 @@ -use dojo::serde::SerdeLen; use orion::numbers::signed_integer::i32::{i32, i32Impl}; @@ -36,7 +35,7 @@ impl VoxelCoordImpl of VoxelCoordTrait { } } -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct Position { #[key] id: u256, @@ -59,7 +58,7 @@ impl PositionIntoVoxelCoord of Into { } -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct PositionOccupation { #[key] hash: felt252, diff --git a/packages/contracts/src/components/recipe.cairo b/packages/contracts/src/models/recipe.cairo similarity index 62% rename from packages/contracts/src/components/recipe.cairo rename to packages/contracts/src/models/recipe.cairo index 7203b79b..747632eb 100644 --- a/packages/contracts/src/components/recipe.cairo +++ b/packages/contracts/src/models/recipe.cairo @@ -1,11 +1,11 @@ -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct Recipe { #[key] id: u256, value: felt252 } -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct RecipeReverseLookup { id: u256, #[key] diff --git a/packages/contracts/src/components/stake.cairo b/packages/contracts/src/models/stake.cairo similarity index 53% rename from packages/contracts/src/components/stake.cairo rename to packages/contracts/src/models/stake.cairo index 7a446996..e4fb264f 100644 --- a/packages/contracts/src/components/stake.cairo +++ b/packages/contracts/src/models/stake.cairo @@ -1,4 +1,4 @@ -#[derive(Component, Copy, Drop, Serde, SerdeLen)] +#[derive(Model, Copy, Drop, Serde)] struct Stake { #[key] id: u256, diff --git a/packages/contracts/src/prototypes/recipes.cairo b/packages/contracts/src/prototypes/recipes.cairo index fc9bf3ad..82041449 100644 --- a/packages/contracts/src/prototypes/recipes.cairo +++ b/packages/contracts/src/prototypes/recipes.cairo @@ -1,5 +1,5 @@ use comcraft::prototypes::blocks; -use comcraft::components::recipe::{Recipe, RecipeReverseLookup}; +use comcraft::models::recipe::{Recipe, RecipeReverseLookup}; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; diff --git a/packages/contracts/src/systems.cairo b/packages/contracts/src/systems.cairo index 54f4ab0b..f1eafcf6 100644 --- a/packages/contracts/src/systems.cairo +++ b/packages/contracts/src/systems.cairo @@ -1,12 +1,8 @@ mod build; mod claim; -mod stake; +mod initialization; +mod minecraft; +mod name; mod occurrence; -mod transfer; -mod mine; -mod init; -mod init2; -mod creative_build; -mod craft; -mod bulk_transfer; -mod bulk_stake; \ No newline at end of file +mod stake; +mod transfer; \ No newline at end of file diff --git a/packages/contracts/src/systems/build.cairo b/packages/contracts/src/systems/build.cairo index 67938aaa..95f8b88c 100644 --- a/packages/contracts/src/systems/build.cairo +++ b/packages/contracts/src/systems/build.cairo @@ -1,58 +1,121 @@ -#[system] -mod Build { +#[dojo::contract] +mod build_systems { use comcraft::alias::ID; - use comcraft::components::position::{Position, PositionOccupation, VoxelCoord, VoxelCoordTrait}; - use comcraft::components::owned_by::OwnedBy; - use comcraft::components::claim::Claim; - use comcraft::systems::claim::{MakeClaim}; + use comcraft::models::position::{Position, PositionOccupation, VoxelCoord, VoxelCoordTrait}; + use comcraft::models::owned_by::OwnedBy; + use comcraft::models::claim::Claim; + use comcraft::models::item::Item; + use comcraft::models::config::GameConfig; - use dojo::world::Context; + use comcraft::constants::GodID; + + use comcraft::systems::claim::claim_systems; use starknet::contract_address_const; + trait IBuildSystems { + fn build( + self: @TContractState, world: IWorldDispatcher, block_id: ID, coord: VoxelCoord + ); - fn execute(ctx: Context, block_id: ID, coord: VoxelCoord ) { - let block_owned_by = get!(ctx.world, block_id, OwnedBy); - assert(block_owned_by.address == ctx.origin, 'block not owned by player'); - - // Require no other ECS blocks at this position except Air - let VoxelCoord { x, y, z } = coord; - let position_occupation = get!( - ctx.world, - coord.hash(), - PositionOccupation + fn creative_build( + self: @TContractState, + world: IWorldDispatcher, + block_id: ID, + coord: VoxelCoord ); - if position_occupation.occupied_by_non_air != 0 { - assert(false, 'cant build at occupied coord'); - } + } - // Check claim in chunk - let claim = MakeClaim::get_claim_at_coord(ctx.world, coord); - if claim.claimer != contract_address_const::<0>() && claim.claimer != block_owned_by.address { - assert(false, 'cant build in claimed chunk'); + #[external(v0)] + impl BuildSystemsImpl of IBuildSystems { + + fn build( + self: @ContractState, world: IWorldDispatcher, block_id: ID, coord: VoxelCoord + ) { + let block_owned_by = get!(world, block_id, OwnedBy); + assert(block_owned_by.address == starknet::get_caller_address(), 'block not owned by player'); + + // Require no other ECS blocks at this position except Air + let VoxelCoord { x, y, z } = coord; + let position_occupation = get!( + world, + coord.hash(), + PositionOccupation + ); + if position_occupation.occupied_by_non_air != 0 { + assert(false, 'cant build at occupied coord'); + } + + // Check claim in chunk + let claim = claim_systems::get_claim_at_coord(world, coord); + if claim.claimer != contract_address_const::<0>() && claim.claimer != block_owned_by.address { + assert(false, 'cant build in claimed chunk'); + } + + set!(world, ( + PositionOccupation { + hash: coord.hash(), + occupied_by_non_air: block_id, + occupied_by_air: position_occupation.occupied_by_air + }, + Position { + id: block_id, + x: x.mag, + x_neg: x.sign, + y: y.mag, + y_neg: y.sign, + z: z.mag, + z_neg: z.sign, + }, + OwnedBy { + id: block_id, + address: contract_address_const::<0>() + } + ) + ); } - set!(ctx.world, ( - PositionOccupation { - hash: coord.hash(), - occupied_by_non_air: block_id, - occupied_by_air: position_occupation.occupied_by_air + + + fn creative_build( + self: @ContractState, + world: IWorldDispatcher, + block_id: ID, + coord: VoxelCoord + ) { + let GodID_u256: ID = GodID.into(); + let game_config = get!(world, GodID_u256, GameConfig); + assert(game_config.creative_mode == false, 'CREATIVE MODE DISABLED'); + + let item = get!(world, block_id, Item); + let block_type = item.value; + + let entity_id: u256 = world.uuid().into(); + let position_occupation = get!( + world, + coord.hash(), + PositionOccupation + ); + set!(world, ( + Item { + id: entity_id, + value: block_type }, Position { - id: block_id, - x: x.mag, - x_neg: x.sign, - y: y.mag, - y_neg: y.sign, - z: z.mag, - z_neg: z.sign, + id: entity_id, + x: coord.x.mag, + x_neg: coord.x.sign, + y: coord.y.mag, + y_neg: coord.y.sign, + z: coord.z.mag, + z_neg: coord.z.sign, }, - OwnedBy { - id: block_id, - address: contract_address_const::<0>() + PositionOccupation { + hash: coord.hash(), + occupied_by_non_air: entity_id, + occupied_by_air: position_occupation.occupied_by_air } - ) - ); + )); + } } - } \ No newline at end of file diff --git a/packages/contracts/src/systems/bulk_stake.cairo b/packages/contracts/src/systems/bulk_stake.cairo deleted file mode 100644 index b13d4729..00000000 --- a/packages/contracts/src/systems/bulk_stake.cairo +++ /dev/null @@ -1,61 +0,0 @@ -#[system] -mod BulkStake { - use comcraft::alias::ID; - use comcraft::components::position::{Coord}; - use comcraft::components::item::Item; - use comcraft::components::owned_by::OwnedBy; - use comcraft::components::stake::Stake; - use comcraft::systems::stake::{MakeStake}; - use comcraft::prototypes::{blocks}; - - use dojo::world::Context; - - use starknet::contract_address_const; - - use core::traits::Into; - use core::array::{ArrayTrait, SpanTrait}; - use core::option::OptionTrait; - - - fn execute(ctx: Context, block_ids: Span, chunk: Coord) { - - let mut i = 0; - loop { - if i >= block_ids.len() { - break; - } - - let block_id = *block_ids[i]; - let block_owned_by = get!(ctx.world, block_id, OwnedBy); - assert(block_owned_by.address == ctx.origin, 'block not owned by player'); - - // block must be diamond - let block_item = get!(ctx.world, block_id, Item); - assert(block_item.value == blocks::DiamondID, 'can only stake diamond blocks'); - - // Remove block from inventory and place it in the world - set!(ctx.world, ( - OwnedBy { - id: block_id, - address: contract_address_const::<0>() - }) - ); - - i+=1; - }; - - - // Increase stake in this chunk - let stake_id = MakeStake::get_stake_entity(chunk, ctx.origin); - let stake = MakeStake::get_stake_in_chunk(ctx.world, stake_id); - set!(ctx.world, ( - Stake { - id: stake_id, - value: stake.value + block_ids.len() - }) - ); - } - -} - - diff --git a/packages/contracts/src/systems/bulk_transfer.cairo b/packages/contracts/src/systems/bulk_transfer.cairo deleted file mode 100644 index 6f47f261..00000000 --- a/packages/contracts/src/systems/bulk_transfer.cairo +++ /dev/null @@ -1,37 +0,0 @@ -#[system] -mod BulkTransfer { - use comcraft::alias::ID; - use comcraft::components::owned_by::OwnedBy; - - use dojo::world::Context; - - use starknet::ContractAddress; - - use core::array::{SpanTrait}; - - fn execute(ctx: Context, item_ids: Span, receiver: ContractAddress) { - - let mut i = 0; - loop { - if i >= item_ids.len() { - break; - } - - let item_id = *item_ids[i]; - - // Require block to be owned by caller - let owned_by = get!(ctx.world, item_id, OwnedBy); - assert(owned_by.address == ctx.origin, 'not owned by player'); - - // Transfer ownership of the item - set!(ctx.world, ( - OwnedBy { - id: item_id, - address: receiver - } - )); - - i+=1; - }; - } -} diff --git a/packages/contracts/src/systems/claim.cairo b/packages/contracts/src/systems/claim.cairo index a8c7723d..7e7311c2 100644 --- a/packages/contracts/src/systems/claim.cairo +++ b/packages/contracts/src/systems/claim.cairo @@ -1,19 +1,48 @@ -#[system] -mod MakeClaim { +#[dojo::contract] +mod claim_systems { use comcraft::alias::ID; - use comcraft::components::position::{Coord, VoxelCoord }; - use comcraft::components::item::Item; - use comcraft::components::owned_by::OwnedBy; - use comcraft::components::claim::Claim; - use comcraft::components::stake::Stake; - use comcraft::systems::stake::{MakeStake}; + use comcraft::models::position::{Coord, VoxelCoord }; + use comcraft::models::item::Item; + use comcraft::models::owned_by::OwnedBy; + use comcraft::models::claim::Claim; + use comcraft::models::stake::Stake; + use comcraft::systems::stake::{stake_systems}; use comcraft::utils::{u64Helpers, get_chunk_coord}; - use dojo::world::Context; - use core::traits::Into; - use core::array::ArrayTrait; - use core::option::OptionTrait; + trait IClaimSystems { + fn make_claim(self: @TContractState, world: IWorldDispatcher, chunk: Coord ); + } + + #[external(v0)] + impl ClaimSystemsImpl of IClaimSystems { + + fn make_claim(self: @ContractState, world: IWorldDispatcher, chunk: Coord ) { + + let sender_stake_in_chunk: Stake = stake_systems::get_stake_in_chunk( + world, + stake_systems::get_stake_entity(chunk, starknet::get_caller_address()) + ); + + let current_claim_in_chunk: Claim = get_claim_in_chunk(world, chunk); + + assert( + sender_stake_in_chunk.value > current_claim_in_chunk.stake, + 'not enough staked' + ); + + // Claim this chunk + let chunk_entity: ID = get_chunk_entity(chunk); + set!(world, ( + Claim { + id: chunk_entity, + stake: sender_stake_in_chunk.value, + claimer: starknet::get_caller_address() + } + )); + } + } + // Chunk entity = concat(chunk.x | chunk.y) @@ -30,19 +59,4 @@ mod MakeClaim { } - fn execute(ctx: Context, chunk: Coord ) { - - let sender_stake_in_chunk: Stake = MakeStake::get_stake_in_chunk( - ctx.world, - MakeStake::get_stake_entity(chunk, ctx.origin) - ); - let current_claim_in_chunk: Claim = get_claim_in_chunk(ctx.world, chunk); - - assert(sender_stake_in_chunk.value > current_claim_in_chunk.stake, 'not enough staked'); - - // Claim this chunk - let chunk_entity: ID = get_chunk_entity(chunk); - set!(ctx.world, Claim {id: chunk_entity, stake: sender_stake_in_chunk.value, claimer: ctx.origin}); - } - } \ No newline at end of file diff --git a/packages/contracts/src/systems/craft.cairo b/packages/contracts/src/systems/craft.cairo deleted file mode 100644 index 1fbcb726..00000000 --- a/packages/contracts/src/systems/craft.cairo +++ /dev/null @@ -1,79 +0,0 @@ -#[system] -mod Craft { - use comcraft::alias::ID; - use comcraft::components::item::Item; - use comcraft::components::owned_by::OwnedBy; - use comcraft::components::recipe::{Recipe, RecipeReverseLookup}; - - use dojo::world::Context; - - use starknet::contract_address_const; - - use core::traits::{Into, TryInto}; - use core::option::OptionTrait; - use core::array::{SpanTrait,ArrayTrait}; - use core::poseidon::poseidon_hash_span; - - - - fn execute(ctx: Context, ingredients: Span) -> ID { - - let mut ingredient_types: Array = array![]; - - let mut i = 0; - loop { - if i >= ingredients.len() { - break; - } - let ingredient = *ingredients[i]; - if ingredient != 0 { - let owned_by = get!(ctx.world, ingredient, OwnedBy); - assert(owned_by.address == ctx.origin, 'not owned by player'); - - let ingredient_item = get!(ctx.world, ingredient, Item); - ingredient_types.append(ingredient_item.value); - } - i+=1; - }; - - let recipe_hash: felt252 = poseidon_hash_span(ingredient_types.span()); - - let recipe = get!(ctx.world, recipe_hash, RecipeReverseLookup); - assert(recipe.id != 0 , 'no recipes found'); - - // Burn all ingredients - let mut i = 0; - loop { - if i >= ingredients.len() { - break; - } - let ingredient = *ingredients[i]; - if ingredient != 0 { - set!(ctx.world, ( - OwnedBy { - id: ingredient, - address: contract_address_const::<0>() - } - )); - } - i+=1; - }; - - // Create the output entity - let entity_id: u256 = ctx.world.uuid().into(); - set!(ctx.world, ( - OwnedBy { - id: entity_id, - address: ctx.origin - }, - Item { - id: entity_id, - value: (recipe.id).try_into().unwrap() - } - )); - - - entity_id - - } -} diff --git a/packages/contracts/src/systems/creative_build.cairo b/packages/contracts/src/systems/creative_build.cairo deleted file mode 100644 index 36093bf4..00000000 --- a/packages/contracts/src/systems/creative_build.cairo +++ /dev/null @@ -1,49 +0,0 @@ -#[system] -mod CreativeBuild { - use comcraft::alias::ID; - use comcraft::components::config::GameConfig; - use comcraft::components::item::Item; - use comcraft::components::position::{Position, PositionOccupation, VoxelCoord, VoxelCoordTrait}; - - use comcraft::constants::GodID; - - use dojo::world::Context; - - use core::traits::Into; - - fn execute(ctx: Context, block_id: ID, coord: VoxelCoord) { - let GodID_u256: ID = GodID.into(); - let game_config = get!(ctx.world, GodID_u256, GameConfig); - assert(game_config.creative_mode == false, 'CREATIVE MODE DISABLED'); - - let item = get!(ctx.world, block_id, Item); - let block_type = item.value; - - let entity_id: u256 = ctx.world.uuid().into(); - let position_occupation = get!( - ctx.world, - coord.hash(), - PositionOccupation - ); - set!(ctx.world, ( - Item { - id: entity_id, - value: block_type - }, - Position { - id: entity_id, - x: coord.x.mag, - x_neg: coord.x.sign, - y: coord.y.mag, - y_neg: coord.y.sign, - z: coord.z.mag, - z_neg: coord.z.sign, - }, - PositionOccupation { - hash: coord.hash(), - occupied_by_non_air: entity_id, - occupied_by_air: position_occupation.occupied_by_air - } - )); - } -} diff --git a/packages/contracts/src/systems/init.cairo b/packages/contracts/src/systems/init.cairo deleted file mode 100644 index 43ee08b7..00000000 --- a/packages/contracts/src/systems/init.cairo +++ /dev/null @@ -1,39 +0,0 @@ -#[system] -mod Init { - use comcraft::alias::ID; - use comcraft::components::config::GameConfig; - use comcraft::components::item::ItemPrototype; - use comcraft::prototypes::blocks::block_types; - use comcraft::constants::GodID; - - use dojo::world::Context; - - use core::array::ArrayTrait; - use core::traits::Into; - - fn execute(ctx: Context) { - - set!(ctx.world, ( - GameConfig { - id: GodID.into(), - creative_mode: false - } - )); - - let mut i = 0; - let blocks = block_types(); - loop { - if i >= blocks.len() { - break; - } - set!(ctx.world, ( - ItemPrototype { - id: (*blocks[i]).into(), - value: true - } - )) - i += 1; - }; - } - -} diff --git a/packages/contracts/src/systems/init2.cairo b/packages/contracts/src/systems/init2.cairo deleted file mode 100644 index b52381c2..00000000 --- a/packages/contracts/src/systems/init2.cairo +++ /dev/null @@ -1,10 +0,0 @@ -#[system] -mod Init2 { - use comcraft::prototypes::recipes; - use dojo::world::Context; - - fn execute(ctx: Context) { - recipes::define_recipes(ctx.world); - } - -} diff --git a/packages/contracts/src/systems/initialization.cairo b/packages/contracts/src/systems/initialization.cairo new file mode 100644 index 00000000..c8cd47b3 --- /dev/null +++ b/packages/contracts/src/systems/initialization.cairo @@ -0,0 +1,50 @@ +#[dojo::contract] +mod initialization_systems { + + use comcraft::alias::ID; + use comcraft::models::config::GameConfig; + use comcraft::models::item::ItemPrototype; + use comcraft::prototypes::blocks::block_types; + use comcraft::constants::GodID; + use comcraft::prototypes::recipes; + + trait IInitializationSystems { + fn init(self: @TContractState, world: IWorldDispatcher); + fn init2(self: @TContractState, world: IWorldDispatcher); + } + + #[external(v0)] + impl InitializationSystemsImpl of IInitializationSystems { + + fn init(self: @ContractState, world: IWorldDispatcher) { + + set!(world, ( + GameConfig { + id: GodID.into(), + creative_mode: false + } + )); + + let mut i = 0; + let blocks = block_types(); + loop { + if i >= blocks.len() { + break; + } + set!(world, ( + ItemPrototype { + id: (*blocks[i]).into(), + value: true + } + )); + i += 1; + }; + } + + + fn init2(self: @ContractState, world: IWorldDispatcher) { + recipes::define_recipes(world); + } + } + +} diff --git a/packages/contracts/src/systems/mine.cairo b/packages/contracts/src/systems/mine.cairo deleted file mode 100644 index ccb50d4c..00000000 --- a/packages/contracts/src/systems/mine.cairo +++ /dev/null @@ -1,118 +0,0 @@ -#[system] -mod Mine { - use comcraft::alias::ID; - use comcraft::components::item::Item; - use comcraft::components::owned_by::OwnedBy; - - use comcraft::components::position::{VoxelCoord, VoxelCoordTrait, PositionOccupation, Position}; - use comcraft::systems::claim::{MakeClaim}; - use comcraft::systems::occurrence::{Occurence}; - use comcraft::libraries::terrain; - use comcraft::prototypes::blocks; - - use orion::numbers::signed_integer::i32::{i32, i32Impl}; - - use dojo::world::Context; - - use starknet::ContractAddress; - use starknet::contract_address_const; - - use core::traits::Into; - - - - fn execute(ctx: Context, coord: VoxelCoord, block_type: felt252) -> ID { - - assert(block_type != blocks::AirID, 'can not mine air'); - assert(block_type != blocks::WaterID, 'can not mine water'); - assert(coord.y < i32Impl::new(256,false) && coord.y >= i32Impl::new(63, true), 'out of chunk bounds'); - - let claimer = MakeClaim::get_claim_at_coord(ctx.world, coord).claimer; - assert(claimer == contract_address_const::<0>() || claimer == ctx.origin, 'cant mine in claimed chunk'); - - let position_occupation = get!( - ctx.world, - coord.hash(), - (PositionOccupation) - ); - let mut entity_id: ID = 0; - - if (position_occupation.occupied_by_non_air == 0 && position_occupation.occupied_by_air == 0) { - // If there is no entity at this position, - // try mining the terrain block at this position - let occurrence = Occurence::get_occurrence(block_type, coord); - assert( occurrence != 0 && occurrence == block_type, 'invalid terrain block type'); - - // Create an ECS block from this coord's terrain block - entity_id = ctx.world.uuid().into(); - set!(ctx.world, ( - Item { - id: entity_id, - value: block_type - } - )); - - // Place an air block at this position - let air_entity: u256 = ctx.world.uuid().into(); - set!(ctx.world, ( - Item { - id: air_entity, - value: blocks::AirID - }, - Position { - id: air_entity, - x: coord.x.mag, - x_neg: coord.x.sign, - y: coord.y.mag, - y_neg: coord.y.sign, - z: coord.z.mag, - z_neg: coord.z.sign, - }, - PositionOccupation { - hash: coord.hash(), - occupied_by_air: air_entity, - occupied_by_non_air: 0 - } - )); - - } else { - - // Else, mine the non-air entity block at this position - entity_id = position_occupation.occupied_by_non_air; - assert(entity_id != 0, 'invalid block type e:a'); - - let item = get!(ctx.world, entity_id, (Item)); - assert(item.value == block_type, 'invalid block type e:b'); - - set!(ctx.world, ( - // remove position - Position { - id: entity_id, - x: 0, - x_neg: false, - y: 0, - y_neg: false, - z: 0, - z_neg: false, - }, - PositionOccupation { - hash: coord.hash(), - occupied_by_air: position_occupation.occupied_by_air, - occupied_by_non_air: 0 - } - ) - ); - } - - set!(ctx.world, ( - OwnedBy { - id: entity_id, - address: ctx.origin - } - )); - - entity_id - - } - -} diff --git a/packages/contracts/src/systems/minecraft.cairo b/packages/contracts/src/systems/minecraft.cairo new file mode 100644 index 00000000..44838362 --- /dev/null +++ b/packages/contracts/src/systems/minecraft.cairo @@ -0,0 +1,199 @@ +#[dojo::contract] +mod minecraft_systems { + use comcraft::alias::ID; + use comcraft::models::item::Item; + use comcraft::models::owned_by::OwnedBy; + + use comcraft::models::position::{VoxelCoord, VoxelCoordTrait, PositionOccupation, Position}; + use comcraft::systems::claim::{claim_systems}; + use comcraft::systems::occurrence::{occurence_systems}; + use comcraft::libraries::terrain; + use comcraft::prototypes::blocks; + + use orion::numbers::signed_integer::i32::{i32, i32Impl}; + + use starknet::ContractAddress; + use starknet::contract_address_const; + + use comcraft::models::recipe::RecipeReverseLookup; + + + use core::poseidon::poseidon_hash_span; + + + trait IMineCraftSystems { + fn mine( + self: @TContractState, + world: IWorldDispatcher, + coord: VoxelCoord, + block_type: felt252 + ) -> ID; + + fn craft(self: @TContractState, world: IWorldDispatcher, ingredients: Span) -> ID; + } + + #[external(v0)] + impl MineCraftSystemsImpl of IMineCraftSystems { + + fn mine( + self: @ContractState, + world: IWorldDispatcher, + coord: VoxelCoord, + block_type: felt252 + ) -> ID { + + assert(block_type != blocks::AirID, 'can not mine air'); + assert(block_type != blocks::WaterID, 'can not mine water'); + assert(coord.y < i32Impl::new(256,false) && coord.y >= i32Impl::new(63, true), 'out of chunk bounds'); + + let claimer = claim_systems::get_claim_at_coord(world, coord).claimer; + assert(claimer == contract_address_const::<0>() || claimer == starknet::get_caller_address(), 'cant mine in claimed chunk'); + + let position_occupation = get!( + world, + coord.hash(), + (PositionOccupation) + ); + let mut entity_id: ID = 0; + + if (position_occupation.occupied_by_non_air == 0 && position_occupation.occupied_by_air == 0) { + // If there is no entity at this position, + // try mining the terrain block at this position + let occurrence = occurence_systems::get_occurrence(block_type, coord); + assert( occurrence != 0 && occurrence == block_type, 'invalid terrain block type'); + + // Create an ECS block from this coord's terrain block + entity_id = world.uuid().into(); + set!(world, ( + Item { + id: entity_id, + value: block_type + } + )); + + // Place an air block at this position + let air_entity: u256 = world.uuid().into(); + set!(world, ( + Item { + id: air_entity, + value: blocks::AirID + }, + Position { + id: air_entity, + x: coord.x.mag, + x_neg: coord.x.sign, + y: coord.y.mag, + y_neg: coord.y.sign, + z: coord.z.mag, + z_neg: coord.z.sign, + }, + PositionOccupation { + hash: coord.hash(), + occupied_by_air: air_entity, + occupied_by_non_air: 0 + } + )); + + } else { + + // Else, mine the non-air entity block at this position + entity_id = position_occupation.occupied_by_non_air; + assert(entity_id != 0, 'invalid block type e:a'); + + let item = get!(world, entity_id, (Item)); + assert(item.value == block_type, 'invalid block type e:b'); + + set!(world, ( + // remove position + Position { + id: entity_id, + x: 0, + x_neg: false, + y: 0, + y_neg: false, + z: 0, + z_neg: false, + }, + PositionOccupation { + hash: coord.hash(), + occupied_by_air: position_occupation.occupied_by_air, + occupied_by_non_air: 0 + } + ) + ); + } + + set!(world, ( + OwnedBy { + id: entity_id, + address: starknet::get_caller_address() + } + )); + + entity_id + } + + + + fn craft(self: @ContractState, world: IWorldDispatcher, ingredients: Span) -> ID { + + let mut ingredient_types: Array = array![]; + + let mut i = 0; + loop { + if i >= ingredients.len() { + break; + } + let ingredient = *ingredients[i]; + if ingredient != 0 { + let owned_by = get!(world, ingredient, OwnedBy); + assert(owned_by.address == starknet::get_caller_address(), 'not owned by player'); + + let ingredient_item = get!(world, ingredient, Item); + ingredient_types.append(ingredient_item.value); + } + i+=1; + }; + + let recipe_hash: felt252 = poseidon_hash_span(ingredient_types.span()); + + let recipe = get!(world, recipe_hash, RecipeReverseLookup); + assert(recipe.id != 0 , 'no recipes found'); + + // Burn all ingredients + let mut i = 0; + loop { + if i >= ingredients.len() { + break; + } + let ingredient = *ingredients[i]; + if ingredient != 0 { + set!(world, ( + OwnedBy { + id: ingredient, + address: contract_address_const::<0>() + } + )); + } + i+=1; + }; + + // Create the output entity + let entity_id: u256 = world.uuid().into(); + set!(world, ( + OwnedBy { + id: entity_id, + address: starknet::get_caller_address() + }, + Item { + id: entity_id, + value: (recipe.id).try_into().unwrap() + } + )); + + + entity_id + + } + } +} diff --git a/packages/contracts/src/systems/name.cairo b/packages/contracts/src/systems/name.cairo index 3988da16..da881d1c 100644 --- a/packages/contracts/src/systems/name.cairo +++ b/packages/contracts/src/systems/name.cairo @@ -1,21 +1,30 @@ -#[system] -mod AddName { +#[dojo::contract] +mod name_systems { use comcraft::alias::ID; - use comcraft::components::name::Name; + use comcraft::models::name::Name; - use dojo::world::Context; - - fn execute(ctx: Context, entity_id: ID, name: felt252) { + trait INameSystems { + fn add_name(self: @TContractState, world: IWorldDispatcher, entity_id: ID, name: felt252); + } + + #[external(v0)] + impl NameSystemsImpl of INameSystems { + fn add_name( + self: @ContractState, world: IWorldDispatcher, entity_id: ID, name: felt252 + ) { - assert(ctx.origin == 0x27C41B2D2368085EF6fe7Dd66Cf32EB01e0e0658, 'Only oracle can call'); + // assert( + // starknet::get_caller_address() == 0x27C41B2D2368085EF6fe7Dd66Cf32EB01e0e0658, + // 'Only oracle can call' + // ); - set!(ctx.world, ( - Name { - id: entity_id, - value: name, - }) - ); + set!(world, ( + Name { + id: entity_id, + value: name, + }) + ); + } } -} - - + +} \ No newline at end of file diff --git a/packages/contracts/src/systems/occurrence.cairo b/packages/contracts/src/systems/occurrence.cairo index 85db2ee3..65aec790 100644 --- a/packages/contracts/src/systems/occurrence.cairo +++ b/packages/contracts/src/systems/occurrence.cairo @@ -1,15 +1,33 @@ -#[system] -mod Occurence { - use comcraft::components::position::{VoxelCoord}; +#[dojo::contract] +mod occurence_systems { + use comcraft::models::position::{VoxelCoord}; use comcraft::libraries::terrain; use comcraft::prototypes::blocks; - use dojo::world::Context; - fn execute(ctx: Context, block_type: felt252, coord: VoxelCoord) -> felt252 { - get_occurrence(block_type, coord) + trait IOccurenceSystems { + fn check_occurence( + self: @TContractState, + world: IWorldDispatcher, + block_type: felt252, + coord: VoxelCoord + ) -> felt252; } + #[external(v0)] + impl OccurenceSystemsImpl of IOccurenceSystems { + + fn check_occurence( + self: @ContractState, + world: IWorldDispatcher, + block_type: felt252, + coord: VoxelCoord + ) -> felt252 { + get_occurrence(block_type, coord) + } + } + + fn get_occurrence(block_type: felt252, coord: VoxelCoord) -> felt252 { if block_type == blocks::GrassID {return terrain::Grass(coord);} if block_type == blocks::DirtID {return terrain::Dirt(coord);} diff --git a/packages/contracts/src/systems/stake.cairo b/packages/contracts/src/systems/stake.cairo index 76573a64..c4a0d6e4 100644 --- a/packages/contracts/src/systems/stake.cairo +++ b/packages/contracts/src/systems/stake.cairo @@ -1,22 +1,114 @@ -#[system] -mod MakeStake { +#[dojo::contract] +mod stake_systems { use comcraft::alias::ID; - use comcraft::components::position::{Coord, VoxelCoord}; - use comcraft::components::item::Item; - use comcraft::components::owned_by::OwnedBy; - use comcraft::components::claim::Claim; - use comcraft::components::stake::Stake; + use comcraft::models::position::{Coord, VoxelCoord}; + use comcraft::models::item::Item; + use comcraft::models::owned_by::OwnedBy; + use comcraft::models::claim::Claim; + use comcraft::models::stake::Stake; + use comcraft::prototypes::{blocks}; use comcraft::utils::{u256Helpers, u64Helpers, get_chunk_coord}; use starknet::ContractAddress; use starknet::contract_address_const; + + trait IStakeSystems { + fn stake( + self: @TContractState, + world: IWorldDispatcher, + block_id: ID, + chunk: Coord + ); + + fn bulk_stake( + self: @TContractState, + world: IWorldDispatcher, + block_ids: Span, + chunk: Coord + ); + } + + #[external(v0)] + impl StakeSystemsImpl of IStakeSystems { + fn stake( + self: @ContractState, + world: IWorldDispatcher, + block_id: ID, + chunk: Coord + ) { + let block_owned_by = get!(world, block_id, OwnedBy); + assert(block_owned_by.address == starknet::get_caller_address(), 'block not owned by player'); + + let block_item = get!(world, block_id, Item); + assert(block_item.value == blocks::DiamondID, 'can only stake diamond blocks'); + // Remove block from inventory and place it in the world + set!(world, ( + OwnedBy { + id: block_id, + address: contract_address_const::<0>() + }) + ); + + // Increase stake in this chunk + let stake_id = get_stake_entity(chunk, starknet::get_caller_address()); + let stake = get_stake_in_chunk(world, stake_id); + set!(world, ( + Stake { + id: stake_id, + value: stake.value + 1 + }) + ); + } + + + + + + fn bulk_stake( + self: @ContractState, + world: IWorldDispatcher, + block_ids: Span, + chunk: Coord + ) { - use dojo::world::Context; + let mut i = 0; + loop { + if i >= block_ids.len() { + break; + } - use core::traits::Into; - use core::array::ArrayTrait; - use core::option::OptionTrait; + let block_id = *block_ids[i]; + let block_owned_by = get!(world, block_id, OwnedBy); + assert(block_owned_by.address == starknet::get_caller_address(), 'block not owned by player'); + + // block must be diamond + let block_item = get!(world, block_id, Item); + assert(block_item.value == blocks::DiamondID, 'can only stake diamond blocks'); + + // Remove block from inventory and place it in the world + set!(world, ( + OwnedBy { + id: block_id, + address: contract_address_const::<0>() + }) + ); + + i+=1; + }; + + + // Increase stake in this chunk + let stake_id = get_stake_entity(chunk, starknet::get_caller_address()); + let stake = get_stake_in_chunk(world, stake_id); + set!(world, ( + Stake { + id: stake_id, + value: stake.value + block_ids.len() + }) + ); + } + } // Stake entity = concat(address | chunk.x | chunk.y) @@ -30,32 +122,7 @@ mod MakeStake { get!(world, stake_id, Stake) } - fn execute(ctx: Context, block_id: ID, chunk: Coord) { - let block_owned_by = get!(ctx.world, block_id, OwnedBy); - assert(block_owned_by.address == ctx.origin, 'block not owned by player'); - - let block_item = get!(ctx.world, block_id, Item); - assert(block_item.value == blocks::DiamondID, 'can only stake diamond blocks'); - - // Remove block from inventory and place it in the world - set!(ctx.world, ( - OwnedBy { - id: block_id, - address: contract_address_const::<0>() - }) - ); - - // Increase stake in this chunk - let stake_id = get_stake_entity(chunk, ctx.origin); - let stake = get_stake_in_chunk(ctx.world, stake_id); - set!(ctx.world, ( - Stake { - id: stake_id, - value: stake.value + 1 - }) - ); - } } diff --git a/packages/contracts/src/systems/transfer.cairo b/packages/contracts/src/systems/transfer.cairo index fc72d4a6..7c9fd8aa 100644 --- a/packages/contracts/src/systems/transfer.cairo +++ b/packages/contracts/src/systems/transfer.cairo @@ -1,25 +1,78 @@ -#[system] -mod Transfer { +#[dojo::contract] +mod transfer_systems { use comcraft::alias::ID; - use comcraft::components::item::Item; - use comcraft::components::owned_by::OwnedBy; + use comcraft::models::item::Item; + use comcraft::models::owned_by::OwnedBy; use starknet::ContractAddress; - use dojo::world::Context; - - fn execute(ctx: Context, item_id: ID, receiver: ContractAddress) { - - let block_owned_by = get!(ctx.world, item_id, OwnedBy); - assert(block_owned_by.address == ctx.origin, 'block not owned by player'); + trait ITransferSystems { + fn transfer( + self: @TContractState, + world: IWorldDispatcher, + item_id: ID, + receiver: ContractAddress + ); - set!(ctx.world, ( - OwnedBy { - id: item_id, - address: receiver - }) + fn bulk_transfer( + self: @TContractState, + world: IWorldDispatcher, + item_ids: Span, + receiver: ContractAddress ); } - -} - + + #[external(v0)] + impl TransferSystemsImpl of ITransferSystems { + fn transfer( + self: @ContractState, + world: IWorldDispatcher, + item_id: ID, + receiver: ContractAddress + ) { + + let block_owned_by = get!(world, item_id, OwnedBy); + assert(block_owned_by.address == starknet::get_caller_address(), 'block not owned by player'); + + set!(world, ( + OwnedBy { + id: item_id, + address: receiver + }) + ); + } + + + + fn bulk_transfer( + self: @ContractState, + world: IWorldDispatcher, + item_ids: Span, + receiver: ContractAddress + ) { + + let mut i = 0; + loop { + if i >= item_ids.len() { + break; + } + + let item_id = *item_ids[i]; + + // Require block to be owned by caller + let owned_by = get!(world, item_id, OwnedBy); + assert(owned_by.address == starknet::get_caller_address(), 'not owned by player'); + + // Transfer ownership of the item + set!(world, ( + OwnedBy { + id: item_id, + address: receiver + } + )); + + i+=1; + }; + } + } +} \ No newline at end of file diff --git a/packages/contracts/src/utils.cairo b/packages/contracts/src/utils.cairo index 3f3bba8f..18ed3e5d 100644 --- a/packages/contracts/src/utils.cairo +++ b/packages/contracts/src/utils.cairo @@ -1,5 +1,5 @@ use comcraft::constants::CHUNK; -use comcraft::components::position::{Coord, VoxelCoord}; +use comcraft::models::position::{Coord, VoxelCoord}; mod u64Helpers {