Debian Codegraph @

similar/main/kmatrix.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Kill matrix displayed at end of level.
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31
32 #include "dxxerror.h"
33 #include "pstypes.h"
34 #include "gr.h"
35 #include "window.h"
36 #include "key.h"
37 #include "palette.h"
38 #include "game.h"
39 #include "gameseq.h"
40 #include "window.h"
41 #include "physfsx.h"
42 #include "gamefont.h"
43 #include "u_mem.h"
44 #include "newmenu.h"
45 #include "menu.h"
46 #include "player.h"
47 #include "screens.h"
48 #include "joy.h"
49 #include "timer.h"
50 #include "text.h"
51 #include "net_udp.h"
52 #include "kmatrix.h"
53 #include "gauges.h"
54 #include "pcx.h"
55 #include "object.h"
56 #include "args.h"
57
58 #include "compiler-range_for.h"
59 #include "d_levelstate.h"
60
61 #define CENTERING_OFFSET(x) ((300 - (70 + (x)*25 ))/2)
62 #define CENTERSCREEN (SWIDTH/2)
63 #define KMATRIX_VIEW_SEC 7 // Time after reactor explosion until new level - in seconds
64
65 namespace {
66
67 enum class kmatrix_status_mode : uint8_t
68 {
69 reactor_countdown_running,
70 level_finished,
71 mission_finished,
72 };
73
74 static void kmatrix_redraw_coop(grs_canvas &canvas, fvcobjptr &vcobjptr, font_y_scale_float);
75
76 static void kmatrix_draw_item(fvcobjptr &vcobjptr, grs_canvas &canvas, const grs_font &cv_font, const playernum_t i, const playernum_array_t &sorted, const font_y_scale_float fspacy)
77 {
78 const auto y = fspacy(80 + i * 9);
79 const auto &&fspacx = FSPACX();
80 auto &p = *vcplayerptr(sorted[i]);
81 gr_string(canvas, cv_font, fspacx(CENTERING_OFFSET(N_players)), y, static_cast<const char *>(p.callsign));
82
83 const auto &&rgb10 = BM_XRGB(10, 10, 10);
84 const auto &&rgb25 = BM_XRGB(25, 25, 25);
85 for (playernum_t j = 0; j < N_players; ++j)
86 {
87 const auto x = fspacx(70 + CENTERING_OFFSET(N_players) + j * 25);
88
89 const auto kmij = kill_matrix[sorted[i]][sorted[j]];
90 if (sorted[i]==sorted[j])
91 {
92 if (kmij == 0)
93 {
94 gr_set_fontcolor(canvas, rgb10, -1);
95 gr_string(canvas, cv_font, x, y, "0");
96 }
97 else
98 {
99 gr_set_fontcolor(canvas, rgb25, -1);
100 gr_printf(canvas, cv_font, x, y, "-%hu", kmij);
101 }
102 }
103 else
104 {
105 gr_set_fontcolor(canvas, kmij <= 0 ? rgb10 : rgb25, -1);
106 gr_printf(canvas, cv_font, x, y, "%hu", kmij);
107 }
108 }
109
110 auto &player_info = vcobjptr(p.objnum)->ctype.player_info;
111 const int eff = (player_info.net_killed_total + player_info.net_kills_total <= 0)
112 ? 0
113 : static_cast<int>(
114 static_cast<float>(player_info.net_kills_total) / (
115 static_cast<float>(player_info.net_killed_total) + static_cast<float>(player_info.net_kills_total)
116 ) * 100.0
117 );
118
119 const auto x = fspacx(60 + CENTERING_OFFSET(N_players) + N_players * 25);
120 gr_set_fontcolor(canvas, rgb25, -1);
121 gr_printf(canvas, cv_font, x, y, "%4d/%i%%", player_info.net_kills_total, eff <= 0 ? 0 : eff);
122 }
123
124 static void kmatrix_draw_names(grs_canvas &canvas, const grs_font &cv_font, const playernum_array_t &sorted, const font_y_scale_float fspacy)
125 {
126 const auto &&fspacx = FSPACX();
127 const auto &&fspacy_header = fspacy(65);
128 const auto &&rgb31 = BM_XRGB(31, 31, 31);
129 for (playernum_t j = 0; j < N_players; ++j)
130 {
131 const auto x = fspacx(70 + CENTERING_OFFSET(N_players) + j * 25);
132
133 color_t c;
134 const auto sj = sorted[j];
135 auto &p = *vcplayerptr(sj);
136 if (p.connected == player_connection_status::disconnected)
137 c = rgb31;
138 else
139 {
140 const auto color{get_player_or_team_color(Netgame, Game_mode, sj)};
141 const auto &rgb = player_rgb[color];
142 c = BM_XRGB(rgb.r, rgb.g, rgb.b);
143 }
144 gr_set_fontcolor(canvas, c, -1);
145 gr_printf(canvas, cv_font, x, fspacy_header, "%c", p.callsign[0u]);
146 }
147 const auto x = fspacx(72 + CENTERING_OFFSET(N_players) + N_players * 25);
148 gr_set_fontcolor(canvas, rgb31, -1);
149 gr_string(canvas, cv_font, x, fspacy_header, "K/E");
150 if (const auto m = Current_mission.get())
151 {
152 gr_string(canvas, cv_font, 0x8000, fspacy(30), m->mission_name.data());
153 if (const auto level_num{Current_level_num}; level_num > 0 && level_num <= m->last_level)
154 gr_printf(canvas, cv_font, 0x8000, fspacy(42), "%s [%u/%u]", Current_level_name.line().data(), level_num, m->last_level);
155 }
156 }
157
158 static void kmatrix_draw_coop_names(grs_canvas &canvas, const grs_font &cv_font, const font_y_scale_float fspacy)
159 {
160 gr_set_fontcolor(canvas, BM_XRGB(63, 31, 31),-1);
161 const auto centerscreen{CENTERSCREEN};
162 if (const auto m = Current_mission.get())
163 {
164 gr_string(canvas, cv_font, 0x8000, fspacy(30), m->mission_name.data());
165 if (const auto level_num{Current_level_num}; level_num > 0 && level_num <= m->last_level)
166 gr_printf(canvas, cv_font, 0x8000, fspacy(42), "%s [%u/%u]", Current_level_name.line().data(), level_num, m->last_level);
167 }
168 const auto &&fspacy_header = fspacy(58);
169 gr_string(canvas, cv_font, centerscreen, fspacy_header, "SCORE");
170 gr_string(canvas, cv_font, centerscreen + FSPACX(50), fspacy_header, "DEATHS");
171 }
172
173 static void kmatrix_status_msg(grs_canvas &canvas, const fix time, const kmatrix_status_mode message_mode)
174 {
175 gr_set_fontcolor(canvas, gr_find_closest_color(255, 255, 255),-1);
176 auto &game_font = *GAME_FONT;
177 gr_printf(canvas, game_font, 0x8000, SHEIGHT - LINE_SPACING(game_font, game_font),
178 message_mode == kmatrix_status_mode::reactor_countdown_running
179 ? "Waiting for players to finish level. Reactor time: T-%d"
180 : (
181 message_mode == kmatrix_status_mode::level_finished
182 ? (
183 time > 0
184 ? "Level finished. Wait %d seconds to proceed or press ESC to Quit."
185 : "Level finished. Focus score screen to proceed."
186 )
187 : "Mission finished. Press ESC to Quit."
188 )
189 , time);
190 }
191
192 }
193
194 namespace dcx {
195
196 namespace {
197
198 struct kmatrix_window : window
199 {
200 kmatrix_window(grs_canvas &src, int x, int y, int w, int h, kmatrix_result &result) :
201 window(src, x, y, w, h), result(result)
202 {
203 }
204 grs_main_bitmap background;
205 fix64 end_time = -1;
206 fix64 last_endlevel_time{0};
207 kmatrix_network network;
208 kmatrix_result &result;
209 };
210
211 }
212
213 }
214
215 namespace dsx {
216
217 namespace {
218
219 struct kmatrix_window : ::dcx::kmatrix_window
220 {
221 using ::dcx::kmatrix_window::kmatrix_window;
222 virtual window_event_result event_handler(const d_event &) override;
223 };
224
225 static void kmatrix_redraw(grs_canvas &canvas, kmatrix_window *const km)
226 {
227 auto &Objects = LevelUniqueObjectState.Objects;
228 auto &vcobjptr = Objects.vcptr;
229 playernum_array_t sorted;
230
231 show_fullscr(canvas, km->background);
232 const auto &&fspacy = FSPACY();
233
234 if (Game_mode & GM_MULTI_COOP)
235 {
236 kmatrix_redraw_coop(canvas, vcobjptr, fspacy);
237 }
238 else
239 {
240 multi_sort_kill_list();
241 const auto title =
242 #if DXX_BUILD_DESCENT == 2
243 game_mode_capture_flag(Game_mode)
244 ? "CAPTURE THE FLAG SUMMARY"
245 : game_mode_hoard(Game_mode)
246 ? "HOARD SUMMARY"
247 :
248 #endif
249 TXT_KILL_MATRIX_TITLE;
250 auto &medium3_font = *MEDIUM3_FONT;
251 gr_string(canvas, medium3_font, 0x8000, FSPACY(10), title);
252
253 auto &game_font = *GAME_FONT;
254 multi_get_kill_list(sorted);
255 kmatrix_draw_names(canvas, game_font, sorted, fspacy);
256
257 for (int i=0; i<N_players; i++ )
258 {
259 if (vcplayerptr(sorted[i])->connected == player_connection_status::disconnected)
260 gr_set_fontcolor(canvas, gr_find_closest_color(31, 31, 31),-1);
261 else
262 {
263 const auto color{get_player_or_team_color(Netgame, Game_mode, sorted[i])};
264 gr_set_fontcolor(canvas, BM_XRGB(player_rgb[color].r, player_rgb[color].g, player_rgb[color].b),-1);
265 }
266 kmatrix_draw_item(vcobjptr, canvas, game_font, i, sorted, fspacy);
267 }
268 }
269
270 gr_palette_load(gr_palette);
271 }
272
273 }
274
275 }
276
277 namespace {
278
279 static void kmatrix_redraw_coop(grs_canvas &canvas, fvcobjptr &vcobjptr, const font_y_scale_float fspacy)
280 {
281 playernum_array_t sorted;
282
283 multi_sort_kill_list();
284 auto &medium3_font = *MEDIUM3_FONT;
285 gr_string(canvas, medium3_font, 0x8000, FSPACY(10), "COOPERATIVE SUMMARY");
286 multi_get_kill_list(sorted);
287 auto &game_font = *GAME_FONT;
288 const auto &&fspacx = FSPACX();
289 kmatrix_draw_coop_names(canvas, game_font, fspacy);
290 const auto x_callsign = fspacx(CENTERING_OFFSET(N_players));
291 const auto x_centerscreen{CENTERSCREEN};
292 const auto &&fspacx50 = fspacx(50);
293 const auto rgb60_40_10 = BM_XRGB(60, 40, 10);
294
295 for (playernum_t i = 0; i < N_players; ++i)
296 {
297 auto &plr = *vcplayerptr(sorted[i]);
298 int r, g, b;
299 if (plr.connected == player_connection_status::disconnected)
300 r = g = b = 31;
301 else
302 {
303 auto &color = player_rgb[get_player_color(sorted[i])];
304 r = color.r * 2;
305 g = color.g * 2;
306 b = color.b * 2;
307 }
308 gr_set_fontcolor(canvas, gr_find_closest_color(r, g, b), -1);
309
310 const auto &&y = fspacy(80 + i * 9);
311 gr_string(canvas, game_font, x_callsign, y, static_cast<const char *>(plr.callsign));
312 gr_set_fontcolor(canvas, rgb60_40_10, -1);
313 auto &player_info = vcobjptr(plr.objnum)->ctype.player_info;
314 gr_printf(canvas, game_font, x_centerscreen, y, "%d", player_info.mission.score);
315 gr_printf(canvas, game_font, x_centerscreen + fspacx50, y, "%d", player_info.net_killed_total);
316 }
317
318 gr_palette_load(gr_palette);
319 }
320
321 }
322
323 namespace dsx {
324
325 window_event_result kmatrix_window::event_handler(const d_event &event)
326 {
327 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
328 int k{0};
329 switch (event.type)
330 {
331 case event_type::key_command:
332 k = event_key_get(event);
333 switch( k )
334 {
335 case KEY_ESC:
336 {
337 using items_type = std::array<newmenu_item, 2>;
338 struct abort_game_menu : items_type, passive_newmenu
339 {
340 abort_game_menu(grs_canvas &canvas) :
341 items_type{{
342 newmenu_item::nm_item_menu{TXT_YES},
343 newmenu_item::nm_item_menu{TXT_NO},
344 }},
345 passive_newmenu(menu_title{nullptr}, menu_subtitle{TXT_ABORT_GAME}, menu_filename{nullptr}, tiny_mode_flag::normal, tab_processing_flag::ignore, adjusted_citem::create(*static_cast<items_type *>(this), 0), canvas)
346 {
347 }
348 };
349 if (run_blocking_newmenu<abort_game_menu>(*grd_curcanv) != 0)
350 return window_event_result::handled;
351 }
352 {
353 get_local_player().connected = player_connection_status::disconnected;
354
355 if (network != kmatrix_network::offline)
356 multi::dispatch->send_endlevel_packet();
357
358 multi_leave_game();
359 this->result = kmatrix_result::abort;
360
361 return window_event_result::close;
362 }
363 default:
364 break;
365 }
366 break;
367 case event_type::window_draw:
368 {
369 timer_delay2(50);
370 gr_set_default_canvas();
371 kmatrix_redraw(*grd_curcanv, this);
372
373 if (network != kmatrix_network::offline)
374 multi::dispatch->do_protocol_frame(0, 1);
375 kmatrix_status_mode playing = (Current_level_num == Current_mission->last_level)
376 ? kmatrix_status_mode::mission_finished
377 : kmatrix_status_mode::level_finished;
378
379 // Check if all connected players are also looking at this screen ...
380 range_for (auto &i, Players)
381 if (i.connected != player_connection_status::disconnected && i.connected != player_connection_status::end_menu && i.connected != player_connection_status::died_in_mine)
382 {
383 playing = kmatrix_status_mode::reactor_countdown_running;
384 break;
385 }
386
387 // ... and let the reactor blow sky high!
388 if (playing != kmatrix_status_mode::reactor_countdown_running)
389 {
390 LevelUniqueControlCenterState.Countdown_seconds_left = -1;
391
392 // If Reactor is finished and end_time not inited, set the time when we will exit this loop
393 if (end_time == -1)
394 end_time = timer_query() + (KMATRIX_VIEW_SEC * F1_0);
395 }
396
397 // Check if end_time has been reached and exit loop
398 if (end_time != -1 && timer_query() >= end_time && this == window_get_front())
399 {
400 if (network != kmatrix_network::offline)
401 multi::dispatch->send_endlevel_packet(); // make sure
402
403 #if DXX_BUILD_DESCENT == 2
404 if (is_D2_OEM)
405 {
406 if (Current_level_num==8)
407 {
408 get_local_player().connected = player_connection_status::disconnected;
409 multi_leave_game();
410 this->result = kmatrix_result::abort;
411 }
412 }
413 #endif
414 if (playing != kmatrix_status_mode::mission_finished)
415 return window_event_result::close;
416 }
417
418 // If Control_center_destroyed is still true this is handled by do_protocol_frame
419 if (timer_query() >= last_endlevel_time + F1_0 && !LevelUniqueObjectState.ControlCenterState.Control_center_destroyed)
420 {
421 last_endlevel_time = timer_query();
422 multi::dispatch->send_endlevel_packet();
423 }
424
425 kmatrix_status_msg(*grd_curcanv, playing == kmatrix_status_mode::reactor_countdown_running ? LevelUniqueControlCenterState.Countdown_seconds_left : f2i(end_time - timer_query()), playing);
426 break;
427 }
428 case event_type::window_close:
429 game_flush_inputs(Controls);
430 newmenu_free_background();
431 break;
432 default:
433 break;
434 }
435 return window_event_result::ignored;
436 }
437
438 kmatrix_result kmatrix_view(const kmatrix_network network, control_info &Controls)
439 {
440 auto &Objects = LevelUniqueObjectState.Objects;
441 auto &vcobjptridx = Objects.vcptridx;
442 kmatrix_result result = kmatrix_result::proceed;
443 {
444 const auto pkm = window_create<kmatrix_window>(grd_curscreen->sc_canvas, 0, 0, SWIDTH, SHEIGHT, result);
445 auto &km = *pkm;
446 pcx_read_bitmap_or_default(STARS_BACKGROUND, km.background, gr_palette);
447 gr_palette_load(gr_palette);
448
449 km.network = network;
450
451 set_screen_mode( SCREEN_MENU );
452 game_flush_inputs(Controls);
453
454 range_for (auto &i, Players)
455 if (i.objnum != object_none)
456 digi_kill_sound_linked_to_object(vcobjptridx(i.objnum));
457
458 event_process_all();
459 }
460 /* After event_process_all returns, kmatrix_window has been freed
461 * and cannot be accessed. The result is therefore stored in a
462 * stack local, which will persist.
463 */
464 return result;
465 }
466
467 }