Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into jgrpp
  • Loading branch information
lucaFiorini committed Feb 18, 2025
2 parents a36ecce + f3e008b commit 517e7d6
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 36 deletions.
29 changes: 17 additions & 12 deletions src/order_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ std::string Order::ToJSONString() const
//in this->GetConditionComparator() other then that, I believe I have fully decoded it

json["type"] = this->GetType();

if (
IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_STATION) ||
(IsType(OT_LABEL) && IsDestinationOrderLabelSubType(this->GetLabelSubType()))
Expand Down Expand Up @@ -1064,13 +1064,20 @@ std::string OrderList::ToJSONString()
};

auto& SD_data = this->GetScheduledDispatchScheduleSet();
auto& headJson = json["head"];
for (unsigned int i = 0; auto &SD : SD_data) {

headJson["scheduled-dispatch"][i++] = nlohmann::ordered_json::parse(SD.ToJSONString());
if (SD_data.size() != 0) {

json["game-properties"]["default-scheduled-dispatch-duration"] = getScheduledDispatchDefaultDuration();

for (unsigned int i = 0; auto & SD : SD_data) {

json["scheduled-dispatch"]["schedules"][i++] = nlohmann::ordered_json::parse(SD.ToJSONString());

}

}


const Order* o = this->GetFirstOrder();

if (o != nullptr) {
Expand Down Expand Up @@ -1125,17 +1132,15 @@ void OrderList::FromJSONString(const Vehicle * v,std::string json_str)
}
}

if (json.contains("head")){
auto &headJson = json["head"];
if (headJson.contains("scheduled-dispatch")) {
auto &SDJson = headJson["scheduled-dispatch"];
if (SDJson.is_array()) {
for (nlohmann::json::iterator it = SDJson.begin(); it != SDJson.end(); ++it) {
DoCommandPEx(0, v->index, 0, 0, CMD_SCHEDULED_DISPATCH_ADD_NEW_SCHEDULE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE), nullptr, it.value().dump().c_str() , 0);
}
if (json.contains("scheduled-dispatch")) {
auto &SDJson = json["scheduled-dispatch"];
if (SDJson.is_array()) {
for (nlohmann::json::iterator it = SDJson.begin(); it != SDJson.end(); ++it) {
AddNewScheduledDispatchSchedule(v->index, it.value().dump().c_str());
}
}
}

}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/schdispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "vehicle_type.h"
#include "settings_type.h"

uint32_t getScheduledDispatchDefaultDuration();

void AddNewScheduledDispatchSchedule(VehicleID vindex, const char *jsonString);

void ShowSchdispatchWindow(const Vehicle *v);
void SchdispatchInvalidateWindows(const Vehicle *v);

Expand Down
125 changes: 103 additions & 22 deletions src/schdispatch_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,28 @@ CommandCost CmdScheduledDispatchAddNewSchedule(TileIndex tile, DoCommandFlag fla
if (flags & DC_EXEC) {
v->orders->GetScheduledDispatchScheduleSet().emplace_back();
DispatchSchedule &ds = v->orders->GetScheduledDispatchScheduleSet().back();
if (scheduleJson == nullptr) {
if (p2 == 0) return CMD_ERROR;
ds.SetScheduledDispatchDuration(p2);
ds.SetScheduledDispatchStartTick((StateTicks)p3);
ds.UpdateScheduledDispatch(nullptr);
SetTimetableWindowsDirty(v, STWDF_SCHEDULED_DISPATCH);

if (p2 == 0) return CMD_ERROR;

if (scheduleJson != nullptr) {

DispatchSchedule ds = DispatchSchedule::FromJSONString(scheduleJson);

} else {

ds = DispatchSchedule::FromJSONString(scheduleJson);
//If Json is present it will set the duration instead
ds.SetScheduledDispatchDuration(p2);

}

ds.SetScheduledDispatchStartTick((StateTicks)p3);
ds.UpdateScheduledDispatch(nullptr);
SetTimetableWindowsDirty(v, STWDF_SCHEDULED_DISPATCH);

}



return CommandCost();
}

Expand Down Expand Up @@ -881,7 +889,7 @@ void DispatchSchedule::UpdateScheduledDispatch(const Vehicle *v)
}
}

