Skip to content

Commit

Permalink
Merge pull request #12 from dojoengine/hex
Browse files Browse the repository at this point in the history
Hex
  • Loading branch information
ponderingdemocritus authored Nov 25, 2023
2 parents cd03cff + 61e5495 commit 5714007
Show file tree
Hide file tree
Showing 16 changed files with 632 additions and 70 deletions.
6 changes: 6 additions & 0 deletions .github/Origami.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Origami CI
on: [push, pull_request]

env:
DOJO_VERSION: v0.3.10
DOJO_VERSION: v0.3.12
SCARB_VERSION: v2.3.1

jobs:
Expand Down Expand Up @@ -41,7 +41,6 @@ jobs:
run: sozo test -f origami
shell: bash


market:
needs: [check, build]
runs-on: ubuntu-latest
Expand All @@ -62,4 +61,4 @@ jobs:
- uses: ./.github/actions/setup
- name: Test
run: sozo test -f projectile
shell: bash
shell: bash
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
## Origami
<!-- markdownlint-disable -->
<div align="center">
<img src=".github/Origami.svg" height="128">
</div>
<div align="center">
<br />
<!-- markdownlint-restore -->

### The magic of origami is in seeing a single piece of cairo evolve into a masterpiece through careful folds.
<a href="https://twitter.com/dojostarknet">
<img src="https://img.shields.io/twitter/follow/dojostarknet?style=social"/>
</a>
<a href="https://github.com/dojoengine/dojo">
<img src="https://img.shields.io/github/stars/dojoengine/dojo?style=social"/>
</a>

