From 2e7ba0b1538deae72abb841ebb381a7dfd7ff787 Mon Sep 17 00:00:00 2001 From: KP Date: Sat, 3 Aug 2024 11:16:49 -0500 Subject: [PATCH] Add scripts --- Data/resources/scripts/build-stairs.scm | 35 +++++++++++++++ Data/sandbox-log.txt | 59 +++++++++++++++++++++++++ KP3Dii/ext/src/imgui/TextEditor.cpp | 6 ++- KP3Dii/src/KP3D_Console.cpp | 14 +++++- KP3Dii/src/KP3D_Console.h | 2 +- KP3Dii/src/ksi/KSI_Environment.cpp | 2 + Sandbox/src/Editor.cpp | 7 ++- 7 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 Data/resources/scripts/build-stairs.scm diff --git a/Data/resources/scripts/build-stairs.scm b/Data/resources/scripts/build-stairs.scm new file mode 100644 index 0000000..ea2cdef --- /dev/null +++ b/Data/resources/scripts/build-stairs.scm @@ -0,0 +1,35 @@ +; This is a janky set of routines to make building stairs easier. +; More than anything it's just here to show off our fancy scripting +; capabilities. I suppose if you bound this to some keyboard shortcut +; it'd be a good time saver though :) +(progn +; CONFIG: + (define AXIS X) + (define STAIR_SIZE 0.25) + (define (lerp start end percent) + (+ start (* percent (- end start)))) + (define (build-stairs-x sx sy sw sl step height-a height-b) + (do ((i 0 (+1 i))) + ((>= i (/ sw step)) (print "done")) + (let* ((points (((+ (* i step) sx) sy) + ((+ (* i step) sx) (+ sy sl)) + ((+ (* i step) sx step) (+ sy sl)) + ((+ (* i step) sx step) sy))) + (floor-height (lerp height-a height-b (/ i (/ sw step)))) + (ceiling-height (+ 4 floor-height))) + (make-sector-from-points floor-height ceiling-height points)))) + (define (build-stairs-y sx sy sw sl step height-a height-b) + (do ((i 0 (+1 i))) + ((>= i (/ sl step)) (print "done")) + (let* ((points ((sx (+ (* i step) sy)) + ((- sx sw) (+ (* i step) sy)) + ((- sx sw) (+ (* i step) sy step)) + (sx (+ (* i step) sy step)))) + (floor-height (lerp height-a height-b (/ i (/ sl step)))) + (ceiling-height (+ 4 floor-height))) + (make-sector-from-points floor-height ceiling-height points)))) + (fill-selection + (lambda (x1 y1 x2 y2 nh1 nh2 dir) + (if (= AXIS X) + (build-stairs-x x1 y1 (abs (- x2 x1)) (abs (- y2 y1)) STAIR_SIZE nh1 nh2) + (build-stairs-y x2 y2 (abs (- x2 x1)) (abs (- y2 y1)) STAIR_SIZE nh1 nh2))))) diff --git a/Data/sandbox-log.txt b/Data/sandbox-log.txt index e69de29..1a60a74 100644 --- a/Data/sandbox-log.txt +++ b/Data/sandbox-log.txt @@ -0,0 +1,59 @@ +[11:10:09 AM] Info: Starting... + + KP3D version 2 +=============================== + Copyright (C) kpworld.xyz 2018-2024 + Contact me! @kp_cftsz + +[11:10:09 AM] Info: Initializing SDL +[11:10:09 AM] Info: Initializing OpenGL +[11:10:09 AM] Info: OpenGL version: 4.6.0 NVIDIA 536.23 +[11:10:09 AM] Info: Initializing GLEW +[11:10:09 AM] Info: Initializing SDL_mixer +[11:10:10 AM] Info: Reticulating splines... +[11:10:10 AM] Info: Ready! +[11:10:10 AM] Info: Loading material resource: block.png +[11:10:10 AM] Info: Found normal map texture: materials/block_n.png +[11:10:10 AM] Info: Loading material resource: brick2.jpg +[11:10:10 AM] Info: Found normal map texture: materials/brick2_n.jpg +[11:10:10 AM] Info: Loading material resource: bricks.jpg +[11:10:10 AM] Info: Found normal map texture: materials/bricks_n.jpg +[11:10:10 AM] Info: Loading material resource: FLAT5_7.png +[11:10:10 AM] Info: Found normal map texture: materials/FLAT5_7_n.png +[11:10:10 AM] Info: Loading material resource: floor0.png +[11:10:10 AM] Info: Found normal map texture: materials/floor0_n.png +[11:10:10 AM] Info: Loading material resource: floor1.png +[11:10:10 AM] Info: Found normal map texture: materials/floor1_n.png +[11:10:10 AM] Info: Loading material resource: GRASS2.png +[11:10:10 AM] Info: Found normal map texture: materials/GRASS2_n.png +[11:10:10 AM] Info: Loading material resource: hardwood.jpg +[11:10:10 AM] Info: Found normal map texture: materials/hardwood_n.jpg +[11:10:10 AM] Info: Map init +[11:10:10 AM] Info: Finalized mesh with 49 batches +[11:10:14 AM] Info: Finalized mesh with 54 batches +[11:10:27 AM] Info: [Editor] Filling selection; sector heights 0, -1 +[11:10:27 AM] Info: Finalized mesh with 60 batches +[11:10:27 AM] Info: Finalized mesh with 66 batches +[11:10:27 AM] Info: Finalized mesh with 72 batches +[11:10:27 AM] Info: Finalized mesh with 78 batches +[11:10:27 AM] Info: Finalized mesh with 84 batches +[11:10:27 AM] Info: Finalized mesh with 90 batches +[11:10:27 AM] Info: Finalized mesh with 96 batches +[11:10:27 AM] Info: Finalized mesh with 102 batches +[11:10:27 AM] Info: Finalized mesh with 108 batches +[11:10:27 AM] Info: Finalized mesh with 114 batches +[11:10:27 AM] Info: Finalized mesh with 120 batches +[11:10:27 AM] Info: Finalized mesh with 126 batches +[11:10:27 AM] Info: Finalized mesh with 132 batches +[11:10:27 AM] Info: Finalized mesh with 138 batches +[11:10:27 AM] Info: Finalized mesh with 144 batches +[11:10:27 AM] Info: Finalized mesh with 150 batches +[11:10:27 AM] Info: Finalized mesh with 156 batches +[11:10:27 AM] Info: Finalized mesh with 162 batches +[11:10:27 AM] Info: Finalized mesh with 168 batches +[11:10:27 AM] Info: Finalized mesh with 174 batches +[11:10:27 AM] Info: Finalized mesh with 180 batches +[11:10:27 AM] Info: Finalized mesh with 186 batches +[11:10:27 AM] Info: Finalized mesh with 192 batches +[11:10:27 AM] Info: Finalized mesh with 200 batches +[11:10:27 AM] Info: $ : done diff --git a/KP3Dii/ext/src/imgui/TextEditor.cpp b/KP3Dii/ext/src/imgui/TextEditor.cpp index 0fd0db6..f131349 100644 --- a/KP3Dii/ext/src/imgui/TextEditor.cpp +++ b/KP3Dii/ext/src/imgui/TextEditor.cpp @@ -3005,7 +3005,11 @@ const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::Lisp() langDef.mKeywords.insert(k); static const char* const identifiers[] = { - "sin", "cos", "sqrt", "abs", "log", "log10", "floor", "ceil", "atan2", "PI", "E", "car", "cdr", "cons", "list", "list-ref", "print", "exit", "+1", "-1", "null?" + "sin", "cos", "sqrt", "abs", "log", "log10", "floor", "ceil", "atan2", "PI", "E", "car", "cdr", "cons", "list", "list-ref", "print", "exit", "+1", "-1", "null?", + // Map editor specific + "make-sector-from-points", + "fill-selection", + "X", "Y" }; for (auto& k : identifiers) { diff --git a/KP3Dii/src/KP3D_Console.cpp b/KP3Dii/src/KP3D_Console.cpp index 6cdc74a..c7c1a14 100644 --- a/KP3Dii/src/KP3D_Console.cpp +++ b/KP3Dii/src/KP3D_Console.cpp @@ -1,5 +1,7 @@ #include "KP3D_Console.h" +#include + #include #include "KP3D_Log.h" @@ -134,8 +136,18 @@ void SendCommand(const std::string& command) SendKSI("(" + command + ")"); } -void SendKSI(const std::string& ksi) +void SendKSI(std::string ksi) { + // Remove comments (BRAINDEAD WAY OF DOING THIS) + auto lines = str::SplitTokens(ksi, "\n"); + for (const auto& line: lines) + if (str::StartsWith(line, ";")) + str::ReplaceAll(ksi, line, ""); + + // Single line + kp3d::str::ReplaceAll(ksi, "\n", ""); + + // Now eval reader.Clear(); reader.Tokenize(ksi); ksi::Cell c = reader.ReadForm(); diff --git a/KP3Dii/src/KP3D_Console.h b/KP3Dii/src/KP3D_Console.h index a2d1fba..8eabdd4 100644 --- a/KP3Dii/src/KP3D_Console.h +++ b/KP3Dii/src/KP3D_Console.h @@ -9,7 +9,7 @@ namespace kp3d::console { void Init(); void Render(); void SendCommand(const std::string& command); -void SendKSI(const std::string& ksi); +void SendKSI(std::string ksi); extern bool open; extern ksi::Environment environment; diff --git a/KP3Dii/src/ksi/KSI_Environment.cpp b/KP3Dii/src/ksi/KSI_Environment.cpp index d3cbf47..51e946e 100644 --- a/KP3Dii/src/ksi/KSI_Environment.cpp +++ b/KP3Dii/src/ksi/KSI_Environment.cpp @@ -598,6 +598,8 @@ void Environment::Init() Set("atan2", FAtan2()); Set("PI", {CELL_NUMBER, 3.14159265358979f}); Set("E", {CELL_NUMBER, 2.71828f}); + Set("X", {CELL_NUMBER, 0}); + Set("Y", {CELL_NUMBER, 1}); // Cons/CAR/CDR Set("car", FCar()); diff --git a/Sandbox/src/Editor.cpp b/Sandbox/src/Editor.cpp index ee82af4..0031038 100644 --- a/Sandbox/src/Editor.cpp +++ b/Sandbox/src/Editor.cpp @@ -116,6 +116,8 @@ ksi::Cell FFillSelection() XYf p3 = {points[1].x, points[1].z}; XYf p4 = {points[3].x, points[3].z}; + int dir = 0; + float nh1 = 0.0f; float nh2 = 4.0f; for (const auto& sp: sandbox->map.sectors) @@ -156,6 +158,7 @@ ksi::Cell FFillSelection() p2.y, // y2 nh1, // nh1 nh2, // nh2 + dir // dir }; size_t i = 0; @@ -389,8 +392,10 @@ void Editor::RenderModeBuild() if (ImGui::Button("Evaluate")) { std::string txt = build_text_editor.GetText(); - kp3d::str::ReplaceAll(txt, "\n", ""); kp3d::console::SendKSI(txt); + points.clear(); + build_has_start_pos = false; + build_show_options = false; // KP3D_LOG_INFO("EVAL: {}", txt); } ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[1]);