DispatchSchedule DispatchSchedule::FromJSONString(std::string jsonString) {
DispatchSchedule DispatchSchedule::FromJSONString(std::string jsonString) { //broken

nlohmann::json json = nlohmann::json::parse(jsonString);

Expand All @@ -891,13 +899,37 @@ DispatchSchedule DispatchSchedule::FromJSONString(std::string jsonString) {

auto &slotsJson = json.at("slots");

if (slotsJson.is_array()) {
for (nlohmann::json::iterator it = slotsJson.begin(); it != slotsJson.end(); ++it) {
if (slotsJson.is_object()) {

for (auto it = slotsJson.begin(); it != slotsJson.end(); ++it) {

DispatchSlot newDispatchSlot;

newDispatchSlot.offset = it.value().at("offset").template get<uint32_t>();
newDispatchSlot.flags = it.value().at("flags").template get<uint16_t>();
int offset;

try {

offset = std::stoi(it.key());
if (offset < 0) throw 1;

} catch (...) {

continue;

}

nlohmann::json slotData = it.value();

new_schedule.AddScheduledDispatch(offset);

if (slotData.contains("re-use-slot") && slotData["re-use-slot"].is_boolean()) {

}

if(slotData.contains("tags") && slotData["tags"].is_array()) {

}


new_schedule.GetScheduledDispatchMutable().push_back(newDispatchSlot);

Expand All @@ -906,30 +938,79 @@ DispatchSchedule DispatchSchedule::FromJSONString(std::string jsonString) {
}

new_schedule.ScheduleName() = json.at("name").get<std::string>();
new_schedule.SetScheduledDispatchStartTick((StateTicks)json.at("start-tick").get<uint32_t>());

if (json.contains("duration") && json["duration"].is_number_integer() && json["duration"] > 0) {

new_schedule.SetScheduledDispatchDuration(json["duration"]);

} else {

new_schedule.SetScheduledDispatchDuration(-1); //set invalid duration so it gets overridden or throws an error[TODO]

}

new_schedule.SetScheduledDispatchDuration(json.at("duration").get<uint32_t>());
new_schedule.SetScheduledDispatchDelay(json.at("max-delay").get<uint32_t>());
new_schedule.SetScheduledDispatchFlags(json.at("flags").get<uint8_t>());

return new_schedule;
}


std::string DispatchSchedule::ToJSONString()
{

nlohmann::ordered_json json;

for (unsigned int i = 0; auto & SD_slot : this->GetScheduledDispatch()) {
auto &slotsJson = json["slots"][i++];
slotsJson["offset"] = SD_slot.offset;
slotsJson["flags"] = SD_slot.flags;
for (int i = 0; i < (DispatchSlot::SDSF_LAST_TAG - DispatchSlot::SDSF_FIRST_TAG); i++) {

std::string_view rename = this->GetSupplementaryName(SDSNT_DEPARTURE_TAG, i);

if (!rename.empty()) {

json["renamed-tags"]["Tag-" + std::to_string(i + 1)] = rename;

}

}

for (auto & SD_slot : this->GetScheduledDispatch()) {

std::string stringOffset = std::to_string(SD_slot.offset);

auto &slotJson = json["slots"][stringOffset];

if (HasBit(SD_slot.flags, DispatchSlot::SDSF_REUSE_SLOT)) {
slotJson["re-use-slot"] = true;
}

for (int i = 0; i <= (DispatchSlot::SDSF_LAST_TAG - DispatchSlot::SDSF_FIRST_TAG); i++) {
int ctr = 0;
if (HasBit(SD_slot.flags, DispatchSlot::SDSF_FIRST_TAG + i)) {

slotJson["tags"][ctr++] = "Tag-" + std::to_string(i + 1);

}

}

}

json["name"] = this->ScheduleName();
json["start-tick"] = this->GetScheduledDispatchStartTick().value;
json["duration"] = this->GetScheduledDispatchDuration();
json["max-delay"] = this->GetScheduledDispatchDelay();
json["flags"] = this->GetScheduledDispatchFlags();
if (!this->ScheduleName().empty()) {
json["name"] = this->ScheduleName();
}

if (this->GetScheduledDispatchDuration() != getScheduledDispatchDefaultDuration()) {
json["duration"] = this->GetScheduledDispatchDuration();
}

if (this->GetScheduledDispatchDelay() != 0) {
json["delay"] = this->GetScheduledDispatchDelay();
}

if (this->GetScheduledDispatchFlags() != 0) {
json["re-use-all-slots"] = true;
}

return json.dump();
}
Expand Down
19 changes: 17 additions & 2 deletions src/schdispatch_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "table/sprites.h"

#include "safeguards.h"
#include "schdispatch.h"

enum SchdispatchWidgets {
WID_SCHDISPATCH_CAPTION, ///< Caption of window.
Expand Down Expand Up @@ -76,6 +77,20 @@ static void SetScheduleStartDateIntl(uint32_t p1, StateTicks date)
DoCommandPEx(0, p1, 0, (uint64_t)date.base(), CMD_SCHEDULED_DISPATCH_SET_START_DATE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE), nullptr, nullptr, 0);
}

//ripped straight from AddNewScheduledDispatchSchedule()
uint32_t getScheduledDispatchDefaultDuration()
{
const Company *c = Company::GetIfValid(_local_company);
if (c != nullptr && c->settings.default_sched_dispatch_duration != 0) {
return c->settings.default_sched_dispatch_duration * _settings_time.ticks_per_minute;
} else if (_settings_time.time_in_minutes) {
return 24 * 60 * _settings_time.ticks_per_minute;
} else {
return (EconTime::UsingWallclockUnits() ? EconTime::DAYS_IN_ECONOMY_WALLCLOCK_YEAR : DAYS_IN_YEAR) * DAY_TICKS;
}

}

/**
* Callback for when a time has been chosen to start the schedule
* @param window the window related to the setting of the date
Expand Down Expand Up @@ -161,7 +176,7 @@ static int CalculateMaxRequiredVehicle(Ticks timetable_duration, uint32_t schedu
return vehicle_count;
}

static void AddNewScheduledDispatchSchedule(VehicleID vindex)
void AddNewScheduledDispatchSchedule(VehicleID vindex, const char * jsonString = nullptr)
{
StateTicks start_tick;
uint32_t duration;
Expand All @@ -186,7 +201,7 @@ static void AddNewScheduledDispatchSchedule(VehicleID vindex)
duration = (EconTime::UsingWallclockUnits() ? EconTime::DAYS_IN_ECONOMY_WALLCLOCK_YEAR : DAYS_IN_YEAR) * DAY_TICKS;
}

DoCommandPEx(0, vindex, duration, (uint64_t)start_tick.base(), CMD_SCHEDULED_DISPATCH_ADD_NEW_SCHEDULE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE), CcAddNewSchDispatchSchedule, nullptr, 0);
DoCommandPEx(0, vindex, duration, (uint64_t)start_tick.base(), CMD_SCHEDULED_DISPATCH_ADD_NEW_SCHEDULE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE), CcAddNewSchDispatchSchedule, jsonString, 0);
}

struct SchdispatchWindow : GeneralVehicleWindow {
Expand Down

0 comments on commit 517e7d6

Please sign in to comment.