### Easy Integration into Your Project
[![discord](https://img.shields.io/badge/join-dojo-green?logo=discord&logoColor=white)](https://discord.gg/PwDa2mKhR4)
![Github Actions][gha-badge] [![Telegram Chat][tg-badge]][tg-url]

[gha-badge]: https://img.shields.io/github/actions/workflow/status/dojoengine/dojo/ci.yml?branch=main
[tg-badge]: https://img.shields.io/endpoint?color=neon&logo=telegram&label=chat&style=flat-square&url=https%3A%2F%2Ftg.sumanjay.workers.dev%2Fdojoengine
[tg-url]: https://t.me/dojoengine

</div>

# Origami - Primitives for onchain Gaming

#### "The magic of origami is in seeing a single piece of cairo evolve into a masterpiece through careful folds" - Sensei

---

### Crates

- [algebra](./crates/src/algebra)
- [defi](./crates//src/defi/)
- [hex map](./crates/src/map/hex)
- [random](./crates/src/random)
- [security](./crates/src/security)

### Easy integration into your project

Incorporate `origami` seamlessly into your projects using Scarb.toml.

Add the following to your `[dependencies]`:

```
```toml
[dependencies]
origami = { git = "https://github.com/OpenZeppelin/cairo-contracts.git" }
```

Now you will be able to use origami like any other Cairo package.
Now you will be able to use origami like any other Cairo package!

### 🏗️ Join Our Contributors

Expand Down
18 changes: 14 additions & 4 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ source = "git+https://github.com/influenceth/cubit?rev=b459053#b4590530d5aeae9aa

[[package]]
name = "dojo"
version = "0.3.10"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.10#be1676252c41f8188465741edcbcb381d2c12080"
version = "0.3.12"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.12#12d58f29ec53454317f1f6d265007a053d279288"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.10"
version = "0.3.11"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659"

[[package]]
name = "hex_map"
version = "0.0.0"
dependencies = [
"cubit",
"dojo",
"origami",
]

[[package]]
name = "market"
Expand All @@ -28,7 +38,7 @@ dependencies = [

[[package]]
name = "origami"
version = "0.0.0"
version = "0.1.0"
dependencies = [
"cubit",
"dojo",
Expand Down
13 changes: 4 additions & 9 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
[workspace]
members = [
"crates",
"examples/*",
]
members = ["crates", "examples/*"]

[workspace.package]
version = "0.0.0"
version = "0.1.0"
description = "Community-maintained libraries for Cairo"
homepage = "https://github.com/dojoengine/origami"
authors = [
"[email protected]",
]
authors = ["[email protected]"]

[workspace.dependencies]
cubit = { git = "https://github.com/influenceth/cubit", rev = "b459053" }
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.10" }
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.12" }
origami = { path = "crates" }
7 changes: 7 additions & 0 deletions crates/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ mod random {
mod security {
mod commitment;
}

mod map {
mod hex {
mod hex;
mod types;
}
}
134 changes: 134 additions & 0 deletions crates/src/map/hex/hex.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use origami::map::hex::{types::{HexTile, Direction, DirectionIntoFelt252}};

trait IHexTile {
fn new(col: u32, row: u32) -> HexTile;
fn neighbor(self: HexTile, direction: Direction) -> HexTile;
fn neighbors(self: HexTile) -> Array<HexTile>;
fn is_neighbor(self: HexTile, other: HexTile) -> bool;
}

impl ImplHexTile of IHexTile {
fn new(col: u32, row: u32) -> HexTile {
HexTile { col, row }
}

fn neighbor(self: HexTile, direction: Direction) -> HexTile {
match direction {
Direction::East(()) => HexTile { col: self.col + 1, row: self.row },
Direction::NorthEast(()) => HexTile { col: self.col + 1, row: self.row - 1 },
Direction::NorthWest(()) => HexTile { col: self.col, row: self.row - 1 },
Direction::West(()) => HexTile { col: self.col - 1, row: self.row },
Direction::SouthWest(()) => HexTile { col: self.col, row: self.row + 1 },
Direction::SouthEast(()) => HexTile { col: self.col + 1, row: self.row + 1 },
}
}

fn neighbors(self: HexTile) -> Array<HexTile> {
array![
self.neighbor(Direction::East(())),
self.neighbor(Direction::NorthEast(())),
self.neighbor(Direction::NorthWest(())),
self.neighbor(Direction::West(())),
self.neighbor(Direction::SouthWest(())),
self.neighbor(Direction::SouthEast(()))
]
}

fn is_neighbor(self: HexTile, other: HexTile) -> bool {
let mut neighbors = self.neighbors();

loop {
if (neighbors.len() == 0) {
break false;
}

let curent_neighbor = neighbors.pop_front().unwrap();

if (curent_neighbor.col == other.col) {
if (curent_neighbor.row == other.row) {
break true;
}
};
}
}
}


// tests ----------------------------------------------------------------------- //

#[cfg(test)]
mod tests {
use super::{IHexTile, ImplHexTile, Direction, HexTile};
#[test]
#[available_gas(500000)]
fn test_row_col() {
let mut hex_tile = ImplHexTile::new(5, 5);

assert(hex_tile.col == 5, 'col should be 5');
assert(hex_tile.row == 5, 'row should be 5');
}


#[test]
#[available_gas(500000)]
fn test_hex_tile_neighbors() {
let mut hex_tile = ImplHexTile::new(5, 5);

let east_neighbor = hex_tile.neighbor(Direction::East(()));

assert(east_neighbor.col == 6, 'col should be 7');
assert(east_neighbor.row == 5, 'row should be 5');

let north_east_neighbor = hex_tile.neighbor(Direction::NorthEast(()));

assert(north_east_neighbor.col == 6, 'col should be 6');
assert(north_east_neighbor.row == 4, 'row should be 4');

let north_west_neighbor = hex_tile.neighbor(Direction::NorthWest(()));

assert(north_west_neighbor.col == 5, 'col should be 5');
assert(north_west_neighbor.row == 4, 'row should be 4');

let west_neighbor = hex_tile.neighbor(Direction::West(()));

assert(west_neighbor.col == 4, 'col should be 3');
assert(west_neighbor.row == 5, 'row should be 5');

let south_west_neighbor = hex_tile.neighbor(Direction::SouthWest(()));

assert(south_west_neighbor.col == 5, 'col should be 4');
assert(south_west_neighbor.row == 6, 'row should be 6');

let south_east_neighbor = hex_tile.neighbor(Direction::SouthEast(()));

assert(south_east_neighbor.col == 6, 'col should be 5');
assert(south_east_neighbor.row == 6, 'row should be 6');
}

#[test]
#[available_gas(501230000)]
fn test_is_neighbor() {
let mut hex_tile = ImplHexTile::new(5, 5);

assert(hex_tile.is_neighbor(HexTile { col: hex_tile.col + 1, row: hex_tile.row }), 'east');

assert(
hex_tile.is_neighbor(HexTile { col: hex_tile.col, row: hex_tile.row + 1 }), 'north east'
);

assert(
hex_tile.is_neighbor(HexTile { col: hex_tile.col, row: hex_tile.row - 1 }), 'north west'
);

assert(hex_tile.is_neighbor(HexTile { col: hex_tile.col - 1, row: hex_tile.row }), 'west');

assert(
hex_tile.is_neighbor(HexTile { col: hex_tile.col, row: hex_tile.row - 1 }), 'south west'
);

assert(
hex_tile.is_neighbor(HexTile { col: hex_tile.col + 1, row: hex_tile.row - 1 }),
'south east'
);
}
}
28 changes: 28 additions & 0 deletions crates/src/map/hex/types.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[derive(Drop, Copy, Serde)]
struct HexTile {
col: u32,
row: u32,
}

#[derive(Drop, Copy, Serde)]
enum Direction {
East: (),
NorthEast: (),
NorthWest: (),
West: (),
SouthWest: (),
SouthEast: (),
}

impl DirectionIntoFelt252 of Into<Direction, felt252> {
fn into(self: Direction) -> felt252 {
match self {
Direction::East => 0,
Direction::NorthEast => 1,
Direction::NorthWest => 2,
Direction::West => 3,
Direction::SouthWest => 4,
Direction::SouthEast => 5,
}
}
}
10 changes: 10 additions & 0 deletions examples/hex_map/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "hex_map"
version = "0.0.0"
description = "Example Hex Map with noise"
homepage = "https://github.com/dojoengine/origami/tree/examples/hex_map"

[dependencies]
cubit.workspace = true
dojo.workspace = true
origami.workspace = true
5 changes: 5 additions & 0 deletions examples/hex_map/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Hex map with noise

This example uses the hex grid origami component along with cubits simplex noise.

You can replicate the noise in a client to mirror the onchain noise with the offchain noise to provide users with a rich map to explore.
Loading

0 comments on commit 5714007

Please sign in to comment.