Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyramid example #74

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Examples
A few small examples to show the capabilities of `mcpp`.

They should be compilable with something along the lines of `g++ -std=c++17 -o example example.cpp -lmcpp`
after you have installed the library.

Feel free to open a PR if you have an interesting usecase to include as part of the examples.
52 changes: 52 additions & 0 deletions example/pyramid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <mcpp/block.h>

Check notice on line 1 in example/pyramid.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on example/pyramid.cpp

File example/pyramid.cpp does not conform to Custom style guidelines. (lines 1, 7, 15, 16, 17, 18, 23, 24, 25, 26, 34, 35, 36, 40, 41, 42, 49, 51)
rozukke marked this conversation as resolved.
Show resolved Hide resolved
#include <mcpp/mcpp.h>
#include <mcpp/util.h>
#include <algorithm>
rozukke marked this conversation as resolved.
Show resolved Hide resolved

// Change location here
mcpp::Coordinate ORIGIN {0, 0, 0};
rozukke marked this conversation as resolved.
Show resolved Hide resolved
int PYRAMID_HEIGHT = 50;

// Can declare globally
mcpp::MinecraftConnection mc;

void make_ring(mcpp::Coordinate base_pt, int side_len) {
// Flat plane
mc.setBlocks(
base_pt,
base_pt + mcpp::Coordinate(side_len, 0, side_len),
mcpp::Blocks::SANDSTONE
);
rozukke marked this conversation as resolved.
Show resolved Hide resolved

// Air inside to make border
base_pt = base_pt + mcpp::Coordinate(1, 0, 1);
mc.setBlocks(
base_pt,
base_pt + mcpp::Coordinate(side_len - 2, 0, side_len - 2),
mcpp::Blocks::AIR
);
rozukke marked this conversation as resolved.
Show resolved Hide resolved
}

int main() {
int pyramid_base_len = PYRAMID_HEIGHT * 2;

// Get heights of build area
mcpp::HeightMap heights = mc.getHeights(
ORIGIN,
ORIGIN + mcpp::Coordinate(pyramid_base_len, 0, pyramid_base_len)
);
rozukke marked this conversation as resolved.
Show resolved Hide resolved

// Use minimum height of the area as the lowest point on the pyramid
int min_height = *std::min_element(
heights.begin(),
heights.end()
);
rozukke marked this conversation as resolved.
Show resolved Hide resolved

// Build rings, diminishing up to pyramid height
mcpp::Coordinate base_pt = heights.base_pt();
base_pt.y = min_height;
int side_len = pyramid_base_len;
for(int i = 0; i < PYRAMID_HEIGHT; i++) {
rozukke marked this conversation as resolved.
Show resolved Hide resolved
make_ring(base_pt + mcpp::Coordinate(i, i, i), side_len - (i * 2));
}
rozukke marked this conversation as resolved.
Show resolved Hide resolved
}
Loading