FBC: NIGHT SHIFT - two-handed agent weapon grip, standalone level editor and validation
- Pin both wrists to the rifle's real grip surfaces via a shared IK pass in every animation clip; hand bones follow the barrel and Fire recoil pivots the weapon/arms as one assembly. - Nudge the weapon forward so the stock clears the vest; make the generator reproducible (clears stale actions) and regenerate .blend, GLB and renders. - Add standalone level editor with JSON level data, validation and baking, plus editor docs and regression fixtures.
This commit is contained in:
parent
f21032d4ad
commit
849b34922d
23 changed files with 1380 additions and 327 deletions
|
|
@ -44,9 +44,23 @@ add_custom_command(
|
|||
COMMENT "Baking level data into the client")
|
||||
add_custom_target(bake_level DEPENDS ${BAKED_HPP})
|
||||
|
||||
add_custom_target(check_level
|
||||
COMMAND level_tool --check ${LEVEL_JSON}
|
||||
COMMAND level_tool --validate ${LEVEL_JSON}
|
||||
DEPENDS level_tool ${LEVEL_JSON}
|
||||
COMMENT "Checking level data")
|
||||
add_test(NAME level_data_check COMMAND level_tool --check ${LEVEL_JSON})
|
||||
add_test(NAME level_data_validate COMMAND level_tool --validate ${LEVEL_JSON})
|
||||
add_test(NAME level_fixture_valid
|
||||
COMMAND level_tool --check ${CMAKE_SOURCE_DIR}/tests/data/valid.json)
|
||||
add_test(NAME level_fixture_bad_gap
|
||||
COMMAND level_tool --check ${CMAKE_SOURCE_DIR}/tests/data/bad_gap.json)
|
||||
set_tests_properties(level_fixture_bad_gap PROPERTIES WILL_FAIL TRUE)
|
||||
|
||||
add_executable(editor
|
||||
src/editor/main.cpp
|
||||
src/editor/editor_app.cpp)
|
||||
src/editor/editor_app.cpp
|
||||
src/render/raygui_impl.cpp)
|
||||
target_include_directories(editor PRIVATE src third_party/raygui third_party/clay)
|
||||
target_link_libraries(editor PRIVATE raylib level_core)
|
||||
|
||||
|
|
|
|||
26
README.md
26
README.md
|
|
@ -86,28 +86,40 @@ The editor is a **separate binary** (`editor`) and shares only the raylib-free
|
|||
|
||||
```sh
|
||||
cmake --build build -j6
|
||||
./build/editor # loads assets/levels/floor01.json
|
||||
./build/editor # loads assets/levels/floor01.json
|
||||
./build/editor --level my_level.json
|
||||
./build/editor --level my_level.json --game ./build/cntrl
|
||||
```
|
||||
|
||||
LMB selects/drags props, RMB orbits, MMB pans, wheel zooms, `F` frames the
|
||||
level. Keys `1`–`4` switch between Select, Wall, Door and Decor tools: walls
|
||||
and doors are drawn by clicking two ground points, decor is dragged from the
|
||||
asset browser (all GLBs under `assets/models/`) into the 3D view. `Del` deletes
|
||||
the selection, `Ctrl+Z`/`Ctrl+Y` undo/redo, `Ctrl+S` saves. The validation
|
||||
panel runs a walkable-grid flood fill from the spawn and reports unreachable
|
||||
rooms, the key or the exit; `level_tool --validate <file>` does the same
|
||||
headlessly.
|
||||
the selection, `Ctrl+Z`/`Ctrl+Y` (`Ctrl+Shift+Z`) undo/redo, `Ctrl+S` saves,
|
||||
`Tab` toggles the inspector, `Esc` quits. Deleting a wall segment opens a gap
|
||||
which can be restored from the inspector. See `docs/LEVEL_EDITOR.md` for the
|
||||
full key map and field reference.
|
||||
|
||||
The level file is the authoring source of truth. `Floor 01` is baked into the
|
||||
The **inspector** (right dock) edits every field of the current selection:
|
||||
prop kind, position, size, yaw, scale, model, `solid`/`destructible`; wall
|
||||
lines and gaps; doors; rooms; rituals; thermo doors; and the spawn/key/exit
|
||||
anchors. With nothing selected it edits the level name and map dimensions.
|
||||
The validation panel runs a walkable-grid flood fill from the spawn (with
|
||||
gated doors both open and closed) and reports unreachable rooms, the key or
|
||||
the exit, overlapping rooms, duplicate ids and missing models.
|
||||
|
||||
`Ctrl+P` saves if needed and launches the game on the current level
|
||||
(`cntrl --level <path>`), so drafts can be playtested without baking. The
|
||||
level file is the authoring source of truth; `Floor 01` is baked into the
|
||||
client at build time (`src/game/level_baked.hpp`, regenerated by
|
||||
`level_tool --bake` when the JSON changes), so `cntrl` ships without a level
|
||||
file dependency:
|
||||
file dependency. Baking refuses invalid levels:
|
||||
|
||||
```sh
|
||||
./build/level_tool --dump-floor01 assets/levels/floor01.json
|
||||
./build/level_tool --check assets/levels/floor01.json
|
||||
./build/level_tool --validate assets/levels/floor01.json
|
||||
cmake --build build --target check_level
|
||||
cmake --build build --target bake_level
|
||||
ctest --test-dir build --output-on-failure
|
||||
```
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
|
|
@ -39,6 +39,8 @@ The findings above are the audit baseline. Fixes have since landed in four phase
|
|||
|
||||
Verification performed: `cmake --build build -j6` clean; standalone C++ harnesses for the ritual state machine and clearance/side-room flow pass; `--floor-overview` screenshots confirm the new corridor, projector marker, locked doors and HUD stamp.
|
||||
|
||||
**Level-editor iteration (same date, follow-up work):** the standalone editor gained a raygui inspector covering every `LevelData` field, stable wall/segment IDs (deleting a segment opens a reversible gap), two-state connectivity validation (doors open *and* closed), a bake gate that refuses invalid levels, `cntrl --level <path>` for playtesting drafts, and fixture-backed CTest coverage. This is tooling only; the open gameplay findings below are unchanged. See `docs/LEVEL_EDITOR.md`.
|
||||
|
||||
---
|
||||
|
||||
## 2. Sources reviewed
|
||||
|
|
|
|||
95
docs/LEVEL_EDITOR.md
Normal file
95
docs/LEVEL_EDITOR.md
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
# FBC: NIGHT SHIFT — Level Editor
|
||||
|
||||
The editor authors `assets/levels/*.json`, the single source of truth for the
|
||||
playable floor. The game reads a JSON at runtime (`cntrl --level <path>`) and
|
||||
falls back to the build-time baked copy (`src/game/level_baked.hpp`), which is
|
||||
regenerated by `level_tool --bake` whenever the JSON changes.
|
||||
|
||||
## Build and launch
|
||||
|
||||
```sh
|
||||
cmake --build build -j6
|
||||
./build/editor # assets/levels/floor01.json
|
||||
./build/editor --level my_level.json --game ./build/cntrl
|
||||
./build/editor --screenshot /tmp/editor.png # headless smoke capture
|
||||
./build/editor --place plant 12.5 44 # debug: place an asset by name
|
||||
```
|
||||
|
||||
`--game` sets the binary launched by `Ctrl+P`; without it the editor detects
|
||||
`./cntrl`, `./build/cntrl` or `../cntrl` in that order. `--place`
|
||||
selects the placed prop so its inspector fields can be inspected in a capture.
|
||||
|
||||
## Controls
|
||||
|
||||
| Input | Action |
|
||||
|---|---|
|
||||
| LMB (Select) | Select nearest prop/wall/door/room/ritual/thermo/anchor; drag props |
|
||||
| LMB (Wall / Door) | First click starts the span, second click finishes it |
|
||||
| LMB (Decor) | Place the selected asset from the browser |
|
||||
| LMB drag from browser | Place the selected asset at the drop point |
|
||||
| RMB or Alt+LMB | Orbit |
|
||||
| MMB | Pan |
|
||||
| Wheel | Zoom (scrolls the asset browser over its panel) |
|
||||
| `1`–`4` | Select / Wall / Door / Decor tools |
|
||||
| `Del` / `Backspace` | Delete the selection (anchors can only be moved) |
|
||||
| `Ctrl+Z` / `Ctrl+Y` / `Ctrl+Shift+Z` | Undo / redo |
|
||||
| `Ctrl+S` | Save to the current level path |
|
||||
| `Ctrl+P` | Save if dirty, then launch `cntrl --level <path>` |
|
||||
| `F` | Frame the whole level |
|
||||
| `Tab` | Toggle the inspector |
|
||||
| `Esc` | Quit |
|
||||
|
||||
## Inspector field reference
|
||||
|
||||
The right-hand dock edits the current selection. Numeric fields are sliders,
|
||||
enums are combo boxes, booleans are check boxes and names/paths are text boxes.
|
||||
Edits share one undo step per interaction (one per drag or text-box focus).
|
||||
|
||||
| Selection | Fields |
|
||||
|---|---|
|
||||
| none (level) | `name`, `map_width`, `map_height`, `scale` |
|
||||
| Prop | `kind`, `pos`, `w`, `d`, `h`, `y offset`, `yaw deg`, `scale`, `model`, `room_kind`, `solid`, `destructible` |
|
||||
| Wall line | `axis`, `coord`, `from`, `to`, `height`, each gap's start/end, **DEL GAP**, **OPEN SEGMENT GAP** |
|
||||
| Door | `id`, `rect` (`x0/y0/x1/y1`), `height` |
|
||||
| Room | `name`, `rect`, `kind`, `flow`, `side idx` |
|
||||
| Ritual | `type`, `pos`, `radius`, `duration`, `ring radius`, `ring count`, `light radius`, `light mult`, `light dur` |
|
||||
| Thermo door | `pos`, `span`, `yaw deg` |
|
||||
| Spawn / Key / Exit | `pos`; Key/Exit also their pickup/trigger radius |
|
||||
|
||||
**Scale convention:** `scale` is the multiplier passed straight to raylib's
|
||||
`DrawModelEx`, identical to the game's presenter. `w/d/h` are metadata used for
|
||||
selection boxes and (for `solid` props) collision. Deleting a wall segment
|
||||
adds a gap to the wall line; gaps are normalized (clamped, sorted, merged) and
|
||||
can be removed from the inspector to close a passage again.
|
||||
|
||||
## Validation and baking
|
||||
|
||||
The validation readout at the top of the inspector is computed by
|
||||
`level::validate()` (the same code `level_tool --validate` uses):
|
||||
|
||||
- gated doors are tested both open and closed; a main room reachable only
|
||||
through a gated door is an error, a gated side room is expected;
|
||||
- solid props and walls both block the walkable-grid flood fill from spawn;
|
||||
- spawn/key/exit must lie inside the map and outside walls;
|
||||
- rooms must be reachable from spawn, key and exit too;
|
||||
- duplicate door ids, duplicate ritual types, overlapping rooms, malformed
|
||||
gaps, missing/out-of-range numeric fields and missing model files are
|
||||
reported.
|
||||
|
||||
```sh
|
||||
./build/level_tool --check assets/levels/floor01.json # round-trip + validation
|
||||
./build/level_tool --validate assets/levels/floor01.json # validation only
|
||||
./build/level_tool --bake <level.json> <baked.hpp> # refuses invalid levels
|
||||
cmake --build build --target check_level
|
||||
ctest --test-dir build --output-on-failure
|
||||
```
|
||||
|
||||
Schema is `version: 1`; `from_json` accepts unknown optional fields but
|
||||
rejects out-of-range values. New fields should default safely when absent.
|
||||
|
||||
## Other tools
|
||||
|
||||
`tools/blender/import_level.py` reads the same JSON into a reviewable Blender
|
||||
blockout (set `CNTRL_LEVEL` for a different file, `CNTRL_OUT` for a different
|
||||
`.blend` output). `tests/data/` holds a valid fixture and an intentionally
|
||||
invalid one wired into CTest.
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 1.2 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1 MiB After Width: | Height: | Size: 1 MiB |
File diff suppressed because it is too large
Load diff
|
|
@ -13,6 +13,14 @@ namespace editor {
|
|||
|
||||
enum class Tool { Select, Wall, Door, Decor };
|
||||
|
||||
enum class Entity { None, Prop, Room, Wall, Door, Ritual, Thermo, Spawn, Key, Exit };
|
||||
|
||||
struct Selection {
|
||||
Entity kind = Entity::None;
|
||||
int index = -1; // entity index; for Wall this is the WallLine index
|
||||
int sub = -1; // visible segment index for Wall
|
||||
};
|
||||
|
||||
class App {
|
||||
public:
|
||||
explicit App(std::string path);
|
||||
|
|
@ -26,24 +34,32 @@ public:
|
|||
const std::string& path() const { return path_; }
|
||||
bool dirty() const { return dirty_; }
|
||||
void debug_place(const std::string& asset_name, Vec2 map_pos);
|
||||
void set_game_path(const std::string& path) { game_path_ = path; }
|
||||
|
||||
private:
|
||||
void update_camera();
|
||||
void handle_keys();
|
||||
void handle_pointer();
|
||||
void select_at(Vector2 mouse);
|
||||
void place_decor(Vec2 map_pos);
|
||||
void finish_wall(Vec2 map_pos);
|
||||
void finish_door(Vec2 map_pos);
|
||||
void delete_selection();
|
||||
void push_undo();
|
||||
void push_snapshot(const std::string& snapshot);
|
||||
void begin_edit();
|
||||
void end_edit();
|
||||
void commit();
|
||||
void revalidate();
|
||||
void undo();
|
||||
void redo();
|
||||
void restore(const std::string& snapshot);
|
||||
void frame_level();
|
||||
void playtest();
|
||||
|
||||
bool ground_point(Vec2& map_pos) const;
|
||||
bool inside_panel(Vector2 mouse) const;
|
||||
bool inside_inspector(Vector2 mouse) const;
|
||||
int asset_at(Vector2 mouse) const;
|
||||
Model& model_for(const std::string& path);
|
||||
Vector3 model_size(const std::string& path);
|
||||
|
|
@ -61,18 +77,31 @@ private:
|
|||
void draw_anchors() const;
|
||||
void draw_ui() const;
|
||||
void draw_asset_panel() const;
|
||||
void draw_inspector();
|
||||
void draw_inspector_fields(float x, float y, float w);
|
||||
|
||||
bool field_header(const char* text, float x, float y, float w);
|
||||
bool field_bool(float x, float y, float w, const char* label, bool& value);
|
||||
bool field_float(float x, float y, float w, const char* label, float& value,
|
||||
float lo, float hi);
|
||||
bool field_int(float x, float y, float w, const char* label, int& value,
|
||||
int lo, int hi);
|
||||
bool field_enum(float x, float y, float w, const char* label, int& value,
|
||||
const char* items, int count);
|
||||
bool field_text(float x, float y, float w, int id, const char* label,
|
||||
std::string& value);
|
||||
void commit_text();
|
||||
|
||||
level::LevelData level_;
|
||||
level::ValidationResult validation_;
|
||||
std::string path_;
|
||||
std::string game_path_ = "./cntrl";
|
||||
Camera3D camera_{};
|
||||
Vector3 target_{};
|
||||
float yaw_ = 0.0f;
|
||||
float pitch_ = 0.85f;
|
||||
float distance_ = 320.0f;
|
||||
int selected_prop_ = -1;
|
||||
int selected_wall_ = -1;
|
||||
int selected_room_ = -1;
|
||||
Selection selection_;
|
||||
bool dirty_ = false;
|
||||
bool should_close_ = false;
|
||||
Vector2 last_mouse_{};
|
||||
|
|
@ -83,15 +112,21 @@ private:
|
|||
int asset_scroll_ = 0;
|
||||
bool dragging_panel_asset_ = false;
|
||||
bool dragging_prop_ = false;
|
||||
bool drag_undo_pushed_ = false;
|
||||
int dragged_prop_ = -1;
|
||||
bool pending_set_ = false;
|
||||
Vec2 pending_{};
|
||||
int next_door_ = 0;
|
||||
std::vector<std::string> undo_;
|
||||
std::vector<std::string> redo_;
|
||||
std::unordered_map<std::string, Model> models_;
|
||||
std::unordered_map<std::string, Vector3> model_sizes_;
|
||||
|
||||
bool inspector_ = true;
|
||||
bool interacting_ = false;
|
||||
int text_focus_ = -1;
|
||||
Rectangle text_rect_{};
|
||||
char text_buf_[256] = {};
|
||||
std::string status_message_;
|
||||
float status_timer_ = 0.0f;
|
||||
};
|
||||
|
||||
} // namespace editor
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include "editor/editor_app.hpp"
|
||||
|
|
@ -7,6 +8,7 @@
|
|||
|
||||
int main(int argc, char** argv) {
|
||||
std::string path = "assets/levels/floor01.json";
|
||||
std::string game_path;
|
||||
std::string screenshot;
|
||||
std::string place_asset;
|
||||
float place_x = 0.0f;
|
||||
|
|
@ -14,6 +16,8 @@ int main(int argc, char** argv) {
|
|||
for (int i = 1; i < argc; ++i) {
|
||||
if (std::strcmp(argv[i], "--level") == 0 && i + 1 < argc) {
|
||||
path = argv[++i];
|
||||
} else if (std::strcmp(argv[i], "--game") == 0 && i + 1 < argc) {
|
||||
game_path = argv[++i];
|
||||
} else if (std::strcmp(argv[i], "--screenshot") == 0 && i + 1 < argc) {
|
||||
screenshot = argv[++i];
|
||||
} else if (std::strcmp(argv[i], "--place") == 0 && i + 3 < argc) {
|
||||
|
|
@ -21,16 +25,28 @@ int main(int argc, char** argv) {
|
|||
place_x = std::stof(argv[++i]);
|
||||
place_y = std::stof(argv[++i]);
|
||||
} else if (std::strcmp(argv[i], "--help") == 0) {
|
||||
std::printf("usage: editor [--level <path>]\n");
|
||||
std::printf("usage: editor [--level <path>] [--game <path>]\n"
|
||||
" [--screenshot <path>]\n"
|
||||
" [--place <asset-name> <map-x> <map-y>]\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (game_path.empty()) {
|
||||
for (const char* candidate : {"./cntrl", "./build/cntrl", "../cntrl"}) {
|
||||
if (std::filesystem::exists(candidate)) {
|
||||
game_path = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (game_path.empty()) game_path = "./build/cntrl";
|
||||
}
|
||||
|
||||
SetTraceLogLevel(LOG_WARNING);
|
||||
InitWindow(1400, 900, "FBC: NIGHT SHIFT - Level Editor");
|
||||
SetTargetFPS(60);
|
||||
|
||||
editor::App app(path);
|
||||
app.set_game_path(game_path);
|
||||
app.init();
|
||||
if (!place_asset.empty()) app.debug_place(place_asset, {place_x, place_y});
|
||||
int frames = 0;
|
||||
|
|
|
|||
|
|
@ -105,7 +105,8 @@ void apply_offer(Game& g, const Offer& o) {
|
|||
|
||||
} // namespace
|
||||
|
||||
Game::Game(std::uint64_t seed) : rng(seed), seed_(seed) {
|
||||
Game::Game(std::uint64_t seed, const std::string& level_path)
|
||||
: floor(level_path), rng(seed), seed_(seed) {
|
||||
weapon_system.reset();
|
||||
ritual_system.init(floor);
|
||||
player.pos = floor.spawn();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "core/events.hpp"
|
||||
|
|
@ -38,7 +39,8 @@ struct GameConfig {
|
|||
// main fills InputState, the presenter reads the world and drains `events`.
|
||||
class Game {
|
||||
public:
|
||||
explicit Game(std::uint64_t seed);
|
||||
explicit Game(std::uint64_t seed,
|
||||
const std::string& level_path = Floor::kDefaultPath);
|
||||
|
||||
void update(const InputState& in, float dt);
|
||||
void reset();
|
||||
|
|
|
|||
|
|
@ -332,6 +332,10 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
if (!read_float_field(root, "map_width", l.map_width, error)) return false;
|
||||
if (!read_float_field(root, "map_height", l.map_height, error)) return false;
|
||||
if (!read_float_field(root, "scale", l.scale, error)) return false;
|
||||
if (!(l.map_width > 0.0f) || !(l.map_height > 0.0f)) {
|
||||
error = "map dimensions must be positive";
|
||||
return false;
|
||||
}
|
||||
if (!(l.scale > 0.0f)) {
|
||||
error = "scale must be positive";
|
||||
return false;
|
||||
|
|
@ -344,6 +348,10 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
Room r;
|
||||
if (!read_string_field(item, "name", r.name, error)) return false;
|
||||
if (!read_rect_field(item, "rect", r.rect, error)) return false;
|
||||
if (!(r.rect.x1 > r.rect.x0) || !(r.rect.y1 > r.rect.y0)) {
|
||||
error = "room " + r.name + " has an invalid rect";
|
||||
return false;
|
||||
}
|
||||
std::string kind;
|
||||
if (!read_string_field(item, "kind", kind, error)
|
||||
|| !room_kind_from_name(kind, r.kind)) {
|
||||
|
|
@ -361,6 +369,10 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
for (const json::Value& item : corridors->array) {
|
||||
Rect r;
|
||||
if (!read_rect(item, r, error)) return false;
|
||||
if (!(r.x1 > r.x0) || !(r.y1 > r.y0)) {
|
||||
error = "corridor has an invalid rect";
|
||||
return false;
|
||||
}
|
||||
l.corridors.push_back(r);
|
||||
}
|
||||
|
||||
|
|
@ -380,6 +392,24 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
if (!read_float_field(item, "height", w.height, error)) return false;
|
||||
const json::Value* gaps = field(item, "gaps", error);
|
||||
if (gaps == nullptr || !read_spans(*gaps, w.gaps, error)) return false;
|
||||
if (!(w.to > w.from)) {
|
||||
error = "wall " + std::to_string(l.walls.size()) + " has from >= to";
|
||||
return false;
|
||||
}
|
||||
if (!(w.height > 0.0f)) {
|
||||
error = "wall " + std::to_string(l.walls.size()) + " has non-positive height";
|
||||
return false;
|
||||
}
|
||||
float gap_end = w.from;
|
||||
for (const Span& gap : w.gaps) {
|
||||
if (!(gap.second > gap.first) || gap.first < w.from || gap.second > w.to
|
||||
|| gap.first < gap_end - 1e-4f) {
|
||||
error = "wall " + std::to_string(l.walls.size())
|
||||
+ " has an invalid or unordered gap";
|
||||
return false;
|
||||
}
|
||||
gap_end = gap.second;
|
||||
}
|
||||
l.walls.push_back(std::move(w));
|
||||
}
|
||||
|
||||
|
|
@ -396,6 +426,14 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
}
|
||||
if (!read_rect_field(item, "rect", d.rect, error)) return false;
|
||||
if (!read_float_field(item, "height", d.height, error)) return false;
|
||||
if (!(d.rect.x1 > d.rect.x0) || !(d.rect.y1 > d.rect.y0)) {
|
||||
error = "door " + id + " has an invalid rect";
|
||||
return false;
|
||||
}
|
||||
if (!(d.height > 0.0f)) {
|
||||
error = "door " + id + " has non-positive height";
|
||||
return false;
|
||||
}
|
||||
l.doors.push_back(d);
|
||||
}
|
||||
|
||||
|
|
@ -450,6 +488,15 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
}
|
||||
p.destructible = destructible->boolean;
|
||||
}
|
||||
const std::string prop_label = "prop " + std::to_string(l.props.size());
|
||||
if (!(p.w > 0.0f) || !(p.d > 0.0f) || !(p.h > 0.0f)) {
|
||||
error = prop_label + " has non-positive size";
|
||||
return false;
|
||||
}
|
||||
if (!(p.scale > 0.0f)) {
|
||||
error = prop_label + " has non-positive scale";
|
||||
return false;
|
||||
}
|
||||
l.props.push_back(p);
|
||||
}
|
||||
|
||||
|
|
@ -469,6 +516,16 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
return false;
|
||||
if (!read_float_field(item, "light_duration", r.light_duration, error))
|
||||
return false;
|
||||
const std::string ritual_label = "ritual " + std::to_string(l.rituals.size());
|
||||
if (!(r.radius > 0.0f) || !(r.duration > 0.0f)) {
|
||||
error = ritual_label + " has non-positive radius or duration";
|
||||
return false;
|
||||
}
|
||||
if (r.ring_radius < 0.0f || r.ring_count < 0 || r.light_radius < 0.0f
|
||||
|| !(r.light_damage_mult > 0.0f) || r.light_duration < 0.0f) {
|
||||
error = ritual_label + " has invalid parameters";
|
||||
return false;
|
||||
}
|
||||
l.rituals.push_back(r);
|
||||
}
|
||||
|
||||
|
|
@ -480,6 +537,10 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
if (!read_vec2_field(item, "pos", t.pos, error)) return false;
|
||||
if (!read_float_field(item, "span", t.span, error)) return false;
|
||||
if (!read_float_field(item, "yaw", t.yaw, error)) return false;
|
||||
if (!(t.span > 0.0f)) {
|
||||
error = "thermo door " + std::to_string(l.thermo.size()) + " has non-positive span";
|
||||
return false;
|
||||
}
|
||||
l.thermo.push_back(t);
|
||||
}
|
||||
|
||||
|
|
@ -492,6 +553,10 @@ bool from_json(const json::Value& root, LevelData& l, std::string& error) {
|
|||
return false;
|
||||
if (!read_float_field(*anchors, "exit_radius", l.anchors.exit_radius, error))
|
||||
return false;
|
||||
if (l.anchors.key_radius < 0.0f || l.anchors.exit_radius < 0.0f) {
|
||||
error = "anchor radii must be non-negative";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -539,48 +604,122 @@ Rect to_world_rect(const LevelData& l, Rect r) {
|
|||
return {a.x, a.y, b.x, b.y};
|
||||
}
|
||||
|
||||
std::vector<WorldWall> build_world_walls(const LevelData& l) {
|
||||
std::vector<WorldWall> out;
|
||||
constexpr float kHalfThickness = 0.35f;
|
||||
namespace {
|
||||
|
||||
for (const WallLine& w : l.walls) {
|
||||
float cur = w.from;
|
||||
auto emit = [&](float a, float b) {
|
||||
if (w.axis == 0) {
|
||||
const Vec2 p = to_world(l, {a, w.coord});
|
||||
const Vec2 q = to_world(l, {b, w.coord});
|
||||
out.push_back({{std::min(p.x, q.x), p.y - kHalfThickness,
|
||||
std::max(p.x, q.x), p.y + kHalfThickness},
|
||||
w.height});
|
||||
} else {
|
||||
const Vec2 p = to_world(l, {w.coord, a});
|
||||
const Vec2 q = to_world(l, {w.coord, b});
|
||||
out.push_back({{p.x - kHalfThickness, std::min(p.y, q.y),
|
||||
p.x + kHalfThickness, std::max(p.y, q.y)},
|
||||
w.height});
|
||||
}
|
||||
};
|
||||
for (const Span& gap : w.gaps) {
|
||||
if (gap.first > cur) emit(cur, std::min(gap.first, w.to));
|
||||
cur = std::max(cur, gap.second);
|
||||
constexpr float kEpsilon = 1e-4f;
|
||||
|
||||
std::vector<Span> visible_segments(float from, float to, const std::vector<Span>& gaps) {
|
||||
std::vector<Span> out;
|
||||
float cur = from;
|
||||
for (const Span& gap : gaps) {
|
||||
if (gap.first > cur + kEpsilon) out.push_back({cur, std::min(gap.first, to)});
|
||||
cur = std::max(cur, gap.second);
|
||||
}
|
||||
if (cur < to - kEpsilon) out.push_back({cur, to});
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<Span> normalize_gaps(float from, float to, const std::vector<Span>& gaps) {
|
||||
std::vector<Span> spans;
|
||||
for (Span s : gaps) {
|
||||
s.first = std::max(s.first, from);
|
||||
s.second = std::min(s.second, to);
|
||||
if (s.second - s.first <= kEpsilon) continue;
|
||||
spans.push_back(s);
|
||||
}
|
||||
std::sort(spans.begin(), spans.end(), [](const Span& a, const Span& b) {
|
||||
return a.first < b.first || (a.first == b.first && a.second < b.second);
|
||||
});
|
||||
std::vector<Span> out;
|
||||
for (const Span& s : spans) {
|
||||
if (!out.empty() && s.first <= out.back().second + kEpsilon) {
|
||||
out.back().second = std::max(out.back().second, s.second);
|
||||
} else {
|
||||
out.push_back(s);
|
||||
}
|
||||
if (cur < w.to) emit(cur, w.to);
|
||||
}
|
||||
|
||||
for (const Door& d : l.doors) {
|
||||
const int id = static_cast<int>(d.id);
|
||||
if (id < 0 || id >= static_cast<int>(DoorId::Count)) continue;
|
||||
const Rect r = to_world_rect(l, d.rect);
|
||||
out.push_back({r, d.height, id});
|
||||
}
|
||||
|
||||
for (const Prop& p : l.props) {
|
||||
if (!p.solid) continue;
|
||||
const Vec2 c = to_world(l, p.pos);
|
||||
out.push_back({{c.x - p.w * 0.5f, c.y - p.d * 0.5f,
|
||||
c.x + p.w * 0.5f, c.y + p.d * 0.5f}, p.h});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool remove_wall_segment(LevelData& l, std::size_t line, std::size_t segment) {
|
||||
if (line >= l.walls.size()) return false;
|
||||
const WallLine& w = l.walls[line];
|
||||
const auto segments = visible_segments(w.from, w.to, w.gaps);
|
||||
if (segment >= segments.size()) return false;
|
||||
auto gaps = w.gaps;
|
||||
gaps.push_back(segments[segment]);
|
||||
l.walls[line].gaps = normalize_gaps(w.from, w.to, gaps);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool restore_wall_gap(LevelData& l, std::size_t line, std::size_t gap) {
|
||||
if (line >= l.walls.size() || gap >= l.walls[line].gaps.size()) return false;
|
||||
l.walls[line].gaps.erase(l.walls[line].gaps.begin() + static_cast<long>(gap));
|
||||
return true;
|
||||
}
|
||||
|
||||
int allocate_door_id(const LevelData& l) {
|
||||
bool used[static_cast<int>(DoorId::Count)] = {};
|
||||
for (const Door& d : l.doors) {
|
||||
const int id = static_cast<int>(d.id);
|
||||
if (id >= 0 && id < static_cast<int>(DoorId::Count)) used[id] = true;
|
||||
}
|
||||
for (int i = 0; i < static_cast<int>(DoorId::Count); ++i) {
|
||||
if (!used[i]) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::vector<IndexedWall> build_indexed_walls(const LevelData& l) {
|
||||
std::vector<IndexedWall> out;
|
||||
constexpr float kHalfThickness = 0.35f;
|
||||
|
||||
for (std::size_t li = 0; li < l.walls.size(); ++li) {
|
||||
const WallLine& w = l.walls[li];
|
||||
const auto segments = visible_segments(w.from, w.to, w.gaps);
|
||||
for (std::size_t si = 0; si < segments.size(); ++si) {
|
||||
const Span s = segments[si];
|
||||
WorldWall wall;
|
||||
if (w.axis == 0) {
|
||||
const Vec2 p = to_world(l, {s.first, w.coord});
|
||||
const Vec2 q = to_world(l, {s.second, w.coord});
|
||||
wall = {{std::min(p.x, q.x), p.y - kHalfThickness,
|
||||
std::max(p.x, q.x), p.y + kHalfThickness}, w.height};
|
||||
} else {
|
||||
const Vec2 p = to_world(l, {w.coord, s.first});
|
||||
const Vec2 q = to_world(l, {w.coord, s.second});
|
||||
wall = {{p.x - kHalfThickness, std::min(p.y, q.y),
|
||||
p.x + kHalfThickness, std::max(p.y, q.y)}, w.height};
|
||||
}
|
||||
out.push_back({wall, WallSource::Line, li, si});
|
||||
}
|
||||
}
|
||||
|
||||
for (std::size_t di = 0; di < l.doors.size(); ++di) {
|
||||
const Door& d = l.doors[di];
|
||||
const int id = static_cast<int>(d.id);
|
||||
if (id < 0 || id >= static_cast<int>(DoorId::Count)) continue;
|
||||
const Rect r = to_world_rect(l, d.rect);
|
||||
out.push_back({{r, d.height, id}, WallSource::Door, di, kNoIndex});
|
||||
}
|
||||
|
||||
for (std::size_t pi = 0; pi < l.props.size(); ++pi) {
|
||||
const Prop& p = l.props[pi];
|
||||
if (!p.solid) continue;
|
||||
const Vec2 c = to_world(l, p.pos);
|
||||
WorldWall wall = {{c.x - p.w * 0.5f, c.y - p.d * 0.5f,
|
||||
c.x + p.w * 0.5f, c.y + p.d * 0.5f}, p.h};
|
||||
out.push_back({wall, WallSource::Prop, pi, kNoIndex});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<WorldWall> build_world_walls(const LevelData& l) {
|
||||
std::vector<WorldWall> out;
|
||||
for (const IndexedWall& wall : build_indexed_walls(l)) out.push_back(wall.wall);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace level
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
|
@ -105,6 +106,20 @@ struct WorldWall {
|
|||
int door = -1;
|
||||
};
|
||||
|
||||
constexpr std::size_t kNoIndex = static_cast<std::size_t>(-1);
|
||||
|
||||
enum class WallSource { Line, Door, Prop };
|
||||
|
||||
// A world-space wall plus the authored entity it came from. `line` indexes
|
||||
// LevelData::walls; `segment` indexes the visible segments of that line after
|
||||
// gaps are applied. Doors and solid props use their own index in `line`.
|
||||
struct IndexedWall {
|
||||
WorldWall wall;
|
||||
WallSource source = WallSource::Line;
|
||||
std::size_t line = kNoIndex;
|
||||
std::size_t segment = kNoIndex;
|
||||
};
|
||||
|
||||
struct LevelData {
|
||||
int version = 1;
|
||||
std::string name;
|
||||
|
|
@ -140,6 +155,14 @@ bool save(const std::string& path, const LevelData& level);
|
|||
Vec2 to_world(const LevelData& level, Vec2 p);
|
||||
Vec2 to_map(const LevelData& level, Vec2 p);
|
||||
Rect to_world_rect(const LevelData& level, Rect r);
|
||||
std::vector<IndexedWall> build_indexed_walls(const LevelData& level);
|
||||
std::vector<WorldWall> build_world_walls(const LevelData& level);
|
||||
|
||||
// Gap editing helpers. `normalize_gaps` clamps spans to [from, to], drops
|
||||
// inverted spans and merges overlaps/adjacent spans in place.
|
||||
std::vector<Span> normalize_gaps(float from, float to, const std::vector<Span>& gaps);
|
||||
bool remove_wall_segment(LevelData& level, std::size_t line, std::size_t segment);
|
||||
bool restore_wall_gap(LevelData& level, std::size_t line, std::size_t gap);
|
||||
int allocate_door_id(const LevelData& level);
|
||||
|
||||
} // namespace level
|
||||
|
|
|
|||
|
|
@ -31,6 +31,12 @@ int main(int argc, char** argv) {
|
|||
std::cerr << error << '\n';
|
||||
return 1;
|
||||
}
|
||||
const auto validation = level::validate(data);
|
||||
for (const auto& e : validation.errors) std::cerr << "ERROR: " << e << '\n';
|
||||
if (!validation.ok()) {
|
||||
std::cerr << "refusing to bake an invalid level\n";
|
||||
return 1;
|
||||
}
|
||||
std::ofstream file(output, std::ios::binary | std::ios::trunc);
|
||||
if (!file) {
|
||||
std::cerr << "failed to write " << output << '\n';
|
||||
|
|
@ -86,12 +92,14 @@ int main(int argc, char** argv) {
|
|||
return 1;
|
||||
}
|
||||
const bool stable = once == level::serialize(again);
|
||||
const auto validation = level::validate(data);
|
||||
for (const auto& e : validation.errors) std::cerr << "ERROR: " << e << '\n';
|
||||
std::cout << path << ": " << data.rooms.size() << " rooms, "
|
||||
<< data.walls.size() << " wall lines, " << data.doors.size()
|
||||
<< " doors, " << data.props.size() << " props, "
|
||||
<< data.rituals.size() << " rituals, roundtrip "
|
||||
<< (stable ? "stable" : "UNSTABLE") << '\n';
|
||||
return stable ? 0 : 1;
|
||||
return stable && validation.ok() ? 0 : 1;
|
||||
}
|
||||
|
||||
return usage();
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
|
|
@ -14,9 +13,9 @@ namespace {
|
|||
|
||||
constexpr float kPlayerRadius = 0.45f;
|
||||
|
||||
bool blocked(const std::vector<WorldWall>& walls, Vec2 p) {
|
||||
bool blocked(const std::vector<WorldWall>& walls, Vec2 p, bool doors_open) {
|
||||
for (const WorldWall& w : walls) {
|
||||
if (w.door >= 0) continue;
|
||||
if (w.door >= 0 && doors_open) continue;
|
||||
const Rect& r = w.rect;
|
||||
if (p.x > r.x0 - kPlayerRadius && p.x < r.x1 + kPlayerRadius
|
||||
&& p.y > r.y0 - kPlayerRadius && p.y < r.y1 + kPlayerRadius) {
|
||||
|
|
@ -45,6 +44,15 @@ ValidationResult validate(const LevelData& level) {
|
|||
door_ids.insert(id);
|
||||
}
|
||||
|
||||
std::set<std::string> ritual_types;
|
||||
for (const Ritual& r : level.rituals) {
|
||||
if (r.type.empty()) {
|
||||
result.errors.push_back("ritual with empty type");
|
||||
} else if (!ritual_types.insert(r.type).second) {
|
||||
result.errors.push_back("duplicate ritual type: " + r.type);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < level.rooms.size(); ++i) {
|
||||
for (size_t j = i + 1; j < level.rooms.size(); ++j) {
|
||||
const Rect& a = level.rooms[i].rect;
|
||||
|
|
@ -72,6 +80,21 @@ ValidationResult validate(const LevelData& level) {
|
|||
}
|
||||
for (const auto& message : missing_models) result.warnings.push_back(message);
|
||||
|
||||
const auto inside_map = [&](Vec2 p) {
|
||||
return p.x >= 0.0f && p.y >= 0.0f
|
||||
&& p.x <= level.map_width && p.y <= level.map_height;
|
||||
};
|
||||
struct NamedAnchor { const char* name; Vec2 pos; };
|
||||
for (const NamedAnchor& a : {NamedAnchor{"spawn", level.anchors.spawn},
|
||||
NamedAnchor{"threshold key", level.anchors.key},
|
||||
NamedAnchor{"threshold exit", level.anchors.exit}}) {
|
||||
if (!inside_map(a.pos)) {
|
||||
result.errors.push_back(std::string(a.name) + " outside the map");
|
||||
} else if (blocked(walls, to_world(level, a.pos), true)) {
|
||||
result.errors.push_back(std::string(a.name) + " is inside a wall");
|
||||
}
|
||||
}
|
||||
|
||||
const int width = static_cast<int>(level.map_width);
|
||||
const int height = static_cast<int>(level.map_height);
|
||||
if (width <= 0 || height <= 0) {
|
||||
|
|
@ -82,20 +105,21 @@ ValidationResult validate(const LevelData& level) {
|
|||
const auto index = [width](int x, int y) {
|
||||
return static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x);
|
||||
};
|
||||
auto walkable = [&](int x, int y) {
|
||||
auto walkable = [&](int x, int y, bool doors_open) {
|
||||
if (x < 0 || y < 0 || x >= width || y >= height) return false;
|
||||
const Vec2 world = to_world(level, {x + 0.5f, y + 0.5f});
|
||||
return !blocked(walls, world);
|
||||
return !blocked(walls, world, doors_open);
|
||||
};
|
||||
|
||||
const auto nearest_walkable = [&](Vec2 map_pos, int& out_x, int& out_y) {
|
||||
const auto nearest_walkable = [&](Vec2 map_pos, int& out_x, int& out_y,
|
||||
bool doors_open) {
|
||||
const int cx = static_cast<int>(map_pos.x);
|
||||
const int cy = static_cast<int>(map_pos.y);
|
||||
for (int radius = 0; radius <= 6; ++radius) {
|
||||
for (int dy = -radius; dy <= radius; ++dy) {
|
||||
for (int dx = -radius; dx <= radius; ++dx) {
|
||||
if (std::max(std::abs(dx), std::abs(dy)) != radius) continue;
|
||||
if (walkable(cx + dx, cy + dy)) {
|
||||
if (walkable(cx + dx, cy + dy, doors_open)) {
|
||||
out_x = cx + dx;
|
||||
out_y = cy + dy;
|
||||
return true;
|
||||
|
|
@ -106,42 +130,48 @@ ValidationResult validate(const LevelData& level) {
|
|||
return false;
|
||||
};
|
||||
|
||||
int sx = 0;
|
||||
int sy = 0;
|
||||
if (!nearest_walkable(level.anchors.spawn, sx, sy)) {
|
||||
result.errors.push_back("spawn is not walkable");
|
||||
return result;
|
||||
}
|
||||
int next_component = 0;
|
||||
std::queue<std::pair<int, int>> queue;
|
||||
queue.push({sx, sy});
|
||||
component[index(sx, sy)] = next_component;
|
||||
while (!queue.empty()) {
|
||||
const auto [x, y] = queue.front();
|
||||
queue.pop();
|
||||
for (const auto [dx, dy] : {std::pair{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
|
||||
const int nx = x + dx;
|
||||
const int ny = y + dy;
|
||||
if (!walkable(nx, ny)) continue;
|
||||
if (component[index(nx, ny)] >= 0) continue;
|
||||
component[index(nx, ny)] = next_component;
|
||||
queue.push({nx, ny});
|
||||
const auto flood_fill = [&](std::vector<int>& out, int sx, int sy,
|
||||
bool doors_open) {
|
||||
std::fill(out.begin(), out.end(), -1);
|
||||
std::queue<std::pair<int, int>> queue;
|
||||
queue.push({sx, sy});
|
||||
out[index(sx, sy)] = 0;
|
||||
while (!queue.empty()) {
|
||||
const auto [x, y] = queue.front();
|
||||
queue.pop();
|
||||
for (const auto [dx, dy] : {std::pair{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
|
||||
const int nx = x + dx;
|
||||
const int ny = y + dy;
|
||||
if (!walkable(nx, ny, doors_open)) continue;
|
||||
if (out[index(nx, ny)] >= 0) continue;
|
||||
out[index(nx, ny)] = 0;
|
||||
queue.push({nx, ny});
|
||||
}
|
||||
}
|
||||
}
|
||||
++next_component;
|
||||
};
|
||||
|
||||
const auto reachable = [&](Vec2 p, const std::string& what) {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
if (!nearest_walkable(p, x, y)) {
|
||||
if (!nearest_walkable(p, x, y, true)) {
|
||||
result.errors.push_back(what + " has no walkable cell");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (component[index(x, y)] != 0) {
|
||||
result.errors.push_back(what + " unreachable from spawn");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
int sx = 0;
|
||||
int sy = 0;
|
||||
if (!nearest_walkable(level.anchors.spawn, sx, sy, true)) {
|
||||
result.errors.push_back("spawn is not walkable");
|
||||
return result;
|
||||
}
|
||||
flood_fill(component, sx, sy, true);
|
||||
|
||||
for (const Room& r : level.rooms) {
|
||||
reachable({(r.rect.x0 + r.rect.x1) * 0.5f, (r.rect.y0 + r.rect.y1) * 0.5f},
|
||||
"room " + r.name);
|
||||
|
|
@ -149,10 +179,38 @@ ValidationResult validate(const LevelData& level) {
|
|||
reachable(level.anchors.key, "threshold key");
|
||||
reachable(level.anchors.exit, "threshold exit");
|
||||
|
||||
int closed_sx = 0;
|
||||
int closed_sy = 0;
|
||||
bool closed_spawn = nearest_walkable(level.anchors.spawn, closed_sx, closed_sy, false);
|
||||
if (closed_spawn) {
|
||||
std::vector<int> closed(component.size(), -1);
|
||||
flood_fill(closed, closed_sx, closed_sy, false);
|
||||
|
||||
const auto closed_reachable = [&](Vec2 p) {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
return nearest_walkable(p, x, y, false) && closed[index(x, y)] == 0;
|
||||
};
|
||||
for (const Room& r : level.rooms) {
|
||||
if (!closed_reachable({(r.rect.x0 + r.rect.x1) * 0.5f,
|
||||
(r.rect.y0 + r.rect.y1) * 0.5f})
|
||||
&& (r.kind != RoomKind::Side || r.side_index < 0)) {
|
||||
result.errors.push_back("room " + r.name
|
||||
+ " is only reachable through a gated door");
|
||||
}
|
||||
}
|
||||
if (!closed_reachable(level.anchors.key)) {
|
||||
result.warnings.push_back("threshold key is behind a gated door");
|
||||
}
|
||||
if (!closed_reachable(level.anchors.exit)) {
|
||||
result.warnings.push_back("threshold exit is behind a gated door");
|
||||
}
|
||||
}
|
||||
|
||||
size_t outside = 0;
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
if (walkable(x, y) && component[index(x, y)] != 0) ++outside;
|
||||
if (walkable(x, y, true) && component[index(x, y)] != 0) ++outside;
|
||||
}
|
||||
}
|
||||
if (outside > 0) {
|
||||
|
|
|
|||
|
|
@ -53,12 +53,15 @@ struct Options {
|
|||
float fov = 0.0f; // --fov <deg>: initial camera FOV
|
||||
float cam_height = 0.0f; // --cam-height <m>: initial camera height
|
||||
float cam_forward = 0.0f; // --cam-forward <m>: initial forward offset
|
||||
const char* level_path = nullptr; // --level <path>: authoring JSON to load
|
||||
};
|
||||
|
||||
Options parse_options(int argc, char** argv) {
|
||||
Options opts;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (std::strcmp(argv[i], "--seed") == 0 && i + 1 < argc) {
|
||||
if (std::strcmp(argv[i], "--level") == 0 && i + 1 < argc) {
|
||||
opts.level_path = argv[++i];
|
||||
} else if (std::strcmp(argv[i], "--seed") == 0 && i + 1 < argc) {
|
||||
opts.seed = std::strtoull(argv[i + 1], nullptr, 10);
|
||||
} else if (std::strcmp(argv[i], "--screenshot") == 0 && i + 1 < argc) {
|
||||
opts.screenshot_path = argv[i + 1];
|
||||
|
|
@ -229,7 +232,8 @@ int main(int argc, char** argv) {
|
|||
ui_font::init();
|
||||
ui_font::init_gui_style();
|
||||
clay_ui::init();
|
||||
Game game(opts.seed);
|
||||
Game game(opts.seed, opts.level_path != nullptr ? opts.level_path
|
||||
: Floor::kDefaultPath);
|
||||
game.state = (opts.bot || opts.hiss_test || opts.room_test || opts.aoe_test
|
||||
|| opts.floor_overview || opts.plaza_test || opts.office_test)
|
||||
? GameState::Playing : GameState::Title;
|
||||
|
|
|
|||
28
tests/data/bad_gap.json
Normal file
28
tests/data/bad_gap.json
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"version": 1,
|
||||
"name": "INVALID GAP FIXTURE",
|
||||
"map_width": 160,
|
||||
"map_height": 120,
|
||||
"scale": 1.5,
|
||||
"rooms": [
|
||||
{"name": "TEST ROOM", "rect": [0, 0, 160, 120], "kind": "Gate", "flow": 1, "side_index": -1}
|
||||
],
|
||||
"corridors": [],
|
||||
"walls": [
|
||||
{"axis": 0, "coord": 0, "from": 0, "to": 160, "height": 4.2, "gaps": [[50, 40]]},
|
||||
{"axis": 0, "coord": 120, "from": 0, "to": 160, "height": 4.2, "gaps": []},
|
||||
{"axis": 1, "coord": 0, "from": 0, "to": 120, "height": 4.2, "gaps": []},
|
||||
{"axis": 1, "coord": 160, "from": 0, "to": 120, "height": 4.2, "gaps": []}
|
||||
],
|
||||
"doors": [],
|
||||
"props": [],
|
||||
"rituals": [],
|
||||
"thermo": [],
|
||||
"anchors": {
|
||||
"spawn": [60, 50],
|
||||
"key": [80, 60],
|
||||
"exit": [100, 70],
|
||||
"key_radius": 3,
|
||||
"exit_radius": 4
|
||||
}
|
||||
}
|
||||
28
tests/data/valid.json
Normal file
28
tests/data/valid.json
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"version": 1,
|
||||
"name": "VALIDATION FIXTURE",
|
||||
"map_width": 160,
|
||||
"map_height": 120,
|
||||
"scale": 1.5,
|
||||
"rooms": [
|
||||
{"name": "TEST ROOM", "rect": [0, 0, 160, 120], "kind": "Gate", "flow": 1, "side_index": -1}
|
||||
],
|
||||
"corridors": [],
|
||||
"walls": [
|
||||
{"axis": 0, "coord": 0, "from": 0, "to": 160, "height": 4.2, "gaps": []},
|
||||
{"axis": 0, "coord": 120, "from": 0, "to": 160, "height": 4.2, "gaps": []},
|
||||
{"axis": 1, "coord": 0, "from": 0, "to": 120, "height": 4.2, "gaps": []},
|
||||
{"axis": 1, "coord": 160, "from": 0, "to": 120, "height": 4.2, "gaps": []}
|
||||
],
|
||||
"doors": [],
|
||||
"props": [],
|
||||
"rituals": [],
|
||||
"thermo": [],
|
||||
"anchors": {
|
||||
"spawn": [60, 50],
|
||||
"key": [80, 60],
|
||||
"exit": [100, 70],
|
||||
"key_radius": 3,
|
||||
"exit_radius": 4
|
||||
}
|
||||
}
|
||||
|
|
@ -90,6 +90,74 @@ int main() {
|
|||
broken.rooms.push_back({"ISLAND", {300, 300, 310, 310}, RoomKind::Side, 0, 0});
|
||||
check(!level::validate(broken).ok(), "unreachable room is reported");
|
||||
|
||||
level::LevelData dup_door = original;
|
||||
dup_door.doors.push_back(dup_door.doors.front());
|
||||
check(!level::validate(dup_door).ok(), "duplicate door id is reported");
|
||||
level::LevelData dup_ritual = original;
|
||||
dup_ritual.rituals.push_back(dup_ritual.rituals.front());
|
||||
check(!level::validate(dup_ritual).ok(), "duplicate ritual type is reported");
|
||||
level::LevelData moved_spawn = original;
|
||||
moved_spawn.anchors.spawn = {999.0f, 999.0f};
|
||||
check(!level::validate(moved_spawn).ok(), "out-of-map anchor is reported");
|
||||
|
||||
const auto gaps = level::normalize_gaps(
|
||||
0.0f, 100.0f, {{80.0f, 120.0f}, {20.0f, 40.0f},
|
||||
{30.0f, 50.0f}, {-10.0f, 5.0f}});
|
||||
check(gaps.size() == 3, "gaps clamp, sort and merge");
|
||||
if (gaps.size() == 3) {
|
||||
check(near(gaps[0].first, 0.0f) && near(gaps[0].second, 5.0f), "gap 0 clamped");
|
||||
check(near(gaps[1].first, 20.0f) && near(gaps[1].second, 50.0f), "gap 1 merged");
|
||||
check(near(gaps[2].first, 80.0f) && near(gaps[2].second, 100.0f), "gap 2 clamped");
|
||||
}
|
||||
check(level::remove_wall_segment(single, 0, 0), "remove first wall segment");
|
||||
check(level::build_world_walls(single).size() == 1, "segment became a gap");
|
||||
check(level::restore_wall_gap(single, 0, 0), "restore wall gap");
|
||||
const auto restored = level::build_world_walls(single);
|
||||
check(restored.size() == 1 && near(restored.front().rect.x1
|
||||
- restored.front().rect.x0, 150.0f),
|
||||
"gap merged back into a full wall segment");
|
||||
check(!level::remove_wall_segment(single, 0, 99), "bad segment index rejected");
|
||||
|
||||
check(level::allocate_door_id(single) == 0, "first door id is free in empty level");
|
||||
check(level::allocate_door_id(original) == -1, "all door ids taken in Floor 01");
|
||||
level::LevelData one_door;
|
||||
one_door.doors.push_back({DoorId::Booth, {0, 0, 1, 1}, 3.2f});
|
||||
check(level::allocate_door_id(one_door) == 0, "Janitor allocated before Booth");
|
||||
|
||||
const auto indexed = level::build_indexed_walls(original);
|
||||
check(indexed.size() == level::build_world_walls(original).size(),
|
||||
"indexed walls cover every world wall");
|
||||
bool saw_line = false;
|
||||
bool saw_door = false;
|
||||
bool saw_prop = false;
|
||||
for (const auto& wall : indexed) {
|
||||
if (wall.source == level::WallSource::Line) {
|
||||
saw_line = true;
|
||||
check(wall.line < original.walls.size(), "line index in range");
|
||||
check(wall.segment != level::kNoIndex, "segment index present");
|
||||
} else if (wall.source == level::WallSource::Door) {
|
||||
saw_door = true;
|
||||
check(wall.line < original.doors.size(), "door index in range");
|
||||
} else {
|
||||
saw_prop = true;
|
||||
check(wall.line < original.props.size(), "prop index in range");
|
||||
}
|
||||
}
|
||||
check(saw_line && saw_door && saw_prop, "all wall sources represented");
|
||||
|
||||
for (const Vec2 p : {Vec2{0.0f, 0.0f}, Vec2{80.0f, 13.0f}, Vec2{159.0f, 119.0f}}) {
|
||||
const Vec2 round = level::to_map(original, level::to_world(original, p));
|
||||
check(near(round.x, p.x) && near(round.y, p.y), "map/world round trip");
|
||||
}
|
||||
|
||||
level::LevelData rejected;
|
||||
check(!level::parse("{", rejected, error), "malformed JSON rejected");
|
||||
check(!level::parse("{}", rejected, error), "missing fields rejected");
|
||||
check(!level::parse("{\"version\":2}", rejected, error), "wrong version rejected");
|
||||
check(!level::parse("{\"version\":1,\"name\":\"x\",\"map_width\":0,"
|
||||
"\"map_height\":1,\"scale\":1}", rejected, error),
|
||||
"invalid dimensions rejected");
|
||||
|
||||
if (failures == 0) std::printf("level_core_test: all checks passed\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import re
|
|||
import sys
|
||||
|
||||
import bpy
|
||||
from mathutils import Euler, Vector
|
||||
from mathutils import Euler, Matrix, Vector
|
||||
|
||||
REPO = "/home/dandovlatov/Documents/cntrl"
|
||||
MODEL_DIR = os.path.join(REPO, "assets", "models")
|
||||
|
|
@ -31,6 +31,10 @@ import actor_kit as K
|
|||
importlib.reload(K)
|
||||
|
||||
K.clear_scene()
|
||||
# Re-running in a live session must not leave stale fake-user actions in the
|
||||
# saved .blend or the GLB's animation list.
|
||||
for old_action in list(bpy.data.actions):
|
||||
bpy.data.actions.remove(old_action)
|
||||
P = K.PALETTE
|
||||
P.update({
|
||||
"coverall": "#6f7f6d", "coverall_dk": "#566452", "coverall_sh": "#414c3e",
|
||||
|
|
@ -259,12 +263,12 @@ def build_agent():
|
|||
1.25, 1.27, 1.0, P["steel_dk"], ao=0.0)
|
||||
K.plate(r, "rifle_stock", rect(0.04, 0.27, -0.14, 0.06),
|
||||
1.12, 1.23, 0.94, P["leather"], ao=0.06)
|
||||
K.plate(r, "rifle_grip", rect(0.10, 0.21, -0.29, -0.17),
|
||||
0.92, 1.12, 0.86, P["black"], ao=0.06)
|
||||
grip_rear = K.plate(r, "rifle_grip", rect(0.10, 0.21, -0.29, -0.17),
|
||||
0.92, 1.12, 0.86, P["black"], ao=0.06)
|
||||
K.plate(r, "rifle_magazine", rect(0.09, 0.22, -0.52, -0.32),
|
||||
0.84, 1.10, 0.92, P["steel_dk"], ao=0.06)
|
||||
K.plate(r, "rifle_foregrip", rect(0.09, 0.21, -0.76, -0.62),
|
||||
0.97, 1.12, 0.9, P["black"], ao=0.06)
|
||||
grip_front = K.plate(r, "rifle_foregrip", rect(0.09, 0.21, -0.76, -0.62),
|
||||
0.97, 1.12, 0.9, P["black"], ao=0.06)
|
||||
K.plate(r, "rifle_barrel", rect(0.12, 0.20, -0.98, -0.62),
|
||||
1.15, 1.20, 1.0, P["steel_dk"], ao=0.0)
|
||||
K.plate(r, "rifle_brake", rect(0.11, 0.21, -1.06, -0.98),
|
||||
|
|
@ -289,7 +293,21 @@ def build_agent():
|
|||
if obj.type == "MESH" and obj.name.lower().startswith("rifle"):
|
||||
for v in obj.data.vertices:
|
||||
v.co = pivot + turn @ (v.co - pivot)
|
||||
# The original carry buried the stock in the chest. Nudge the
|
||||
# weapon forward toward the vest face so the grip is reachable,
|
||||
# then move the weapon bone with it.
|
||||
v.co.y -= 0.08
|
||||
obj.data.update()
|
||||
# Sample the real grip surfaces after the rifle has been turned. The rig
|
||||
# uses these points as IK targets, so the hands never drift off the gun.
|
||||
for side, obj in (("R", grip_rear), ("L", grip_front)):
|
||||
vs = obj.data.vertices
|
||||
GRIP_REST[side] = Vector((
|
||||
sum(v.co.x for v in vs) / len(vs),
|
||||
sum(v.co.y for v in vs) / len(vs),
|
||||
sum(v.co.z for v in vs) / len(vs),
|
||||
))
|
||||
print("[grip] %s %.3f %.3f %.3f" % (side, *GRIP_REST[side]))
|
||||
for i, (x, y, z, tilt) in enumerate([
|
||||
(0.12, 0.22, 1.40, (0.3, 0.2, 0.4)),
|
||||
(-0.10, 0.23, 1.24, (-0.4, 0.1, -0.3)),
|
||||
|
|
@ -304,6 +322,11 @@ def build_agent():
|
|||
return r
|
||||
|
||||
|
||||
# Rest-space centres of the rifle's rear grip and foregrip, filled in by
|
||||
# build_agent() once the rifle has been angled across the chest.
|
||||
GRIP_REST = {}
|
||||
|
||||
|
||||
# ------------------------------------------------------------------- rigging
|
||||
BONES = [
|
||||
("root", (0, 0, 0.0), (0, 0, 0.20), None),
|
||||
|
|
@ -312,16 +335,19 @@ BONES = [
|
|||
("chest", (0, 0, 1.26), (0, 0, 1.48), "spine"),
|
||||
("neck", (0, 0, 1.50), (0, 0, 1.62), "chest"),
|
||||
("head", (0, 0, 1.62), (0, 0, 1.98), "neck"),
|
||||
("weapon", (0.15, -0.25, 1.17), (-0.20, -0.75, 1.17), "chest"),
|
||||
("weapon", (0.15, -0.33, 1.17), (-0.20, -0.83, 1.17), "chest"),
|
||||
]
|
||||
for SIDE, SIGN in (("L", -1), ("R", 1)):
|
||||
BONES += [
|
||||
("upperarm." + SIDE, (SIGN * 0.43, 0, 1.46),
|
||||
(SIGN * 0.50, -0.04, 1.09), "chest"),
|
||||
("forearm." + SIDE, (SIGN * 0.50, -0.04, 1.09),
|
||||
(SIGN * 0.54, -0.08, 0.78), "upperarm." + SIDE),
|
||||
("hand." + SIDE, (SIGN * 0.54, -0.08, 0.78),
|
||||
(SIGN * 0.54, -0.20, 0.62), "forearm." + SIDE),
|
||||
# Joints follow the authored arm meshes (sleeve -> elbow -> wrist ->
|
||||
# fingers). The old bones hung at the hips while the mesh held the
|
||||
# rifle, so arm rotations swung the hands away from the gun.
|
||||
("upperarm." + SIDE, (SIGN * 0.41, -0.02, 1.46),
|
||||
(SIGN * 0.43, -0.20, 1.14), "chest"),
|
||||
("forearm." + SIDE, (SIGN * 0.43, -0.22, 1.14),
|
||||
(SIGN * 0.31, -0.60, 1.17), "upperarm." + SIDE),
|
||||
("hand." + SIDE, (SIGN * 0.31, -0.60, 1.17),
|
||||
(SIGN * 0.27, -0.84, 1.14), "forearm." + SIDE),
|
||||
("thigh." + SIDE, (SIGN * 0.17, 0, 0.88),
|
||||
(SIGN * 0.18, 0, 0.51), "hips"),
|
||||
("shin." + SIDE, (SIGN * 0.18, 0, 0.51),
|
||||
|
|
@ -421,12 +447,79 @@ def reset_pose(bones):
|
|||
pb.location = (0.0, 0.0, 0.0)
|
||||
|
||||
|
||||
# Every clip finishes with the same two-bone IK pass. The targets come from
|
||||
# the rifle's own grip surfaces (GRIP_REST), skinned by the current weapon
|
||||
# pose, so neither hand can drift off the gun when the chest leans or recoils.
|
||||
RECOIL_SCALE = 0.85 # 15% softer than the original firing kick
|
||||
|
||||
|
||||
def aim_bone(pb, parent_pose, head, tail):
|
||||
direction = (tail - head).normalized()
|
||||
up = Vector((0.0, 0.0, 1.0))
|
||||
if abs(direction.dot(up)) > 0.95:
|
||||
up = Vector((0.0, 1.0, 0.0))
|
||||
right = direction.cross(up).normalized()
|
||||
up2 = right.cross(direction).normalized()
|
||||
desired = Matrix((right, direction, up2)).transposed().to_4x4()
|
||||
desired.translation = head
|
||||
rest_parent = pb.parent.bone.matrix_local if pb.parent else Matrix.Identity(4)
|
||||
basis = parent_pose @ rest_parent.inverted() @ pb.bone.matrix_local
|
||||
pb.matrix_basis = basis.inverted() @ desired
|
||||
return desired
|
||||
|
||||
|
||||
def solve_arm(pb, side, target, pole, parent_pose, hand_dir):
|
||||
upper = pb["upperarm." + side]
|
||||
lower = pb["forearm." + side]
|
||||
hand = pb["hand." + side]
|
||||
shoulder = upper.head.copy()
|
||||
l1 = (upper.bone.tail_local - upper.bone.head_local).length
|
||||
l2 = (lower.bone.tail_local - lower.bone.head_local).length
|
||||
delta = target - shoulder
|
||||
reach = max(abs(l1 - l2) + 1e-3, min(delta.length, l1 + l2 - 1e-3))
|
||||
direction = delta.normalized()
|
||||
along = (l1 * l1 - l2 * l2 + reach * reach) / (2.0 * reach)
|
||||
height = math.sqrt(max(0.0, l1 * l1 - along * along))
|
||||
bend = pole - direction * pole.dot(direction)
|
||||
if bend.length < 1e-5:
|
||||
bend = Vector((1.0 if side == "R" else -1.0, 0.0, 0.0))
|
||||
bend -= direction * bend.dot(direction)
|
||||
bend.normalize()
|
||||
elbow = shoulder + direction * along + bend * height
|
||||
upper_pose = aim_bone(upper, parent_pose, shoulder, elbow)
|
||||
lower_pose = aim_bone(lower, upper_pose, elbow, target)
|
||||
# Fingers follow the barrel instead of hanging off the wrist.
|
||||
aim_bone(hand, lower_pose, target, target + hand_dir)
|
||||
|
||||
|
||||
def grip_weapon(pb, recoil=0.0):
|
||||
"""Pin both wrists to the rifle; optional pitch is the firing recoil."""
|
||||
bpy.context.view_layer.update()
|
||||
weapon = pb["weapon"]
|
||||
if recoil:
|
||||
pivot = pb["chest"].head.copy()
|
||||
around_chest = (Matrix.Translation(pivot)
|
||||
@ Matrix.Rotation(-0.22 * recoil, 4, "X")
|
||||
@ Matrix.Translation(-pivot))
|
||||
weapon.matrix = around_chest @ weapon.matrix.copy()
|
||||
bpy.context.view_layer.update()
|
||||
|
||||
# Skin the sampled grip centres by the weapon's current pose.
|
||||
skin = weapon.matrix @ weapon.bone.matrix_local.inverted()
|
||||
forward = (weapon.tail - weapon.head).normalized()
|
||||
hand_dir = (forward - Vector((0.0, 0.0, 0.55))).normalized()
|
||||
chest_pose = pb["chest"].matrix.copy()
|
||||
for side, behind in (("R", 0.05), ("L", 0.07)):
|
||||
target = skin @ GRIP_REST[side] - forward * behind
|
||||
pole = Vector((0.5 if side == "R" else -0.5, 0.1, -1.0))
|
||||
solve_arm(pb, side, target, pole, chest_pose, hand_dir)
|
||||
|
||||
|
||||
def pose_idle(pb, t):
|
||||
breath = math.sin(t * 2.0 * math.pi)
|
||||
pb["chest"].rotation_euler.x = 0.015 + 0.018 * breath
|
||||
pb["head"].rotation_euler.z = 0.025 * math.sin(t * math.pi)
|
||||
pb["upperarm.L"].rotation_euler.x = -0.03
|
||||
pb["upperarm.R"].rotation_euler.x = 0.03
|
||||
grip_weapon(pb)
|
||||
|
||||
|
||||
def pose_run(pb, t):
|
||||
|
|
@ -439,10 +532,7 @@ def pose_run(pb, t):
|
|||
pb["thigh.R"].rotation_euler.x = -0.62 * s
|
||||
pb["shin.L"].rotation_euler.x = 0.55 * max(0.0, -s)
|
||||
pb["shin.R"].rotation_euler.x = 0.55 * max(0.0, s)
|
||||
pb["upperarm.L"].rotation_euler.x = -0.28 * s
|
||||
pb["upperarm.R"].rotation_euler.x = 0.18 * s
|
||||
pb["forearm.L"].rotation_euler.x = 0.35
|
||||
pb["forearm.R"].rotation_euler.x = 0.42
|
||||
grip_weapon(pb)
|
||||
|
||||
|
||||
def pose_dash(pb, t):
|
||||
|
|
@ -452,27 +542,21 @@ def pose_dash(pb, t):
|
|||
pb["thigh.L"].rotation_euler.x = 0.80
|
||||
pb["thigh.R"].rotation_euler.x = -0.55
|
||||
pb["shin.L"].rotation_euler.x = 0.30
|
||||
pb["upperarm.L"].rotation_euler.x = -0.55
|
||||
pb["upperarm.R"].rotation_euler.x = -0.35
|
||||
pb["forearm.L"].rotation_euler.x = 0.30
|
||||
pb["forearm.R"].rotation_euler.x = 0.30
|
||||
grip_weapon(pb)
|
||||
|
||||
|
||||
def pose_fire(pb, t):
|
||||
kick = math.exp(-9.0 * t)
|
||||
pb["weapon"].rotation_euler.x = -0.22 * kick
|
||||
kick = math.exp(-9.0 * t) * RECOIL_SCALE
|
||||
pb["chest"].rotation_euler.x = 0.06 * kick
|
||||
pb["upperarm.R"].rotation_euler.x = 0.16 * kick
|
||||
pb["forearm.R"].rotation_euler.x = 0.20 * kick
|
||||
pb["head"].rotation_euler.x = -0.05 * kick
|
||||
grip_weapon(pb, recoil=kick)
|
||||
|
||||
|
||||
def pose_stagger(pb, t):
|
||||
fade = 1.0 - t
|
||||
pb["chest"].rotation_euler.x = -0.30 * fade
|
||||
pb["head"].rotation_euler.x = -0.35 * fade
|
||||
pb["upperarm.L"].rotation_euler.x = -0.35 * fade
|
||||
pb["upperarm.R"].rotation_euler.x = 0.30 * fade
|
||||
grip_weapon(pb)
|
||||
|
||||
|
||||
def make_actions(arm):
|
||||
|
|
@ -497,7 +581,8 @@ def make_actions(arm):
|
|||
for pb in arm.pose.bones:
|
||||
pb.keyframe_insert(data_path="rotation_euler", frame=frame,
|
||||
group=name)
|
||||
if pb.name == "root":
|
||||
if pb.name in ("root", "weapon", "upperarm.L", "upperarm.R",
|
||||
"forearm.L", "forearm.R", "hand.L", "hand.R"):
|
||||
pb.keyframe_insert(data_path="location", frame=frame,
|
||||
group=name)
|
||||
made.append((name, act))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from mathutils import Vector
|
|||
|
||||
REPO = "/home/dandovlatov/Documents/cntrl"
|
||||
LEVEL = os.environ.get("CNTRL_LEVEL", REPO + "/assets/levels/floor01.json")
|
||||
OUTPUT = REPO + "/assets/blender/fbc_level01_blockout.blend"
|
||||
OUTPUT = os.environ.get("CNTRL_OUT", REPO + "/assets/blender/fbc_level01_blockout.blend")
|
||||
|
||||
ACTOR_PATHS = {
|
||||
"TV": "assets/models/props/tv.glb",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue