Debian Codegraph @

prboom2/src/dsda/hud_components/command_display.c

raw
1 //
2 // Copyright(C) 2022 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Command Display HUD Component
16 //
17
18 #include "d_ticcmd.h"
19
20 #include "dsda/build.h"
21
22 #include "base.h"
23
24 #include "command_display.h"
25
26 #define MAX_HISTORY 20
27
28 typedef struct {
29 signed char forwardmove;
30 signed char sidemove;
31 signed char angleturn;
32 char use;
33 char attack;
34 char change;
35 } dsda_command_t;
36
37 typedef struct dsda_command_display_s {
38 dsda_command_t command;
39 int repeat;
40 dsda_text_t* component;
41 const char* color;
42 struct dsda_command_display_s* next;
43 struct dsda_command_display_s* prev;
44 } dsda_command_display_t;
45
46 int dsda_command_history_size;
47 int dsda_hide_empty_commands;
48
49 typedef struct {
50 dsda_text_t component[MAX_HISTORY];
51 dsda_text_t next_command_component;
52 int base_y;
53 } local_component_t;
54
55 static local_component_t* local;
56
57 static dsda_command_display_t command_history[MAX_HISTORY];
58 static dsda_command_display_t* current_command = command_history;
59 static dsda_command_display_t next_command_display;
60
61 static void dsda_TicCmdToCommand(dsda_command_t* command, ticcmd_t* cmd) {
62 command->forwardmove = cmd->forwardmove;
63 command->sidemove = cmd->sidemove;
64 command->angleturn = (cmd->angleturn + 128) >> 8;
65
66 command->use = command->attack = command->change = 0;
67
68 if (cmd->buttons && !(cmd->buttons & BT_SPECIAL)) {
69 if (cmd->buttons & BT_ATTACK)
70 command->attack = 1;
71
72 if (cmd->buttons & BT_USE)
73 command->use = 1;
74
75 if (cmd->buttons & BT_CHANGE)
76 command->change = 1 + ((cmd->buttons & BT_WEAPONMASK) >> BT_WEAPONSHIFT);
77 }
78 }
79
80 static int dsda_IsEmptyCommand(dsda_command_t* command) {
81 return command->forwardmove == 0 &&
82 command->sidemove == 0 &&
83 command->angleturn == 0 &&
84 command->use == 0 &&
85 command->attack == 0 &&
86 command->change == 0;
87 }
88
89 static int dsda_IsCommandEqual(dsda_command_t* command, dsda_command_t* other) {
90 return command->forwardmove == other->forwardmove &&
91 command->sidemove == other->sidemove &&
92 command->angleturn == other->angleturn &&
93 command->use == other->use &&
94 command->attack == other->attack &&
95 command->change == other->change;
96 }
97
98 void dsda_ResetCommandHistory(void) {
99 int i;
100
101 for (i = 0; i < MAX_HISTORY; ++i) {
102 memset(&command_history[i].command, 0, sizeof(command_history[i].command));
103 command_history[i].repeat = 0;
104
105 if (command_history[i].component) {
106 command_history[i].component->msg[0] = '\0';
107 dsda_RefreshHudText(command_history[i].component);
108 }
109 }
110 }
111
112 void dsda_InitCommandHistory(void) {
113 int i;
114
115 dsda_command_history_size = dsda_IntConfig(dsda_config_command_history_size);
116 dsda_hide_empty_commands = dsda_IntConfig(dsda_config_hide_empty_commands);
117
118 for (i = 1; i < MAX_HISTORY; ++i) {
119 command_history[i].prev = &command_history[i - 1];
120 command_history[i - 1].next = &command_history[i];
121 }
122
123 command_history[0].prev = &command_history[MAX_HISTORY - 1];
124 command_history[MAX_HISTORY - 1].next = &command_history[0];
125 }
126
127 void dsda_InitCommandDisplayHC(int x_offset, int y_offset, int vpt, int* args, int arg_count, void** data) {
128 int i;
129
130 *data = Z_Calloc(1, sizeof(local_component_t));
131 local = *data;
132
133 for (i = 0; i < MAX_HISTORY; ++i) {
134 command_history[i].color = dsda_TextColor(dsda_tc_exhud_command_entry);
135 command_history[i].component = &local->component[i];
136 dsda_InitTextHC(command_history[i].component, x_offset, y_offset + i * 8, vpt);
137 }
138
139 next_command_display.color = dsda_TextColor(dsda_tc_exhud_command_queue);
140 next_command_display.component = &local->next_command_component;
141 dsda_InitTextHC(next_command_display.component, x_offset, y_offset, vpt);
142
143 local->base_y = next_command_display.component->text.y;
144 }
145
146 static void dsda_UpdateCommandText(dsda_command_t* command,
147 dsda_command_display_t* display_command,
148 dsda_text_t* component, dboolean playback) {
149 int length;
150
151 length = snprintf(component->msg, sizeof(component->msg), "%s", display_command->color);
152
153 if (playback)
154 length += snprintf(component->msg + length, sizeof(component->msg) - length, " PL ");
155 else if (display_command->repeat)
156 length += snprintf(component->msg + length, sizeof(component->msg) - length, "x%-3d ", display_command->repeat + 1);
157 else
158 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
159
160 if (command->forwardmove > 0)
161 length += snprintf(component->msg + length, sizeof(component->msg) - length, "MF%2d ", command->forwardmove);
162 else if (command->forwardmove < 0)
163 length += snprintf(component->msg + length, sizeof(component->msg) - length, "MB%2d ", -command->forwardmove);
164 else
165 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
166
167 if (command->sidemove > 0)
168 length += snprintf(component->msg + length, sizeof(component->msg) - length, "SR%2d ", command->sidemove);
169 else if (command->sidemove < 0)
170 length += snprintf(component->msg + length, sizeof(component->msg) - length, "SL%2d ", -command->sidemove);
171 else
172 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
173
174 if (command->angleturn > 0)
175 length += snprintf(component->msg + length, sizeof(component->msg) - length, "TL%2d ", command->angleturn);
176 else if (command->angleturn < 0)
177 length += snprintf(component->msg + length, sizeof(component->msg) - length, "TR%2d ", -command->angleturn);
178 else
179 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
180
181 if (command->attack)
182 length += snprintf(component->msg + length, sizeof(component->msg) - length, "A");
183 else
184 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
185
186 if (command->use)
187 length += snprintf(component->msg + length, sizeof(component->msg) - length, "U");
188 else
189 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
190
191 if (command->change)
192 length += snprintf(component->msg + length, sizeof(component->msg) - length, "C%d", command->change);
193 else
194 length += snprintf(component->msg + length, sizeof(component->msg) - length, " ");
195
196 dsda_RefreshHudText(component);
197 }
198
199 void dsda_AddCommandToCommandDisplay(ticcmd_t* cmd) {
200 dsda_command_t command;
201
202 dsda_TicCmdToCommand(&command, cmd);
203
204 if (dsda_hide_empty_commands && dsda_IsEmptyCommand(&command))
205 return;
206
207 if (dsda_IsCommandEqual(&command, &current_command->command))
208 ++current_command->repeat;
209 else {
210 current_command->repeat = 0;
211 current_command = current_command->next;
212 current_command->command = command;
213 }
214
215 if (current_command->component)
216 dsda_UpdateCommandText(&command, current_command, current_command->component, false);
217 }
218
219 static void dsda_UpdateLocal(void* data) {
220 int i;
221
222 if (local != data) {
223 local = data;
224
225 for (i = 0; i < MAX_HISTORY; ++i) {
226 command_history[i].component = &local->component[i];
227 dsda_UpdateCommandText(&command_history[i].command,
228 &command_history[i], command_history[i].component, false);
229 }
230
231 next_command_display.component = &local->next_command_component;
232 }
233 }
234
235 void dsda_UpdateCommandDisplayHC(void* data) {
236 dsda_UpdateLocal(data);
237 }
238
239 static void dsda_DrawCommandDisplayLine(dsda_text_t* component, int offset) {
240 component->text.y = local->base_y - 8 * offset;
241 dsda_DrawBasicText(component);
242 }
243
244 void dsda_DrawCommandDisplayHC(void* data) {
245 int i;
246 int offset = 0;
247 dsda_command_display_t* command = current_command;
248
249 dsda_UpdateLocal(data);
250
251 if (dsda_BuildMode()) {
252 ticcmd_t next_cmd;
253 dsda_command_t next_command;
254
255 dsda_CopyBuildCmd(&next_cmd);
256 dsda_TicCmdToCommand(&next_command, &next_cmd);
257 dsda_UpdateCommandText(&next_command, &next_command_display,
258 next_command_display.component, dsda_BuildPlayback());
259 dsda_DrawCommandDisplayLine(next_command_display.component, offset);
260
261 ++offset;
262 }
263
264 for (i = 0; i < dsda_command_history_size; ++i) {
265 dsda_DrawCommandDisplayLine(command->component, i + offset);
266 command = command->prev;
267 }
268 }