This repository was archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsomething_commands.cpp
More file actions
226 lines (198 loc) · 6.85 KB
/
something_commands.cpp
File metadata and controls
226 lines (198 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "./something_game.hpp"
#include "./something_commands.hpp"
void command_help(Game *game, String_View)
{
for (size_t i = 0; i < commands_count; ++i) {
game->console.println(commands[i].name, " - ", commands[i].description);
}
}
void command_quit(Game *, String_View)
{
exit(0);
}
void command_reset(Game *game, String_View)
{
game->reset_entities();
}
void command_spawn_enemy(Game *game, String_View)
{
game->spawn_enemy_at(game->mouse_position);
}
void command_close(Game *game, String_View)
{
game->console.toggle();
}
#ifndef SOMETHING_RELEASE
void command_set(Game *game, String_View args)
{
const auto varname = args.chop_word();
const auto varindex = config_index_by_name(varname);
if (varindex < 0) {
game->console.println("Variable `", varname, "` does not exist!");
return;
}
auto varvalue = args.trim();
switch (config_types[varindex]) {
case CONFIG_TYPE_INT: {
auto x = varvalue.as_integer<int>();
if (!x.has_value) {
game->console.println("`", varvalue, "` is not an int");
} else {
config_values[varindex].int_value = x.unwrap;
}
} break;
case CONFIG_TYPE_FLOAT: {
auto x = varvalue.as_float();
if (!x.has_value) {
game->console.println("`", varvalue, "` is not a float");
} else {
config_values[varindex].float_value = x.unwrap;
}
} break;
case CONFIG_TYPE_COLOR: {
auto x = string_view_as_color(varvalue);
if (!x.has_value) {
game->console.println("`", varvalue, "` is not a color");
} else {
config_values[varindex].color_value = x.unwrap;
}
} break;
case CONFIG_TYPE_STRING: {
if (varvalue.count > 0 && *varvalue.data != '"') {
game->console.println("`", varvalue, "` is not a string");
break;
}
varvalue.chop(1);
auto string_value = varvalue.chop_by_delim('"');
if (config_file_buffer_size + string_value.count > CONFIG_FILE_CAPACITY) {
game->console.println("Not enough config file memory to set this variable");
break;
}
memcpy(config_file_buffer + config_file_buffer_size,
string_value.data,
string_value.count);
config_values[varindex].string_value = {
string_value.count,
config_file_buffer + config_file_buffer_size
};
config_file_buffer_size += string_value.count;
} break;
case CONFIG_TYPE_UNKNOWN:
default: {
game->console.println("Variable `", varname, "` has unknown type");
}
}
}
void command_reload(Game *game, String_View)
{
auto result = reload_config_file(VARS_CONF_FILE_PATH);
if (result.is_error) {
game->console.println(VARS_CONF_FILE_PATH, ":", result.line, ": ", result.message);
game->popup.notify(FONT_FAILURE_COLOR, "%s:%d: %s", VARS_CONF_FILE_PATH, result.line, result.message);
} else {
game->console.println("Reloaded config file `", VARS_CONF_FILE_PATH, "`");
game->popup.notify(FONT_SUCCESS_COLOR, "Reloaded config file\n\n%s", VARS_CONF_FILE_PATH);
}
}
#endif // SOMETHING_RELEASE
void command_save_room(Game *game, String_View)
{
auto &player = game->entities[PLAYER_ENTITY_INDEX];
Recti *lock = NULL;
for (size_t i = 0; i < game->camera_locks_count; ++i) {
Rectf lock_abs = rect_cast<float>(game->camera_locks[i]) * TILE_SIZE;
if (rect_contains_vec2(lock_abs, player.pos)) {
lock = &game->camera_locks[i];
}
}
if(lock) {
size_t tile_index = 0;
for (int y = lock->y; y < lock->y + ROOM_HEIGHT; ++y) {
for (int x = lock->x; x < lock->x + ROOM_WIDTH; ++x) {
room_to_save[tile_index] = game->grid.tiles[y][x];
tile_index++;
}
}
const int rooms_count = game->get_rooms_count();
char filepath[256];
snprintf(filepath, sizeof(filepath), "./assets/rooms/room-%d.bin", rooms_count);
FILE *f = fopen(filepath, "wb");
if (!f) {
game->console.println("Could not open file `", filepath, "`: ",
strerror(errno));
return;
}
fwrite(room_to_save, sizeof(room_to_save[0]), ROOM_HEIGHT * ROOM_WIDTH, f);
fclose(f);
game->console.println("New room is saved");
} else {
game->console.println("Can't find a room with Player in it");
}
}
void command_history(Game *game, String_View)
{
game->console.println("--------------------");
for (int i = game->console.history.count; i > 0; --i) {
const size_t j = (game->console.history.end - i) % CONSOLE_HISTORY_CAPACITY;
const String_View entry = {
game->console.history.entry_sizes[j],
game->console.history.entries[j]
};
game->console.println(entry);
}
game->console.println("--------------------");
}
void command_noclip(Game *game, String_View args)
{
args = args.trim();
if (args == "on"_sv) {
game->noclip(true);
} else if (args == "off"_sv) {
game->noclip(false);
} else {
game->console.println("Unknown parameter `", args, "`. Expected 'on' or 'off'");
}
}
template <typename T>
void sprint1(String_Buffer *buffer, Vec2<T> v)
{
sprint(buffer, "(", v.x, ", ", v.y, ")");
}
void command_inspect_entity(Game *game, String_View args)
{
auto arg = args.chop_word();
auto entity_index = arg.as_integer<long int>();
if (entity_index.has_value &&
0 <= entity_index.unwrap &&
entity_index.unwrap < static_cast<long int>(ENTITIES_COUNT))
{
auto &entity = game->entities[entity_index.unwrap];
game->console.println("Entity #", entity_index.unwrap);
game->console.println(" State: ", entity.state);
game->console.println(" Position: ", entity.pos);
} else {
game->console.println(
"ERROR: expected a number (entity index) from ",
"0..", ENTITIES_COUNT - 1," but got `", arg, "`");
}
}
void command_ls(Game *game, String_View args)
{
auto arg = args.chop_word();
if (arg == "entities"_sv) {
for (size_t i = 0; i < ENTITIES_COUNT; ++i) {
if (game->entities[i].state != Entity_State::Ded) {
game->console.println("Entity #", i);
}
}
} else if (arg == "sprites"_sv) {
game->console.println(assets.sprite_count, " sprites found");
for (size_t i = 0; i < assets.sprite_count; ++i) {
game->console.println(
i, ": ", assets.sprites[i].id, ": ", assets.sprites[i].path);
game->console.println(" srcrect=", assets.sprites[i].unwrap.srcrect);
}
} else {
game->console.println("ERROR: expected `entities` or `sprites` but got `", arg, "`");
}
}