various updates

This commit is contained in:
skythedragon 2025-02-22 17:55:01 -08:00
parent 14fc1e8841
commit 9ef5bd04af
8 changed files with 253 additions and 49 deletions

View File

@ -10,7 +10,8 @@
"cppStandard": "${default}", "cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64", "intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [ "compilerArgs": [
"" "-pthread",
"-std=c++23"
] ]
} }
], ],

2
.vscode/tasks.json vendored
View File

@ -10,7 +10,7 @@
"${file}", "${file}",
"-o", "-o",
"${fileDirname}/${fileBasenameNoExtension}", "${fileDirname}/${fileBasenameNoExtension}",
"-std=c++20", "-std=c++23",
"-pthread", "-pthread",
"-lgmp", "-lgmp",
"-lgmpxx" "-lgmpxx"

120
save/hello.json Normal file
View File

@ -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
}
}
}

View File

@ -73,11 +73,11 @@
}, },
"totalTransistors": { "totalTransistors": {
"denominator": "160", "denominator": "160",
"numerator": "437365449" "numerator": "928946893"
}, },
"transistorBalance": { "transistorBalance": {
"denominator": "167772160000000000000000000000", "denominator": "167772160000000000000000000000",
"numerator": "310455754441404824549415535544065359" "numerator": "825916258665148824549415535544065359"
}, },
"upgrades": { "upgrades": {
"cheap lithography machines": { "cheap lithography machines": {

View File

@ -12,16 +12,39 @@
namespace CMD { namespace CMD {
long int ticks = 0;
using str = std::string; using str = std::string;
typedef void (*ScriptFunction)(std::vector<str>&); class ScriptFunction {
typedef void (*UpdateFunction)(void); public:
using Onzero = UpdateFunction; virtual void operator() (const std::vector<str>& args) = 0;
};
typedef bool (*Condition)(std::vector<str>&); class UpdateFunction {
typedef void (*Result)(std::vector<str>&); public:
virtual void operator() () = 0;
};
class Bungle : public UpdateFunction, public ScriptFunction {
void operator() () override {}
void operator() (const std::vector<str>& args) override {}
};
class Trigger {
public:
bool selfdelete;
virtual bool check(const std::vector<str>& args) = 0;
virtual void run(const std::vector<str>& 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:\\"; str name = "CMD:\\";
@ -34,7 +57,7 @@ namespace CMD {
std::ofstream logfile; std::ofstream logfile;
std::unordered_map<str, ScriptFunction> commands; std::unordered_map<str, std::unique_ptr<ScriptFunction>> commands;
std::stringstream beforeComm; std::stringstream beforeComm;
std::stringstream afterComm; std::stringstream afterComm;
@ -60,8 +83,8 @@ namespace CMD {
logfile << "[" << gettime() << "] <" << user << ">: " << message << std::endl; logfile << "[" << gettime() << "] <" << user << ">: " << message << std::endl;
} }
void addcommand(str name, ScriptFunction func) { void addcommand(str name, std::unique_ptr<ScriptFunction> funcPtr) {
commands[name] = func; commands[name] = std::move(funcPtr);
} }
void remcommand(str name) { void remcommand(str name) {
@ -71,34 +94,38 @@ namespace CMD {
} }
} }
void bungle() {}
void bungle(std::vector<str>& bung) {}
void execute_command( void execute_command(
ScriptFunction command, str command,
::std::vector <str> args) ::std::vector <str> 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) { void execute_update(std::unique_ptr<UpdateFunction>& updater) {
updater(); (*updater)();
} }
std::unique_ptr<UpdateFunction> bungle = std::make_unique<Bungle>();
::std::mutex command_mutex; ::std::mutex command_mutex;
::std::condition_variable command_ready_condition; ::std::condition_variable command_ready_condition;
ScriptFunction command = nullptr; str command;
ScriptFunction exit = bungle; std::unique_ptr<ScriptFunction> exit = std::make_unique<Bungle>();
::std::vector<str> arguments; ::std::vector<str> arguments;
bool runcomm(str commandfull, Onzero onzero) { bool runcomm(str commandfull, std::unique_ptr<Onzero>& onzero) {
std::vector<str> args = sky::split(commandfull, " "); std::vector<str> args = sky::split(commandfull, " ");
if(args.size() > 0) { if(args.size() > 0) {
str command_name = args[0]; str command_name = args[0];
args.erase(args.begin()); args.erase(args.begin());
if (command_name == exitcomm) { if (command_name == exitcomm) {
// Treat exit specially. // Treat exit specially.
execute_command(exit, args); (*exit)(args);
return false; return false;
} }
auto const search_result = commands.find(command_name); auto const search_result = commands.find(command_name);
@ -109,23 +136,23 @@ namespace CMD {
}; };
command_ready_condition.wait( command_ready_condition.wait(
lk, lk,
[]() { return command == nullptr; } []() { return command == ""; }
); );
command = search_result->second; command = command_name;
arguments.swap(args); arguments.swap(args);
lk.unlock(); lk.unlock();
command_ready_condition.notify_all(); command_ready_condition.notify_all();
} else { } else {
std::cout << "Command " + command_name + " not found.\n"; std::cout << "Command " + command_name + " not found.\n";
} }
} else {onzero();} } else {(*onzero)();}
return true; return true;
} }
void engine_loop( void engine_loop(
::std::stop_token const stop, ::std::stop_token const stop,
str const name, str const name,
UpdateFunction updater, std::unique_ptr<UpdateFunction> updater,
::std::chrono::microseconds update_interval ::std::chrono::microseconds update_interval
) { ) {
using clock = ::std::chrono::high_resolution_clock; using clock = ::std::chrono::high_resolution_clock;
@ -166,12 +193,12 @@ namespace CMD {
command_ready_condition.wait_for( command_ready_condition.wait_for(
lk, time_to_wait, lk, time_to_wait,
[&stop] { [&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. // Copy out command and arguments.
auto command_copy = command; str command_copy = command;
// So, if someone changes the type of arguments, // So, if someone changes the type of arguments,
// this will still be right. // this will still be right.
decltype(arguments) arg_copy; decltype(arguments) arg_copy;
@ -181,7 +208,7 @@ namespace CMD {
arg_copy.swap(arguments); arg_copy.swap(arguments);
// Now rest command to nullptr to indicate we have it and // Now rest command to nullptr to indicate we have it and
// are ready for a new command. // are ready for a new command.
command = nullptr; command = "";
// Unlock since we're done messing with // Unlock since we're done messing with
// command and arguments now. // command and arguments now.
lk.unlock(); lk.unlock();
@ -225,9 +252,7 @@ namespace CMD {
log("Stop of engine loop requested!", "CMD:\\"); log("Stop of engine loop requested!", "CMD:\\");
} }
void errzero() { std::unique_ptr<Onzero> errzero = std::make_unique<ErrZero>();
std::cout << "BAD BOY: ENETER A REAL COMMAND\n";
}
constexpr ::std::chrono::microseconds sixteenth_second{62500}; constexpr ::std::chrono::microseconds sixteenth_second{62500};
@ -236,19 +261,19 @@ namespace CMD {
inline ::std::jthread init( inline ::std::jthread init(
str tename, str tename,
str const &promptstyle, str const &promptstyle,
UpdateFunction updater = bungle, std::unique_ptr<UpdateFunction> updater = std::move(bungle),
::std::chrono::microseconds interval = sixteenth_second ::std::chrono::microseconds interval = sixteenth_second
) { ) {
prgname = tename; prgname = tename;
prompt = promptstyle; prompt = promptstyle;
uinterval = interval; uinterval = interval;
logfile.open("log.txt", std::ios::app); 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'; logfile << "\n\n\n[" << gettime() << "] : " << name << " initialized\n" << name << " ver " << ver << '\n';
return game_thread; // Named Value Return Optimization applies. return game_thread; // Named Value Return Optimization applies.
} }
void command_loop(Onzero onzero = errzero) { void command_loop(std::unique_ptr<Onzero>& onzero = errzero) {
bool exited = false; bool exited = false;
while(!exited) { while(!exited) {
std::this_thread::sleep_for(uinterval); std::this_thread::sleep_for(uinterval);

View File

@ -166,6 +166,8 @@ public:
} }
}; };
class Command : public CMD::ScriptFunction {};
#pragma endregion #pragma endregion
@ -230,11 +232,11 @@ str gameName;
#pragma region number stuff #pragma region number stuff
integer getrationalnumerator(number n) { integer getrationalnumerator(const number& n) {
return integer(n.get_num_mpz_t()); return integer(n.get_num_mpz_t());
} }
integer getrationaldenominator(number n) { integer getrationaldenominator(const number& n) {
return integer(n.get_den_mpz_t()); return integer(n.get_den_mpz_t());
} }
@ -279,7 +281,7 @@ str TransitorsString(number transistors, integer precision = 0, str colA = BOLDG
#pragma region misc #pragma region misc
void increaseTransistors(number by) { void increaseTransistors(const number& by) {
gameState.transistorBalance += by; gameState.transistorBalance += by;
gameState.totalTransistors += by; gameState.totalTransistors += by;
} }
@ -413,9 +415,8 @@ void testTriggers(std::vector<str> args) {
} }
void onTick() { void onTick() {
number TPS = calcTPS(); tPSCache = calcTPS();
tPSCache = TPS; increaseTransistors(tPSCache / 16);
increaseTransistors(TPS / 16);
testTriggers(CMD::arguments); testTriggers(CMD::arguments);
} }
@ -448,19 +449,19 @@ void createBuilding(str name, str description, str flavorText, number basePrice,
buildingIndex[tolower(shorthand)] = buildings.size() - 1; buildingIndex[tolower(shorthand)] = buildings.size() - 1;
} }
size_t getUpgradeByName(str Uname) { size_t getUpgradeByName(const str& Uname) {
return upgradeIndex[tolower(Uname)]; return upgradeIndex[tolower(Uname)];
} }
size_t getAchievementByName(str Aname) { size_t getAchievementByName(const str& Aname) {
return achievementIndex[tolower(Aname)]; return achievementIndex[tolower(Aname)];
} }
size_t getBuildingByShorthand(str shorthand) { size_t getBuildingByShorthand(const str& shorthand) {
return buildingIndex[tolower(shorthand)]; return buildingIndex[tolower(shorthand)];
} }
Building GBySH(str shorthand) { Building GBySH(const str& shorthand) {
return buildings[buildingIndex[tolower(shorthand)]]; return buildings[buildingIndex[tolower(shorthand)]];
} }

57
src/test.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <functional>
#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<str>& 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<str> 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<Dummy>());
CMD::log("Program booted");
CMD::command_loop(CMD::errzero);
CMD::log("Requesting stop.");
engine_thread.request_stop();
engine_thread.join();
return 0;
}

BIN
test Executable file

Binary file not shown.