diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index c2098a2..438822b 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -10,7 +10,8 @@ "cppStandard": "${default}", "intelliSenseMode": "linux-gcc-x64", "compilerArgs": [ - "" + "-pthread", + "-std=c++23" ] } ], diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 0aa30aa..f42da44 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -10,7 +10,7 @@ "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", - "-std=c++20", + "-std=c++23", "-pthread", "-lgmp", "-lgmpxx" diff --git a/save/hello.json b/save/hello.json new file mode 100644 index 0000000..adb65ae --- /dev/null +++ b/save/hello.json @@ -0,0 +1,120 @@ +{ + "achievements": { + "???": { + "earned": false + }, + "FABrication": { + "earned": false + }, + "all you had to do was ask": { + "earned": false + }, + "autoclick": { + "earned": false + }, + "casual semiconductors": { + "earned": false + }, + "click": { + "earned": false + }, + "datamining": { + "earned": false + }, + "double click": { + "earned": false + }, + "hardcore semiconductors": { + "earned": false + }, + "moss gardens": { + "earned": false + }, + "mossy": { + "earned": false + }, + "mouse wheel": { + "earned": false + }, + "semigood": { + "earned": false + }, + "steady semistream": { + "earned": false + }, + "that's big": { + "earned": false + }, + "transistant": { + "earned": false + } + }, + "buildings": { + "cursor": { + "count": "0", + "unlocked": false + }, + "largefab": { + "count": "0", + "unlocked": false + }, + "mediumfab": { + "count": "0", + "unlocked": false + }, + "moss": { + "count": "0", + "unlocked": false + }, + "smallfab": { + "count": "0", + "unlocked": false + } + }, + "totalTransistors": { + "denominator": "1", + "numerator": "0" + }, + "transistorBalance": { + "denominator": "1", + "numerator": "0" + }, + "upgrades": { + "cheap lithography machines": { + "bought": false, + "unlocked": false + }, + "chippy": { + "bought": false, + "unlocked": false + }, + "denser chips": { + "bought": false, + "unlocked": false + }, + "endgame": { + "bought": false, + "unlocked": false + }, + "faster fingers": { + "bought": false, + "unlocked": false + }, + "integrated mouse": { + "bought": false, + "unlocked": false + }, + "moss walls": { + "bought": false, + "unlocked": false + }, + "mossier tech": { + "bought": false, + "unlocked": false + }, + "mossy mossy": { + "bought": false, + "unlocked": false + } + } +} \ No newline at end of file diff --git a/save/main.json b/save/main.json index ce529a0..2a01466 100644 --- a/save/main.json +++ b/save/main.json @@ -73,11 +73,11 @@ }, "totalTransistors": { "denominator": "160", - "numerator": "437365449" + "numerator": "928946893" }, "transistorBalance": { "denominator": "167772160000000000000000000000", - "numerator": "310455754441404824549415535544065359" + "numerator": "825916258665148824549415535544065359" }, "upgrades": { "cheap lithography machines": { diff --git a/src/lib/CMD.hpp b/src/lib/CMD.hpp index fbfc6e9..52dcde2 100644 --- a/src/lib/CMD.hpp +++ b/src/lib/CMD.hpp @@ -12,16 +12,39 @@ namespace CMD { - long int ticks = 0; - using str = std::string; - typedef void (*ScriptFunction)(std::vector&); - typedef void (*UpdateFunction)(void); - using Onzero = UpdateFunction; + class ScriptFunction { + public: + virtual void operator() (const std::vector& args) = 0; + }; - typedef bool (*Condition)(std::vector&); - typedef void (*Result)(std::vector&); + class UpdateFunction { + public: + virtual void operator() () = 0; + }; + + class Bungle : public UpdateFunction, public ScriptFunction { + void operator() () override {} + void operator() (const std::vector& args) override {} + }; + + class Trigger { + public: + bool selfdelete; + virtual bool check(const std::vector& args) = 0; + virtual void run(const std::vector& args) = 0; + }; + + class ErrZero : public UpdateFunction { + void operator() () override { + std::cout << "BAD BOY: ENETER A REAL COMMAND\n"; + } + }; + + long int ticks = 0; + + using Onzero = UpdateFunction; str name = "CMD:\\"; @@ -34,7 +57,7 @@ namespace CMD { std::ofstream logfile; - std::unordered_map commands; + std::unordered_map> commands; std::stringstream beforeComm; std::stringstream afterComm; @@ -60,8 +83,8 @@ namespace CMD { logfile << "[" << gettime() << "] <" << user << ">: " << message << std::endl; } - void addcommand(str name, ScriptFunction func) { - commands[name] = func; + void addcommand(str name, std::unique_ptr funcPtr) { + commands[name] = std::move(funcPtr); } void remcommand(str name) { @@ -71,34 +94,38 @@ namespace CMD { } } - void bungle() {} - void bungle(std::vector& bung) {} - void execute_command( - ScriptFunction command, + str command, ::std::vector args) { - command(args); + auto const search_result = commands.find(command); + if(search_result != commands.end()) { + (*search_result->second)(args); + } else { + CMD::log("command search failed for:" + command, "CMD:\\"); + } } - void execute_update(UpdateFunction updater) { - updater(); + void execute_update(std::unique_ptr& updater) { + (*updater)(); } + std::unique_ptr bungle = std::make_unique(); + ::std::mutex command_mutex; ::std::condition_variable command_ready_condition; - ScriptFunction command = nullptr; - ScriptFunction exit = bungle; + str command; + std::unique_ptr exit = std::make_unique(); ::std::vector arguments; - bool runcomm(str commandfull, Onzero onzero) { + bool runcomm(str commandfull, std::unique_ptr& onzero) { std::vector args = sky::split(commandfull, " "); if(args.size() > 0) { str command_name = args[0]; args.erase(args.begin()); if (command_name == exitcomm) { // Treat exit specially. - execute_command(exit, args); + (*exit)(args); return false; } auto const search_result = commands.find(command_name); @@ -109,23 +136,23 @@ namespace CMD { }; command_ready_condition.wait( lk, - []() { return command == nullptr; } + []() { return command == ""; } ); - command = search_result->second; + command = command_name; arguments.swap(args); lk.unlock(); command_ready_condition.notify_all(); } else { std::cout << "Command " + command_name + " not found.\n"; } - } else {onzero();} + } else {(*onzero)();} return true; } void engine_loop( ::std::stop_token const stop, str const name, - UpdateFunction updater, + std::unique_ptr updater, ::std::chrono::microseconds update_interval ) { using clock = ::std::chrono::high_resolution_clock; @@ -166,12 +193,12 @@ namespace CMD { command_ready_condition.wait_for( lk, time_to_wait, [&stop] { - return stop.stop_requested() || command != nullptr; + return stop.stop_requested() || command != ""; } ); - if (!stop.stop_requested() && command != nullptr) { + if (!stop.stop_requested() && command != "") { // Copy out command and arguments. - auto command_copy = command; + str command_copy = command; // So, if someone changes the type of arguments, // this will still be right. decltype(arguments) arg_copy; @@ -181,7 +208,7 @@ namespace CMD { arg_copy.swap(arguments); // Now rest command to nullptr to indicate we have it and // are ready for a new command. - command = nullptr; + command = ""; // Unlock since we're done messing with // command and arguments now. lk.unlock(); @@ -225,9 +252,7 @@ namespace CMD { log("Stop of engine loop requested!", "CMD:\\"); } - void errzero() { - std::cout << "BAD BOY: ENETER A REAL COMMAND\n"; - } + std::unique_ptr errzero = std::make_unique(); constexpr ::std::chrono::microseconds sixteenth_second{62500}; @@ -236,19 +261,19 @@ namespace CMD { inline ::std::jthread init( str tename, str const &promptstyle, - UpdateFunction updater = bungle, + std::unique_ptr updater = std::move(bungle), ::std::chrono::microseconds interval = sixteenth_second ) { prgname = tename; prompt = promptstyle; uinterval = interval; logfile.open("log.txt", std::ios::app); - ::std::jthread game_thread{engine_loop, tename, updater, interval}; + ::std::jthread game_thread{engine_loop, tename, std::move(updater), interval}; logfile << "\n\n\n[" << gettime() << "] : " << name << " initialized\n" << name << " ver " << ver << '\n'; return game_thread; // Named Value Return Optimization applies. } - void command_loop(Onzero onzero = errzero) { + void command_loop(std::unique_ptr& onzero = errzero) { bool exited = false; while(!exited) { std::this_thread::sleep_for(uinterval); diff --git a/src/main.cpp b/src/main.cpp index a68c075..9f3c08b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -166,6 +166,8 @@ public: } }; +class Command : public CMD::ScriptFunction {}; + #pragma endregion @@ -230,11 +232,11 @@ str gameName; #pragma region number stuff -integer getrationalnumerator(number n) { +integer getrationalnumerator(const number& n) { return integer(n.get_num_mpz_t()); } -integer getrationaldenominator(number n) { +integer getrationaldenominator(const number& n) { return integer(n.get_den_mpz_t()); } @@ -279,7 +281,7 @@ str TransitorsString(number transistors, integer precision = 0, str colA = BOLDG #pragma region misc -void increaseTransistors(number by) { +void increaseTransistors(const number& by) { gameState.transistorBalance += by; gameState.totalTransistors += by; } @@ -413,9 +415,8 @@ void testTriggers(std::vector args) { } void onTick() { - number TPS = calcTPS(); - tPSCache = TPS; - increaseTransistors(TPS / 16); + tPSCache = calcTPS(); + increaseTransistors(tPSCache / 16); testTriggers(CMD::arguments); } @@ -448,19 +449,19 @@ void createBuilding(str name, str description, str flavorText, number basePrice, buildingIndex[tolower(shorthand)] = buildings.size() - 1; } -size_t getUpgradeByName(str Uname) { +size_t getUpgradeByName(const str& Uname) { return upgradeIndex[tolower(Uname)]; } -size_t getAchievementByName(str Aname) { +size_t getAchievementByName(const str& Aname) { return achievementIndex[tolower(Aname)]; } -size_t getBuildingByShorthand(str shorthand) { +size_t getBuildingByShorthand(const str& shorthand) { return buildingIndex[tolower(shorthand)]; } -Building GBySH(str shorthand) { +Building GBySH(const str& shorthand) { return buildings[buildingIndex[tolower(shorthand)]]; } diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..4d623c6 --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,57 @@ + +#include +#include + +#include "./lib/CMD.hpp" + +using str = std::string; +str name = "test"; + +str ver = "0.0.0"; + + +class Dummy : public CMD::ScriptFunction { +public: + void operator() (const std::vector& args) override { + using std::cout; + if (args.empty()) { + cout << "dummy\n"; + } else { + cout << "dummy ["; + bool need_comma = false; + for (auto const &arg: args) { + if (need_comma) { + cout << ", "; + } else { + need_comma = true; + } + cout << '"' << arg << '"'; + } + cout << "]\n"; + } + } +}; + +void my_updater() { + ::std::cout << "Updating game state.\n"; +} + +int abalabada = 0; + +bool adisAbaba(std::vector args) { + return abalabada == 1234; +} + +int main() { + std::jthread engine_thread = CMD::init(name, "Command? "); + CMD::log("Booting " + name + ".\n ver " + ver); + + CMD::addcommand("dummi", std::make_unique()); + + CMD::log("Program booted"); + CMD::command_loop(CMD::errzero); + CMD::log("Requesting stop."); + engine_thread.request_stop(); + engine_thread.join(); + return 0; +} diff --git a/test b/test new file mode 100755 index 0000000..04f904e Binary files /dev/null and b/test differ