Debian Codegraph @

Descent3/buddymenu.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/buddymenu.cpp $
21 * $Revision: 31 $
22 * $Date: 9/05/01 12:26p $
23 * $Author: Matt $
24 *
25 * Guidebot menu system.
26 *
27 * $Log: /DescentIII/main/buddymenu.cpp $
28 *
29 * 31 9/05/01 12:26p Matt
30 * Flush low-level mouse click queue before bringing up buddy, save, and
31 * load menus since these menus would otherwise get clicks that occurred
32 * before they existed.
33 *
34 * 30 4/30/99 7:49p Jason
35 * setups urgent/non urgent reliable stuff better
36 *
37 * 29 4/30/99 1:29p Samir
38 * fixed buddy menu a-z selections.
39 *
40 * 28 4/27/99 1:56p Jeff
41 * audio taunts stuff in pilot menu, added stringtables
42 *
43 * 27 4/27/99 4:41a Jeff
44 * made multiplayer friendly
45 *
46 * 26 4/24/99 5:41p Samir
47 * keys should work properly for buddy menu.
48 *
49 * 25 4/22/99 10:25p Chris
50 * Added a comment block
51 *
52 * $NoKeywords: $
53 */
54
55 #include "help.h"
56 #include "mono.h"
57 #include "renderer.h"
58 #include "render.h"
59 #include "ddio.h"
60 #include "descent.h"
61 #include "game.h"
62 #include "cfile.h"
63 #include "application.h"
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdarg.h>
67 #include "newui.h"
68 #include "grtext.h"
69 #include "gamefont.h"
70 #include "AIMain.h"
71 #include "robot.h"
72 #include "hud.h"
73 #include "stringtable.h"
74 #include "textaux.h"
75 #include "multi.h"
76
77 #include "osiris_share.h"
78 #include "multi.h"
79
80 #define GB_MENU_REQ_TEXT 1
81 #define GB_MENU_REQ_SELECT 2
82
83 #define IDH_QUIT 50
84 #define WND_HELP_W 320
85 #define WND_HELP_H 384
86 #define WND_HELP_X (Game_window_w - WND_HELP_W) / 2
87 #define WND_HELP_Y (Game_window_h - WND_HELP_H) / 2
88 #define BUDDYMENU_ITEM_X 10
89 #define BUDDYMENU_ITEM_W 226
90 #define UID_BUDDYCMD 0x100
91
92 static gb_menu Guidebot_menu_data;
93 static int Guidebot_data_download_status;
94
95 // downloads data for the buddybot
96 // on return: if 1, process data
97 // if 0, abort
98 // if -1, no guidebot
99 static int BuddyBotDownloadData(void);
100 static void BuddyProcessCommand(int res);
101
102 void BuddyDisplay(void) {
103 newuiTiledWindow gbcommand_wnd;
104 newuiSheet *sheet;
105 bool exit_menu = false;
106 int strs_to_print, index, height;
107
108 if (Game_mode & GM_MULTI && (!(Netgame.flags & NF_ALLOWGUIDEBOT))) {
109 // only in single player games, and multiplayer games that allow guidebot
110 return;
111 }
112
113 strs_to_print = 0;
114
115 if (BuddyBotDownloadData() != 1)
116 return;
117
118 gbcommand_wnd.Create(Guidebot_menu_data.title, 0, 0, WND_HELP_W, WND_HELP_H);
119 sheet = gbcommand_wnd.GetSheet();
120
121 // add commands
122 sheet->NewGroup(NULL, 10, 10);
123 gbcommand_wnd.AddAcceleratorKey(KEY_F4, UID_CANCEL);
124
125 for (index = 0; index < Guidebot_menu_data.num_commands; index++) {
126 char buf[256 + 32], buf2[256 + 32];
127 if (index < 9) {
128 snprintf(buf, sizeof(buf), "%d. %s", index + 1, Guidebot_menu_data.command_text[index]);
129 } else {
130 snprintf(buf, sizeof(buf), "%c. %s", 'A' + (index - 9), Guidebot_menu_data.command_text[index]);
131 }
132
133 textaux_WordWrap(buf, buf2, BUDDYMENU_ITEM_W, MONITOR9_NEWUI_FONT);
134 grtext_SetFont(MONITOR9_NEWUI_FONT);
135 height = grtext_GetTextHeight(buf2);
136
137 sheet->AddHotspot(buf, BUDDYMENU_ITEM_W, height, index + UID_BUDDYCMD,
138 (index == 0 || index == (Guidebot_menu_data.num_commands - 1)) ? true : false);
139 if (index < (N_WINDOW_ACCELS - 1)) {
140 if (index < 9) {
141 int key = ddio_AsciiToKey('1' + index);
142 gbcommand_wnd.AddAcceleratorKey(key, index + UID_BUDDYCMD);
143 } else {
144 int key = ddio_AsciiToKey('a' + (index - 9));
145 gbcommand_wnd.AddAcceleratorKey(key, index + UID_BUDDYCMD);
146 }
147 }
148 }
149
150 // Double space
151 sheet->NewGroup(NULL, WND_HELP_W - 240, WND_HELP_H - 96, NEWUI_ALIGN_HORIZ);
152 sheet->AddLongButton(TXT_PRESSESCRET, UID_CANCEL);
153 gbcommand_wnd.AddAcceleratorKey(KEY_ESC, UID_OK);
154
155 gbcommand_wnd.Open();
156
157 // Mouse clicks from gameplay will be read by the dialog without this flush
158 ddio_MouseQueueFlush();
159
160 while (!exit_menu) {
161 int res = gbcommand_wnd.DoUI();
162
163 // handle all UI results.
164 switch (res) {
165 case UID_OK:
166 case UID_CANCEL:
167 // TODO: Save menu options to variables
168 exit_menu = true;
169 break;
170 case NEWUIRES_FORCEQUIT:
171 exit_menu = true;
172 break;
173 default:
174 BuddyProcessCommand(res);
175 exit_menu = true;
176 break;
177 }
178 }
179
180 gbcommand_wnd.Close();
181 gbcommand_wnd.Destroy();
182 }
183
184 static void MultiAskforGuidebotMenu() {
185 int size_offset;
186 int count = 0;
187 uint8_t data[MAX_GAME_DATA_SIZE];
188 // Only process this if we are the client
189 if (Netgame.local_role != LR_CLIENT) {
190 Int3(); // We shouldn't be here... get Kevin
191 return;
192 }
193 size_offset = START_DATA(MP_GUIDEBOTMENU_REQUEST, data, &count);
194 MultiAddByte(GB_MENU_REQ_TEXT, data, &count);
195 END_DATA(count, data, size_offset);
196 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
197 }
198
199 static void MultiStuffGuidebotMenuData(uint8_t *data, int *count, gb_menu *menu) {
200 int i;
201 // Get the menu data and stick it here
202 // length of the title
203 int slen = strlen(menu->title) + 1;
204 MultiAddShort(slen, data, count);
205 memcpy(data + *count, menu->title, slen);
206 *count += slen;
207
208 MultiAddShort(menu->num_commands, data, count);
209 for (i = 0; i < menu->num_commands; i++) {
210 slen = strlen(menu->command_text[i]) + 1;
211 MultiAddShort(slen, data, count);
212 memcpy(data + *count, menu->command_text[i], slen);
213 *count += slen;
214
215 MultiAddByte(menu->command_id[i], data, count);
216
217 MultiAddByte(menu->command_type[i], data, count);
218
219 slen = strlen(menu->dialog_text[i]) + 1;
220 MultiAddShort(slen, data, count);
221 memcpy(data + *count, menu->dialog_text[i], slen);
222 *count += slen;
223 }
224 }
225
226 // TODO -- Call this function when the client selects a menu item
227 void MultiSendGuidebotMenuSelection(gb_com *command) {
228 int size_offset;
229 int count = 0;
230 uint8_t data[MAX_GAME_DATA_SIZE];
231
232 // Only process this if we are the client
233 if (Netgame.local_role != LR_CLIENT) {
234 Int3(); // We shouldn't be here... get Kevin
235 return;
236 }
237
238 size_offset = START_DATA(MP_GUIDEBOTMENU_REQUEST, data, &count);
239 MultiAddByte(GB_MENU_REQ_SELECT, data, &count);
240
241 MultiAddInt(command->action, data, &count);
242 MultiAddInt(command->index, data, &count);
243
244 if (command->ptr) {
245 int len = strlen((char *)command->ptr) + 1;
246 MultiAddShort(len, data, &count);
247 memcpy(data + count, command->ptr, len);
248 count += len;
249 } else {
250 MultiAddShort(0, data, &count);
251 }
252
253 END_DATA(count, data, size_offset);
254 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
255 }
256
257 static void MultiSendGuidebotMenuText(gb_menu *menu, int slot) {
258 int size_offset;
259 int count = 0;
260
261 uint8_t data[MAX_GAME_DATA_SIZE];
262 // Only process this if we are the server
263 if (Netgame.local_role != LR_SERVER) {
264 Int3(); // We shouldn't be here... get Kevin
265 return;
266 }
267 size_offset = START_DATA(MP_GUIDEBOTMENU_DATA, data, &count);
268
269 MultiStuffGuidebotMenuData(data, &count, menu);
270
271 END_DATA(count, data, size_offset);
272 nw_SendReliable(NetPlayers[slot].reliable_socket, data, count, false);
273 }
274
275 static void MultiReadGuidebotMenuData(uint8_t *data, int *count, gb_menu *menu) {
276
277 int i;
278 // Get the menu data and stick it here
279 // length of the title
280 int slen = MultiGetShort(data, count);
281 memcpy(menu->title, data + *count, slen);
282 *count += slen;
283
284 menu->num_commands = MultiGetShort(data, count);
285
286 for (i = 0; i < menu->num_commands; i++) {
287 slen = MultiGetShort(data, count);
288 memcpy(menu->command_text[i], data + *count, slen);
289 *count += slen;
290
291 menu->command_id[i] = MultiGetByte(data, count);
292
293 menu->command_type[i] = MultiGetByte(data, count);
294
295 slen = MultiGetShort(data, count);
296 memcpy(menu->dialog_text[i], data + *count, slen);
297 *count += slen;
298 }
299 }
300
301 void MultiDoGuidebotMenuRequest(uint8_t *data, int slot) {
302
303 int count = 0;
304 SKIP_HEADER(data, &count);
305 // Only process this if we are the server
306 if (Netgame.local_role == LR_CLIENT) {
307 Int3(); // We shouldn't be here... get Kevin
308 return;
309 }
310
311 int command = MultiGetByte(data, &count);
312
313 switch (command) {
314 case GB_MENU_REQ_TEXT: {
315 // Client wants text to put in his menu
316 // Generate a menu struct and send it off
317 object *b_obj = ObjGet(Buddy_handle[slot]);
318 ASSERT(b_obj);
319
320 gb_com command;
321 gb_menu menu;
322
323 command.action = COM_GET_MENU;
324 command.ptr = (void *)&menu;
325
326 AINotify(b_obj, AIN_USER_DEFINED, (void *)&command);
327
328 MultiSendGuidebotMenuText(&menu, slot);
329 } break;
330 case GB_MENU_REQ_SELECT: {
331 gb_com command;
332 object *b_obj = ObjGet(Buddy_handle[slot]);
333 ASSERT(b_obj);
334
335 command.action = MultiGetInt(data, &count);
336 command.index = MultiGetInt(data, &count);
337
338 int len = MultiGetShort(data, &count);
339
340 if (len > 0) {
341 command.ptr = data + count;
342 count += len;
343 } else {
344 command.ptr = NULL;
345 }
346
347 AINotify(b_obj, AIN_USER_DEFINED, (void *)&command);
348 } break;
349 default:
350 Int3(); // We shouldn't be here either...get Kevin
351 }
352 }
353
354 void MultiDoGuidebotMenuData(uint8_t *data) {
355 int count = 0;
356 SKIP_HEADER(data, &count);
357 if (Netgame.local_role != LR_CLIENT) {
358 Int3();
359 return;
360 }
361 // Server is telling us about the menu -- we should display it now
362 MultiReadGuidebotMenuData(data, &count, &Guidebot_menu_data);
363
364 Guidebot_data_download_status = 1;
365 Multi_bail_ui_menu = true;
•366 mprintf(0, "Received Guidebot data from server\n");
367 }
368
369 // downloads data for the buddybot
370 // on return: if 1, process data
371 // if 0, abort
372 // if -1, no guidebot
373 int BuddyBotDownloadData(void) {
374 if ((!(Game_mode & GM_MULTI)) || (Netgame.local_role != LR_CLIENT)) {
375 // no need to download data, we already have access to it
376 // fill in Guidebot_menu_data and leave
377 object *b_obj = ObjGet(Buddy_handle[Player_object->id]);
378 ASSERT(b_obj);
379
380 gb_com command;
381 command.action = COM_GET_MENU;
382 command.ptr = (void *)&Guidebot_menu_data;
383
384 AINotify(b_obj, AIN_USER_DEFINED, (void *)&command);
385
386 if (Guidebot_menu_data.num_commands == 0) {
387 AddHUDMessage(TXT_NOGUIDEBOT);
388 return -1;
389 }
390
391 return 1;
392 }
393
394 // we are a client in a multiplayer game, we must request from the server
395 Guidebot_data_download_status = 0; // processesing
396 MultiAskforGuidebotMenu();
397
398 // now we must 'process' until Guidebot_data_download_status no longer is 0
399 newuiTiledWindow hwnd;
400 newuiSheet *sheet;
401 hwnd.Create(TXT_GUIDEBOT, 0, 0, 288, 128);
402 sheet = hwnd.GetSheet();
403 sheet->NewGroup(NULL, 5, 10);
404 sheet->AddText(TXT_ACCESSGUIDEBOT);
405 sheet->NewGroup(NULL, 40, 30);
406 sheet->AddText(TXT_ESCTOCANCEL);
407 hwnd.AddAcceleratorKey(KEY_ESC, UID_CANCEL);
408
409 hwnd.Open();
410 while (Guidebot_data_download_status == 0) {
411 int res = hwnd.DoUI();
412
413 switch (res) {
414 case UID_OK:
415 case UID_CANCEL:
416 case NEWUIRES_FORCEQUIT:
417 if (Guidebot_data_download_status == 1) {
•418 // we received data
419 Multi_bail_ui_menu = false;
420 } else {
421 Guidebot_data_download_status = -1;
422 }
423 break;
424 default:
425 break;
426 }
427 }
428 hwnd.Close();
429 hwnd.Destroy();
430
431 if (Guidebot_data_download_status == 1) {
432 if (Guidebot_menu_data.num_commands == 0) {
433 AddHUDMessage(TXT_NOGUIDEBOT);
434 return -1;
435 }
436 return 1;
437 }
438 return 0;
439 }
440
441 void BuddyProcessCommand(int res) {
442 gb_com command;
443 char answer[8];
444
445 int index = res - UID_BUDDYCMD;
446
447 command.action = COM_DO_ACTION;
448 command.index = Guidebot_menu_data.command_id[index];
449 if (Guidebot_menu_data.command_type[index] == GBCT_STRING_DIALOG) {
450 answer[0] = '\0';
451
452 DoEditDialog(Guidebot_menu_data.dialog_text[index], answer, sizeof(answer) - 1);
453 command.ptr = (void *)answer;
454 } else {
455 command.ptr = NULL;
456 }
457
458 if ((!(Game_mode & GM_MULTI)) || (Netgame.local_role != LR_CLIENT)) {
459 object *b_obj = ObjGet(Buddy_handle[Player_object->id]);
460 ASSERT(b_obj);
461
462 AINotify(b_obj, AIN_USER_DEFINED, (void *)&command);
463 } else {
464 MultiSendGuidebotMenuSelection(&command);
465 }
466 }