Cube-World-Commands-Mod/main.cpp

91 lines
2.9 KiB
C++
Raw Normal View History

2019-09-23 07:50:21 +08:00
#include "main.h"
#include "cwmods/cwmods.h"
#include <wchar.h>
#include <stdio.h>
2019-09-25 07:01:35 +08:00
#include <string.h>
2018-10-16 05:59:05 +08:00
2019-09-25 07:01:35 +08:00
float GetMilliseconds(int hour, int minute) {
2019-09-23 07:50:21 +08:00
return (hour * 60 * 60 * 1000) + (minute * 60 * 1000);
}
2018-10-16 05:59:05 +08:00
2019-09-25 07:01:35 +08:00
void CommandsModMessage(wchar_t message[]) {
2019-09-23 07:50:21 +08:00
cube::Game* game = cube::GetGame();
game->chat_widget->PrintMessage(L"[");
game->chat_widget->PrintMessage(L"CommandsMod", 255, 140, 0);
game->chat_widget->PrintMessage(L"] ");
game->chat_widget->PrintMessage(message);
}
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
int DotsToMapCoord(long long coord) {
return (coord - 0x2000000) / (cube::DOTS_PER_BLOCK * cube::BLOCKS_PER_MAP_CHUNK);
}
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
long long MapCoordToDots(int coord) {
return ((long long)coord * cube::DOTS_PER_BLOCK * cube::BLOCKS_PER_MAP_CHUNK) + 0x2000000;
2018-10-17 02:55:24 +08:00
}
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
EXPORT int HandleChat(wchar_t* msg) {
cube::Game* game = cube::GetGame();
cube::Host* host = &game->host;
cube::World* world = &host->world;
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
wchar_t response[256];
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
int targetx, targety;
2018-10-17 08:47:48 +08:00
2019-09-23 07:50:21 +08:00
int targetHour, targetMinute;
2018-10-17 02:55:24 +08:00
2019-09-25 07:01:35 +08:00
if ( swscanf(msg, L"/settime %d:%d", &targetHour, &targetMinute) == 2) {
2019-09-23 07:50:21 +08:00
printf("%ld:%ld\n", targetHour, targetMinute);
float targetMS = GetMilliseconds(targetHour, targetMinute);
2018-10-17 02:55:24 +08:00
2019-09-23 07:50:21 +08:00
float maxTime = GetMilliseconds(24, 0);
float minTime = GetMilliseconds(0, 0);
2018-10-17 06:49:16 +08:00
2019-09-25 07:01:35 +08:00
if (targetMS > maxTime)
targetMS = maxTime;
else if (targetMS < minTime)
targetMS = minTime;
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
world->time = targetMS;
2018-10-16 05:59:05 +08:00
2019-09-23 07:50:21 +08:00
swprintf(response, L"Changing time to %d:%02d.\n", targetHour % 24, targetMinute % 60);
2018-10-17 02:55:24 +08:00
CommandsModMessage(response);
2019-09-23 07:50:21 +08:00
return 1;
2019-09-25 07:01:35 +08:00
} else if (!wcscmp(msg, L"/coords")) {
2019-09-23 07:50:21 +08:00
cube::Creature* player = game->GetPlayer();
swprintf(response, L"World coordinates:\nX: %lld\nY: %lld\nZ: %lld\n", player->position.x, player->position.y, player->position.z);
2018-10-17 02:55:24 +08:00
CommandsModMessage(response);
2019-09-23 07:50:21 +08:00
return 1;
2019-09-25 07:01:35 +08:00
} else if ( swscanf(msg, L"/tp %d %d", &targetx, &targety) == 2) {
cube::Creature* player = game->GetPlayer();
player->position.x = MapCoordToDots(targetx);
player->position.y = MapCoordToDots(targety);
CommandsModMessage(L"Teleporting.\n");
return 1;
} else if ( !wcsncmp(msg, L"/name ", 6) ) {
cube::Creature* player = game->GetPlayer();
wchar_t* wideName = &msg[6];
char newName[18];
memset(newName, 0, sizeof(newName));
int len = wcstombs(newName, wideName, 16);
if (len < 1) {
CommandsModMessage(L"Invalid name.\n");
}
else if (len >= 16) {
CommandsModMessage(L"Name too long!\n");
}
else {
strcpy(player->name, newName);
swprintf(response, L"You are now known as %s.\n", player->name);
2018-10-17 02:55:24 +08:00
CommandsModMessage(response);
2018-10-16 05:59:05 +08:00
}
2019-09-25 07:01:35 +08:00
return 1;
}
2019-09-23 07:50:21 +08:00
return 0;
2018-10-16 05:59:05 +08:00
}