diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 42a6018..ad217fe 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -28,77 +28,43 @@ - + + + - + - - + + + - - - - - - - - - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - { - "keyToString": { - "CMake Application.quakespasm.executor": "Run", - "CidrDebugProfileConversion": "true", - "ModuleVcsDetector.initialDetectionPerformed": "true", - "RunOnceActivity.RadMigrateCodeStyle": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "RunOnceActivity.cidr.known.project.marker": "true", - "RunOnceActivity.git.unshallow": "true", - "RunOnceActivity.readMode.enableVisualFormatting": "true", - "RunOnceActivity.typescript.service.memoryLimit.init": "true", - "SHARE_PROJECT_CONFIGURATION_FILES": "true", - "cf.first.check.clang-format": "false", - "cidr.known.project.marker": "true", - "codeWithMe.voiceChat.enabledByDefault": "false", - "git-widget-placeholder": "master", - "last_opened_file_path": "/home/iikorni/Downloads/quakespasm-0.96.3/cmake-build-debug/id1", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.jshint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.jshint": "", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "settings.editor.selected.configurable": "language.cpp.clang-tidy" + +}]]> @@ -226,7 +188,8 @@ - diff --git a/CMakeLists.txt b/CMakeLists.txt index 14cc516..0110a52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,8 @@ find_package(SDL) find_package(SDL_net) find_package(OpenGL) -add_executable(quakespasm Quake/bgmusic.cpp Quake/cd_sdl.cpp Quake/cfgfile.cpp Quake/chase.cpp Quake/cl_demo.cpp Quake/cl_input.cpp Quake/cl_main.cpp Quake/cl_parse.cpp Quake/cl_tent.cpp Quake/cmd.cpp Quake/common.cpp Quake/console.cpp Quake/crc.cpp Quake/convar.cpp Quake/gl_draw.cpp Quake/gl_fog.cpp Quake/gl_mesh.cpp Quake/gl_model.cpp Quake/gl_refrag.cpp Quake/gl_rlight.cpp Quake/gl_rmain.cpp Quake/gl_rmisc.cpp Quake/gl_screen.cpp Quake/gl_sky.cpp Quake/gl_texmgr.cpp Quake/gl_vidsdl.cpp Quake/gl_warp.cpp Quake/host.cpp Quake/host_cmd.cpp Quake/image.cpp Quake/in_sdl.cpp Quake/keys.cpp Quake/main_sdl.cpp Quake/mathlib.cpp Quake/menu.cpp Quake/miniz.cpp Quake/net_bsd.cpp Quake/net_dgrm.cpp Quake/net_loop.cpp Quake/net_main.cpp Quake/net_udp.cpp Quake/pl_linux.cpp Quake/pr_cmds.cpp Quake/pr_edict.cpp Quake/pr_exec.cpp Quake/r_alias.cpp Quake/r_brush.cpp Quake/r_part.cpp Quake/r_sprite.cpp Quake/r_world.cpp Quake/sbar.cpp Quake/snd_codec.cpp Quake/snd_dma.cpp Quake/snd_flac.cpp Quake/snd_mem.cpp Quake/snd_mikmod.cpp Quake/snd_mix.cpp Quake/snd_modplug.cpp Quake/snd_mp3.cpp Quake/snd_mp3tag.cpp Quake/snd_mpg123.cpp Quake/snd_opus.cpp Quake/snd_sdl.cpp Quake/snd_umx.cpp Quake/snd_vorbis.cpp Quake/snd_wave.cpp Quake/snd_xmp.cpp Quake/strlcat.cpp Quake/strlcpy.cpp Quake/sv_main.cpp Quake/sv_move.cpp Quake/sv_phys.cpp Quake/sv_user.cpp Quake/sys_sdl_unix.cpp Quake/view.cpp Quake/wad.cpp Quake/world.cpp Quake/zone.cpp ) +add_executable(quakespasm Quake/bgmusic.cpp Quake/cd_sdl.cpp Quake/cfgfile.cpp Quake/chase.cpp Quake/cl_demo.cpp Quake/cl_input.cpp Quake/cl_main.cpp Quake/cl_parse.cpp Quake/cl_tent.cpp Quake/cmd.cpp Quake/common.cpp Quake/console.cpp Quake/crc.cpp Quake/convar.cpp Quake/gl_draw.cpp Quake/gl_fog.cpp Quake/gl_mesh.cpp Quake/gl_model.cpp Quake/gl_refrag.cpp Quake/gl_rlight.cpp Quake/gl_rmain.cpp Quake/gl_rmisc.cpp Quake/gl_screen.cpp Quake/gl_sky.cpp Quake/gl_texmgr.cpp Quake/gl_vidsdl.cpp Quake/gl_warp.cpp Quake/host.cpp Quake/host_cmd.cpp Quake/image.cpp Quake/in_sdl.cpp Quake/keys.cpp Quake/main_sdl.cpp Quake/mathlib.cpp Quake/menu.cpp Quake/miniz.cpp Quake/net_bsd.cpp Quake/net_dgrm.cpp Quake/net_loop.cpp Quake/net_main.cpp Quake/net_udp.cpp Quake/pl_linux.cpp Quake/pr_cmds.cpp Quake/pr_edict.cpp Quake/pr_exec.cpp Quake/r_alias.cpp Quake/r_brush.cpp Quake/r_part.cpp Quake/r_sprite.cpp Quake/r_world.cpp Quake/sbar.cpp Quake/snd_codec.cpp Quake/snd_dma.cpp Quake/snd_flac.cpp Quake/snd_mem.cpp Quake/snd_mikmod.cpp Quake/snd_mix.cpp Quake/snd_modplug.cpp Quake/snd_mp3.cpp Quake/snd_mp3tag.cpp Quake/snd_mpg123.cpp Quake/snd_opus.cpp Quake/snd_sdl.cpp Quake/snd_umx.cpp Quake/snd_vorbis.cpp Quake/snd_wave.cpp Quake/snd_xmp.cpp Quake/strlcat.cpp Quake/strlcpy.cpp Quake/sv_main.cpp Quake/sv_move.cpp Quake/sv_phys.cpp Quake/sv_user.cpp Quake/sys_sdl_unix.cpp Quake/view.cpp Quake/wad.cpp Quake/world.cpp Quake/zone.cpp + Quake/net_msg.cpp) target_link_libraries(quakespasm OpenGL::GL) target_link_libraries(quakespasm SDL::SDL) diff --git a/Quake/bgmusic.cpp b/Quake/bgmusic.cpp index 9c5ca5f..4212129 100644 --- a/Quake/bgmusic.cpp +++ b/Quake/bgmusic.cpp @@ -35,11 +35,11 @@ #define CDRIPTYPE(x) (((x) & CDRIP_TYPES) != 0) namespace music { - bool bgmloop; - convar bgm_extmusic{"bgm_extmusic", "1", {.archive = true}}; + bool looping; + convar extmusic{"bgm_extmusic", "1", {.archive = true}}; namespace { - snd_stream_t *bgmstream = nullptr; + snd_stream_t *stream = nullptr; bool no_extmusic = false; float old_volume = -1.0f; @@ -125,17 +125,17 @@ namespace music { if (command::argc() == 2) { if (q_strcasecmp(command::argv(1)->c_str(), "0") == 0 || q_strcasecmp(command::argv(1)->c_str(), "off") == 0) - bgmloop = false; + looping = false; else if (q_strcasecmp(command::argv(1)->c_str(), "1") == 0 || q_strcasecmp(command::argv(1)->c_str(), "on") == 0) - bgmloop = true; + looping = true; else if (q_strcasecmp(command::argv(1)->c_str(), "toggle") == 0) - bgmloop = !bgmloop; + looping = !looping; - if (bgmstream) bgmstream->loop = bgmloop; + if (stream) stream->loop = looping; } - if (bgmloop) + if (looping) console::info("Music will be looped\n"); else console::info("Music will not be looped\n"); @@ -148,8 +148,8 @@ namespace music { void _jump() { if (command::argc() != 2) { console::info("music_jump \n"); - } else if (bgmstream) { - S_CodecJumpToOrder(bgmstream, static_cast(std::strtol(command::argv(1)->c_str(), nullptr, 10))); + } else if (stream) { + S_CodecJumpToOrder(stream, static_cast(std::strtol(command::argv(1)->c_str(), nullptr, 10))); } } @@ -170,8 +170,8 @@ namespace music { /* not supported in quake */ break; case player_kind::streamer: - bgmstream = S_CodecOpenStreamType(tmp, handler.type, bgmloop); - if (bgmstream) + stream = S_CodecOpenStreamType(tmp, handler.type, looping); + if (stream) return; /* success */ break; case player_kind::none: @@ -191,7 +191,7 @@ namespace music { int fileBytes; byte raw[16384]; - if (bgmstream->status != STREAM_PLAY) { + if (stream->status != STREAM_PLAY) { return; } @@ -209,42 +209,42 @@ namespace music { bufferSamples = MAX_RAW_SAMPLES - (s_rawend - paintedtime); /* decide how much data needs to be read from the file */ - fileSamples = bufferSamples * bgmstream->info.rate / shm->speed; + fileSamples = bufferSamples * stream->info.rate / shm->speed; if (!fileSamples) return; /* our max buffer size */ - fileBytes = fileSamples * (bgmstream->info.width * bgmstream->info.channels); + fileBytes = fileSamples * (stream->info.width * stream->info.channels); if (fileBytes > (int) sizeof(raw)) { fileBytes = (int) sizeof(raw); fileSamples = fileBytes / - (bgmstream->info.width * bgmstream->info.channels); + (stream->info.width * stream->info.channels); } /* Read */ - res = S_CodecReadStream(bgmstream, fileBytes, raw); + res = S_CodecReadStream(stream, fileBytes, raw); if (res < fileBytes) { fileBytes = res; - fileSamples = res / (bgmstream->info.width * bgmstream->info.channels); + fileSamples = res / (stream->info.width * stream->info.channels); } if (res > 0) /* data: add to raw buffer */ { - S_RawSamples(fileSamples, bgmstream->info.rate, - bgmstream->info.width, - bgmstream->info.channels, + S_RawSamples(fileSamples, stream->info.rate, + stream->info.width, + stream->info.channels, raw, bgmvolume.value); did_rewind = false; } else if (res == 0) /* EOF */ { - if (bgmloop) { + if (looping) { if (did_rewind) { console::info("Stream keeps returning EOF.\n"); stop(); return; } - res = S_CodecRewindStream(bgmstream); + res = S_CodecRewindStream(stream); if (res != 0) { console::info("Stream seek error (%i), stopping.\n", res); stop(); @@ -266,7 +266,7 @@ namespace music { } void init() { - bgm_extmusic.inscribe(); + extmusic.inscribe(); command::add("music", _play); command::add("music_pause", _pause); command::add("music_resume", _resume); @@ -277,7 +277,7 @@ namespace music { if (common::check_param("-noextmusic").has_value()) no_extmusic = true; - bgmloop = true; + looping = true; for (auto i = 0; wanted_handlers[i].type != CODECTYPE_NONE; i++) { switch (wanted_handlers[i].player) { @@ -341,8 +341,8 @@ namespace music { /* not supported in quake */ break; case player_kind::streamer: - bgmstream = S_CodecOpenStreamType(tmp, chosen->type, bgmloop); - if (bgmstream) + stream = S_CodecOpenStreamType(tmp, chosen->type, looping); + if (stream) return; /* success */ break; case player_kind::none: @@ -354,10 +354,10 @@ namespace music { } void stop() { - if (bgmstream) { - bgmstream->status = STREAM_NONE; - S_CodecCloseStream(bgmstream); - bgmstream = nullptr; + if (stream) { + stream->status = STREAM_NONE; + S_CodecCloseStream(stream); + stream = nullptr; s_rawend = 0; } } @@ -370,25 +370,25 @@ namespace music { bgmvolume.set("1"); old_volume = bgmvolume.value; } - if (bgmstream) + if (stream) update_stream(); } void pause() { - if (bgmstream) { - if (bgmstream->status == STREAM_PLAY) - bgmstream->status = STREAM_PAUSE; + if (stream) { + if (stream->status == STREAM_PLAY) + stream->status = STREAM_PAUSE; } } void resume() { - if (bgmstream) { - if (bgmstream->status == STREAM_PAUSE) - bgmstream->status = STREAM_PLAY; + if (stream) { + if (stream->status == STREAM_PAUSE) + stream->status = STREAM_PLAY; } } - void play_cd_track(const byte track, const bool looping) { + void play_cd_track(const byte track, const bool should_loop) { /* instead of searching by the order of music_handlers, do so by * the order of searchpath priority: the file from the searchpath * with the highest path_id is most likely from our own gamedir @@ -400,14 +400,14 @@ namespace music { unsigned int path_id; stop(); - if (CDAudio_Play(track, looping) == 0) { + if (CDAudio_Play(track, should_loop) == 0) { return; /* success */ } if (active_handlers.empty()) return; // We obviously can't play anything without handlers. - if (no_extmusic || bgm_extmusic.value == 0.0) + if (no_extmusic || extmusic.value == 0.0) return; unsigned int prev_id = 0; @@ -437,8 +437,8 @@ namespace music { else { q_snprintf(tmp, sizeof(tmp), "%s/track%02d.%s", MUSIC_DIRNAME, static_cast(track), ext); - bgmstream = S_CodecOpenStreamType(tmp, type, bgmloop); - if (!bgmstream) + stream = S_CodecOpenStreamType(tmp, type, should_loop); + if (!stream) console::info("Couldn't handle music file %s\n", tmp); } } diff --git a/Quake/bgmusic.hpp b/Quake/bgmusic.hpp index 1c32869..3c77c3a 100644 --- a/Quake/bgmusic.hpp +++ b/Quake/bgmusic.hpp @@ -26,8 +26,8 @@ #pragma once namespace music { - extern bool bgmloop; - extern convar bgm_extmusic; + extern bool looping; + extern convar extmusic; void init(); @@ -43,5 +43,5 @@ namespace music { void resume(); - void play_cd_track(byte track, bool looping); + void play_cd_track(byte track, bool should_loop); } diff --git a/Quake/cl_demo.cpp b/Quake/cl_demo.cpp index f4d26ce..f86efe8 100644 --- a/Quake/cl_demo.cpp +++ b/Quake/cl_demo.cpp @@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.hpp" -static void CL_FinishTimeDemo (void); +static void CL_FinishTimeDemo(void); /* ============================================================================== @@ -40,19 +40,18 @@ read from the demo file. */ // from ProQuake: space to fill out the demo header for record at any time -static byte *demo_head; -static int *demo_head_sizes; +static byte *demo_head; +static int *demo_head_sizes; /* ============== CL_ClearSignons ============== */ -void CL_ClearSignons (void) -{ - VEC_CLEAR (demo_head); - VEC_CLEAR (demo_head_sizes); - cls.signon = 0; +void CL_ClearSignons(void) { + VEC_CLEAR(demo_head); + VEC_CLEAR(demo_head_sizes); + cls.signon = 0; } /* @@ -62,19 +61,18 @@ CL_StopPlayback Called when a demo file runs out, or the user starts a game ============== */ -void CL_StopPlayback (void) -{ - if (!cls.demoplayback) - return; +void CL_StopPlayback(void) { + if (!cls.demoplayback) + return; - fclose (cls.demofile); - cls.demoplayback = false; - cls.demopaused = false; - cls.demofile = NULL; - cls.state = ca_disconnected; + fclose(cls.demofile); + cls.demoplayback = false; + cls.demopaused = false; + cls.demofile = NULL; + cls.state = ca_disconnected; - if (cls.timedemo) - CL_FinishTimeDemo (); + if (cls.timedemo) + CL_FinishTimeDemo(); } /* @@ -84,71 +82,65 @@ CL_WriteDemoMessage Dumps the current net message, prefixed by the length and view angles ==================== */ -static void CL_WriteDemoMessage (void) -{ - int len; - int i; - float f; +static void CL_WriteDemoMessage(void) { + int len; + int i; + float f; - len = LittleLong (net_message.cursize); - fwrite (&len, 4, 1, cls.demofile); - for (i = 0; i < 3; i++) - { - f = LittleFloat (cl.viewangles[i]); - fwrite (&f, 4, 1, cls.demofile); - } - fwrite (net_message.data, net_message.cursize, 1, cls.demofile); - fflush (cls.demofile); + len = LittleLong(net_message.size()); + fwrite(&len, 4, 1, cls.demofile); + for (i = 0; i < 3; i++) { + f = LittleFloat(cl.viewangles[i]); + fwrite(&f, 4, 1, cls.demofile); + } + fwrite(net_message.data(), net_message.size(), 1, cls.demofile); + fflush(cls.demofile); } -static int CL_GetDemoMessage (void) -{ - int r, i; - float f; +static int CL_GetDemoMessage(void) { + int r, i; + float f; - if (cls.demopaused) - return 0; + if (cls.demopaused) + return 0; - // decide if it is time to grab the next message - if (cls.signon == SIGNONS) // always grab until fully connected - { - if (cls.timedemo) - { - if (host_framecount == cls.td_lastframe) - return 0; // already read this frame's message - cls.td_lastframe = host_framecount; - // if this is the second frame, grab the real td_starttime - // so the bogus time on the first frame doesn't count - if (host_framecount == cls.td_startframe + 1) - cls.td_starttime = realtime; - } - else if (/* cl.time > 0 && */ cl.time <= cl.mtime[0]) - { - return 0; // don't need another message yet - } - } + // decide if it is time to grab the next message + if (cls.signon == SIGNONS) // always grab until fully connected + { + if (cls.timedemo) { + if (host_framecount == cls.td_lastframe) + return 0; // already read this frame's message + cls.td_lastframe = host_framecount; + // if this is the second frame, grab the real td_starttime + // so the bogus time on the first frame doesn't count + if (host_framecount == cls.td_startframe + 1) + cls.td_starttime = realtime; + } else if (/* cl.time > 0 && */ cl.time <= cl.mtime[0]) { + return 0; // don't need another message yet + } + } -// get the next message - if (! fread(&net_message.cursize, 4, 1, cls.demofile)) - Sys_Error ("Demo read error"); - VectorCopy (cl.mviewangles[0], cl.mviewangles[1]); - for (i = 0 ; i < 3 ; i++) - { - r = fread (&f, 4, 1, cls.demofile); - cl.mviewangles[0][i] = LittleFloat (f); - } + // get the next message + int msgsize; + if (!fread(&msgsize, 4, 1, cls.demofile)) + Sys_Error("Demo read error"); + VectorCopy(cl.mviewangles[0], cl.mviewangles[1]); + for (i = 0; i < 3; i++) { + r = fread(&f, 4, 1, cls.demofile); + cl.mviewangles[0][i] = LittleFloat(f); + } - net_message.cursize = LittleLong (net_message.cursize); - if (net_message.cursize > MAX_MSGLEN) - Sys_Error ("Demo message > MAX_MSGLEN"); - r = fread (net_message.data, net_message.cursize, 1, cls.demofile); - if (r != 1) - { - CL_StopPlayback (); - return 0; - } + if (msgsize > MAX_MSGLEN) + Sys_Error("Demo message > MAX_MSGLEN"); + std::uint8_t buf[msgsize]; + r = fread(&buf, msgsize, 1, cls.demofile); + net_message.write(buf, msgsize); + if (r != 1) { + CL_StopPlayback(); + return 0; + } - return 1; + return 1; } /* @@ -158,39 +150,36 @@ CL_GetMessage Handles recording and playback of demos, on top of NET_ code ==================== */ -int CL_GetMessage (void) -{ - int r; +int CL_GetMessage(void) { + int r; - if (cls.demoplayback) - return CL_GetDemoMessage (); + if (cls.demoplayback) + return CL_GetDemoMessage(); - while (1) - { - r = NET_GetMessage (cls.netcon); + while (1) { + r = NET_GetMessage(cls.netcon); - if (r != 1 && r != 2) - return r; + if (r != 1 && r != 2) + return r; - // discard nop keepalive message - if (net_message.cursize == 1 && net_message.data[0] == svc_nop) - console::info ("<-- server to client keepalive\n"); - else - break; - } + // discard nop keepalive message + if (net_message.size() == 1 && net_message.data()[0] == svc_nop) + console::info("<-- server to client keepalive\n"); + else + break; + } - if (cls.demorecording) - CL_WriteDemoMessage (); + if (cls.demorecording) + CL_WriteDemoMessage(); - if (cls.signon < 2) - { - // record messages before full connection, so that a - // demo record can happen after connection is done - Vec_Append ((void**)&demo_head, 1, net_message.data, net_message.cursize); - VEC_PUSH (demo_head_sizes, net_message.cursize); - } + if (cls.signon < 2) { + // record messages before full connection, so that a + // demo record can happen after connection is done + Vec_Append((void **) &demo_head, 1, net_message.data(), net_message.size()); + VEC_PUSH(demo_head_sizes, net_message.size()); + } - return r; + return r; } @@ -201,30 +190,28 @@ CL_Stop_f stop recording a demo ==================== */ -void CL_Stop_f (void) -{ - if (command::last_source != command::source::command) - return; +void CL_Stop_f(void) { + if (command::last_source != command::source::command) + return; - if (!cls.demorecording) - { - console::info ("Not recording a demo.\n"); - return; - } + if (!cls.demorecording) { + console::info("Not recording a demo.\n"); + return; + } -// write a disconnect message to the demo file - SZ_Clear (&net_message); - MSG_WriteByte (&net_message, svc_disconnect); - CL_WriteDemoMessage (); + // write a disconnect message to the demo file + net_message.clear(); + net_message.write_byte(svc_disconnect); + CL_WriteDemoMessage(); -// finish up - fclose (cls.demofile); - cls.demofile = NULL; - cls.demorecording = false; - console::info ("Completed demo\n"); - -// ericw -- update demo tab-completion list - DemoList_Rebuild (); + // finish up + fclose(cls.demofile); + cls.demofile = NULL; + cls.demorecording = false; + console::info("Completed demo\n"); + + // ericw -- update demo tab-completion list + DemoList_Rebuild(); } /* @@ -234,162 +221,143 @@ CL_Record_f record [cd track] ==================== */ -void CL_Record_f (void) -{ - int c; - char name[MAX_OSPATH]; - int track; +void CL_Record_f(void) { + int c; + char name[MAX_OSPATH]; + int track; - if (command::last_source != command::source::command) - return; + if (command::last_source != command::source::command) + return; - if (cls.demoplayback) - { - console::info ("Can't record during demo playback\n"); - return; - } + if (cls.demoplayback) { + console::info("Can't record during demo playback\n"); + return; + } - if (cls.demorecording) - CL_Stop_f(); + if (cls.demorecording) + CL_Stop_f(); - c = command::argc(); - if (c != 2 && c != 3 && c != 4) - { - console::info ("record [ [cd track]]\n"); - return; - } + c = command::argc(); + if (c != 2 && c != 3 && c != 4) { + console::info("record [ [cd track]]\n"); + return; + } - if (strstr(command::argv(1).value_or("").c_str(), "..")) - { - console::info ("Relative pathnames are not allowed.\n"); - return; - } + if (strstr(command::argv(1).value_or("").c_str(), "..")) { + console::info("Relative pathnames are not allowed.\n"); + return; + } - if (c == 2 && cls.state == ca_connected) - { + if (c == 2 && cls.state == ca_connected) { #if 0 - console::info("Can not record - already connected to server\nClient demo recording must be started before connecting\n"); - return; + console::info( + "Can not record - already connected to server\nClient demo recording must be started before connecting\n"); + return; #endif - if (cls.signon < 2) - { - console::info("Can't record - try again when connected\n"); - return; - } - } + if (cls.signon < 2) { + console::info("Can't record - try again when connected\n"); + return; + } + } -// write the forced cd track number, or -1 - if (c == 4) - { - track = atoi(command::argv(3)->c_str()); - console::info ("Forcing CD track to %i\n", cls.forcetrack); - } - else - { - track = -1; - } + // write the forced cd track number, or -1 + if (c == 4) { + track = atoi(command::argv(3)->c_str()); + console::info("Forcing CD track to %i\n", cls.forcetrack); + } else { + track = -1; + } - q_snprintf (name, sizeof(name), "%s/%s", com_gamedir, command::argv(1).value_or("").c_str()); + q_snprintf(name, sizeof(name), "%s/%s", com_gamedir, command::argv(1).value_or("").c_str()); -// start the map up - if (c > 2) - { - command::execute_string ( std::format("map {:s}", *command::argv(2)), command::source::command); - if (cls.state != ca_connected) - return; - } + // start the map up + if (c > 2) { + command::execute_string(std::format("map {:s}", *command::argv(2)), command::source::command); + if (cls.state != ca_connected) + return; + } -// open the demo file - COM_AddExtension (name, ".dem", sizeof(name)); + // open the demo file + COM_AddExtension(name, ".dem", sizeof(name)); - console::info ("recording to %s.\n", name); - cls.demofile = fopen (name, "wb"); - if (!cls.demofile) - { - console::info ("ERROR: couldn't create %s\n", name); - return; - } + console::info("recording to %s.\n", name); + cls.demofile = fopen(name, "wb"); + if (!cls.demofile) { + console::info("ERROR: couldn't create %s\n", name); + return; + } - cls.forcetrack = track; - fprintf (cls.demofile, "%i\n", cls.forcetrack); + cls.forcetrack = track; + fprintf(cls.demofile, "%i\n", cls.forcetrack); - cls.demorecording = true; + cls.demorecording = true; - // from ProQuake: initialize the demo file if we're already connected - if (c == 2 && cls.state == ca_connected) - { - static byte tmpbuf[NET_MAXMESSAGE]; - byte *data = net_message.data; - int cursize = net_message.cursize; - int maxsize = net_message.maxsize; - int i, count; + // from ProQuake: initialize the demo file if we're already connected + if (c == 2 && cls.state == ca_connected) { + static byte tmpbuf[NET_MAXMESSAGE]; + net::msg old = net_message; - net_message.data = demo_head; - for (i = 0, count = VEC_SIZE (demo_head_sizes); i < count; i++) - { - net_message.cursize = demo_head_sizes[i]; - CL_WriteDemoMessage (); - net_message.data += net_message.cursize; - } + const auto count = VEC_SIZE(demo_head_sizes); + auto offset = 0; + for (auto i = 0; i < count; i++) { + net_message = net::msg{}; + net_message.write(&demo_head[offset], demo_head_sizes[i]); + offset += demo_head_sizes[i]; + CL_WriteDemoMessage(); + } - net_message.data = tmpbuf; - net_message.maxsize = sizeof (tmpbuf); - SZ_Clear (&net_message); + net_message.clear(); - // current names, colors, and frag counts - for (i = 0; i < cl.maxclients; i++) - { - MSG_WriteByte (&net_message, svc_updatename); - MSG_WriteByte (&net_message, i); - MSG_WriteString (&net_message, cl.scores[i].name); - MSG_WriteByte (&net_message, svc_updatefrags); - MSG_WriteByte (&net_message, i); - MSG_WriteShort (&net_message, cl.scores[i].frags); - MSG_WriteByte (&net_message, svc_updatecolors); - MSG_WriteByte (&net_message, i); - MSG_WriteByte (&net_message, cl.scores[i].colors); - } + // current names, colors, and frag counts + for (auto i = 0; i < cl.maxclients; i++) { + net_message.write_byte(svc_updatename); + net_message.write_byte(i); + net_message.write_string(cl.scores[i].name); + net_message.write_byte(svc_updatefrags); + net_message.write_byte(i); + net_message.write_short(cl.scores[i].frags); + net_message.write_byte(svc_updatecolors); + net_message.write_byte(i); + net_message.write_byte(cl.scores[i].colors); + } - // send all current light styles - for (i = 0; i < MAX_LIGHTSTYLES; i++) - { - MSG_WriteByte (&net_message, svc_lightstyle); - MSG_WriteByte (&net_message, i); - MSG_WriteString (&net_message, cl_lightstyle[i].map); - } + // send all current light styles + for (auto i = 0; i < MAX_LIGHTSTYLES; i++) { + net_message.write_byte(svc_lightstyle); + net_message.write_byte(i); + net_message.write_string(cl_lightstyle[i].map); + } - // what about the CD track or SVC fog... future consideration. - MSG_WriteByte (&net_message, svc_updatestat); - MSG_WriteByte (&net_message, STAT_TOTALSECRETS); - MSG_WriteLong (&net_message, cl.stats[STAT_TOTALSECRETS]); + // what about the CD track or SVC fog... future consideration. + net_message.write_byte(svc_updatestat); + net_message.write_byte(STAT_TOTALSECRETS); + net_message.write_long(cl.stats[STAT_TOTALSECRETS]); - MSG_WriteByte (&net_message, svc_updatestat); - MSG_WriteByte (&net_message, STAT_TOTALMONSTERS); - MSG_WriteLong (&net_message, cl.stats[STAT_TOTALMONSTERS]); + net_message.write_byte(svc_updatestat); + net_message.write_byte(STAT_TOTALMONSTERS); + net_message.write_long(cl.stats[STAT_TOTALMONSTERS]); - MSG_WriteByte (&net_message, svc_updatestat); - MSG_WriteByte (&net_message, STAT_SECRETS); - MSG_WriteLong (&net_message, cl.stats[STAT_SECRETS]); + net_message.write_byte(svc_updatestat); + net_message.write_byte(STAT_SECRETS); + net_message.write_long(cl.stats[STAT_SECRETS]); - MSG_WriteByte (&net_message, svc_updatestat); - MSG_WriteByte (&net_message, STAT_MONSTERS); - MSG_WriteLong (&net_message, cl.stats[STAT_MONSTERS]); + net_message.write_byte(svc_updatestat); + net_message.write_byte(STAT_MONSTERS); + net_message.write_long(cl.stats[STAT_MONSTERS]); - // view entity - MSG_WriteByte (&net_message, svc_setview); - MSG_WriteShort (&net_message, cl.viewentity); + // view entity + net_message.write_byte(svc_setview); + net_message.write_short(cl.viewentity); - // signon - MSG_WriteByte (&net_message, svc_signonnum); - MSG_WriteByte (&net_message, 3); + // signon + net_message.write_byte(svc_signonnum); + net_message.write_byte(3); - CL_WriteDemoMessage(); + CL_WriteDemoMessage(); - // restore net_message - net_message.data = data; - net_message.cursize = cursize; - net_message.maxsize = maxsize; - } + // restore net_message + net_message = old; + } } @@ -400,55 +368,51 @@ CL_PlayDemo_f play [demoname] ==================== */ -void CL_PlayDemo_f (void) -{ - char name[MAX_OSPATH]; +void CL_PlayDemo_f(void) { + char name[MAX_OSPATH]; - if (command::last_source != command::source::command) - return; + if (command::last_source != command::source::command) + return; - if (command::argc() != 2) - { - console::info ("playdemo : plays a demo\n"); - return; - } + if (command::argc() != 2) { + console::info("playdemo : plays a demo\n"); + return; + } -// disconnect from server - CL_Disconnect (); + // disconnect from server + CL_Disconnect(); -// open the demo file - std::strncpy(name, command::argv(1).value_or("").c_str(), sizeof(name)); - COM_AddExtension (name, ".dem", sizeof(name)); + // open the demo file + std::strncpy(name, command::argv(1).value_or("").c_str(), sizeof(name)); + COM_AddExtension(name, ".dem", sizeof(name)); - console::info ("Playing demo from %s.\n", name); + console::info("Playing demo from %s.\n", name); - COM_FOpenFile (name, &cls.demofile, NULL); - if (!cls.demofile) - { - console::info ("ERROR: couldn't open %s\n", name); - cls.demonum = -1; // stop demo loop - return; - } + COM_FOpenFile(name, &cls.demofile, NULL); + if (!cls.demofile) { + console::info("ERROR: couldn't open %s\n", name); + cls.demonum = -1; // stop demo loop + return; + } -// ZOID, fscanf is evil -// O.S.: if a space character e.g. 0x20 (' ') follows '\n', -// fscanf skips that byte too and screws up further reads. -// fscanf (cls.demofile, "%i\n", &cls.forcetrack); - if (fscanf (cls.demofile, "%i", &cls.forcetrack) != 1 || fgetc (cls.demofile) != '\n') - { - fclose (cls.demofile); - cls.demofile = NULL; - cls.demonum = -1; // stop demo loop - console::info ("ERROR: demo \"%s\" is invalid\n", name); - return; - } + // ZOID, fscanf is evil + // O.S.: if a space character e.g. 0x20 (' ') follows '\n', + // fscanf skips that byte too and screws up further reads. + // fscanf (cls.demofile, "%i\n", &cls.forcetrack); + if (fscanf(cls.demofile, "%i", &cls.forcetrack) != 1 || fgetc(cls.demofile) != '\n') { + fclose(cls.demofile); + cls.demofile = NULL; + cls.demonum = -1; // stop demo loop + console::info("ERROR: demo \"%s\" is invalid\n", name); + return; + } - cls.demoplayback = true; - cls.demopaused = false; - cls.state = ca_connected; + cls.demoplayback = true; + cls.demopaused = false; + cls.state = ca_connected; -// get rid of the menu and/or console - key_dest = key_game; + // get rid of the menu and/or console + key_dest = key_game; } /* @@ -457,19 +421,18 @@ CL_FinishTimeDemo ==================== */ -static void CL_FinishTimeDemo (void) -{ - int frames; - float time; +static void CL_FinishTimeDemo(void) { + int frames; + float time; - cls.timedemo = false; + cls.timedemo = false; -// the first frame didn't count - frames = (host_framecount - cls.td_startframe) - 1; - time = realtime - cls.td_starttime; - if (!time) - time = 1; - console::info ("%i frames %5.1f seconds %5.1f fps\n", frames, time, frames/time); + // the first frame didn't count + frames = (host_framecount - cls.td_startframe) - 1; + time = realtime - cls.td_starttime; + if (!time) + time = 1; + console::info("%i frames %5.1f seconds %5.1f fps\n", frames, time, frames / time); } /* @@ -479,26 +442,23 @@ CL_TimeDemo_f timedemo [demoname] ==================== */ -void CL_TimeDemo_f (void) -{ - if (command::last_source != command::source::command) - return; +void CL_TimeDemo_f(void) { + if (command::last_source != command::source::command) + return; - if (command::argc() != 2) - { - console::info ("timedemo : gets demo speeds\n"); - return; - } + if (command::argc() != 2) { + console::info("timedemo : gets demo speeds\n"); + return; + } - CL_PlayDemo_f (); - if (!cls.demofile) - return; + CL_PlayDemo_f(); + if (!cls.demofile) + return; -// cls.td_starttime will be grabbed at the second frame of the demo, so -// all the loading time doesn't get counted + // cls.td_starttime will be grabbed at the second frame of the demo, so + // all the loading time doesn't get counted - cls.timedemo = true; - cls.td_startframe = host_framecount; - cls.td_lastframe = -1; // get a new message this frame + cls.timedemo = true; + cls.td_startframe = host_framecount; + cls.td_lastframe = -1; // get a new message this frame } - diff --git a/Quake/cl_input.cpp b/Quake/cl_input.cpp index 0509d6a..2c847a1 100644 --- a/Quake/cl_input.cpp +++ b/Quake/cl_input.cpp @@ -328,33 +328,27 @@ CL_SendMove void CL_SendMove(const usercmd_t *cmd) { int i; int bits; - sizebuf_t buf; - byte data[128]; - - buf.maxsize = 128; - buf.cursize = 0; - buf.data = data; + net::msg msg{}; cl.cmd = *cmd; // // send the movement message // - MSG_WriteByte(&buf, clc_move); - - MSG_WriteFloat(&buf, cl.mtime[0]); // so server can get ping times + msg.write_byte(clc_move); + msg.write_float(cl.mtime[0]); for (i = 0; i < 3; i++) //johnfitz -- 16-bit angles for PROTOCOL_FITZQUAKE if (cl.protocol == PROTOCOL_NETQUAKE) - MSG_WriteAngle(&buf, cl.viewangles[i], cl.protocolflags); + msg.write_angle(cl.viewangles[i], static_cast(cl.protocolflags)); else - MSG_WriteAngle16(&buf, cl.viewangles[i], cl.protocolflags); + msg.write_angle_16(cl.viewangles[i], static_cast(cl.protocolflags)); //johnfitz - MSG_WriteShort(&buf, cmd->forwardmove); - MSG_WriteShort(&buf, cmd->sidemove); - MSG_WriteShort(&buf, cmd->upmove); + msg.write_short(cmd->forwardmove); + msg.write_short(cmd->sidemove); + msg.write_short(cmd->upmove); // // send button bits @@ -369,9 +363,9 @@ void CL_SendMove(const usercmd_t *cmd) { bits |= 2; in_jump.state &= ~2; - MSG_WriteByte(&buf, bits); + msg.write_byte(bits); - MSG_WriteByte(&buf, in_impulse); + msg.write_byte(in_impulse); in_impulse = 0; // @@ -387,7 +381,7 @@ void CL_SendMove(const usercmd_t *cmd) { if (++cl.movemessages <= 2) return; - if (NET_SendUnreliableMessage(cls.netcon, &buf) == -1) { + if (NET_SendUnreliableMessage(cls.netcon, msg) == -1) { console::info("CL_SendMove: lost server connection\n"); CL_Disconnect(); } diff --git a/Quake/cl_main.cpp b/Quake/cl_main.cpp index 02e951f..eb6c11c 100644 --- a/Quake/cl_main.cpp +++ b/Quake/cl_main.cpp @@ -78,7 +78,7 @@ void CL_ClearState(void) { // wipe the entire cl structure memset(&cl, 0, sizeof(cl)); - SZ_Clear(&cls.message); + cls.message = net::msg{}; // clear other arrays memset(cl_dlights, 0, sizeof(cl_dlights)); @@ -117,10 +117,10 @@ void CL_Disconnect(void) { CL_Stop_f(); console::debug("Sending clc_disconnect\n"); - SZ_Clear(&cls.message); - MSG_WriteByte(&cls.message, clc_disconnect); - NET_SendUnreliableMessage(cls.netcon, &cls.message); - SZ_Clear(&cls.message); + cls.message.clear(); + cls.message.write_byte(clc_disconnect); + NET_SendUnreliableMessage(cls.netcon, cls.message); + cls.message.clear(); NET_Close(cls.netcon); cls.state = ca_disconnected; @@ -165,7 +165,7 @@ void CL_EstablishConnection(const char *host) { cls.demonum = -1; // not in the demo loop now cls.state = ca_connected; CL_ClearSignons(); // need all the signon messages before playing - MSG_WriteByte(&cls.message, clc_nop); // NAT Fix from ProQuake + cls.message.write_byte(clc_nop); // NAT Fix from ProQuake } /* @@ -182,26 +182,26 @@ void CL_SignonReply(void) { switch (cls.signon) { case 1: - MSG_WriteByte(&cls.message, clc_stringcmd); - MSG_WriteString(&cls.message, "prespawn"); + cls.message.write_byte(clc_stringcmd); + cls.message.write_string("prespawn"); break; case 2: - MSG_WriteByte(&cls.message, clc_stringcmd); - MSG_WriteString(&cls.message, va("name \"%s\"\n", cl_name.string)); + cls.message.write_byte(clc_stringcmd); + cls.message.write_string(va("name \"%s\"\n", cl_name.string)); - MSG_WriteByte(&cls.message, clc_stringcmd); - MSG_WriteString(&cls.message, + cls.message.write_byte(clc_stringcmd); + cls.message.write_string( va("color %i %i\n", ((int) cl_color.value) >> 4, ((int) cl_color.value) & 15)); - MSG_WriteByte(&cls.message, clc_stringcmd); + cls.message.write_byte(clc_stringcmd); sprintf(str, "spawn %s", cls.spawnparms); - MSG_WriteString(&cls.message, str); + cls.message.write_string(str); break; case 3: - MSG_WriteByte(&cls.message, clc_stringcmd); - MSG_WriteString(&cls.message, "begin"); + cls.message.write_byte(clc_stringcmd); + cls.message.write_string("begin"); Cache_Report(); // print remaining memory break; @@ -666,12 +666,12 @@ void CL_SendCmd(void) { } if (cls.demoplayback) { - SZ_Clear(&cls.message); + cls.message.clear(); return; } // send the reliable message - if (!cls.message.cursize) + if (cls.message.empty()) return; // no message at all if (!NET_CanSendMessage(cls.netcon)) { @@ -679,10 +679,10 @@ void CL_SendCmd(void) { return; } - if (NET_SendMessage(cls.netcon, &cls.message) == -1) + if (NET_SendMessage(cls.netcon, cls.message) == -1) Host_Error("CL_SendCmd: lost server connection"); - SZ_Clear(&cls.message); + cls.message.clear(); } /* @@ -744,7 +744,7 @@ CL_Init ================= */ void CL_Init(void) { - SZ_Alloc(&cls.message, 1024); + cls.message = net::msg{}; CL_InitInput(); CL_InitTEnts(); diff --git a/Quake/cl_parse.cpp b/Quake/cl_parse.cpp index ff721bb..14993de 100644 --- a/Quake/cl_parse.cpp +++ b/Quake/cl_parse.cpp @@ -148,32 +148,32 @@ void CL_ParseStartSoundPacket(void) { float attenuation; int i; - field_mask = MSG_ReadByte(); + field_mask = net_message.read_byte().value(); if (field_mask & SND_VOLUME) - volume = MSG_ReadByte(); + volume = net_message.read_byte().value(); else volume = DEFAULT_SOUND_PACKET_VOLUME; if (field_mask & SND_ATTENUATION) - attenuation = MSG_ReadByte() / 64.0; + attenuation = net_message.read_byte().value() / 64.0; else attenuation = DEFAULT_SOUND_PACKET_ATTENUATION; //johnfitz -- PROTOCOL_FITZQUAKE if (field_mask & SND_LARGEENTITY) { - ent = (unsigned short) MSG_ReadShort(); - channel = MSG_ReadByte(); + ent = (unsigned short) net_message.read_short().value(); + channel = net_message.read_byte().value(); } else { - channel = (unsigned short) MSG_ReadShort(); + channel = (unsigned short) net_message.read_short().value(); ent = channel >> 3; channel &= 7; } if (field_mask & SND_LARGESOUND) - sound_num = (unsigned short) MSG_ReadShort(); + sound_num = (unsigned short) net_message.read_short().value(); else - sound_num = MSG_ReadByte(); + sound_num = net_message.read_byte().value(); //johnfitz //johnfitz -- check soundnum @@ -185,7 +185,7 @@ void CL_ParseStartSoundPacket(void) { Host_Error("CL_ParseStartSoundPacket: ent = %i", ent); for (i = 0; i < 3; i++) - pos[i] = MSG_ReadCoord(cl.protocolflags); + pos[i] = net_message.read_coord(static_cast(cl.protocolflags)).value(); S_StartSound(ent, channel, cl.sound_precache[sound_num], pos, volume / 255.0, attenuation); } @@ -198,8 +198,8 @@ CL_ParseLocalSound - for 2021 rerelease void CL_ParseLocalSound(void) { int field_mask, sound_num; - field_mask = MSG_ReadByte(); - sound_num = (field_mask & SND_LARGESOUND) ? MSG_ReadShort() : MSG_ReadByte(); + field_mask = net_message.read_byte().value(); + sound_num = (field_mask & SND_LARGESOUND) ? net_message.read_short().value() : net_message.read_byte().value(); if (sound_num >= MAX_SOUNDS) Host_Error("CL_ParseLocalSound: %i > MAX_SOUNDS", sound_num); @@ -220,8 +220,6 @@ void CL_KeepaliveMessage(void) { float time; static float lastmsg; int ret; - sizebuf_t old; - byte *olddata; if (sv.active) return; // no need if server is local @@ -229,9 +227,7 @@ void CL_KeepaliveMessage(void) { return; // read messages from server, should just be nops - olddata = net_olddata; - old = net_message; - memcpy(olddata, net_message.data, net_message.cursize); + auto old = net_message; do { ret = CL_GetMessage(); @@ -244,14 +240,13 @@ void CL_KeepaliveMessage(void) { Host_Error("CL_KeepaliveMessage: received a message"); break; case 2: - if (MSG_ReadByte() != svc_nop) + if (net_message.read_byte().value() != svc_nop) Host_Error("CL_KeepaliveMessage: datagram wasn't a nop"); break; } } while (ret); net_message = old; - memcpy(net_message.data, olddata, net_message.cursize); // check time time = Sys_DoubleTime(); @@ -262,9 +257,9 @@ void CL_KeepaliveMessage(void) { // write out a nop console::info("--> client to server keepalive\n"); - MSG_WriteByte(&cls.message, clc_nop); - NET_SendMessage(cls.netcon, &cls.message); - SZ_Clear(&cls.message); + cls.message.write_byte(clc_nop); + NET_SendMessage(cls.netcon, cls.message); + cls.message.clear(); } /* @@ -273,7 +268,6 @@ CL_ParseServerInfo ================== */ void CL_ParseServerInfo(void) { - const char *str; int i; int nummodels, numsounds; char model_precache[MAX_MODELS][MAX_QPATH]; @@ -292,7 +286,7 @@ void CL_ParseServerInfo(void) { CL_ClearState(); // parse protocol version number - i = MSG_ReadLong(); + i = net_message.read_long().value(); //johnfitz -- support multiple protocols if (i != PROTOCOL_NETQUAKE && i != PROTOCOL_FITZQUAKE && i != PROTOCOL_RMQ) { console::info("\n"); //because there's no newline after serverinfo print @@ -307,7 +301,7 @@ void CL_ParseServerInfo(void) { PRFL_SHORTANGLE | PRFL_FLOATANGLE | PRFL_24BITCOORD | PRFL_FLOATCOORD | PRFL_EDICTSCALE | PRFL_INT32COORD); // mh - read protocol flags from server so that we know what protocol features to expect - cl.protocolflags = (unsigned int) MSG_ReadLong(); + cl.protocolflags = (unsigned int) net_message.read_long().value(); if (0 != (cl.protocolflags & (~supportedflags))) { console::warn("PROTOCOL_RMQ protocolflags %i contains unsupported flags\n", cl.protocolflags); @@ -315,22 +309,22 @@ void CL_ParseServerInfo(void) { } else cl.protocolflags = 0; // parse maxclients - cl.maxclients = MSG_ReadByte(); + cl.maxclients = net_message.read_byte().value(); if (cl.maxclients < 1 || cl.maxclients > MAX_SCOREBOARD) { Host_Error("Bad maxclients (%u) from server", cl.maxclients); } cl.scores = (scoreboard_t *) Hunk_AllocName(cl.maxclients * sizeof(*cl.scores), "scores"); // parse gametype - cl.gametype = MSG_ReadByte(); + cl.gametype = net_message.read_byte().value(); // parse signon message - str = MSG_ReadString(); - std::strncpy(cl.levelname, str, sizeof(cl.levelname)); + auto str = net_message.read_string().value(); + std::strncpy(cl.levelname, str.c_str(), sizeof(cl.levelname)); // seperate the printfs so the server message can have a color console::info("\n%s\n", Con_Quakebar(40)); //johnfitz - console::info("%c%s\n", 2, str); + console::info("%c%s\n", 2, str.c_str()); //johnfitz -- tell user which protocol this is console::info("Using protocol %i\n", i); @@ -342,14 +336,14 @@ void CL_ParseServerInfo(void) { // precache models memset(cl.model_precache, 0, sizeof(cl.model_precache)); for (nummodels = 1; ; nummodels++) { - str = MSG_ReadString(); + str = net_message.read_string().value(); if (!str[0]) break; if (nummodels == MAX_MODELS) { Host_Error("Server sent too many model precaches"); } - std::strncpy(model_precache[nummodels], str, MAX_QPATH); - Mod_TouchModel(str); + std::strncpy(model_precache[nummodels], str.c_str(), MAX_QPATH); + Mod_TouchModel(str.c_str()); } //johnfitz -- check for excessive models @@ -360,14 +354,14 @@ void CL_ParseServerInfo(void) { // precache sounds memset(cl.sound_precache, 0, sizeof(cl.sound_precache)); for (numsounds = 1; ; numsounds++) { - str = MSG_ReadString(); + str = net_message.read_string().value(); if (!str[0]) break; if (numsounds == MAX_SOUNDS) { Host_Error("Server sent too many sound precaches"); } - std::strncpy(sound_precache[numsounds], str, MAX_QPATH); - S_TouchSound(str); + std::strncpy(sound_precache[numsounds], str.c_str(), MAX_QPATH); + S_TouchSound(str.c_str()); } //johnfitz -- check for excessive sounds @@ -404,7 +398,7 @@ void CL_ParseServerInfo(void) { //johnfitz -- clear out string; we don't consider identical //messages to be duplicates if the map has changed in between - con_lastcenterstring[0] = 0; + console::last_center_string.clear(); //johnfitz Hunk_Check(); // make sure nothing is hurt @@ -444,23 +438,23 @@ void CL_ParseUpdate(int bits) { } if (bits & U_MOREBITS) { - i = MSG_ReadByte(); + i = net_message.read_byte().value(); bits |= (i << 8); } //johnfitz -- PROTOCOL_FITZQUAKE if (cl.protocol == PROTOCOL_FITZQUAKE || cl.protocol == PROTOCOL_RMQ) { if (bits & U_EXTEND1) - bits |= MSG_ReadByte() << 16; + bits |= net_message.read_byte().value() << 16; if (bits & U_EXTEND2) - bits |= MSG_ReadByte() << 24; + bits |= net_message.read_byte().value() << 24; } //johnfitz if (bits & U_LONGENTITY) - num = MSG_ReadShort(); + num = net_message.read_short().value(); else - num = MSG_ReadByte(); + num = net_message.read_byte().value(); ent = CL_EntityNum(num); @@ -478,19 +472,19 @@ void CL_ParseUpdate(int bits) { ent->msgtime = cl.mtime[0]; if (bits & U_MODEL) { - modnum = MSG_ReadByte(); + modnum = net_message.read_byte().value(); if (modnum >= MAX_MODELS) Host_Error("CL_ParseModel: bad modnum"); } else modnum = ent->baseline.modelindex; if (bits & U_FRAME) - ent->frame = MSG_ReadByte(); + ent->frame = net_message.read_byte().value(); else ent->frame = ent->baseline.frame; if (bits & U_COLORMAP) - i = MSG_ReadByte(); + i = net_message.read_byte().value(); else i = ent->baseline.colormap; if (!i) @@ -501,7 +495,7 @@ void CL_ParseUpdate(int bits) { ent->colormap = cl.scores[i - 1].translations; } if (bits & U_SKIN) - skin = MSG_ReadByte(); + skin = net_message.read_byte().value(); else skin = ent->baseline.skin; if (skin != ent->skinnum) { @@ -510,7 +504,7 @@ void CL_ParseUpdate(int bits) { R_TranslateNewPlayerSkin(num - 1); //johnfitz -- was R_TranslatePlayerSkin } if (bits & U_EFFECTS) - ent->effects = MSG_ReadByte(); + ent->effects = net_message.read_byte().value(); else ent->effects = ent->baseline.effects; @@ -519,29 +513,29 @@ void CL_ParseUpdate(int bits) { VectorCopy(ent->msg_angles[0], ent->msg_angles[1]); if (bits & U_ORIGIN1) - ent->msg_origins[0][0] = MSG_ReadCoord(cl.protocolflags); + ent->msg_origins[0][0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); else ent->msg_origins[0][0] = ent->baseline.origin[0]; if (bits & U_ANGLE1) - ent->msg_angles[0][0] = MSG_ReadAngle(cl.protocolflags); + ent->msg_angles[0][0] = net_message.read_angle(static_cast(cl.protocolflags)).value(); else ent->msg_angles[0][0] = ent->baseline.angles[0]; if (bits & U_ORIGIN2) - ent->msg_origins[0][1] = MSG_ReadCoord(cl.protocolflags); + ent->msg_origins[0][1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); else ent->msg_origins[0][1] = ent->baseline.origin[1]; if (bits & U_ANGLE2) - ent->msg_angles[0][1] = MSG_ReadAngle(cl.protocolflags); + ent->msg_angles[0][1] = net_message.read_angle(static_cast(cl.protocolflags)).value(); else ent->msg_angles[0][1] = ent->baseline.angles[1]; if (bits & U_ORIGIN3) - ent->msg_origins[0][2] = MSG_ReadCoord(cl.protocolflags); + ent->msg_origins[0][2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); else ent->msg_origins[0][2] = ent->baseline.origin[2]; if (bits & U_ANGLE3) - ent->msg_angles[0][2] = MSG_ReadAngle(cl.protocolflags); + ent->msg_angles[0][2] = net_message.read_angle(static_cast(cl.protocolflags)).value(); else ent->msg_angles[0][2] = ent->baseline.angles[2]; @@ -556,19 +550,19 @@ void CL_ParseUpdate(int bits) { //johnfitz -- PROTOCOL_FITZQUAKE and PROTOCOL_NEHAHRA if (cl.protocol == PROTOCOL_FITZQUAKE || cl.protocol == PROTOCOL_RMQ) { if (bits & U_ALPHA) - ent->alpha = MSG_ReadByte(); + ent->alpha = net_message.read_byte().value(); else ent->alpha = ent->baseline.alpha; if (bits & U_SCALE) - ent->scale = MSG_ReadByte(); + ent->scale = net_message.read_byte().value(); else ent->scale = ent->baseline.scale; if (bits & U_FRAME2) - ent->frame = (ent->frame & 0x00FF) | (MSG_ReadByte() << 8); + ent->frame = (ent->frame & 0x00FF) | (net_message.read_byte().value() << 8); if (bits & U_MODEL2) - modnum = (modnum & 0x00FF) | (MSG_ReadByte() << 8); + modnum = (modnum & 0x00FF) | (net_message.read_byte().value() << 8); if (bits & U_LERPFINISH) { - ent->lerpfinish = ent->msgtime + ((float) (MSG_ReadByte()) / 255); + ent->lerpfinish = ent->msgtime + ((float) (net_message.read_byte().value()) / 255); ent->lerpflags |= LERP_FINISH; } else ent->lerpflags &= ~LERP_FINISH; @@ -582,10 +576,10 @@ void CL_ParseUpdate(int bits) { warn_about_nehahra_protocol = false; } - a = MSG_ReadFloat(); - b = MSG_ReadFloat(); //alpha + a = net_message.read_float().value(); + b = net_message.read_float().value(); //alpha if (a == 2) - MSG_ReadFloat(); //fullbright (not using this yet) + net_message.read_float().value(); //fullbright (not using this yet) ent->alpha = ENTALPHA_ENCODE(b); } else ent->alpha = ent->baseline.alpha; @@ -634,20 +628,20 @@ void CL_ParseBaseline(entity_t *ent, int version) //johnfitz -- added argument int bits; //johnfitz //johnfitz -- PROTOCOL_FITZQUAKE - bits = (version == 2) ? MSG_ReadByte() : 0; - ent->baseline.modelindex = (bits & B_LARGEMODEL) ? MSG_ReadShort() : MSG_ReadByte(); - ent->baseline.frame = (bits & B_LARGEFRAME) ? MSG_ReadShort() : MSG_ReadByte(); + bits = (version == 2) ? net_message.read_byte().value() : 0; + ent->baseline.modelindex = (bits & B_LARGEMODEL) ? net_message.read_short().value() : net_message.read_byte().value(); + ent->baseline.frame = (bits & B_LARGEFRAME) ? net_message.read_short().value() : net_message.read_byte().value(); //johnfitz - ent->baseline.colormap = MSG_ReadByte(); - ent->baseline.skin = MSG_ReadByte(); + ent->baseline.colormap = net_message.read_byte().value(); + ent->baseline.skin = net_message.read_byte().value(); for (i = 0; i < 3; i++) { - ent->baseline.origin[i] = MSG_ReadCoord(cl.protocolflags); - ent->baseline.angles[i] = MSG_ReadAngle(cl.protocolflags); + ent->baseline.origin[i] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + ent->baseline.angles[i] = net_message.read_angle(static_cast(cl.protocolflags)).value(); } - ent->baseline.alpha = (bits & B_ALPHA) ? MSG_ReadByte() : ENTALPHA_DEFAULT; //johnfitz -- PROTOCOL_FITZQUAKE - ent->baseline.scale = (bits & B_SCALE) ? MSG_ReadByte() : ENTSCALE_DEFAULT; + ent->baseline.alpha = (bits & B_ALPHA) ? net_message.read_byte().value() : ENTALPHA_DEFAULT; //johnfitz -- PROTOCOL_FITZQUAKE + ent->baseline.scale = (bits & B_SCALE) ? net_message.read_byte().value() : ENTSCALE_DEFAULT; } @@ -662,34 +656,34 @@ void CL_ParseClientdata(void) { int i, j; int bits; //johnfitz - bits = (unsigned short) MSG_ReadShort(); //johnfitz -- read bits here isntead of in CL_ParseServerMessage() + bits = (unsigned short) net_message.read_short().value(); //johnfitz -- read bits here isntead of in CL_ParseServerMessage() //johnfitz -- PROTOCOL_FITZQUAKE if (bits & SU_EXTEND1) - bits |= (MSG_ReadByte() << 16); + bits |= (net_message.read_byte().value() << 16); if (bits & SU_EXTEND2) - bits |= (MSG_ReadByte() << 24); + bits |= (net_message.read_byte().value() << 24); //johnfitz if (bits & SU_VIEWHEIGHT) - cl.viewheight = MSG_ReadChar(); + cl.viewheight = net_message.read_char().value(); else cl.viewheight = DEFAULT_VIEWHEIGHT; if (bits & SU_IDEALPITCH) - cl.idealpitch = MSG_ReadChar(); + cl.idealpitch = net_message.read_char().value(); else cl.idealpitch = 0; VectorCopy(cl.mvelocity[0], cl.mvelocity[1]); for (i = 0; i < 3; i++) { if (bits & (SU_PUNCH1 << i)) - cl.punchangle[i] = MSG_ReadChar(); + cl.punchangle[i] = net_message.read_char().value(); else cl.punchangle[i] = 0; if (bits & (SU_VELOCITY1 << i)) - cl.mvelocity[0][i] = MSG_ReadChar() * 16; + cl.mvelocity[0][i] = net_message.read_char().value() * 16; else cl.mvelocity[0][i] = 0; } @@ -703,7 +697,7 @@ void CL_ParseClientdata(void) { //johnfitz // [always sent] if (bits & SU_ITEMS) - i = MSG_ReadLong(); + i = net_message.read_long().value(); if (cl.items != i) { // set flash times @@ -718,12 +712,12 @@ void CL_ParseClientdata(void) { cl.inwater = (bits & SU_INWATER) != 0; if (bits & SU_WEAPONFRAME) - cl.stats[STAT_WEAPONFRAME] = MSG_ReadByte(); + cl.stats[STAT_WEAPONFRAME] = net_message.read_byte().value(); else cl.stats[STAT_WEAPONFRAME] = 0; if (bits & SU_ARMOR) - i = MSG_ReadByte(); + i = net_message.read_byte().value(); else i = 0; if (cl.stats[STAT_ARMOR] != i) { @@ -732,7 +726,7 @@ void CL_ParseClientdata(void) { } if (bits & SU_WEAPON) - i = MSG_ReadByte(); + i = net_message.read_byte().value(); else i = 0; if (cl.stats[STAT_WEAPON] != i) { @@ -740,27 +734,27 @@ void CL_ParseClientdata(void) { Sbar_Changed(); } - i = MSG_ReadShort(); + i = net_message.read_short().value(); if (cl.stats[STAT_HEALTH] != i) { cl.stats[STAT_HEALTH] = i; Sbar_Changed(); } - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (cl.stats[STAT_AMMO] != i) { cl.stats[STAT_AMMO] = i; Sbar_Changed(); } for (i = 0; i < 4; i++) { - j = MSG_ReadByte(); + j = net_message.read_byte().value(); if (cl.stats[STAT_SHELLS + i] != j) { cl.stats[STAT_SHELLS + i] = j; Sbar_Changed(); } } - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (standard_quake) { if (cl.stats[STAT_ACTIVEWEAPON] != i) { @@ -776,23 +770,23 @@ void CL_ParseClientdata(void) { //johnfitz -- PROTOCOL_FITZQUAKE if (bits & SU_WEAPON2) - cl.stats[STAT_WEAPON] |= (MSG_ReadByte() << 8); + cl.stats[STAT_WEAPON] |= (net_message.read_byte().value() << 8); if (bits & SU_ARMOR2) - cl.stats[STAT_ARMOR] |= (MSG_ReadByte() << 8); + cl.stats[STAT_ARMOR] |= (net_message.read_byte().value() << 8); if (bits & SU_AMMO2) - cl.stats[STAT_AMMO] |= (MSG_ReadByte() << 8); + cl.stats[STAT_AMMO] |= (net_message.read_byte().value() << 8); if (bits & SU_SHELLS2) - cl.stats[STAT_SHELLS] |= (MSG_ReadByte() << 8); + cl.stats[STAT_SHELLS] |= (net_message.read_byte().value() << 8); if (bits & SU_NAILS2) - cl.stats[STAT_NAILS] |= (MSG_ReadByte() << 8); + cl.stats[STAT_NAILS] |= (net_message.read_byte().value() << 8); if (bits & SU_ROCKETS2) - cl.stats[STAT_ROCKETS] |= (MSG_ReadByte() << 8); + cl.stats[STAT_ROCKETS] |= (net_message.read_byte().value() << 8); if (bits & SU_CELLS2) - cl.stats[STAT_CELLS] |= (MSG_ReadByte() << 8); + cl.stats[STAT_CELLS] |= (net_message.read_byte().value() << 8); if (bits & SU_WEAPONFRAME2) - cl.stats[STAT_WEAPONFRAME] |= (MSG_ReadByte() << 8); + cl.stats[STAT_WEAPONFRAME] |= (net_message.read_byte().value() << 8); if (bits & SU_WEAPONALPHA) - cl.viewent.alpha = MSG_ReadByte(); + cl.viewent.alpha = net_message.read_byte().value(); else cl.viewent.alpha = ENTALPHA_DEFAULT; //johnfitz @@ -887,17 +881,17 @@ void CL_ParseStaticSound(int version) //johnfitz -- added argument int i; for (i = 0; i < 3; i++) - org[i] = MSG_ReadCoord(cl.protocolflags); + org[i] = net_message.read_coord(static_cast(cl.protocolflags)).value(); //johnfitz -- PROTOCOL_FITZQUAKE if (version == 2) - sound_num = MSG_ReadShort(); + sound_num = net_message.read_short().value(); else - sound_num = MSG_ReadByte(); + sound_num = net_message.read_byte().value(); //johnfitz - vol = MSG_ReadByte(); - atten = MSG_ReadByte(); + vol = net_message.read_byte().value(); + atten = net_message.read_byte().value(); S_StaticSound(cl.sound_precache[sound_num], org, vol, atten); } @@ -934,7 +928,7 @@ static void CL_DumpPacket(void) { } #endif /* CL_DumpPacket */ -#define SHOWNET(x) if(cl_shownet.value==2)console::info ("%3i:%s\n", msg_readcount-1, x); +#define SHOWNET(x) if(cl_shownet.value==2)console::info ("%3i:%s\n", net_message.offset()-1, x); /* ===================== @@ -944,14 +938,14 @@ CL_ParseServerMessage void CL_ParseServerMessage(void) { int cmd; int i; - const char *str; //johnfitz + std::string str{}; //johnfitz int total, j, lastcmd; //johnfitz // // if recording demos, copy the message out // if (cl_shownet.value == 1) - console::info("%i ", net_message.cursize); + console::info("%i ", net_message.size()); else if (cl_shownet.value == 2) console::info("------------------\n"); @@ -960,20 +954,22 @@ void CL_ParseServerMessage(void) { // // parse the message // - MSG_BeginReading(); + net_message.begin_reading(); lastcmd = 0; - while (1) { - if (msg_badread) + while (true) { + if (net_message.eos()) Host_Error("CL_ParseServerMessage: Bad server message"); - cmd = MSG_ReadByte(); + auto cmdv = net_message.read_byte(); - if (cmd == -1) { + if (!cmdv.has_value()) { SHOWNET("END OF MESSAGE"); return; // end of message } + cmd = cmdv.value(); + // if the high bit of the command byte is set, it is a fast update if (cmd & U_SIGNAL) //johnfitz -- was 128, changed for clarity { @@ -1000,7 +996,7 @@ void CL_ParseServerMessage(void) { case svc_time: cl.mtime[1] = cl.mtime[0]; - cl.mtime[0] = MSG_ReadFloat(); + cl.mtime[0] = net_message.read_float().value(); break; case svc_clientdata: @@ -1009,7 +1005,7 @@ void CL_ParseServerMessage(void) { break; case svc_version: - i = MSG_ReadLong(); + i = net_message.read_long().value(); //johnfitz -- support multiple protocols if (i != PROTOCOL_NETQUAKE && i != PROTOCOL_FITZQUAKE && i != PROTOCOL_RMQ) Host_Error("Server returned version %i, not %i or %i or %i", i, PROTOCOL_NETQUAKE, @@ -1022,19 +1018,19 @@ void CL_ParseServerMessage(void) { Host_EndGame("Server disconnected\n"); case svc_print: - console::info("%s", MSG_ReadString()); + console::info("%s", net_message.read_string().value().c_str()); break; case svc_centerprint: //johnfitz -- log centerprints to console - str = MSG_ReadString(); - SCR_CenterPrint(str); - Con_LogCenterPrint(str); + str = net_message.read_string().value(); + SCR_CenterPrint(str.c_str()); + Con_LogCenterPrint(str.c_str()); //johnfitz break; case svc_stufftext: - command::buffer::add_text(MSG_ReadString()); + command::buffer::add_text(net_message.read_string().value()); break; case svc_damage: @@ -1048,18 +1044,18 @@ void CL_ParseServerMessage(void) { case svc_setangle: for (i = 0; i < 3; i++) - cl.viewangles[i] = MSG_ReadAngle(cl.protocolflags); + cl.viewangles[i] = net_message.read_angle(static_cast(cl.protocolflags)).value(); break; case svc_setview: - cl.viewentity = MSG_ReadShort(); + cl.viewentity = net_message.read_short().value(); break; case svc_lightstyle: - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (i >= MAX_LIGHTSTYLES) Sys_Error("svc_lightstyle > MAX_LIGHTSTYLES"); - q_strlcpy(cl_lightstyle[i].map, MSG_ReadString(), MAX_STYLESTRING); + q_strlcpy(cl_lightstyle[i].map, net_message.read_string().value().c_str(), MAX_STYLESTRING); cl_lightstyle[i].length = std::strlen(cl_lightstyle[i].map); //johnfitz -- save extra info if (cl_lightstyle[i].length) { @@ -1080,32 +1076,32 @@ void CL_ParseServerMessage(void) { break; case svc_stopsound: - i = MSG_ReadShort(); + i = net_message.read_short().value(); S_StopSound(i >> 3, i & 7); break; case svc_updatename: Sbar_Changed(); - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (i >= cl.maxclients) Host_Error("CL_ParseServerMessage: svc_updatename > MAX_SCOREBOARD"); - q_strlcpy(cl.scores[i].name, MSG_ReadString(), MAX_SCOREBOARDNAME); + q_strlcpy(cl.scores[i].name, net_message.read_string().value().c_str(), MAX_SCOREBOARDNAME); break; case svc_updatefrags: Sbar_Changed(); - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (i >= cl.maxclients) Host_Error("CL_ParseServerMessage: svc_updatefrags > MAX_SCOREBOARD"); - cl.scores[i].frags = MSG_ReadShort(); + cl.scores[i].frags = net_message.read_short().value(); break; case svc_updatecolors: Sbar_Changed(); - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (i >= cl.maxclients) Host_Error("CL_ParseServerMessage: svc_updatecolors > MAX_SCOREBOARD"); - cl.scores[i].colors = MSG_ReadByte(); + cl.scores[i].colors = net_message.read_byte().value(); CL_NewTranslation(i); break; @@ -1114,7 +1110,7 @@ void CL_ParseServerMessage(void) { break; case svc_spawnbaseline: - i = MSG_ReadShort(); + i = net_message.read_short().value(); // must use CL_EntityNum() to force cl.num_entities up CL_ParseBaseline(CL_EntityNum(i), 1); // johnfitz -- added second parameter break; @@ -1128,7 +1124,7 @@ void CL_ParseServerMessage(void) { break; case svc_setpause: - cl.paused = MSG_ReadByte(); + cl.paused = net_message.read_byte().value(); if (cl.paused) { CDAudio_Pause(); music::pause(); @@ -1139,7 +1135,7 @@ void CL_ParseServerMessage(void) { break; case svc_signonnum: - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (i <= cls.signon) Host_Error("Received signon %i when at %i", i, cls.signon); cls.signon = i; @@ -1163,10 +1159,10 @@ void CL_ParseServerMessage(void) { break; case svc_updatestat: - i = MSG_ReadByte(); + i = net_message.read_byte().value(); if (i < 0 || i >= MAX_CL_STATS) Sys_Error("svc_updatestat: %i is invalid", i); - cl.stats[i] = MSG_ReadLong();; + cl.stats[i] = net_message.read_long().value();; break; case svc_spawnstaticsound: @@ -1174,8 +1170,8 @@ void CL_ParseServerMessage(void) { break; case svc_cdtrack: - cl.cdtrack = MSG_ReadByte(); - cl.looptrack = MSG_ReadByte(); + cl.cdtrack = net_message.read_byte().value(); + cl.looptrack = net_message.read_byte().value(); if ((cls.demoplayback || cls.demorecording) && (cls.forcetrack != -1)) music::play_cd_track((byte) cls.forcetrack, true); else @@ -1194,9 +1190,9 @@ void CL_ParseServerMessage(void) { cl.completed_time = cl.time; vid.recalc_refdef = true; // go to full screen //johnfitz -- log centerprints to console - str = MSG_ReadString(); - SCR_CenterPrint(str); - Con_LogCenterPrint(str); + str = net_message.read_string().value(); + SCR_CenterPrint(str.c_str()); + Con_LogCenterPrint(str.c_str()); //johnfitz V_RestoreAngles(); break; @@ -1206,9 +1202,9 @@ void CL_ParseServerMessage(void) { cl.completed_time = cl.time; vid.recalc_refdef = true; // go to full screen //johnfitz -- log centerprints to console - str = MSG_ReadString(); - SCR_CenterPrint(str); - Con_LogCenterPrint(str); + str = net_message.read_string().value(); + SCR_CenterPrint(str.c_str()); + Con_LogCenterPrint(str.c_str()); //johnfitz V_RestoreAngles(); break; @@ -1219,7 +1215,7 @@ void CL_ParseServerMessage(void) { //johnfitz -- new svc types case svc_skybox: - Sky_LoadSkyBox(MSG_ReadString()); + Sky_LoadSkyBox(net_message.read_string().value().c_str()); break; case svc_bf: @@ -1231,7 +1227,7 @@ void CL_ParseServerMessage(void) { break; case svc_spawnbaseline2: //PROTOCOL_FITZQUAKE - i = MSG_ReadShort(); + i = net_message.read_short().value(); // must use CL_EntityNum() to force cl.num_entities up CL_ParseBaseline(CL_EntityNum(i), 2); break; @@ -1247,8 +1243,8 @@ void CL_ParseServerMessage(void) { //used by the 2021 rerelease case svc_achievement: - str = MSG_ReadString(); - console::debug("Ignoring svc_achievement (%s)\n", str); + str = net_message.read_string().value(); + console::debug("Ignoring svc_achievement (%s)\n", str.c_str()); break; case svc_localsound: CL_ParseLocalSound(); diff --git a/Quake/cl_tent.cpp b/Quake/cl_tent.cpp index 5eb2430..f989537 100644 --- a/Quake/cl_tent.cpp +++ b/Quake/cl_tent.cpp @@ -63,15 +63,15 @@ void CL_ParseBeam (qmodel_t *m) beam_t *b; int i; - ent = MSG_ReadShort (); + ent = net_message.read_short().value(); - start[0] = MSG_ReadCoord (cl.protocolflags); - start[1] = MSG_ReadCoord (cl.protocolflags); - start[2] = MSG_ReadCoord (cl.protocolflags); + start[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + start[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + start[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); - end[0] = MSG_ReadCoord (cl.protocolflags); - end[1] = MSG_ReadCoord (cl.protocolflags); - end[2] = MSG_ReadCoord (cl.protocolflags); + end[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + end[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + end[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); // override any beam with the same entity for (i=0, b=cl_beams ; i< MAX_BEAMS ; i++, b++) @@ -121,29 +121,29 @@ void CL_ParseTEnt (void) int rnd; int colorStart, colorLength; - type = MSG_ReadByte (); + type = net_message.read_byte().value(); switch (type) { case TE_WIZSPIKE: // spike hitting wall - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_RunParticleEffect (pos, vec3_origin, 20, 30); S_StartSound (-1, 0, cl_sfx_wizhit, pos, 1, 1); break; case TE_KNIGHTSPIKE: // spike hitting wall - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_RunParticleEffect (pos, vec3_origin, 226, 20); S_StartSound (-1, 0, cl_sfx_knighthit, pos, 1, 1); break; case TE_SPIKE: // spike hitting wall - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_RunParticleEffect (pos, vec3_origin, 0, 10); if ( rand() % 5 ) S_StartSound (-1, 0, cl_sfx_tink1, pos, 1, 1); @@ -159,9 +159,9 @@ void CL_ParseTEnt (void) } break; case TE_SUPERSPIKE: // super spike hitting wall - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_RunParticleEffect (pos, vec3_origin, 0, 20); if ( rand() % 5 ) @@ -179,16 +179,16 @@ void CL_ParseTEnt (void) break; case TE_GUNSHOT: // bullet hitting wall - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_RunParticleEffect (pos, vec3_origin, 0, 20); break; case TE_EXPLOSION: // rocket explosion - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_ParticleExplosion (pos); dl = CL_AllocDlight (0); VectorCopy (pos, dl->origin); @@ -199,9 +199,9 @@ void CL_ParseTEnt (void) break; case TE_TAREXPLOSION: // tarbaby explosion - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_BlobExplosion (pos); S_StartSound (-1, 0, cl_sfx_r_exp3, pos, 1, 1); @@ -226,25 +226,25 @@ void CL_ParseTEnt (void) // PGM 01/21/97 case TE_LAVASPLASH: - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_LavaSplash (pos); break; case TE_TELEPORT: - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); R_TeleportSplash (pos); break; case TE_EXPLOSION2: // color mapped explosion - pos[0] = MSG_ReadCoord (cl.protocolflags); - pos[1] = MSG_ReadCoord (cl.protocolflags); - pos[2] = MSG_ReadCoord (cl.protocolflags); - colorStart = MSG_ReadByte (); - colorLength = MSG_ReadByte (); + pos[0] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[1] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + pos[2] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + colorStart = net_message.read_byte().value(); + colorLength = net_message.read_byte().value(); R_ParticleExplosion2 (pos, colorStart, colorLength); dl = CL_AllocDlight (0); VectorCopy (pos, dl->origin); diff --git a/Quake/client.hpp b/Quake/client.hpp index fb33f14..3aa852a 100644 --- a/Quake/client.hpp +++ b/Quake/client.hpp @@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _CLIENT_H_ #define _CLIENT_H_ +#include "net_msg.hpp" + // client.h typedef struct @@ -129,7 +131,7 @@ typedef struct // connection information int signon; // 0 to SIGNONS struct qsocket_s *netcon; - sizebuf_t message; // writing buffer to send to server + net::msg message; // writing buffer to send to server } client_static_t; diff --git a/Quake/cmd.cpp b/Quake/cmd.cpp index 43c17ac..26a0317 100644 --- a/Quake/cmd.cpp +++ b/Quake/cmd.cpp @@ -445,15 +445,16 @@ namespace command { if (cls.demoplayback) return; // not really connected - MSG_WriteByte(&cls.message, clc_stringcmd); + cls.message.write_byte(clc_stringcmd); if (q_strcasecmp(argv(0).value_or("").c_str(), "cmd") != 0) { - SZ_Print(&cls.message, argv(0).value_or("").c_str()); - SZ_Print(&cls.message, " "); + if (argc() > 1) { + cls.message.write_string(std::format("{} {}", argv(0).value(), args())); + } else { + cls.message.write_string(std::format("{} \n", argv(0).value())); + } + } else { + cls.message.write_string("\n"); } - if (argc() > 1) - SZ_Print(&cls.message, args().c_str()); - else - SZ_Print(&cls.message, "\n"); } definition_view get_commands() { diff --git a/Quake/common.cpp b/Quake/common.cpp index af6a23b..6f16620 100644 --- a/Quake/common.cpp +++ b/Quake/common.cpp @@ -393,267 +393,267 @@ Handles byte ordering and avoids alignment errors // writing functions // -void MSG_WriteChar(sizebuf_t *sb, int c) { - byte *buf; - -#ifdef PARANOID - if (c < -128 || c > 127) - Sys_Error("MSG_WriteChar: range error"); -#endif - - buf = (byte *) SZ_GetSpace(sb, 1); - buf[0] = c; -} - -void MSG_WriteByte(sizebuf_t *sb, int c) { - byte *buf; - -#ifdef PARANOID - if (c < 0 || c > 255) - Sys_Error("MSG_WriteByte: range error"); -#endif - - buf = (byte *) SZ_GetSpace(sb, 1); - buf[0] = c; -} - -void MSG_WriteShort(sizebuf_t *sb, int c) { - byte *buf; - -#ifdef PARANOID - if (c < ((short) 0x8000) || c > (short) 0x7fff) - Sys_Error("MSG_WriteShort: range error"); -#endif - - buf = (byte *) SZ_GetSpace(sb, 2); - buf[0] = c & 0xff; - buf[1] = c >> 8; -} - -void MSG_WriteLong(sizebuf_t *sb, int c) { - byte *buf; - - buf = (byte *) SZ_GetSpace(sb, 4); - buf[0] = c & 0xff; - buf[1] = (c >> 8) & 0xff; - buf[2] = (c >> 16) & 0xff; - buf[3] = c >> 24; -} - -void MSG_WriteFloat(sizebuf_t *sb, float f) { - union { - float f; - int l; - } dat; - - dat.f = f; - dat.l = LittleLong(dat.l); - - SZ_Write(sb, &dat.l, 4); -} - -void MSG_WriteString(sizebuf_t *sb, const char *s) { - if (!s) - SZ_Write(sb, "", 1); - else - SZ_Write(sb, s, std::strlen(s) + 1); -} - -//johnfitz -- original behavior, 13.3 fixed point coords, max range +-4096 -void MSG_WriteCoord16(sizebuf_t *sb, float f) { - MSG_WriteShort(sb, Q_rint(f*8)); -} - -//johnfitz -- 16.8 fixed point coords, max range +-32768 -void MSG_WriteCoord24(sizebuf_t *sb, float f) { - MSG_WriteShort(sb, f); - MSG_WriteByte(sb, (int) (f * 255) % 255); -} - -//johnfitz -- 32-bit float coords -void MSG_WriteCoord32f(sizebuf_t *sb, float f) { - MSG_WriteFloat(sb, f); -} - -void MSG_WriteCoord(sizebuf_t *sb, float f, unsigned int flags) { - if (flags & PRFL_FLOATCOORD) - MSG_WriteFloat(sb, f); - else if (flags & PRFL_INT32COORD) - MSG_WriteLong(sb, Q_rint(f * 16)); - else if (flags & PRFL_24BITCOORD) - MSG_WriteCoord24(sb, f); - else MSG_WriteCoord16(sb, f); -} - -void MSG_WriteAngle(sizebuf_t *sb, float f, unsigned int flags) { - if (flags & PRFL_FLOATANGLE) - MSG_WriteFloat(sb, f); - else if (flags & PRFL_SHORTANGLE) - MSG_WriteShort(sb, Q_rint(f * 65536.0 / 360.0) & 65535); - else MSG_WriteByte(sb, Q_rint(f * 256.0 / 360.0) & 255); //johnfitz -- use Q_rint instead of (int) } -} - -//johnfitz -- for PROTOCOL_FITZQUAKE -void MSG_WriteAngle16(sizebuf_t *sb, float f, unsigned int flags) { - if (flags & PRFL_FLOATANGLE) - MSG_WriteFloat(sb, f); - else MSG_WriteShort(sb, Q_rint(f * 65536.0 / 360.0) & 65535); -} - -//johnfitz - +// void MSG_WriteChar(sizebuf_t *sb, int c) { +// byte *buf; // -// reading functions +// #ifdef PARANOID +// if (c < -128 || c > 127) +// Sys_Error("MSG_WriteChar: range error"); +// #endif // -int msg_readcount; -bool msg_badread; - -void MSG_BeginReading(void) { - msg_readcount = 0; - msg_badread = false; -} - -// returns -1 and sets msg_badread if no more characters are available -int MSG_ReadChar(void) { - int c; - - if (msg_readcount + 1 > net_message.cursize) { - msg_badread = true; - return -1; - } - - c = (signed char) net_message.data[msg_readcount]; - msg_readcount++; - - return c; -} - -int MSG_ReadByte(void) { - int c; - - if (msg_readcount + 1 > net_message.cursize) { - msg_badread = true; - return -1; - } - - c = (unsigned char) net_message.data[msg_readcount]; - msg_readcount++; - - return c; -} - -int MSG_ReadShort(void) { - int c; - - if (msg_readcount + 2 > net_message.cursize) { - msg_badread = true; - return -1; - } - - c = (short) (net_message.data[msg_readcount] - + (net_message.data[msg_readcount + 1] << 8)); - - msg_readcount += 2; - - return c; -} - -int MSG_ReadLong(void) { - int c; - - if (msg_readcount + 4 > net_message.cursize) { - msg_badread = true; - return -1; - } - - c = net_message.data[msg_readcount] - + (net_message.data[msg_readcount + 1] << 8) - + (net_message.data[msg_readcount + 2] << 16) - + (net_message.data[msg_readcount + 3] << 24); - - msg_readcount += 4; - - return c; -} - -float MSG_ReadFloat(void) { - union { - byte b[4]; - float f; - int l; - } dat; - - dat.b[0] = net_message.data[msg_readcount]; - dat.b[1] = net_message.data[msg_readcount + 1]; - dat.b[2] = net_message.data[msg_readcount + 2]; - dat.b[3] = net_message.data[msg_readcount + 3]; - msg_readcount += 4; - - dat.l = LittleLong(dat.l); - - return dat.f; -} - -const char *MSG_ReadString(void) { - static char string[2048]; - int c; - size_t l; - - l = 0; - do { - c = MSG_ReadByte(); - if (c == -1 || c == 0) - break; - string[l] = c; - l++; - } while (l < sizeof(string) - 1); - - string[l] = 0; - - return string; -} - -//johnfitz -- original behavior, 13.3 fixed point coords, max range +-4096 -float MSG_ReadCoord16(void) { - return MSG_ReadShort() * (1.0 / 8); -} - -//johnfitz -- 16.8 fixed point coords, max range +-32768 -float MSG_ReadCoord24(void) { - return MSG_ReadShort() + MSG_ReadByte() * (1.0 / 255); -} - -//johnfitz -- 32-bit float coords -float MSG_ReadCoord32f(void) { - return MSG_ReadFloat(); -} - -float MSG_ReadCoord(unsigned int flags) { - if (flags & PRFL_FLOATCOORD) - return MSG_ReadFloat(); - else if (flags & PRFL_INT32COORD) - return MSG_ReadLong() * (1.0 / 16.0); - else if (flags & PRFL_24BITCOORD) - return MSG_ReadCoord24(); - else return MSG_ReadCoord16(); -} - -float MSG_ReadAngle(unsigned int flags) { - if (flags & PRFL_FLOATANGLE) - return MSG_ReadFloat(); - else if (flags & PRFL_SHORTANGLE) - return MSG_ReadShort() * (360.0 / 65536); - else return MSG_ReadChar() * (360.0 / 256); -} - -//johnfitz -- for PROTOCOL_FITZQUAKE -float MSG_ReadAngle16(unsigned int flags) { - if (flags & PRFL_FLOATANGLE) - return MSG_ReadFloat(); // make sure - else return MSG_ReadShort() * (360.0 / 65536); -} - -//johnfitz +// buf = (byte *) SZ_GetSpace(sb, 1); +// buf[0] = c; +// } +// +// void MSG_WriteByte(sizebuf_t *sb, int c) { +// byte *buf; +// +// #ifdef PARANOID +// if (c < 0 || c > 255) +// Sys_Error("MSG_WriteByte: range error"); +// #endif +// +// buf = (byte *) SZ_GetSpace(sb, 1); +// buf[0] = c; +// } +// +// void MSG_WriteShort(sizebuf_t *sb, int c) { +// byte *buf; +// +// #ifdef PARANOID +// if (c < ((short) 0x8000) || c > (short) 0x7fff) +// Sys_Error("MSG_WriteShort: range error"); +// #endif +// +// buf = (byte *) SZ_GetSpace(sb, 2); +// buf[0] = c & 0xff; +// buf[1] = c >> 8; +// } +// +// void MSG_WriteLong(sizebuf_t *sb, int c) { +// byte *buf; +// +// buf = (byte *) SZ_GetSpace(sb, 4); +// buf[0] = c & 0xff; +// buf[1] = (c >> 8) & 0xff; +// buf[2] = (c >> 16) & 0xff; +// buf[3] = c >> 24; +// } +// +// void MSG_WriteFloat(sizebuf_t *sb, float f) { +// union { +// float f; +// int l; +// } dat; +// +// dat.f = f; +// dat.l = LittleLong(dat.l); +// +// SZ_Write(sb, &dat.l, 4); +// } +// +// void MSG_WriteString(sizebuf_t *sb, const char *s) { +// if (!s) +// SZ_Write(sb, "", 1); +// else +// SZ_Write(sb, s, std::strlen(s) + 1); +// } +// +// //johnfitz -- original behavior, 13.3 fixed point coords, max range +-4096 +// void MSG_WriteCoord16(sizebuf_t *sb, float f) { +// MSG_WriteShort(sb, Q_rint(f*8)); +// } +// +// //johnfitz -- 16.8 fixed point coords, max range +-32768 +// void MSG_WriteCoord24(sizebuf_t *sb, float f) { +// MSG_WriteShort(sb, f); +// MSG_WriteByte(sb, (int) (f * 255) % 255); +// } +// +// //johnfitz -- 32-bit float coords +// void MSG_WriteCoord32f(sizebuf_t *sb, float f) { +// MSG_WriteFloat(sb, f); +// } +// +// void MSG_WriteCoord(sizebuf_t *sb, float f, unsigned int flags) { +// if (flags & PRFL_FLOATCOORD) +// MSG_WriteFloat(sb, f); +// else if (flags & PRFL_INT32COORD) +// MSG_WriteLong(sb, Q_rint(f * 16)); +// else if (flags & PRFL_24BITCOORD) +// MSG_WriteCoord24(sb, f); +// else MSG_WriteCoord16(sb, f); +// } +// +// void MSG_WriteAngle(sizebuf_t *sb, float f, unsigned int flags) { +// if (flags & PRFL_FLOATANGLE) +// MSG_WriteFloat(sb, f); +// else if (flags & PRFL_SHORTANGLE) +// MSG_WriteShort(sb, Q_rint(f * 65536.0 / 360.0) & 65535); +// else MSG_WriteByte(sb, Q_rint(f * 256.0 / 360.0) & 255); //johnfitz -- use Q_rint instead of (int) } +// } +// +// //johnfitz -- for PROTOCOL_FITZQUAKE +// void MSG_WriteAngle16(sizebuf_t *sb, float f, unsigned int flags) { +// if (flags & PRFL_FLOATANGLE) +// MSG_WriteFloat(sb, f); +// else MSG_WriteShort(sb, Q_rint(f * 65536.0 / 360.0) & 65535); +// } +// +// //johnfitz +// +// // +// // reading functions +// // +// int msg_readcount; +// bool msg_badread; +// +// void MSG_BeginReading(void) { +// msg_readcount = 0; +// msg_badread = false; +// } +// +// // returns -1 and sets msg_badread if no more characters are available +// int MSG_ReadChar(void) { +// int c; +// +// if (msg_readcount + 1 > net_message.cursize) { +// msg_badread = true; +// return -1; +// } +// +// c = (signed char) net_message.data[msg_readcount]; +// msg_readcount++; +// +// return c; +// } +// +// int MSG_ReadByte(void) { +// int c; +// +// if (msg_readcount + 1 > net_message.cursize) { +// msg_badread = true; +// return -1; +// } +// +// c = (unsigned char) net_message.data[msg_readcount]; +// msg_readcount++; +// +// return c; +// } +// +// int MSG_ReadShort(void) { +// int c; +// +// if (msg_readcount + 2 > net_message.cursize) { +// msg_badread = true; +// return -1; +// } +// +// c = (short) (net_message.data[msg_readcount] +// + (net_message.data[msg_readcount + 1] << 8)); +// +// msg_readcount += 2; +// +// return c; +// } +// +// int MSG_ReadLong(void) { +// int c; +// +// if (msg_readcount + 4 > net_message.cursize) { +// msg_badread = true; +// return -1; +// } +// +// c = net_message.data[msg_readcount] +// + (net_message.data[msg_readcount + 1] << 8) +// + (net_message.data[msg_readcount + 2] << 16) +// + (net_message.data[msg_readcount + 3] << 24); +// +// msg_readcount += 4; +// +// return c; +// } +// +// float MSG_ReadFloat(void) { +// union { +// byte b[4]; +// float f; +// int l; +// } dat; +// +// dat.b[0] = net_message.data[msg_readcount]; +// dat.b[1] = net_message.data[msg_readcount + 1]; +// dat.b[2] = net_message.data[msg_readcount + 2]; +// dat.b[3] = net_message.data[msg_readcount + 3]; +// msg_readcount += 4; +// +// dat.l = LittleLong(dat.l); +// +// return dat.f; +// } +// +// const char *MSG_ReadString(void) { +// static char string[2048]; +// int c; +// size_t l; +// +// l = 0; +// do { +// c = net_message.read_byte().value(); +// if (c == -1 || c == 0) +// break; +// string[l] = c; +// l++; +// } while (l < sizeof(string) - 1); +// +// string[l] = 0; +// +// return string; +// } +// +// //johnfitz -- original behavior, 13.3 fixed point coords, max range +-4096 +// float MSG_ReadCoord16(void) { +// return MSG_ReadShort() * (1.0 / 8); +// } +// +// //johnfitz -- 16.8 fixed point coords, max range +-32768 +// float MSG_ReadCoord24(void) { +// return MSG_ReadShort() + net_message.read_byte().value() * (1.0 / 255); +// } +// +// //johnfitz -- 32-bit float coords +// float MSG_ReadCoord32f(void) { +// return MSG_ReadFloat(); +// } +// +// float MSG_ReadCoord(unsigned int flags) { +// if (flags & PRFL_FLOATCOORD) +// return MSG_ReadFloat(); +// else if (flags & PRFL_INT32COORD) +// return MSG_ReadLong() * (1.0 / 16.0); +// else if (flags & PRFL_24BITCOORD) +// return MSG_ReadCoord24(); +// else return MSG_ReadCoord16(); +// } +// +// float MSG_ReadAngle(unsigned int flags) { +// if (flags & PRFL_FLOATANGLE) +// return MSG_ReadFloat(); +// else if (flags & PRFL_SHORTANGLE) +// return MSG_ReadShort() * (360.0 / 65536); +// else return MSG_ReadChar() * (360.0 / 256); +// } +// +// //johnfitz -- for PROTOCOL_FITZQUAKE +// float MSG_ReadAngle16(unsigned int flags) { +// if (flags & PRFL_FLOATANGLE) +// return MSG_ReadFloat(); // make sure +// else return MSG_ReadShort() * (360.0 / 65536); +// } +// +// //johnfitz //=========================================================================== diff --git a/Quake/common.hpp b/Quake/common.hpp index 90ed66e..0efad68 100644 --- a/Quake/common.hpp +++ b/Quake/common.hpp @@ -148,31 +148,6 @@ extern float (*LittleFloat) (float l); //============================================================================ -void MSG_WriteChar (sizebuf_t *sb, int c); -void MSG_WriteByte (sizebuf_t *sb, int c); -void MSG_WriteShort (sizebuf_t *sb, int c); -void MSG_WriteLong (sizebuf_t *sb, int c); -void MSG_WriteFloat (sizebuf_t *sb, float f); -void MSG_WriteString (sizebuf_t *sb, const char *s); -void MSG_WriteCoord (sizebuf_t *sb, float f, unsigned int flags); -void MSG_WriteAngle (sizebuf_t *sb, float f, unsigned int flags); -void MSG_WriteAngle16 (sizebuf_t *sb, float f, unsigned int flags); //johnfitz - -extern int msg_readcount; -extern bool msg_badread; // set if a read goes beyond end of message - -void MSG_BeginReading (void); -int MSG_ReadChar (void); -int MSG_ReadByte (void); -int MSG_ReadShort (void); -int MSG_ReadLong (void); -float MSG_ReadFloat (void); -const char *MSG_ReadString (void); - -float MSG_ReadCoord (unsigned int flags); -float MSG_ReadAngle (unsigned int flags); -float MSG_ReadAngle16 (unsigned int flags); //johnfitz - //============================================================================ diff --git a/Quake/console.cpp b/Quake/console.cpp index 2d0bf11..8bf71f7 100644 --- a/Quake/console.cpp +++ b/Quake/console.cpp @@ -49,10 +49,7 @@ int con_current; // where next message will be printed int con_x; // offset in current line for next print char *con_text = NULL; -convar con_notifytime{"con_notifytime", "3"}; //seconds -convar con_logcenterprint{"con_logcenterprint", "1"}; //johnfitz -char con_lastcenterstring[1024]; //johnfitz #define NUM_CON_TIMES 4 float con_times[NUM_CON_TIMES]; // realtime time the line was generated @@ -74,7 +71,12 @@ namespace console { int total_lines{0}; // total lines in console scrollback int back_scroll{0}; // lines up from bottom to display + std::string last_center_string{}; + namespace { + convar notify_time{"con_notifytime", "3"}; //seconds + convar log_center_print{"con_logcenterprint", "1"}; //johnfitz + void _toggle_console() { toggle_console(); } @@ -150,9 +152,6 @@ namespace console { int l; static int cr; int mask; - bool boundary; - - //console::back_scroll = 0; //johnfitz -- better console scrolling std::istringstream ss{text}; @@ -171,7 +170,7 @@ namespace console { break; } - boundary = true; + bool boundary = true; while (!ss.eof()) { const int c = ss.peek(); @@ -292,8 +291,8 @@ namespace console { info("Console initialized.\n"); - con_notifytime.inscribe(); - con_logcenterprint.inscribe(); //johnfitz + notify_time.inscribe(); + log_center_print.inscribe(); //johnfitz command::add("toggleconsole", _toggle_console); command::add("messagemode", _message_mode); @@ -444,6 +443,49 @@ namespace console { SCR_EndLoadingPlaque(); memset(con_times, 0, sizeof(con_times)); } + + void check_resize() { + int i, j; + + const int width = (vid.conwidth >> 3) - 2; //johnfitz -- use vid.conwidth instead of vid.width + + if (width == con_linewidth) + return; + + const int oldwidth = con_linewidth; + con_linewidth = width; + const int oldtotallines = total_lines; + total_lines = con_buffersize / con_linewidth; //johnfitz -- con_buffersize replaces CON_TEXTSIZE + int numlines = oldtotallines; + + if (total_lines < numlines) + numlines = total_lines; + + int numchars = oldwidth; + + if (con_linewidth < numchars) + numchars = con_linewidth; + + const int mark = Hunk_LowMark(); //johnfitz + char *tbuf = (char *) Hunk_Alloc(con_buffersize); //johnfitz + + std::memcpy(tbuf, con_text, con_buffersize); //johnfitz -- con_buffersize replaces CON_TEXTSIZE + std::memset(con_text, ' ', con_buffersize); //johnfitz -- con_buffersize replaces CON_TEXTSIZE + + for (i = 0; i < numlines; i++) { + for (j = 0; j < numchars; j++) { + con_text[(total_lines - 1 - i) * con_linewidth + j] = + tbuf[((con_current - i + oldtotallines) % oldtotallines) * oldwidth + j]; + } + } + + Hunk_FreeToLowMark(mark); //johnfitz + + Con_ClearNotify(); + + back_scroll = 0; + con_current = total_lines - 1; + } } @@ -488,58 +530,6 @@ void Con_ClearNotify(void) { } -/* -================ -Con_CheckResize - -If the line width has changed, reformat the buffer. -================ -*/ -void Con_CheckResize(void) { - int i, j, width, oldwidth, oldtotallines, numlines, numchars; - char *tbuf; //johnfitz -- tbuf no longer a static array - int mark; //johnfitz - - width = (vid.conwidth >> 3) - 2; //johnfitz -- use vid.conwidth instead of vid.width - - if (width == con_linewidth) - return; - - oldwidth = con_linewidth; - con_linewidth = width; - oldtotallines = console::total_lines; - console::total_lines = con_buffersize / con_linewidth; //johnfitz -- con_buffersize replaces CON_TEXTSIZE - numlines = oldtotallines; - - if (console::total_lines < numlines) - numlines = console::total_lines; - - numchars = oldwidth; - - if (con_linewidth < numchars) - numchars = con_linewidth; - - mark = Hunk_LowMark(); //johnfitz - tbuf = (char *) Hunk_Alloc(con_buffersize); //johnfitz - - std::memcpy(tbuf, con_text, con_buffersize); //johnfitz -- con_buffersize replaces CON_TEXTSIZE - std::memset(con_text, ' ', con_buffersize); //johnfitz -- con_buffersize replaces CON_TEXTSIZE - - for (i = 0; i < numlines; i++) { - for (j = 0; j < numchars; j++) { - con_text[(console::total_lines - 1 - i) * con_linewidth + j] = - tbuf[((con_current - i + oldtotallines) % oldtotallines) * oldwidth + j]; - } - } - - Hunk_FreeToLowMark(mark); //johnfitz - - Con_ClearNotify(); - - console::back_scroll = 0; - con_current = console::total_lines - 1; -} - // borrowed from uhexen2 by S.A. for new procs, LOG_Init, LOG_Close @@ -569,15 +559,15 @@ Con_LogCenterPrint -- johnfitz -- echo centerprint message to the console ================== */ void Con_LogCenterPrint(const char *str) { - if (!strcmp(str, con_lastcenterstring)) + if (str == console::last_center_string) return; //ignore duplicates - if (cl.gametype == GAME_DEATHMATCH && con_logcenterprint.value != 2) + if (cl.gametype == GAME_DEATHMATCH && console::log_center_print.value != 2) return; //don't log in deathmatch - strcpy(con_lastcenterstring, str); + console::last_center_string = str; - if (con_logcenterprint.value) { + if (console::log_center_print.value != 0) { console::info("%s", Con_Quakebar(40)); console::center_print(40, "%s\n", str); console::info("%s", Con_Quakebar(40)); @@ -931,7 +921,7 @@ void Con_DrawNotify(void) { if (time == 0) continue; time = realtime - time; - if (time > con_notifytime.value) + if (time > console::notify_time.value) continue; text = con_text + (i % console::total_lines) * con_linewidth; diff --git a/Quake/console.hpp b/Quake/console.hpp index 985c3e9..26c0b0f 100644 --- a/Quake/console.hpp +++ b/Quake/console.hpp @@ -23,8 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef __CONSOLE_H #define __CONSOLE_H -extern char con_lastcenterstring[]; //johnfitz - void Con_DrawCharacter(int cx, int line, int num); namespace console { @@ -32,6 +30,7 @@ namespace console { extern int back_scroll; extern bool forced_up; extern bool initialized; + extern std::string last_center_string; void init(); @@ -50,9 +49,10 @@ namespace console { void safe_print(const char *fmt, ...); void toggle_console(); -} -void Con_CheckResize(void); + void check_resize(); + +} void Con_DrawConsole(int lines, bool drawinput); @@ -62,10 +62,6 @@ void Con_ClearNotify(void); void Con_NotifyBox(const char *text); // during startup for sound / cd warnings -void Con_Show(void); - -void Con_Hide(void); - const char *Con_Quakebar(int len); void Con_TabComplete(void); diff --git a/Quake/gl_fog.cpp b/Quake/gl_fog.cpp index 85ef862..b759de6 100644 --- a/Quake/gl_fog.cpp +++ b/Quake/gl_fog.cpp @@ -92,11 +92,11 @@ handle an SVC_FOG message from server void Fog_ParseServerMessage(void) { float density, red, green, blue, time; - density = MSG_ReadByte() / 255.0; - red = MSG_ReadByte() / 255.0; - green = MSG_ReadByte() / 255.0; - blue = MSG_ReadByte() / 255.0; - time = MSG_ReadShort() / 100.0; + density = net_message.read_byte().value() / 255.0; + red = net_message.read_byte().value() / 255.0; + green = net_message.read_byte().value() / 255.0; + blue = net_message.read_byte().value() / 255.0; + time = net_message.read_short().value() / 100.0; if (time < 0.0f) time = 0.0f; Fog_Update(density, red, green, blue, time); diff --git a/Quake/gl_screen.cpp b/Quake/gl_screen.cpp index 3f7a241..2f1e83f 100644 --- a/Quake/gl_screen.cpp +++ b/Quake/gl_screen.cpp @@ -647,7 +647,7 @@ void SCR_SetUpToDrawConsole(void) { float timescale, conspeed; //johnfitz - Con_CheckResize(); + console::check_resize(); if (scr_drawloading) return; // never a console with loading plaque diff --git a/Quake/host.cpp b/Quake/host.cpp index 8089038..c19458f 100644 --- a/Quake/host.cpp +++ b/Quake/host.cpp @@ -333,8 +333,8 @@ void SV_ClientPrintf(const char *fmt, ...) { q_vsnprintf(string, sizeof(string), fmt, argptr); va_end(argptr); - MSG_WriteByte(&host_client->message, svc_print); - MSG_WriteString(&host_client->message, string); + host_client->message.write_byte(svc_print); + host_client->message.write_string(string); } /* @@ -355,8 +355,8 @@ void SV_BroadcastPrintf(const char *fmt, ...) { for (i = 0; i < svs.maxclients; i++) { if (svs.clients[i].active && svs.clients[i].spawned) { - MSG_WriteByte(&svs.clients[i].message, svc_print); - MSG_WriteString(&svs.clients[i].message, string); + svs.clients[i].message.write_byte(svc_print); + svs.clients[i].message.write_string(string); } } } @@ -376,8 +376,8 @@ void Host_ClientCommands(const char *fmt, ...) { q_vsnprintf(string, sizeof(string), fmt, argptr); va_end(argptr); - MSG_WriteByte(&host_client->message, svc_stufftext); - MSG_WriteString(&host_client->message, string); + host_client->message.write_byte(svc_stufftext); + host_client->message.write_string(string); } /* @@ -396,8 +396,8 @@ void SV_DropClient(bool crash) { if (!crash) { // send any final messages (don't check for errors) if (NET_CanSendMessage(host_client->netconnection)) { - MSG_WriteByte(&host_client->message, svc_disconnect); - NET_SendMessage(host_client->netconnection, &host_client->message); + host_client->message.write_byte(svc_disconnect); + NET_SendMessage(host_client->netconnection, host_client->message); } if (host_client->edict && host_client->spawned) { @@ -426,15 +426,15 @@ void SV_DropClient(bool crash) { for (i = 0, client = svs.clients; i < svs.maxclients; i++, client++) { if (!client->active) continue; - MSG_WriteByte(&client->message, svc_updatename); - MSG_WriteByte(&client->message, host_client - svs.clients); - MSG_WriteString(&client->message, ""); - MSG_WriteByte(&client->message, svc_updatefrags); - MSG_WriteByte(&client->message, host_client - svs.clients); - MSG_WriteShort(&client->message, 0); - MSG_WriteByte(&client->message, svc_updatecolors); - MSG_WriteByte(&client->message, host_client - svs.clients); - MSG_WriteByte(&client->message, 0); + client->message.write_byte(svc_updatename); + client->message.write_byte(host_client - svs.clients); + client->message.write_string(""); + client->message.write_byte(svc_updatefrags); + client->message.write_byte(host_client - svs.clients); + client->message.write_short(0); + client->message.write_byte(svc_updatecolors); + client->message.write_byte(host_client - svs.clients); + client->message.write_byte(0); } } @@ -448,8 +448,7 @@ This only happens at the end of a game, not between levels void Host_ShutdownServer(bool crash) { int i; int count; - sizebuf_t buf; - byte message[4]; + net::msg msg{}; double start; if (!sv.active) @@ -466,10 +465,10 @@ void Host_ShutdownServer(bool crash) { do { count = 0; for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { - if (host_client->active && host_client->message.cursize) { + if (host_client->active && !host_client->message.empty()) { if (NET_CanSendMessage(host_client->netconnection)) { - NET_SendMessage(host_client->netconnection, &host_client->message); - SZ_Clear(&host_client->message); + NET_SendMessage(host_client->netconnection, host_client->message); + host_client->message.clear(); } else { NET_GetMessage(host_client->netconnection); count++; @@ -481,11 +480,9 @@ void Host_ShutdownServer(bool crash) { } while (count); // make sure all the clients know we're disconnecting - buf.data = message; - buf.maxsize = 4; - buf.cursize = 0; - MSG_WriteByte(&buf, svc_disconnect); - count = NET_SendToAll(&buf, 5.0); + + msg.write_byte(svc_disconnect); + count = NET_SendToAll(msg, 5.0); if (count) console::info("Host_ShutdownServer: NET_SendToAll failed for %u clients\n", count); diff --git a/Quake/host_cmd.cpp b/Quake/host_cmd.cpp index 1b9cf2b..efd9684 100644 --- a/Quake/host_cmd.cpp +++ b/Quake/host_cmd.cpp @@ -1203,9 +1203,9 @@ static void Host_Name_f(void) { host_client->edict->v.netname = PR_SetEngineString(host_client->name); // send notification to all clients - MSG_WriteByte(&sv.reliable_datagram, svc_updatename); - MSG_WriteByte(&sv.reliable_datagram, host_client - svs.clients); - MSG_WriteString(&sv.reliable_datagram, host_client->name); + sv.reliable_datagram.write_byte(svc_updatename); + sv.reliable_datagram.write_byte(host_client - svs.clients); + sv.reliable_datagram.write_string(host_client->name); } static void Host_Say(bool teamonly) { @@ -1383,9 +1383,9 @@ static void Host_Color_f(void) { host_client->edict->v.team = bottom + 1; // send notification to all clients - MSG_WriteByte(&sv.reliable_datagram, svc_updatecolors); - MSG_WriteByte(&sv.reliable_datagram, host_client - svs.clients); - MSG_WriteByte(&sv.reliable_datagram, host_client->colors); + sv.reliable_datagram.write_byte(svc_updatecolors); + sv.reliable_datagram.write_byte(host_client - svs.clients); + sv.reliable_datagram.write_byte(host_client->colors); } /* @@ -1438,8 +1438,8 @@ static void Host_Pause_f(void) { } // send notification to all clients - MSG_WriteByte(&sv.reliable_datagram, svc_setpause); - MSG_WriteByte(&sv.reliable_datagram, sv.paused); + sv.reliable_datagram.write_byte(svc_setpause); + sv.reliable_datagram.write_byte(sv.paused); } } @@ -1514,49 +1514,53 @@ static void Host_Spawn_f(void) { } // send all current names, colors, and frag counts - SZ_Clear(&host_client->message); + host_client->message.clear(); // send time of update - MSG_WriteByte(&host_client->message, svc_time); - MSG_WriteFloat(&host_client->message, sv.time); + host_client->message.write_byte(svc_time); + host_client->message.write_float(sv.time); for (i = 0, client = svs.clients; i < svs.maxclients; i++, client++) { - MSG_WriteByte(&host_client->message, svc_updatename); - MSG_WriteByte(&host_client->message, i); - MSG_WriteString(&host_client->message, client->name); - MSG_WriteByte(&host_client->message, svc_updatefrags); - MSG_WriteByte(&host_client->message, i); - MSG_WriteShort(&host_client->message, client->old_frags); - MSG_WriteByte(&host_client->message, svc_updatecolors); - MSG_WriteByte(&host_client->message, i); - MSG_WriteByte(&host_client->message, client->colors); + host_client->message.write_byte(svc_updatename); + host_client->message.write_byte(i); + host_client->message.write_string(client->name); + host_client->message.write_byte(svc_updatefrags); + host_client->message.write_byte(i); + host_client->message.write_short(client->old_frags); + host_client->message.write_byte(svc_updatecolors); + host_client->message.write_byte(i); + host_client->message.write_byte(client->colors); } // send all current light styles for (i = 0; i < MAX_LIGHTSTYLES; i++) { - MSG_WriteByte(&host_client->message, svc_lightstyle); - MSG_WriteByte(&host_client->message, (char) i); - MSG_WriteString(&host_client->message, sv.lightstyles[i]); + host_client->message.write_byte(svc_lightstyle); + host_client->message.write_byte((char) i); + if (sv.lightstyles[i]) { + host_client->message.write_string(sv.lightstyles[i]); + } else { + host_client->message.write_string("m"); // TODO: HACK + } } // // send some stats // - MSG_WriteByte(&host_client->message, svc_updatestat); - MSG_WriteByte(&host_client->message, STAT_TOTALSECRETS); - MSG_WriteLong(&host_client->message, pr_global_struct->total_secrets); + host_client->message.write_byte(svc_updatestat); + host_client->message.write_byte(STAT_TOTALSECRETS); + host_client->message.write_long(pr_global_struct->total_secrets); - MSG_WriteByte(&host_client->message, svc_updatestat); - MSG_WriteByte(&host_client->message, STAT_TOTALMONSTERS); - MSG_WriteLong(&host_client->message, pr_global_struct->total_monsters); + host_client->message.write_byte(svc_updatestat); + host_client->message.write_byte(STAT_TOTALMONSTERS); + host_client->message.write_long(pr_global_struct->total_monsters); - MSG_WriteByte(&host_client->message, svc_updatestat); - MSG_WriteByte(&host_client->message, STAT_SECRETS); - MSG_WriteLong(&host_client->message, pr_global_struct->found_secrets); + host_client->message.write_byte(svc_updatestat); + host_client->message.write_byte(STAT_SECRETS); + host_client->message.write_long(pr_global_struct->found_secrets); - MSG_WriteByte(&host_client->message, svc_updatestat); - MSG_WriteByte(&host_client->message, STAT_MONSTERS); - MSG_WriteLong(&host_client->message, pr_global_struct->killed_monsters); + host_client->message.write_byte(svc_updatestat); + host_client->message.write_byte(STAT_MONSTERS); + host_client->message.write_long(pr_global_struct->killed_monsters); // // send a fixangle @@ -1565,15 +1569,15 @@ static void Host_Spawn_f(void) { // and it won't happen if the game was just loaded, so you wind up // with a permanent head tilt ent = EDICT_NUM(1 + (host_client - svs.clients)); - MSG_WriteByte(&host_client->message, svc_setangle); + host_client->message.write_byte(svc_setangle); for (i = 0; i < 2; i++) - MSG_WriteAngle(&host_client->message, ent->v.angles[i], sv.protocolflags); - MSG_WriteAngle(&host_client->message, 0, sv.protocolflags); + host_client->message.write_angle(ent->v.angles[i], static_cast(sv.protocolflags)); + host_client->message.write_angle(0, static_cast(sv.protocolflags)); - SV_WriteClientdataToMessage(sv_player, &host_client->message); + SV_WriteClientdataToMessage(sv_player, host_client->message); - MSG_WriteByte(&host_client->message, svc_signonnum); - MSG_WriteByte(&host_client->message, 3); + host_client->message.write_byte(svc_signonnum); + host_client->message.write_byte(3); host_client->sendsignon = PRESPAWN_FLUSH; } diff --git a/Quake/menu.cpp b/Quake/menu.cpp index 0555cdb..a84179f 100644 --- a/Quake/menu.cpp +++ b/Quake/menu.cpp @@ -1061,7 +1061,7 @@ void M_AdjustSliders(int dir) { convar::set_value("bgmvolume", f); break; case OPT_MUSICEXT: // enable external music vs cdaudio - convar::set("bgm_extmusic", music::bgm_extmusic.value ? "0" : "1"); + convar::set("bgm_extmusic", music::extmusic.value ? "0" : "1"); break; case OPT_SNDVOL: // sfx volume f = sfxvolume.value + dir * 0.1; @@ -1207,7 +1207,7 @@ void M_Options_Draw(void) { // OPT_MUSICEXT: M_Print(16, 32 + 8 * OPT_MUSICEXT, " External Music"); - M_DrawCheckbox(220, 32 + 8 * OPT_MUSICEXT, music::bgm_extmusic.value); + M_DrawCheckbox(220, 32 + 8 * OPT_MUSICEXT, music::extmusic.value); // OPT_ALWAYRUN: M_Print(16, 32 + 8 * OPT_ALWAYRUN, " Always Run"); diff --git a/Quake/net.hpp b/Quake/net.hpp index bd2b37e..f5e5396 100644 --- a/Quake/net.hpp +++ b/Quake/net.hpp @@ -35,6 +35,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define NET_NAMELEN 64 #define NET_MAXMESSAGE 64000 /* ericw -- was 32000 */ +#include "net_msg.hpp" extern int DEFAULTnet_hostport; extern int net_hostport; @@ -42,7 +43,7 @@ extern int net_hostport; extern convar hostname; extern double net_time; -extern sizebuf_t net_message; +extern net::msg net_message; extern int net_activeconnections; @@ -69,14 +70,14 @@ int NET_GetMessage (struct qsocket_s *sock); // returns 2 if an unreliable message was received // returns -1 if the connection died -int NET_SendMessage (struct qsocket_s *sock, sizebuf_t *data); -int NET_SendUnreliableMessage (struct qsocket_s *sock, sizebuf_t *data); +int NET_SendMessage (struct qsocket_s *sock, net::msg &msg); +int NET_SendUnreliableMessage (struct qsocket_s *sock, net::msg &msg); // returns 0 if the message connot be delivered reliably, but the connection // is still considered valid // returns 1 if the message was sent properly // returns -1 if the connection died -int NET_SendToAll(sizebuf_t *data, double blocktime); +int NET_SendToAll(net::msg &msg, double blocktime); // This is a reliable *blocking* send to all attached clients. void NET_Close (struct qsocket_s *sock); diff --git a/Quake/net_defs.hpp b/Quake/net_defs.hpp index a9447b2..aef3607 100644 --- a/Quake/net_defs.hpp +++ b/Quake/net_defs.hpp @@ -202,8 +202,8 @@ typedef struct qsocket_t *(*Connect) (const char *host); qsocket_t *(*CheckNewConnections) (void); int (*QGetMessage) (qsocket_t *sock); - int (*QSendMessage) (qsocket_t *sock, sizebuf_t *data); - int (*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data); + int (*QSendMessage) (qsocket_t *sock, net::msg &msg); + int (*SendUnreliableMessage) (qsocket_t *sock, net::msg &msg); bool (*CanSendMessage) (qsocket_t *sock); bool (*CanSendUnreliableMessage) (qsocket_t *sock); void (*Close) (qsocket_t *sock); diff --git a/Quake/net_dgrm.cpp b/Quake/net_dgrm.cpp index c6cc40a..3add1fd 100644 --- a/Quake/net_dgrm.cpp +++ b/Quake/net_dgrm.cpp @@ -121,7 +121,7 @@ static void NET_Ban_f(void) { #endif // BAN_TEST -int Datagram_SendMessage(qsocket_t *sock, sizebuf_t *data) { +int Datagram_SendMessage(qsocket_t *sock, net::msg &msg) { unsigned int packetLen; unsigned int dataLen; unsigned int eom; @@ -137,11 +137,11 @@ int Datagram_SendMessage(qsocket_t *sock, sizebuf_t *data) { Sys_Error("SendMessage: called with canSend == false"); #endif - std::memcpy(sock->sendMessage, data->data, data->cursize); - sock->sendMessageLength = data->cursize; + std::memcpy(sock->sendMessage, msg.data(), msg.size()); + sock->sendMessageLength = msg.size(); - if (data->cursize <= MAX_DATAGRAM) { - dataLen = data->cursize; + if (msg.size() <= MAX_DATAGRAM) { + dataLen = msg.size(); eom = NETFLAG_EOM; } else { dataLen = MAX_DATAGRAM; @@ -235,7 +235,7 @@ bool Datagram_CanSendUnreliableMessage(qsocket_t *sock) { } -int Datagram_SendUnreliableMessage(qsocket_t *sock, sizebuf_t *data) { +int Datagram_SendUnreliableMessage(qsocket_t *sock, net::msg &msg) { int packetLen; #ifdef DEBUG @@ -246,11 +246,11 @@ int Datagram_SendUnreliableMessage(qsocket_t *sock, sizebuf_t *data) { Sys_Error("Datagram_SendUnreliableMessage: message too big: %u", data->cursize); #endif - packetLen = NET_HEADERSIZE + data->cursize; + packetLen = NET_HEADERSIZE + msg.size(); packetBuffer.length = BigLong(packetLen | NETFLAG_UNRELIABLE); packetBuffer.sequence = BigLong(sock->unreliableSendSequence++); - std::memcpy(packetBuffer.data, data->data, data->cursize); + std::memcpy(packetBuffer.data, msg.data(), msg.size()); if (sfunc.Write(sock->socket, (byte *) &packetBuffer, packetLen, &sock->addr) == -1) return -1; @@ -324,8 +324,8 @@ int Datagram_GetMessage(qsocket_t *sock) { length -= NET_HEADERSIZE; - SZ_Clear(&net_message); - SZ_Write(&net_message, packetBuffer.data, length); + net_message.clear(); + net_message.write(packetBuffer.data, length); ret = 2; break; @@ -369,9 +369,9 @@ int Datagram_GetMessage(qsocket_t *sock) { length -= NET_HEADERSIZE; if (flags & NETFLAG_EOM) { - SZ_Clear(&net_message); - SZ_Write(&net_message, sock->receiveMessage, sock->receiveMessageLength); - SZ_Write(&net_message, packetBuffer.data, length); + net_message.clear(); + net_message.write(sock->receiveMessage, sock->receiveMessageLength); + net_message.write(packetBuffer.data, length); sock->receiveMessageLength = 0; ret = 1; @@ -482,15 +482,19 @@ static void Test_Poll(void *unused) { net_landriverlevel = testDriver; while (1) { - len = dfunc.Read(testSocket, net_message.data, net_message.maxsize, &clientaddr); - if (len < (int) sizeof(int)) + const auto buf = new std::uint8_t[MAX_DATAGRAM]; + len = dfunc.Read(testSocket, &buf[0], MAX_DATAGRAM, &clientaddr); + if (len < (int) sizeof(int)) { + delete []buf; break; + } + net_message.write(buf, len); + delete []buf; - net_message.cursize = len; - MSG_BeginReading(); - control = BigLong(*((int *) net_message.data)); - MSG_ReadLong(); + net_message.begin_reading(); + control = BigLong(*((int *) net_message.data())); + net_message.read_long(); if (control == -1) break; if ((control & (~NETFLAG_LENGTH_MASK)) != (int) NETFLAG_CTL) @@ -498,18 +502,18 @@ static void Test_Poll(void *unused) { if ((control & NETFLAG_LENGTH_MASK) != len) break; - if (MSG_ReadByte() != CCREP_PLAYER_INFO) + if (net_message.read_byte().value() != CCREP_PLAYER_INFO) Sys_Error("Unexpected response to Player Info request\n"); - MSG_ReadByte(); /* playerNumber */ - std::strcpy(name, MSG_ReadString()); - colors = MSG_ReadLong(); - frags = MSG_ReadLong(); - connectTime = MSG_ReadLong(); - std::strcpy(address, MSG_ReadString()); + net_message.read_byte().value(); /* playerNumber */ + std::strcpy(name, net_message.read_string().value().c_str()); + colors = net_message.read_long().value(); + frags = net_message.read_long().value(); + connectTime = net_message.read_long().value(); + std::strcpy(address, net_message.read_string().value().c_str()); console::info("%s\n frags:%3i colors:%d %d time:%d\n %s\n", name, frags, colors >> 4, colors & 0x0f, - connectTime / 60, address); + connectTime / 60, address); } testPollCount--; @@ -572,15 +576,15 @@ JustDoIt: testDriver = net_landriverlevel; for (n = 0; n < maxusers; n++) { - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREQ_PLAYER_INFO); - MSG_WriteByte(&net_message, n); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(testSocket, net_message.data, net_message.cursize, &sendaddr); + net_message.write_long(0); + net_message.write_byte(CCREQ_PLAYER_INFO); + net_message.write_byte(n); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(testSocket, net_message.data(), net_message.size(), &sendaddr); } - SZ_Clear(&net_message); + net_message.clear(); SchedulePollProcedure(&testPollProcedure, 0.1); } @@ -603,15 +607,19 @@ static void Test2_Poll(void *unused) { net_landriverlevel = test2Driver; name[0] = 0; - len = dfunc.Read(test2Socket, net_message.data, net_message.maxsize, &clientaddr); - if (len < (int) sizeof(int)) + auto buf = new std::uint8_t[MAX_DATAGRAM]; + len = dfunc.Read(test2Socket, &buf[0], MAX_DATAGRAM, &clientaddr); + if (len < (int) sizeof(int)) { + delete []buf; goto Reschedule; + } + net_message.write(buf, len); + delete []buf; - net_message.cursize = len; - MSG_BeginReading(); - control = BigLong(*((int *) net_message.data)); - MSG_ReadLong(); + net_message.begin_reading(); + control = BigLong(*((int *) net_message.data())); + net_message.read_long(); if (control == -1) goto Error; if ((control & (~NETFLAG_LENGTH_MASK)) != (int) NETFLAG_CTL) @@ -619,24 +627,24 @@ static void Test2_Poll(void *unused) { if ((control & NETFLAG_LENGTH_MASK) != len) goto Error; - if (MSG_ReadByte() != CCREP_RULE_INFO) + if (net_message.read_byte().value() != CCREP_RULE_INFO) goto Error; - std::strcpy(name, MSG_ReadString()); + std::strcpy(name, net_message.read_string().value().c_str()); if (name[0] == 0) goto Done; - std::strcpy(value, MSG_ReadString()); + std::strcpy(value, net_message.read_string().value().c_str()); console::info("%-16.16s %-16.16s\n", name, value); - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREQ_RULE_INFO); - MSG_WriteString(&net_message, name); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(test2Socket, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREQ_RULE_INFO); + net_message.write_string(name); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(test2Socket, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); Reschedule: SchedulePollProcedure(&test2PollProcedure, 0.05); @@ -697,14 +705,14 @@ JustDoIt: test2InProgress = true; test2Driver = net_landriverlevel; - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREQ_RULE_INFO); - MSG_WriteString(&net_message, ""); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(test2Socket, net_message.data, net_message.cursize, &sendaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREQ_RULE_INFO); + net_message.write_string(""); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(test2Socket, net_message.data(), net_message.size(), &sendaddr); + net_message.clear(); SchedulePollProcedure(&test2PollProcedure, 0.05); } @@ -793,16 +801,20 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { if (acceptsock == INVALID_SOCKET) return NULL; - SZ_Clear(&net_message); + net_message.clear(); - len = dfunc.Read(acceptsock, net_message.data, net_message.maxsize, &clientaddr); - if (len < (int) sizeof(int)) - return NULL; - net_message.cursize = len; + auto buf = new byte[MAX_DATAGRAM]; + len = dfunc.Read(acceptsock, &buf[0], MAX_DATAGRAM, &clientaddr); + if (len < (int) sizeof(int)) { + delete []buf; + return nullptr; + } + net_message.write(buf, len); + delete []buf; - MSG_BeginReading(); - control = BigLong(*((int *) net_message.data)); - MSG_ReadLong(); + net_message.begin_reading(); + control = BigLong(*((int *) net_message.data())); + net_message.read_long(); if (control == -1) return NULL; if ((control & (~NETFLAG_LENGTH_MASK)) != (int) NETFLAG_CTL) @@ -810,25 +822,25 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { if ((control & NETFLAG_LENGTH_MASK) != len) return NULL; - command = MSG_ReadByte(); + command = net_message.read_byte().value(); if (command == CCREQ_SERVER_INFO) { - if (std::strcmp(MSG_ReadString(), "QUAKE") != 0) + if (std::strcmp(net_message.read_string().value().c_str(), "QUAKE") != 0) return NULL; - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_SERVER_INFO); + net_message.write_long(0); + net_message.write_byte(CCREP_SERVER_INFO); dfunc.GetSocketAddr(acceptsock, &newaddr); - MSG_WriteString(&net_message, dfunc.AddrToString(&newaddr)); - MSG_WriteString(&net_message, hostname.string); - MSG_WriteString(&net_message, sv.name); - MSG_WriteByte(&net_message, net_activeconnections); - MSG_WriteByte(&net_message, svs.maxclients); - MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_string(dfunc.AddrToString(&newaddr)); + net_message.write_string(hostname.string); + net_message.write_string(sv.name); + net_message.write_byte(net_activeconnections); + net_message.write_byte(svs.maxclients); + net_message.write_byte(NET_PROTOCOL_VERSION); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } @@ -838,7 +850,7 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { int clientNumber; client_t *client; - playerNumber = MSG_ReadByte(); + playerNumber = net_message.read_byte().value(); activeNumber = -1; for (clientNumber = 0, client = svs.clients; clientNumber < svs.maxclients; clientNumber++, client++) { @@ -852,42 +864,40 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { if (clientNumber == svs.maxclients) return NULL; - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_PLAYER_INFO); - MSG_WriteByte(&net_message, playerNumber); - MSG_WriteString(&net_message, client->name); - MSG_WriteLong(&net_message, client->colors); - MSG_WriteLong(&net_message, (int) client->edict->v.frags); - MSG_WriteLong(&net_message, (int) (net_time - client->netconnection->connecttime)); - MSG_WriteString(&net_message, client->netconnection->address); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREP_PLAYER_INFO); + net_message.write_byte(playerNumber); + net_message.write_string(client->name); + net_message.write_long(client->colors); + net_message.write_long((int) client->edict->v.frags); + net_message.write_long((int) (net_time - client->netconnection->connecttime)); + net_message.write_string(client->netconnection->address); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } if (command == CCREQ_RULE_INFO) { - const char *prevCvarName; - // find the search start location - prevCvarName = MSG_ReadString(); + auto prevCvarName = net_message.read_string().value(); auto var = convar::find_var_after(prevCvarName, std::nullopt); // send the response - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_RULE_INFO); + net_message.write_long(0); + net_message.write_byte(CCREP_RULE_INFO); if (var) { - MSG_WriteString(&net_message, var.value()->name.c_str()); - MSG_WriteString(&net_message, var.value()->string); + net_message.write_string(var.value()->name.c_str()); + net_message.write_string(var.value()->string); } - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } @@ -895,18 +905,18 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { if (command != CCREQ_CONNECT) return NULL; - if (std::strcmp(MSG_ReadString(), "QUAKE") != 0) + if (std::strcmp(net_message.read_string().value().c_str(), "QUAKE") != 0) return NULL; - if (MSG_ReadByte() != NET_PROTOCOL_VERSION) { - SZ_Clear(&net_message); + if (net_message.read_byte().value() != NET_PROTOCOL_VERSION) { + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_REJECT); - MSG_WriteString(&net_message, "Incompatible version.\n"); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREP_REJECT); + net_message.write_string("Incompatible version.\n"); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } @@ -916,14 +926,14 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { in_addr_t testAddr; testAddr = ((struct sockaddr_in *) &clientaddr)->sin_addr.s_addr; if ((testAddr & banMask.s_addr) == banAddr.s_addr) { - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_REJECT); - MSG_WriteString(&net_message, "You have been banned.\n"); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREP_REJECT); + net_message.write_string("You have been banned.\n"); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } } @@ -938,15 +948,15 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { // is this a duplicate connection reqeust? if (ret == 0 && net_time - s->connecttime < 2.0) { // yes, so send a duplicate reply - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_ACCEPT); + net_message.write_long(0); + net_message.write_byte(CCREP_ACCEPT); dfunc.GetSocketAddr(s->socket, &newaddr); - MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr)); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_long(dfunc.GetSocketPort(&newaddr)); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } // it's somebody coming back in from a crash/disconnect @@ -960,14 +970,14 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { sock = NET_NewQSocket(); if (sock == NULL) // no room; try to let him know { - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_REJECT); - MSG_WriteString(&net_message, "Server is full.\n"); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREP_REJECT); + net_message.write_string("Server is full.\n"); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return NULL; } @@ -992,16 +1002,16 @@ static qsocket_t *_Datagram_CheckNewConnections(void) { std::strcpy(sock->address, dfunc.AddrToString(&clientaddr)); // send him back the info about the server connection he has been allocated - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREP_ACCEPT); + net_message.write_long(0); + net_message.write_byte(CCREP_ACCEPT); dfunc.GetSocketAddr(newsock, &newaddr); - MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr)); + net_message.write_long(dfunc.GetSocketPort(&newaddr)); // MSG_WriteString(&net_message, dfunc.AddrToString(&newaddr)); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(acceptsock, net_message.data, net_message.cursize, &clientaddr); - SZ_Clear(&net_message); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(acceptsock, net_message.data(), net_message.size(), &clientaddr); + net_message.clear(); return sock; } @@ -1029,21 +1039,22 @@ static void _Datagram_SearchForHosts(bool xmit) { dfunc.GetSocketAddr(dfunc.controlSock, &myaddr); if (xmit) { - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREQ_SERVER_INFO); - MSG_WriteString(&net_message, "QUAKE"); - MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Broadcast(dfunc.controlSock, net_message.data, net_message.cursize); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREQ_SERVER_INFO); + net_message.write_string("QUAKE"); + net_message.write_byte(NET_PROTOCOL_VERSION); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Broadcast(dfunc.controlSock, net_message.data(), net_message.size()); + net_message.clear(); } - while ((ret = dfunc.Read(dfunc.controlSock, net_message.data, net_message.maxsize, &readaddr)) > 0) { + auto buf = new std::uint8_t[MAX_DATAGRAM]; + while ((ret = dfunc.Read(dfunc.controlSock, &buf[0], MAX_DATAGRAM, &readaddr)) > 0) { if (ret < (int) sizeof(int)) continue; - net_message.cursize = ret; + net_message.write(buf, ret); // don't answer our own query if (dfunc.AddrCompare(&readaddr, &myaddr) >= 0) @@ -1053,9 +1064,9 @@ static void _Datagram_SearchForHosts(bool xmit) { if (hostCacheCount == HOSTCACHESIZE) continue; - MSG_BeginReading(); - control = BigLong(*((int *) net_message.data)); - MSG_ReadLong(); + net_message.begin_reading(); + control = BigLong(*((int *) net_message.data())); + net_message.read_long(); if (control == -1) continue; if ((control & (~NETFLAG_LENGTH_MASK)) != (int) NETFLAG_CTL) @@ -1063,10 +1074,10 @@ static void _Datagram_SearchForHosts(bool xmit) { if ((control & NETFLAG_LENGTH_MASK) != ret) continue; - if (MSG_ReadByte() != CCREP_SERVER_INFO) + if (net_message.read_byte().value() != CCREP_SERVER_INFO) continue; - dfunc.GetAddrFromName(MSG_ReadString(), &readaddr); + dfunc.GetAddrFromName(net_message.read_string().value().c_str(), &readaddr); // search the cache for this server for (n = 0; n < hostCacheCount; n++) { if (dfunc.AddrCompare(&readaddr, &hostcache[n].addr) == 0) @@ -1079,11 +1090,11 @@ static void _Datagram_SearchForHosts(bool xmit) { // add it hostCacheCount++; - std::strcpy(hostcache[n].name, MSG_ReadString()); - std::strcpy(hostcache[n].map, MSG_ReadString()); - hostcache[n].users = MSG_ReadByte(); - hostcache[n].maxusers = MSG_ReadByte(); - if (MSG_ReadByte() != NET_PROTOCOL_VERSION) { + std::strcpy(hostcache[n].name, net_message.read_string().value().c_str()); + std::strcpy(hostcache[n].map, net_message.read_string().value().c_str()); + hostcache[n].users = net_message.read_byte().value(); + hostcache[n].maxusers = net_message.read_byte().value(); + if (net_message.read_byte().value() != NET_PROTOCOL_VERSION) { std::strcpy(hostcache[n].cname, hostcache[n].name); hostcache[n].cname[14] = 0; std::strcpy(hostcache[n].name, "*"); @@ -1110,6 +1121,7 @@ static void _Datagram_SearchForHosts(bool xmit) { } } } + delete []buf; } void Datagram_SearchForHosts(bool xmit) { @@ -1131,7 +1143,7 @@ static qsocket_t *_Datagram_Connect(const char *host) { int reps; double start_time; int control; - const char *reason; + std::string reason; // see if we can resolve the host name if (dfunc.GetAddrFromName(host, &sendaddr) == -1) { @@ -1159,17 +1171,18 @@ static qsocket_t *_Datagram_Connect(const char *host) { start_time = net_time; for (reps = 0; reps < 3; reps++) { - SZ_Clear(&net_message); + net_message.clear(); // save space for the header, filled in later - MSG_WriteLong(&net_message, 0); - MSG_WriteByte(&net_message, CCREQ_CONNECT); - MSG_WriteString(&net_message, "QUAKE"); - MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION); - *((int *) net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK)); - dfunc.Write(newsock, net_message.data, net_message.cursize, &sendaddr); - SZ_Clear(&net_message); + net_message.write_long(0); + net_message.write_byte(CCREQ_CONNECT); + net_message.write_string("QUAKE"); + net_message.write_byte(NET_PROTOCOL_VERSION); + *((int *) net_message.data()) = BigLong(NETFLAG_CTL | (net_message.size() & NETFLAG_LENGTH_MASK)); + dfunc.Write(newsock, net_message.data(), net_message.size(), &sendaddr); + net_message.clear(); + auto buf = new std::uint8_t[MAX_DATAGRAM]; do { - ret = dfunc.Read(newsock, net_message.data, net_message.maxsize, &readaddr); + ret = dfunc.Read(newsock, &buf[0], MAX_DATAGRAM, &readaddr); // if we got something, validate it if (ret > 0) { // is it from the right place? @@ -1187,11 +1200,11 @@ static qsocket_t *_Datagram_Connect(const char *host) { continue; } - net_message.cursize = ret; - MSG_BeginReading(); + net_message.write(buf, ret); + net_message.begin_reading(); - control = BigLong(*((int *) net_message.data)); - MSG_ReadLong(); + control = BigLong(*((int *) net_message.data())); + net_message.read_long(); if (control == -1) { ret = 0; continue; @@ -1206,6 +1219,7 @@ static qsocket_t *_Datagram_Connect(const char *host) { } } } while (ret == 0 && (SetNetTime() - start_time) < 2.5); + delete []buf; if (ret) break; @@ -1217,33 +1231,33 @@ static qsocket_t *_Datagram_Connect(const char *host) { if (ret == 0) { reason = "No Response"; - console::info("%s\n", reason); - std::strcpy(m_return_reason, reason); + console::info("%s\n", reason.c_str()); + std::strcpy(m_return_reason, reason.c_str()); goto ErrorReturn; } if (ret == -1) { reason = "Network Error"; - console::info("%s\n", reason); - std::strcpy(m_return_reason, reason); + console::info("%s\n", reason.c_str()); + std::strcpy(m_return_reason, reason.c_str()); goto ErrorReturn; } - ret = MSG_ReadByte(); + ret = net_message.read_byte().value(); if (ret == CCREP_REJECT) { - reason = MSG_ReadString(); - console::info("%s\n", reason); - q_strlcpy(m_return_reason, reason, sizeof(m_return_reason)); + reason = net_message.read_string().value(); + console::info("%s\n", reason.c_str()); + q_strlcpy(m_return_reason, reason.c_str(), sizeof(m_return_reason)); goto ErrorReturn; } if (ret == CCREP_ACCEPT) { std::memcpy(&sock->addr, &sendaddr, sizeof(struct qsockaddr)); - dfunc.SetSocketPort(&sock->addr, MSG_ReadLong()); + dfunc.SetSocketPort(&sock->addr, net_message.read_long().value()); } else { reason = "Bad Response"; - console::info("%s\n", reason); - std::strcpy(m_return_reason, reason); + console::info("%s\n", reason.c_str()); + std::strcpy(m_return_reason, reason.c_str()); goto ErrorReturn; } @@ -1255,8 +1269,8 @@ static qsocket_t *_Datagram_Connect(const char *host) { // switch the connection to the specified address if (dfunc.Connect(newsock, &sock->addr) == -1) { reason = "Connect to Game failed"; - console::info("%s\n", reason); - std::strcpy(m_return_reason, reason); + console::info("%s\n", reason.c_str()); + std::strcpy(m_return_reason, reason.c_str()); goto ErrorReturn; } diff --git a/Quake/net_dgrm.hpp b/Quake/net_dgrm.hpp index ed09dd1..6779c87 100644 --- a/Quake/net_dgrm.hpp +++ b/Quake/net_dgrm.hpp @@ -28,8 +28,8 @@ void Datagram_SearchForHosts (bool xmit); qsocket_t *Datagram_Connect (const char *host); qsocket_t *Datagram_CheckNewConnections (void); int Datagram_GetMessage (qsocket_t *sock); -int Datagram_SendMessage (qsocket_t *sock, sizebuf_t *data); -int Datagram_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data); +int Datagram_SendMessage (qsocket_t *sock, net::msg &msg); +int Datagram_SendUnreliableMessage (qsocket_t *sock, net::msg &msg); bool Datagram_CanSendMessage (qsocket_t *sock); bool Datagram_CanSendUnreliableMessage (qsocket_t *sock); void Datagram_Close (qsocket_t *sock); diff --git a/Quake/net_loop.cpp b/Quake/net_loop.cpp index 74f7ba8..3f61878 100644 --- a/Quake/net_loop.cpp +++ b/Quake/net_loop.cpp @@ -141,8 +141,8 @@ int Loop_GetMessage (qsocket_t *sock) ret = sock->receiveMessage[0]; length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8); // alignment byte skipped here - SZ_Clear (&net_message); - SZ_Write (&net_message, &sock->receiveMessage[4], length); + net_message.clear(); + net_message.write(&sock->receiveMessage[4], length); length = IntAlign(length + 4); sock->receiveMessageLength -= length; @@ -157,7 +157,7 @@ int Loop_GetMessage (qsocket_t *sock) } -int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data) +int Loop_SendMessage (qsocket_t *sock, net::msg &msg) { byte *buffer; int *bufferLength; @@ -167,7 +167,7 @@ int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data) bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength; - if ((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE) + if ((*bufferLength + msg.size() + 4) > NET_MAXMESSAGE) Sys_Error("Loop_SendMessage: overflow"); buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength; @@ -176,22 +176,22 @@ int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data) *buffer++ = 1; // length - *buffer++ = data->cursize & 0xff; - *buffer++ = data->cursize >> 8; + *buffer++ = msg.size() & 0xff; + *buffer++ = msg.size() >> 8; // align buffer++; // message - std::memcpy(buffer, data->data, data->cursize); - *bufferLength = IntAlign(*bufferLength + data->cursize + 4); + std::memcpy(buffer, msg.data(), msg.size()); + *bufferLength = IntAlign(*bufferLength + msg.size() + 4); sock->canSend = false; return 1; } -int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data) +int Loop_SendUnreliableMessage (qsocket_t *sock, net::msg &msg) { byte *buffer; int *bufferLength; @@ -201,7 +201,7 @@ int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data) bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength; - if ((*bufferLength + data->cursize + sizeof(byte) + sizeof(short)) > NET_MAXMESSAGE) + if ((*bufferLength + msg.size() + sizeof(byte) + sizeof(short)) > NET_MAXMESSAGE) return 0; buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength; @@ -210,15 +210,15 @@ int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data) *buffer++ = 2; // length - *buffer++ = data->cursize & 0xff; - *buffer++ = data->cursize >> 8; + *buffer++ = msg.size() & 0xff; + *buffer++ = msg.size() >> 8; // align buffer++; // message - std::memcpy(buffer, data->data, data->cursize); - *bufferLength = IntAlign(*bufferLength + data->cursize + 4); + std::memcpy(buffer, msg.data(), msg.size()); + *bufferLength = IntAlign(*bufferLength + msg.size() + 4); return 1; } diff --git a/Quake/net_loop.hpp b/Quake/net_loop.hpp index 0817a6a..0ddec69 100644 --- a/Quake/net_loop.hpp +++ b/Quake/net_loop.hpp @@ -29,8 +29,8 @@ void Loop_SearchForHosts (bool xmit); qsocket_t *Loop_Connect (const char *host); qsocket_t *Loop_CheckNewConnections (void); int Loop_GetMessage (qsocket_t *sock); -int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data); -int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data); +int Loop_SendMessage (qsocket_t *sock, net::msg &msg); +int Loop_SendUnreliableMessage (qsocket_t *sock, net::msg &msg); bool Loop_CanSendMessage (qsocket_t *sock); bool Loop_CanSendUnreliableMessage (qsocket_t *sock); void Loop_Close (qsocket_t *sock); diff --git a/Quake/net_main.cpp b/Quake/net_main.cpp index 45911db..8d4c422 100644 --- a/Quake/net_main.cpp +++ b/Quake/net_main.cpp @@ -55,7 +55,7 @@ static void Slist_Poll(void *); static PollProcedure slistSendProcedure = {NULL, 0.0, Slist_Send}; static PollProcedure slistPollProcedure = {NULL, 0.0, Slist_Poll}; -sizebuf_t net_message; +net::msg net_message{}; int net_activeconnections = 0; int messagesSent = 0; @@ -550,7 +550,7 @@ returns 1 if the message was sent properly returns -1 if the connection died ================== */ -int NET_SendMessage(qsocket_t *sock, sizebuf_t *data) { +int NET_SendMessage(qsocket_t *sock, net::msg &msg) { int r; if (!sock) @@ -562,7 +562,7 @@ int NET_SendMessage(qsocket_t *sock, sizebuf_t *data) { } SetNetTime(); - r = sfunc.QSendMessage(sock, data); + r = sfunc.QSendMessage(sock, msg); if (r == 1 && !IS_LOOP_DRIVER(sock->driver)) messagesSent++; @@ -570,7 +570,7 @@ int NET_SendMessage(qsocket_t *sock, sizebuf_t *data) { } -int NET_SendUnreliableMessage(qsocket_t *sock, sizebuf_t *data) { +int NET_SendUnreliableMessage(qsocket_t *sock, net::msg &msg) { int r; if (!sock) @@ -582,7 +582,7 @@ int NET_SendUnreliableMessage(qsocket_t *sock, sizebuf_t *data) { } SetNetTime(); - r = sfunc.SendUnreliableMessage(sock, data); + r = sfunc.SendUnreliableMessage(sock, msg); if (r == 1 && !IS_LOOP_DRIVER(sock->driver)) unreliableMessagesSent++; @@ -611,7 +611,7 @@ bool NET_CanSendMessage(qsocket_t *sock) { } -int NET_SendToAll(sizebuf_t *data, double blocktime) { +int NET_SendToAll(net::msg &msg, double blocktime) { double start; int i; int count = 0; @@ -626,7 +626,7 @@ int NET_SendToAll(sizebuf_t *data, double blocktime) { */ if (host_client->netconnection && host_client->active) { if (IS_LOOP_DRIVER(host_client->netconnection->driver)) { - NET_SendMessage(host_client->netconnection, data); + NET_SendMessage(host_client->netconnection, msg); msg_init[i] = true; msg_sent[i] = true; continue; @@ -647,7 +647,7 @@ int NET_SendToAll(sizebuf_t *data, double blocktime) { if (!msg_init[i]) { if (NET_CanSendMessage(host_client->netconnection)) { msg_init[i] = true; - NET_SendMessage(host_client->netconnection, data); + NET_SendMessage(host_client->netconnection, msg); } else { NET_GetMessage(host_client->netconnection); } @@ -713,8 +713,7 @@ void NET_Init(void) { } // allocate space for network message buffer - SZ_Alloc(&net_message, NET_MAXMESSAGE); - + net_message = net::msg{}; net_messagetimeout.inscribe(); hostname.inscribe(); diff --git a/Quake/net_msg.cpp b/Quake/net_msg.cpp new file mode 100644 index 0000000..d8a9776 --- /dev/null +++ b/Quake/net_msg.cpp @@ -0,0 +1,248 @@ +#include "net_msg.hpp" + +#include + +#include "q_stdinc.hpp" +#include "quakedef.hpp" +#include "common.hpp" +#include "mathlib.hpp" + +namespace net { + void msg::write_lores_coord(const float f) { + write_short(static_cast(std::round(f * 8))); + } + + void msg::write_hires_coord(const float f) { + write_short(static_cast(f)); + write_byte(static_cast(f * 255) % 255); + } + + std::optional msg::read_lores_coord() { + const auto c = read_short(); + + if (!c.has_value()) { + return std::nullopt; + } + + return c.value() * (1.0 / 8); + } + + std::optional msg::read_hires_coord() { + const auto c = read_short(); + const auto d = read_byte(); + + if (!c.has_value()) { + return std::nullopt; + } + + return c.value() + (d.value() * (1.0 / 255)); + } + + void msg::clear() { + _data.clear(); + _eos = false; + _offset = 0; + } + + void msg::write_char(const char c) { + _data.push_back(c); + } + + void msg::write_byte(const std::uint8_t b) { + _data.push_back(b); + } + + void msg::write_short(const short s) { + _data.push_back(s & 0xFF); + _data.push_back((s >> 8) & 0xFF); + } + + void msg::write_long(const int l) { + _data.push_back(l & 0xFF); + _data.push_back((l >> 8) & 0xFF); + _data.push_back((l >> 16) & 0xFF); + _data.push_back((l >> 24) & 0xFF); + } + + void msg::write_float(const float f) { + write_long(std::bit_cast(f)); + } + + void msg::write_string(const std::string_view s) { + if (!s.empty()) { + for (const auto &c: s) { + _data.push_back(c); + } + } + _data.push_back('\0'); + } + + static rmq_flags operator&(rmq_flags lhs, rmq_flags rhs) { + return static_cast(static_cast(lhs) & static_cast(rhs)); + } + + void msg::write_coord(const float f, const rmq_flags flags) { + if ((flags & rmq_flags::float_coord) != rmq_flags::none) { + write_float(f); + } else if ((flags & rmq_flags::int_coord) != rmq_flags::none) { + write_long(static_cast(std::round(f * 16))); + } else if ((flags & rmq_flags::hires_coord) != rmq_flags::none) { + write_hires_coord(f); + } else { + write_lores_coord(f); + } + } + + void msg::write_angle(const float f, const rmq_flags flags) { + if ((flags & rmq_flags::float_angle) != rmq_flags::none) { + write_float(f); + } else if ((flags & rmq_flags::short_angle) != rmq_flags::none) { + const auto rounded = static_cast(std::round(f * 65536.0 / 360.0)); + write_short(static_cast(rounded & 65535)); + } else { + const auto rounded = static_cast(std::round(f * 256.0 / 360.0)); + write_byte(static_cast(rounded & 255)); + } + } + + void msg::write_angle_16(const float f, const rmq_flags flags) { + if ((flags & rmq_flags::float_angle) != rmq_flags::none) { + write_float(f); + } else { + const auto rounded = static_cast(std::round(f * 65536.0 / 360.0)); + write_short(static_cast(rounded & 65535)); + } + } + + void msg::begin_reading() { + _offset = 0; + _eos = false; + } + + std::optional msg::read_char() { + if (_eos || _offset + 1 > _data.size()) { + _eos = true; + return std::nullopt; + } + + return _data[_offset++]; + } + + std::optional msg::read_byte() { + if (_eos || _offset + 1 > _data.size()) { + _eos = true; + return std::nullopt; + } + + return _data[_offset++]; + } + + std::optional msg::read_short() { + if (_eos || _offset + 2 > _data.size()) { + _eos = true; + return std::nullopt; + } + + auto res = static_cast(_data[_offset] + (_data[_offset + 1] << 8)); + + _offset += 2; + + return res; + } + + std::optional msg::read_long() { + if (_eos || _offset + 4 > _data.size()) { + _eos = true; + return std::nullopt; + } + + auto res = _data[_offset] + (_data[_offset + 1] << 8) + (_data[_offset + 2] << 16) + ( + _data[_offset + 3] << 24); + + _offset += 4; + + return res; + } + + + std::optional msg::read_float() { + if (_eos || _offset + 4 > _data.size()) { + _eos = true; + return std::nullopt; + } + + auto res = std::bit_cast(read_long().value()); + + return res; + } + + std::optional msg::read_string() { + std::string res{}; + + if (_eos) { + return std::nullopt; + } + + do { + auto c = read_char(); + if (!c.has_value() || c.value() == 0) { + break; + } + res += c.value(); + } while (!_eos); + + return res; + } + + std::optional msg::read_coord(const rmq_flags flags) { + if ((flags & rmq_flags::float_coord) != rmq_flags::none) { + return read_float(); + } + if ((flags & rmq_flags::int_coord) != rmq_flags::none) { + const auto i = read_long(); + if (!i.has_value()) { + return std::nullopt; + } + return i.value() * (1.0 / 16.0); + } + if ((flags & rmq_flags::hires_coord) != rmq_flags::none) { + return read_hires_coord(); + } + return read_lores_coord(); + } + + std::optional msg::read_angle(const rmq_flags flags) { + if ((flags & rmq_flags::float_angle) != rmq_flags::none) { + return read_float(); + } + if ((flags & rmq_flags::short_angle) != rmq_flags::none) { + auto a = read_short(); + if (!a.has_value()) { + return std::nullopt; + } + return a.value() * (360.0 / 65536); + } + const auto c = read_char(); + if (!c.has_value()) { + return std::nullopt; + } + return c.value() * (360.0 / 256); + } + + std::optional msg::read_angle_16(rmq_flags flags) { + if ((flags & rmq_flags::float_angle) != rmq_flags::none) { + return read_float(); + } + const auto s = read_short(); + if (!s.has_value()) { + return std::nullopt; + } + return s.value() * (360.0 / 65536); + } + + void msg::write(const std::uint8_t *data, std::size_t size) { + for (auto i = 0; i < size; i++) { + _data.push_back(data[i]); + } + } +} diff --git a/Quake/net_msg.hpp b/Quake/net_msg.hpp new file mode 100644 index 0000000..f98aab5 --- /dev/null +++ b/Quake/net_msg.hpp @@ -0,0 +1,84 @@ +#pragma once +#include +#include +#include + +namespace net { + enum class rmq_flags { + none = 0, + short_angle = (1 << 1), + float_angle = (1 << 2), + hires_coord = (1 << 3), + float_coord = (1 << 4), + edict_scale = (1 << 5), + alpha_sanity = (1 << 6), + int_coord = (1 << 7), + }; + + class msg { + std::vector _data{}; + bool _eos{false}; + std::size_t _offset{0}; + + void write_lores_coord(float f); + + void write_hires_coord(float f); + + std::optional read_lores_coord(); + + std::optional read_hires_coord(); + + public: + void clear(); + + void write_char(char c); + + void write_byte(std::uint8_t b); + + void write_short(short s); + + void write_long(int l); + + void write_float(float f); + + void write_string(std::string_view s); + + void write_coord(float f, rmq_flags flags); + + void write_angle(float f, rmq_flags flags); + + void write_angle_16(float f, rmq_flags flags); + + [[nodiscard]] bool eos() const { return _eos; } + + void begin_reading(); + + std::optional read_char(); + + std::optional read_byte(); + + std::optional read_short(); + + std::optional read_long(); + + std::optional read_float(); + + std::optional read_string(); + + std::optional read_coord(rmq_flags flags); + + std::optional read_angle(rmq_flags flags); + + std::optional read_angle_16(rmq_flags flags); + + [[nodiscard]] bool empty() const { return _data.empty(); } + + [[nodiscard]] std::size_t size() const { return _data.size(); } + + auto data() { return _data.data(); } + + void write(const std::uint8_t* data, std::size_t size); + + auto offset() const { return _offset; } + }; +} diff --git a/Quake/pr_cmds.cpp b/Quake/pr_cmds.cpp index cd78886..d3f398b 100644 --- a/Quake/pr_cmds.cpp +++ b/Quake/pr_cmds.cpp @@ -374,8 +374,8 @@ static void PF_sprint (void) client = &svs.clients[entnum-1]; - MSG_WriteChar (&client->message,svc_print); - MSG_WriteString (&client->message, s ); + client->message.write_char(svc_print); + client->message.write_string(s); } @@ -405,8 +405,8 @@ static void PF_centerprint (void) client = &svs.clients[entnum-1]; - MSG_WriteChar (&client->message,svc_centerprint); - MSG_WriteString (&client->message, s); + client->message.write_char(svc_centerprint); + client->message.write_string(s); } @@ -615,23 +615,23 @@ static void PF_ambientsound (void) //johnfitz -- PROTOCOL_FITZQUAKE if (large) - MSG_WriteByte (sv.signon,svc_spawnstaticsound2); + sv.signons[sv.current_signon].write_byte(svc_spawnstaticsound2); else - MSG_WriteByte (sv.signon,svc_spawnstaticsound); + sv.signons[sv.current_signon].write_byte(svc_spawnstaticsound); //johnfitz for (i = 0; i < 3; i++) - MSG_WriteCoord(sv.signon, pos[i], sv.protocolflags); + sv.signons[sv.current_signon].write_coord(pos[i], static_cast(sv.protocolflags)); //johnfitz -- PROTOCOL_FITZQUAKE if (large) - MSG_WriteShort(sv.signon, soundnum); + sv.signons[sv.current_signon].write_short(soundnum); else - MSG_WriteByte (sv.signon, soundnum); + sv.signons[sv.current_signon].write_byte(soundnum); //johnfitz - MSG_WriteByte (sv.signon, vol*255); - MSG_WriteByte (sv.signon, attenuation*64); + sv.signons[sv.current_signon].write_byte(vol*255); + sv.signons[sv.current_signon].write_byte(attenuation*64); } @@ -1284,9 +1284,9 @@ static void PF_lightstyle (void) { if (client->active || client->spawned) { - MSG_WriteChar (&client->message, svc_lightstyle); - MSG_WriteChar (&client->message, style); - MSG_WriteString (&client->message, val); + client->message.write_char(svc_lightstyle); + client->message.write_char(style); + client->message.write_string(val); } } } @@ -1502,7 +1502,7 @@ MESSAGE WRITING =============================================================================== */ -static sizebuf_t *WriteDest (void) +static net::msg &WriteDest (void) { int entnum; int dest; @@ -1512,67 +1512,67 @@ static sizebuf_t *WriteDest (void) switch (dest) { case MSG_BROADCAST: - return &sv.datagram; + return sv.datagram; case MSG_ONE: ent = PROG_TO_EDICT(pr_global_struct->msg_entity); entnum = NUM_FOR_EDICT(ent); if (entnum < 1 || entnum > svs.maxclients) PR_RunError ("WriteDest: not a client"); - return &svs.clients[entnum-1].message; + return svs.clients[entnum-1].message; case MSG_ALL: - return &sv.reliable_datagram; + return sv.reliable_datagram; case MSG_INIT: - return sv.signon; + return sv.signons[sv.current_signon]; default: PR_RunError ("WriteDest: bad destination"); break; } - return NULL; + throw std::runtime_error{"WHOOPS"}; } static void PF_WriteByte (void) { - MSG_WriteByte (WriteDest(), G_FLOAT(OFS_PARM1)); + WriteDest().write_byte(G_FLOAT(OFS_PARM1)); } static void PF_WriteChar (void) { - MSG_WriteChar (WriteDest(), G_FLOAT(OFS_PARM1)); + WriteDest().write_char(G_FLOAT(OFS_PARM1)); } static void PF_WriteShort (void) { - MSG_WriteShort (WriteDest(), G_FLOAT(OFS_PARM1)); + WriteDest().write_short(G_FLOAT(OFS_PARM1)); } static void PF_WriteLong (void) { - MSG_WriteLong (WriteDest(), G_FLOAT(OFS_PARM1)); + WriteDest().write_long(G_FLOAT(OFS_PARM1)); } static void PF_WriteAngle (void) { - MSG_WriteAngle (WriteDest(), G_FLOAT(OFS_PARM1), sv.protocolflags); + WriteDest().write_angle(G_FLOAT(OFS_PARM1), static_cast(sv.protocolflags)); } static void PF_WriteCoord (void) { - MSG_WriteCoord (WriteDest(), G_FLOAT(OFS_PARM1), sv.protocolflags); + WriteDest().write_coord(G_FLOAT(OFS_PARM1), static_cast(sv.protocolflags)); } static void PF_WriteString (void) { - MSG_WriteString (WriteDest(), LOC_GetString(G_STRING(OFS_PARM1))); + WriteDest().write_string(LOC_GetString(G_STRING(OFS_PARM1))); } static void PF_WriteEntity (void) { - MSG_WriteShort (WriteDest(), G_EDICTNUM(OFS_PARM1)); + WriteDest().write_short(G_EDICTNUM(OFS_PARM1)); } //============================================================================= @@ -1628,38 +1628,38 @@ static void PF_makestatic (void) if (bits) { - MSG_WriteByte (sv.signon, svc_spawnstatic2); - MSG_WriteByte (sv.signon, bits); + sv.signons[sv.current_signon].write_byte(svc_spawnstatic2); + sv.signons[sv.current_signon].write_byte(bits); } else - MSG_WriteByte (sv.signon, svc_spawnstatic); + sv.signons[sv.current_signon].write_byte(svc_spawnstatic); if (bits & B_LARGEMODEL) - MSG_WriteShort (sv.signon, SV_ModelIndex(PR_GetString(ent->v.model))); + sv.signons[sv.current_signon].write_short(SV_ModelIndex(PR_GetString(ent->v.model))); else - MSG_WriteByte (sv.signon, SV_ModelIndex(PR_GetString(ent->v.model))); + sv.signons[sv.current_signon].write_byte(SV_ModelIndex(PR_GetString(ent->v.model))); if (bits & B_LARGEFRAME) - MSG_WriteShort (sv.signon, ent->v.frame); + sv.signons[sv.current_signon].write_short(ent->v.frame); else - MSG_WriteByte (sv.signon, ent->v.frame); + sv.signons[sv.current_signon].write_byte(ent->v.frame); //johnfitz - MSG_WriteByte (sv.signon, ent->v.colormap); - MSG_WriteByte (sv.signon, ent->v.skin); + sv.signons[sv.current_signon].write_byte(ent->v.colormap); + sv.signons[sv.current_signon].write_byte(ent->v.skin); for (i = 0; i < 3; i++) { - MSG_WriteCoord(sv.signon, ent->v.origin[i], sv.protocolflags); - MSG_WriteAngle(sv.signon, ent->v.angles[i], sv.protocolflags); + sv.signons[sv.current_signon].write_coord(ent->v.origin[i], static_cast(sv.protocolflags)); + sv.signons[sv.current_signon].write_angle(ent->v.angles[i], static_cast(sv.protocolflags)); } //johnfitz -- PROTOCOL_FITZQUAKE if (bits & B_ALPHA) - MSG_WriteByte (sv.signon, ent->alpha); + sv.signons[sv.current_signon].write_byte(ent->alpha); //johnfitz if (bits & B_SCALE) - MSG_WriteByte (sv.signon, ent->scale); + sv.signons[sv.current_signon].write_byte(ent->scale); // throw the entity away now ED_Free (ent); diff --git a/Quake/pr_edict.cpp b/Quake/pr_edict.cpp index e634590..d1f0294 100644 --- a/Quake/pr_edict.cpp +++ b/Quake/pr_edict.cpp @@ -737,7 +737,7 @@ void ED_ParseGlobals (std::istringstream &ss) if (ss.eof()) Host_Error ("ED_ParseEntity: EOF without closing brace"); - if (token->front() == '}') + if (!token->empty() && token->front() == '}') Host_Error ("ED_ParseEntity: closing brace without data"); key = ED_FindGlobal (keyname.c_str()); @@ -940,7 +940,9 @@ void ED_ParseEdict(std::istringstream &ss, edict_t *ent) Host_Error ("ED_ParseEntity: EOF without closing brace"); } - if (token->front() == '}') { + if (!token->empty() && + + token->front() == '}') { Host_Error ("ED_ParseEntity: closing brace without data"); } diff --git a/Quake/r_part.cpp b/Quake/r_part.cpp index 713c2c6..bf44f83 100644 --- a/Quake/r_part.cpp +++ b/Quake/r_part.cpp @@ -25,40 +25,39 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define ABSOLUTE_MAX_PARTICLES 32768 // default max # of particles at one time #define ABSOLUTE_MIN_PARTICLES 512 // no fewer than this no matter what's - // on the command line +// on the command line #define DEFAULT_NUM_PARTICLES 16384 -static int ramp1[8] = {0x6f, 0x6d, 0x6b, 0x69, 0x67, 0x65, 0x63, 0x61}; -static int ramp2[8] = {0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x68, 0x66}; -static int ramp3[8] = {0x6d, 0x6b, 6, 5, 4, 3}; +static int ramp1[8] = {0x6f, 0x6d, 0x6b, 0x69, 0x67, 0x65, 0x63, 0x61}; +static int ramp2[8] = {0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x68, 0x66}; +static int ramp3[8] = {0x6d, 0x6b, 6, 5, 4, 3}; -static particle_t *active_particles, *free_particles, *particles; +static particle_t *active_particles, *free_particles, *particles; -static int r_numparticles; +static int r_numparticles; static gltexture_t *particletexture, *particletexture1, *particletexture2, *particletexture3; //johnfitz static float texturescalefactor; //johnfitz -- compensate for apparent size of different particle textures -convar r_particles{"r_particles","1", {.archive = true}}; //johnfitz -convar r_quadparticles{"r_quadparticles","1", {.archive = true}}; //johnfitz +convar r_particles{"r_particles", "1", {.archive = true}}; //johnfitz +convar r_quadparticles{"r_quadparticles", "1", {.archive = true}}; //johnfitz /* =============== R_ParticleTextureLookup -- johnfitz -- generate nice antialiased 32x32 circle for particles =============== */ -int R_ParticleTextureLookup (int x, int y, int sharpness) -{ - int r; //distance from point x,y to circle origin, squared - int a; //alpha value to return +int R_ParticleTextureLookup(int x, int y, int sharpness) { + int r; //distance from point x,y to circle origin, squared + int a; //alpha value to return - x -= 16; - y -= 16; - r = x * x + y * y; - r = r > 255 ? 255 : r; - a = sharpness * (255 - r); - a = std::min(a,255); - return a; + x -= 16; + y -= 16; + r = x * x + y * y; + r = r > 255 ? 255 : r; + a = sharpness * (255 - r); + a = std::min(a, 255); + return a; } /* @@ -66,53 +65,55 @@ int R_ParticleTextureLookup (int x, int y, int sharpness) R_InitParticleTextures -- johnfitz -- rewritten =============== */ -void R_InitParticleTextures (void) -{ - int x,y; - static byte particle1_data[64*64*4]; - static byte particle2_data[2*2*4]; - static byte particle3_data[64*64*4]; - byte *dst; +void R_InitParticleTextures(void) { + int x, y; + static byte particle1_data[64 * 64 * 4]; + static byte particle2_data[2 * 2 * 4]; + static byte particle3_data[64 * 64 * 4]; + byte *dst; - // particle texture 1 -- circle - dst = particle1_data; - for (x=0 ; x<64 ; x++) - for (y=0 ; y<64 ; y++) - { - *dst++ = 255; - *dst++ = 255; - *dst++ = 255; - *dst++ = R_ParticleTextureLookup(x, y, 8); - } - particletexture1 = TexMgr_LoadImage (NULL, "particle1", 64, 64, SRC_RGBA, particle1_data, "", (src_offset_t)particle1_data, TEXPREF_PERSIST | TEXPREF_ALPHA | TEXPREF_LINEAR); + // particle texture 1 -- circle + dst = particle1_data; + for (x = 0; x < 64; x++) + for (y = 0; y < 64; y++) { + *dst++ = 255; + *dst++ = 255; + *dst++ = 255; + *dst++ = R_ParticleTextureLookup(x, y, 8); + } + particletexture1 = TexMgr_LoadImage(NULL, "particle1", 64, 64, SRC_RGBA, particle1_data, "", + (src_offset_t) particle1_data, + TEXPREF_PERSIST | TEXPREF_ALPHA | TEXPREF_LINEAR); - // particle texture 2 -- square - dst = particle2_data; - for (x=0 ; x<2 ; x++) - for (y=0 ; y<2 ; y++) - { - *dst++ = 255; - *dst++ = 255; - *dst++ = 255; - *dst++ = x || y ? 0 : 255; - } - particletexture2 = TexMgr_LoadImage (NULL, "particle2", 2, 2, SRC_RGBA, particle2_data, "", (src_offset_t)particle2_data, TEXPREF_PERSIST | TEXPREF_ALPHA | TEXPREF_NEAREST); + // particle texture 2 -- square + dst = particle2_data; + for (x = 0; x < 2; x++) + for (y = 0; y < 2; y++) { + *dst++ = 255; + *dst++ = 255; + *dst++ = 255; + *dst++ = x || y ? 0 : 255; + } + particletexture2 = TexMgr_LoadImage(NULL, "particle2", 2, 2, SRC_RGBA, particle2_data, "", + (src_offset_t) particle2_data, + TEXPREF_PERSIST | TEXPREF_ALPHA | TEXPREF_NEAREST); - // particle texture 3 -- blob - dst = particle3_data; - for (x=0 ; x<64 ; x++) - for (y=0 ; y<64 ; y++) - { - *dst++ = 255; - *dst++ = 255; - *dst++ = 255; - *dst++ = R_ParticleTextureLookup(x, y, 2); - } - particletexture3 = TexMgr_LoadImage (NULL, "particle3", 64, 64, SRC_RGBA, particle3_data, "", (src_offset_t)particle3_data, TEXPREF_PERSIST | TEXPREF_ALPHA | TEXPREF_LINEAR); + // particle texture 3 -- blob + dst = particle3_data; + for (x = 0; x < 64; x++) + for (y = 0; y < 64; y++) { + *dst++ = 255; + *dst++ = 255; + *dst++ = 255; + *dst++ = R_ParticleTextureLookup(x, y, 2); + } + particletexture3 = TexMgr_LoadImage(NULL, "particle3", 64, 64, SRC_RGBA, particle3_data, "", + (src_offset_t) particle3_data, + TEXPREF_PERSIST | TEXPREF_ALPHA | TEXPREF_LINEAR); - //set default - particletexture = particletexture1; - texturescalefactor = 1.27; + //set default + particletexture = particletexture1; + texturescalefactor = 1.27; } /* @@ -120,23 +121,21 @@ void R_InitParticleTextures (void) R_SetParticleTexture_f -- johnfitz =============== */ -static void R_SetParticleTexture_f (convar *var) -{ - switch ((int)(r_particles.value)) - { - case 1: - particletexture = particletexture1; - texturescalefactor = 1.27; - break; - case 2: - particletexture = particletexture2; - texturescalefactor = 1.0; - break; -// case 3: -// particletexture = particletexture3; -// texturescalefactor = 1.5; -// break; - } +static void R_SetParticleTexture_f(convar *var) { + switch ((int) (r_particles.value)) { + case 1: + particletexture = particletexture1; + texturescalefactor = 1.27; + break; + case 2: + particletexture = particletexture2; + texturescalefactor = 1.0; + break; + // case 3: + // particletexture = particletexture3; + // texturescalefactor = 1.5; + // break; + } } /* @@ -144,31 +143,27 @@ static void R_SetParticleTexture_f (convar *var) R_InitParticles =============== */ -void R_InitParticles (void) -{ - auto i = common::check_param ("-particles"); +void R_InitParticles(void) { + auto i = common::check_param("-particles"); - if (i.has_value() && i.value() < com_argc - 1) - { - r_numparticles = atoi(com_argv[i.value() + 1]); - if (r_numparticles < ABSOLUTE_MIN_PARTICLES) - r_numparticles = ABSOLUTE_MIN_PARTICLES; - else if (r_numparticles > ABSOLUTE_MAX_PARTICLES) - r_numparticles = ABSOLUTE_MAX_PARTICLES; - } - else - { - r_numparticles = DEFAULT_NUM_PARTICLES; - } + if (i.has_value() && i.value() < com_argc - 1) { + r_numparticles = atoi(com_argv[i.value() + 1]); + if (r_numparticles < ABSOLUTE_MIN_PARTICLES) + r_numparticles = ABSOLUTE_MIN_PARTICLES; + else if (r_numparticles > ABSOLUTE_MAX_PARTICLES) + r_numparticles = ABSOLUTE_MAX_PARTICLES; + } else { + r_numparticles = DEFAULT_NUM_PARTICLES; + } - particles = (particle_t *) - Hunk_AllocName (r_numparticles * sizeof(particle_t), "particles"); + particles = (particle_t *) + Hunk_AllocName(r_numparticles * sizeof(particle_t), "particles"); - r_particles.inscribe(); //johnfitz - r_particles.set_callback(R_SetParticleTexture_f); - r_quadparticles.inscribe(); //johnfitz + r_particles.inscribe(); //johnfitz + r_particles.set_callback(R_SetParticleTexture_f); + r_quadparticles.inscribe(); //johnfitz - R_InitParticleTextures (); //johnfitz + R_InitParticleTextures(); //johnfitz } /* @@ -176,64 +171,60 @@ void R_InitParticles (void) R_EntityParticles =============== */ -static vec3_t avelocities[NUMVERTEXNORMALS]; -static float beamlength = 16; +static vec3_t avelocities[NUMVERTEXNORMALS]; +static float beamlength = 16; -void R_EntityParticles (entity_t *ent) -{ - int i; - particle_t *p; - float angle; - float sp, sy, cp, cy; -// float sr, cr; -// int count; - vec3_t forward; - float dist; +void R_EntityParticles(entity_t *ent) { + int i; + particle_t *p; + float angle; + float sp, sy, cp, cy; + // float sr, cr; + // int count; + vec3_t forward; + float dist; - dist = 64; -// count = 50; + dist = 64; + // count = 50; - if (!avelocities[0][0]) - { - for (i = 0; i < NUMVERTEXNORMALS; i++) - { - avelocities[i][0] = (rand() & 255) * 0.01; - avelocities[i][1] = (rand() & 255) * 0.01; - avelocities[i][2] = (rand() & 255) * 0.01; - } - } + if (!avelocities[0][0]) { + for (i = 0; i < NUMVERTEXNORMALS; i++) { + avelocities[i][0] = (rand() & 255) * 0.01; + avelocities[i][1] = (rand() & 255) * 0.01; + avelocities[i][2] = (rand() & 255) * 0.01; + } + } - for (i = 0; i < NUMVERTEXNORMALS; i++) - { - angle = cl.time * avelocities[i][0]; - sy = sin(angle); - cy = cos(angle); - angle = cl.time * avelocities[i][1]; - sp = sin(angle); - cp = cos(angle); - angle = cl.time * avelocities[i][2]; - // sr = sin(angle); - // cr = cos(angle); + for (i = 0; i < NUMVERTEXNORMALS; i++) { + angle = cl.time * avelocities[i][0]; + sy = sin(angle); + cy = cos(angle); + angle = cl.time * avelocities[i][1]; + sp = sin(angle); + cp = cos(angle); + angle = cl.time * avelocities[i][2]; + // sr = sin(angle); + // cr = cos(angle); - forward[0] = cp*cy; - forward[1] = cp*sy; - forward[2] = -sp; + forward[0] = cp * cy; + forward[1] = cp * sy; + forward[2] = -sp; - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = cl.time + 0.01; - p->color = 0x6f; - p->type = pt_explode; + p->die = cl.time + 0.01; + p->color = 0x6f; + p->type = pt_explode; - p->org[0] = ent->origin[0] + r_avertexnormals[i][0]*dist + forward[0]*beamlength; - p->org[1] = ent->origin[1] + r_avertexnormals[i][1]*dist + forward[1]*beamlength; - p->org[2] = ent->origin[2] + r_avertexnormals[i][2]*dist + forward[2]*beamlength; - } + p->org[0] = ent->origin[0] + r_avertexnormals[i][0] * dist + forward[0] * beamlength; + p->org[1] = ent->origin[1] + r_avertexnormals[i][1] * dist + forward[1] * beamlength; + p->org[2] = ent->origin[2] + r_avertexnormals[i][2] * dist + forward[2] * beamlength; + } } /* @@ -241,16 +232,15 @@ void R_EntityParticles (entity_t *ent) R_ClearParticles =============== */ -void R_ClearParticles (void) -{ - int i; +void R_ClearParticles(void) { + int i; - free_particles = &particles[0]; - active_particles = NULL; + free_particles = &particles[0]; + active_particles = NULL; - for (i=0 ;inext; - p->next = active_particles; - active_particles = p; + if (!free_particles) { + console::info("Not enough free particles\n"); + break; + } + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = 99999; - p->color = (-c)&15; - p->type = pt_static; - VectorCopy (vec3_origin, p->vel); - VectorCopy (org, p->org); - } + p->die = 99999; + p->color = (-c) & 15; + p->type = pt_static; + VectorCopy(vec3_origin, p->vel); + VectorCopy(org, p->org); + } - fclose (f); - console::info ("%i points read\n", c); + fclose(f); + console::info("%i points read\n", c); } /* @@ -317,24 +303,23 @@ R_ParseParticleEffect Parse an effect out of the server message =============== */ -void R_ParseParticleEffect (void) -{ - vec3_t org, dir; - int i, count, msgcount, color; +void R_ParseParticleEffect(void) { + vec3_t org, dir; + int i, count, msgcount, color; - for (i=0 ; i<3 ; i++) - org[i] = MSG_ReadCoord (cl.protocolflags); - for (i=0 ; i<3 ; i++) - dir[i] = MSG_ReadChar () * (1.0/16); - msgcount = MSG_ReadByte (); - color = MSG_ReadByte (); + for (i = 0; i < 3; i++) + org[i] = net_message.read_coord(static_cast(cl.protocolflags)).value(); + for (i = 0; i < 3; i++) + dir[i] = net_message.read_char().value() * (1.0 / 16); + msgcount = net_message.read_byte().value(); + color = net_message.read_byte().value(); - if (msgcount == 255) - count = 1024; - else - count = msgcount; + if (msgcount == 255) + count = 1024; + else + count = msgcount; - R_RunParticleEffect (org, dir, color, count); + R_RunParticleEffect(org, dir, color, count); } /* @@ -342,42 +327,35 @@ void R_ParseParticleEffect (void) R_ParticleExplosion =============== */ -void R_ParticleExplosion (vec3_t org) -{ - int i, j; - particle_t *p; +void R_ParticleExplosion(vec3_t org) { + int i, j; + particle_t *p; - for (i=0 ; i<1024 ; i++) - { - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + for (i = 0; i < 1024; i++) { + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = cl.time + 5; - p->color = ramp1[0]; - p->ramp = rand()&3; - if (i & 1) - { - p->type = pt_explode; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } - else - { - p->type = pt_explode2; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } - } + p->die = cl.time + 5; + p->color = ramp1[0]; + p->ramp = rand() & 3; + if (i & 1) { + p->type = pt_explode; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } else { + p->type = pt_explode2; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } + } } /* @@ -385,32 +363,29 @@ void R_ParticleExplosion (vec3_t org) R_ParticleExplosion2 =============== */ -void R_ParticleExplosion2 (vec3_t org, int colorStart, int colorLength) -{ - int i, j; - particle_t *p; - int colorMod = 0; +void R_ParticleExplosion2(vec3_t org, int colorStart, int colorLength) { + int i, j; + particle_t *p; + int colorMod = 0; - for (i=0; i<512; i++) - { - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + for (i = 0; i < 512; i++) { + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = cl.time + 0.3; - p->color = colorStart + (colorMod % colorLength); - colorMod++; + p->die = cl.time + 0.3; + p->color = colorStart + (colorMod % colorLength); + colorMod++; - p->type = pt_blob; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } + p->type = pt_blob; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } } /* @@ -418,43 +393,36 @@ void R_ParticleExplosion2 (vec3_t org, int colorStart, int colorLength) R_BlobExplosion =============== */ -void R_BlobExplosion (vec3_t org) -{ - int i, j; - particle_t *p; +void R_BlobExplosion(vec3_t org) { + int i, j; + particle_t *p; - for (i=0 ; i<1024 ; i++) - { - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + for (i = 0; i < 1024; i++) { + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = cl.time + 1 + (rand()&8)*0.05; + p->die = cl.time + 1 + (rand() & 8) * 0.05; - if (i & 1) - { - p->type = pt_blob; - p->color = 66 + rand()%6; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } - else - { - p->type = pt_blob2; - p->color = 150 + rand()%6; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } - } + if (i & 1) { + p->type = pt_blob; + p->color = 66 + rand() % 6; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } else { + p->type = pt_blob2; + p->color = 150 + rand() % 6; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } + } } /* @@ -462,56 +430,46 @@ void R_BlobExplosion (vec3_t org) R_RunParticleEffect =============== */ -void R_RunParticleEffect (vec3_t org, vec3_t dir, int color, int count) -{ - int i, j; - particle_t *p; +void R_RunParticleEffect(vec3_t org, vec3_t dir, int color, int count) { + int i, j; + particle_t *p; - for (i=0 ; inext; - p->next = active_particles; - active_particles = p; + for (i = 0; i < count; i++) { + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - if (count == 1024) - { // rocket explosion - p->die = cl.time + 5; - p->color = ramp1[0]; - p->ramp = rand()&3; - if (i & 1) - { - p->type = pt_explode; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } - else - { - p->type = pt_explode2; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()%32)-16); - p->vel[j] = (rand()%512)-256; - } - } - } - else - { - p->die = cl.time + 0.1*(rand()%5); - p->color = (color&~7) + (rand()&7); - p->type = pt_slowgrav; - for (j=0 ; j<3 ; j++) - { - p->org[j] = org[j] + ((rand()&15)-8); - p->vel[j] = dir[j]*15;// + (rand()%300)-150; - } - } - } + if (count == 1024) { + // rocket explosion + p->die = cl.time + 5; + p->color = ramp1[0]; + p->ramp = rand() & 3; + if (i & 1) { + p->type = pt_explode; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } else { + p->type = pt_explode2; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() % 32) - 16); + p->vel[j] = (rand() % 512) - 256; + } + } + } else { + p->die = cl.time + 0.1 * (rand() % 5); + p->color = (color & ~7) + (rand() & 7); + p->type = pt_slowgrav; + for (j = 0; j < 3; j++) { + p->org[j] = org[j] + ((rand() & 15) - 8); + p->vel[j] = dir[j] * 15; // + (rand()%300)-150; + } + } + } } /* @@ -519,40 +477,38 @@ void R_RunParticleEffect (vec3_t org, vec3_t dir, int color, int count) R_LavaSplash =============== */ -void R_LavaSplash (vec3_t org) -{ - int i, j, k; - particle_t *p; - float vel; - vec3_t dir; +void R_LavaSplash(vec3_t org) { + int i, j, k; + particle_t *p; + float vel; + vec3_t dir; - for (i=-16 ; i<16 ; i++) - for (j=-16 ; j<16 ; j++) - for (k=0 ; k<1 ; k++) - { - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + for (i = -16; i < 16; i++) + for (j = -16; j < 16; j++) + for (k = 0; k < 1; k++) { + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = cl.time + 2 + (rand()&31) * 0.02; - p->color = 224 + (rand()&7); - p->type = pt_slowgrav; + p->die = cl.time + 2 + (rand() & 31) * 0.02; + p->color = 224 + (rand() & 7); + p->type = pt_slowgrav; - dir[0] = j*8 + (rand()&7); - dir[1] = i*8 + (rand()&7); - dir[2] = 256; + dir[0] = j * 8 + (rand() & 7); + dir[1] = i * 8 + (rand() & 7); + dir[2] = 256; - p->org[0] = org[0] + dir[0]; - p->org[1] = org[1] + dir[1]; - p->org[2] = org[2] + (rand()&63); + p->org[0] = org[0] + dir[0]; + p->org[1] = org[1] + dir[1]; + p->org[2] = org[2] + (rand() & 63); - VectorNormalize (dir); - vel = 50 + (rand()&63); - VectorScale (dir, vel, p->vel); - } + VectorNormalize(dir); + vel = 50 + (rand() & 63); + VectorScale(dir, vel, p->vel); + } } /* @@ -560,44 +516,40 @@ void R_LavaSplash (vec3_t org) R_TeleportSplash =============== */ -void R_TeleportSplash (vec3_t org) -{ - int i, j, k; - particle_t *p; - float vel; - vec3_t dir; +void R_TeleportSplash(vec3_t org) { + int i, j, k; + particle_t *p; + float vel; + vec3_t dir; - for (i=-16 ; i<16 ; i+=4) - { - for (j=-16 ; j<16 ; j+=4) - { - for (k=-24 ; k<32 ; k+=4) - { - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + for (i = -16; i < 16; i += 4) { + for (j = -16; j < 16; j += 4) { + for (k = -24; k < 32; k += 4) { + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - p->die = cl.time + 0.2 + (rand()&7) * 0.02; - p->color = 7 + (rand()&7); - p->type = pt_slowgrav; + p->die = cl.time + 0.2 + (rand() & 7) * 0.02; + p->color = 7 + (rand() & 7); + p->type = pt_slowgrav; - dir[0] = j*8; - dir[1] = i*8; - dir[2] = k*8; + dir[0] = j * 8; + dir[1] = i * 8; + dir[2] = k * 8; - p->org[0] = org[0] + i + (rand()&3); - p->org[1] = org[1] + j + (rand()&3); - p->org[2] = org[2] + k + (rand()&3); + p->org[0] = org[0] + i + (rand() & 3); + p->org[1] = org[1] + j + (rand() & 3); + p->org[2] = org[2] + k + (rand() & 3); - VectorNormalize (dir); - vel = 50 + (rand()&63); - VectorScale (dir, vel, p->vel); - } - } - } + VectorNormalize(dir); + vel = 50 + (rand() & 63); + VectorScale(dir, vel, p->vel); + } + } + } } /* @@ -607,107 +559,100 @@ R_RocketTrail FIXME -- rename function and use #defined types instead of numbers =============== */ -void R_RocketTrail (vec3_t start, vec3_t end, int type) -{ - vec3_t vec; - float len; - int j; - particle_t *p; - int dec; - static int tracercount; +void R_RocketTrail(vec3_t start, vec3_t end, int type) { + vec3_t vec; + float len; + int j; + particle_t *p; + int dec; + static int tracercount; - VectorSubtract (end, start, vec); - len = VectorNormalize (vec); - if (type < 128) - dec = 3; - else - { - dec = 1; - type -= 128; - } + VectorSubtract(end, start, vec); + len = VectorNormalize(vec); + if (type < 128) + dec = 3; + else { + dec = 1; + type -= 128; + } - while (len > 0) - { - len -= dec; + while (len > 0) { + len -= dec; - if (!free_particles) - return; - p = free_particles; - free_particles = p->next; - p->next = active_particles; - active_particles = p; + if (!free_particles) + return; + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; - VectorCopy (vec3_origin, p->vel); - p->die = cl.time + 2; + VectorCopy(vec3_origin, p->vel); + p->die = cl.time + 2; - switch (type) - { - case 0: // rocket trail - p->ramp = (rand()&3); - p->color = ramp3[(int)p->ramp]; - p->type = pt_fire; - for (j=0 ; j<3 ; j++) - p->org[j] = start[j] + ((rand()%6)-3); - break; + switch (type) { + case 0: // rocket trail + p->ramp = (rand() & 3); + p->color = ramp3[(int) p->ramp]; + p->type = pt_fire; + for (j = 0; j < 3; j++) + p->org[j] = start[j] + ((rand() % 6) - 3); + break; - case 1: // smoke smoke - p->ramp = (rand()&3) + 2; - p->color = ramp3[(int)p->ramp]; - p->type = pt_fire; - for (j=0 ; j<3 ; j++) - p->org[j] = start[j] + ((rand()%6)-3); - break; + case 1: // smoke smoke + p->ramp = (rand() & 3) + 2; + p->color = ramp3[(int) p->ramp]; + p->type = pt_fire; + for (j = 0; j < 3; j++) + p->org[j] = start[j] + ((rand() % 6) - 3); + break; - case 2: // blood - p->type = pt_grav; - p->color = 67 + (rand()&3); - for (j=0 ; j<3 ; j++) - p->org[j] = start[j] + ((rand()%6)-3); - break; + case 2: // blood + p->type = pt_grav; + p->color = 67 + (rand() & 3); + for (j = 0; j < 3; j++) + p->org[j] = start[j] + ((rand() % 6) - 3); + break; - case 3: - case 5: // tracer - p->die = cl.time + 0.5; - p->type = pt_static; - if (type == 3) - p->color = 52 + ((tracercount&4)<<1); - else - p->color = 230 + ((tracercount&4)<<1); + case 3: + case 5: // tracer + p->die = cl.time + 0.5; + p->type = pt_static; + if (type == 3) + p->color = 52 + ((tracercount & 4) << 1); + else + p->color = 230 + ((tracercount & 4) << 1); - tracercount++; + tracercount++; - VectorCopy (start, p->org); - if (tracercount & 1) - { - p->vel[0] = 30*vec[1]; - p->vel[1] = 30*-vec[0]; - } - else - { - p->vel[0] = 30*-vec[1]; - p->vel[1] = 30*vec[0]; - } - break; + VectorCopy(start, p->org); + if (tracercount & 1) { + p->vel[0] = 30 * vec[1]; + p->vel[1] = 30 * -vec[0]; + } else { + p->vel[0] = 30 * -vec[1]; + p->vel[1] = 30 * vec[0]; + } + break; - case 4: // slight blood - p->type = pt_grav; - p->color = 67 + (rand()&3); - for (j=0 ; j<3 ; j++) - p->org[j] = start[j] + ((rand()%6)-3); - len -= 3; - break; + case 4: // slight blood + p->type = pt_grav; + p->color = 67 + (rand() & 3); + for (j = 0; j < 3; j++) + p->org[j] = start[j] + ((rand() % 6) - 3); + len -= 3; + break; - case 6: // voor trail - p->color = 9*16 + 8 + (rand()&3); - p->type = pt_static; - p->die = cl.time + 0.3; - for (j=0 ; j<3 ; j++) - p->org[j] = start[j] + ((rand()&15)-8); - break; - } + case 6: // voor trail + p->color = 9 * 16 + 8 + (rand() & 3); + p->type = pt_static; + p->die = cl.time + 0.3; + for (j = 0; j < 3; j++) + p->org[j] = start[j] + ((rand() & 15) - 8); + break; + } - VectorAdd (start, vec, start); - } + VectorAdd(start, vec, start); + } } /* @@ -715,105 +660,98 @@ void R_RocketTrail (vec3_t start, vec3_t end, int type) CL_RunParticles -- johnfitz -- all the particle behavior, separated from R_DrawParticles =============== */ -void CL_RunParticles (void) -{ - particle_t *p, *kill; - int i; - float time1, time2, time3, dvel, frametime, grav; - extern convar sv_gravity; +void CL_RunParticles(void) { + particle_t *p, *kill; + int i; + float time1, time2, time3, dvel, frametime, grav; + extern convar sv_gravity; - frametime = cl.time - cl.oldtime; - time3 = frametime * 15; - time2 = frametime * 10; - time1 = frametime * 5; - grav = frametime * sv_gravity.value * 0.05; - dvel = 4*frametime; + frametime = cl.time - cl.oldtime; + time3 = frametime * 15; + time2 = frametime * 10; + time1 = frametime * 5; + grav = frametime * sv_gravity.value * 0.05; + dvel = 4 * frametime; - for ( ;; ) - { - kill = active_particles; - if (kill && kill->die < cl.time) - { - active_particles = kill->next; - kill->next = free_particles; - free_particles = kill; - continue; - } - break; - } + for (;;) { + kill = active_particles; + if (kill && kill->die < cl.time) { + active_particles = kill->next; + kill->next = free_particles; + free_particles = kill; + continue; + } + break; + } - for (p=active_particles ; p ; p=p->next) - { - for ( ;; ) - { - kill = p->next; - if (kill && kill->die < cl.time) - { - p->next = kill->next; - kill->next = free_particles; - free_particles = kill; - continue; - } - break; - } + for (p = active_particles; p; p = p->next) { + for (;;) { + kill = p->next; + if (kill && kill->die < cl.time) { + p->next = kill->next; + kill->next = free_particles; + free_particles = kill; + continue; + } + break; + } - p->org[0] += p->vel[0]*frametime; - p->org[1] += p->vel[1]*frametime; - p->org[2] += p->vel[2]*frametime; + p->org[0] += p->vel[0] * frametime; + p->org[1] += p->vel[1] * frametime; + p->org[2] += p->vel[2] * frametime; - switch (p->type) - { - case pt_static: - break; - case pt_fire: - p->ramp += time1; - if (p->ramp >= 6) - p->die = -1; - else - p->color = ramp3[(int)p->ramp]; - p->vel[2] += grav; - break; + switch (p->type) { + case pt_static: + break; + case pt_fire: + p->ramp += time1; + if (p->ramp >= 6) + p->die = -1; + else + p->color = ramp3[(int) p->ramp]; + p->vel[2] += grav; + break; - case pt_explode: - p->ramp += time2; - if (p->ramp >=8) - p->die = -1; - else - p->color = ramp1[(int)p->ramp]; - for (i=0 ; i<3 ; i++) - p->vel[i] += p->vel[i]*dvel; - p->vel[2] -= grav; - break; + case pt_explode: + p->ramp += time2; + if (p->ramp >= 8) + p->die = -1; + else + p->color = ramp1[(int) p->ramp]; + for (i = 0; i < 3; i++) + p->vel[i] += p->vel[i] * dvel; + p->vel[2] -= grav; + break; - case pt_explode2: - p->ramp += time3; - if (p->ramp >=8) - p->die = -1; - else - p->color = ramp2[(int)p->ramp]; - for (i=0 ; i<3 ; i++) - p->vel[i] -= p->vel[i]*frametime; - p->vel[2] -= grav; - break; + case pt_explode2: + p->ramp += time3; + if (p->ramp >= 8) + p->die = -1; + else + p->color = ramp2[(int) p->ramp]; + for (i = 0; i < 3; i++) + p->vel[i] -= p->vel[i] * frametime; + p->vel[2] -= grav; + break; - case pt_blob: - for (i=0 ; i<3 ; i++) - p->vel[i] += p->vel[i]*dvel; - p->vel[2] -= grav; - break; + case pt_blob: + for (i = 0; i < 3; i++) + p->vel[i] += p->vel[i] * dvel; + p->vel[2] -= grav; + break; - case pt_blob2: - for (i=0 ; i<2 ; i++) - p->vel[i] -= p->vel[i]*dvel; - p->vel[2] -= grav; - break; + case pt_blob2: + for (i = 0; i < 2; i++) + p->vel[i] -= p->vel[i] * dvel; + p->vel[2] -= grav; + break; - case pt_grav: - case pt_slowgrav: - p->vel[2] -= grav; - break; - } - } + case pt_grav: + case pt_slowgrav: + p->vel[2] -= grav; + break; + } + } } /* @@ -821,119 +759,115 @@ void CL_RunParticles (void) R_DrawParticles -- johnfitz -- moved all non-drawing code to CL_RunParticles =============== */ -void R_DrawParticles (void) -{ - particle_t *p; - float scale; - vec3_t up, right, p_up, p_right, p_upright; //johnfitz -- p_ vectors - GLubyte color[4], *c; //johnfitz -- particle transparency - extern convar r_particles; //johnfitz - //float alpha; //johnfitz -- particle transparency +void R_DrawParticles(void) { + particle_t *p; + float scale; + vec3_t up, right, p_up, p_right, p_upright; //johnfitz -- p_ vectors + GLubyte color[4], *c; //johnfitz -- particle transparency + extern convar r_particles; //johnfitz + //float alpha; //johnfitz -- particle transparency - if (!r_particles.value) - return; + if (!r_particles.value) + return; - //ericw -- avoid empty glBegin(),glEnd() pair below; causes issues on AMD - if (!active_particles) - return; + //ericw -- avoid empty glBegin(),glEnd() pair below; causes issues on AMD + if (!active_particles) + return; - VectorScale (vup, 1.5, up); - VectorScale (vright, 1.5, right); + VectorScale(vup, 1.5, up); + VectorScale(vright, 1.5, right); - GL_Bind(particletexture); - glEnable (GL_BLEND); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glDepthMask (GL_FALSE); //johnfitz -- fix for particle z-buffer bug + GL_Bind(particletexture); + glEnable(GL_BLEND); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glDepthMask(GL_FALSE); //johnfitz -- fix for particle z-buffer bug - if (r_quadparticles.value) //johnitz -- quads save fillrate - { - glBegin (GL_QUADS); - for (p=active_particles ; p ; p=p->next) - { - // hack a scale up to keep particles from disapearing - scale = (p->org[0] - r_origin[0]) * vpn[0] - + (p->org[1] - r_origin[1]) * vpn[1] - + (p->org[2] - r_origin[2]) * vpn[2]; - if (scale < 20) - scale = 1 + 0.08; //johnfitz -- added .08 to be consistent - else - scale = 1 + scale * 0.004; + if (r_quadparticles.value) //johnitz -- quads save fillrate + { + glBegin(GL_QUADS); + for (p = active_particles; p; p = p->next) { + // hack a scale up to keep particles from disapearing + scale = (p->org[0] - r_origin[0]) * vpn[0] + + (p->org[1] - r_origin[1]) * vpn[1] + + (p->org[2] - r_origin[2]) * vpn[2]; + if (scale < 20) + scale = 1 + 0.08; //johnfitz -- added .08 to be consistent + else + scale = 1 + scale * 0.004; - scale /= 2.0; //quad is half the size of triangle + scale /= 2.0; //quad is half the size of triangle - scale *= texturescalefactor; //johnfitz -- compensate for apparent size of different particle textures + scale *= texturescalefactor; //johnfitz -- compensate for apparent size of different particle textures - //johnfitz -- particle transparency and fade out - c = (GLubyte *) &d_8to24table[(int)p->color]; - color[0] = c[0]; - color[1] = c[1]; - color[2] = c[2]; - //alpha = std::clamp(p->die + 0.5 - cl.time, 0, 1); - color[3] = 255; //(int)(alpha * 255); - glColor4ubv(color); - //johnfitz + //johnfitz -- particle transparency and fade out + c = (GLubyte *) &d_8to24table[(int) p->color]; + color[0] = c[0]; + color[1] = c[1]; + color[2] = c[2]; + //alpha = std::clamp(p->die + 0.5 - cl.time, 0, 1); + color[3] = 255; //(int)(alpha * 255); + glColor4ubv(color); + //johnfitz - glTexCoord2f (0,0); - glVertex3fv (p->org); + glTexCoord2f(0, 0); + glVertex3fv(p->org); - glTexCoord2f (0.5,0); - VectorMA (p->org, scale, up, p_up); - glVertex3fv (p_up); + glTexCoord2f(0.5, 0); + VectorMA(p->org, scale, up, p_up); + glVertex3fv(p_up); - glTexCoord2f (0.5,0.5); - VectorMA (p_up, scale, right, p_upright); - glVertex3fv (p_upright); + glTexCoord2f(0.5, 0.5); + VectorMA(p_up, scale, right, p_upright); + glVertex3fv(p_upright); - glTexCoord2f (0,0.5); - VectorMA (p->org, scale, right, p_right); - glVertex3fv (p_right); - } - glEnd (); - } - else //johnitz -- triangles save verts - { - glBegin (GL_TRIANGLES); - for (p=active_particles ; p ; p=p->next) - { - // hack a scale up to keep particles from disapearing - scale = (p->org[0] - r_origin[0]) * vpn[0] - + (p->org[1] - r_origin[1]) * vpn[1] - + (p->org[2] - r_origin[2]) * vpn[2]; - if (scale < 20) - scale = 1 + 0.08; //johnfitz -- added .08 to be consistent - else - scale = 1 + scale * 0.004; + glTexCoord2f(0, 0.5); + VectorMA(p->org, scale, right, p_right); + glVertex3fv(p_right); + } + glEnd(); + } else //johnitz -- triangles save verts + { + glBegin(GL_TRIANGLES); + for (p = active_particles; p; p = p->next) { + // hack a scale up to keep particles from disapearing + scale = (p->org[0] - r_origin[0]) * vpn[0] + + (p->org[1] - r_origin[1]) * vpn[1] + + (p->org[2] - r_origin[2]) * vpn[2]; + if (scale < 20) + scale = 1 + 0.08; //johnfitz -- added .08 to be consistent + else + scale = 1 + scale * 0.004; - scale *= texturescalefactor; //johnfitz -- compensate for apparent size of different particle textures + scale *= texturescalefactor; //johnfitz -- compensate for apparent size of different particle textures - //johnfitz -- particle transparency and fade out - c = (GLubyte *) &d_8to24table[(int)p->color]; - color[0] = c[0]; - color[1] = c[1]; - color[2] = c[2]; - //alpha = std::clamp(p->die + 0.5 - cl.time, 0, 1); - color[3] = 255; //(int)(alpha * 255); - glColor4ubv(color); - //johnfitz + //johnfitz -- particle transparency and fade out + c = (GLubyte *) &d_8to24table[(int) p->color]; + color[0] = c[0]; + color[1] = c[1]; + color[2] = c[2]; + //alpha = std::clamp(p->die + 0.5 - cl.time, 0, 1); + color[3] = 255; //(int)(alpha * 255); + glColor4ubv(color); + //johnfitz - glTexCoord2f (0,0); - glVertex3fv (p->org); + glTexCoord2f(0, 0); + glVertex3fv(p->org); - glTexCoord2f (1,0); - VectorMA (p->org, scale, up, p_up); - glVertex3fv (p_up); + glTexCoord2f(1, 0); + VectorMA(p->org, scale, up, p_up); + glVertex3fv(p_up); - glTexCoord2f (0,1); - VectorMA (p->org, scale, right, p_right); - glVertex3fv (p_right); - } - glEnd (); - } + glTexCoord2f(0, 1); + VectorMA(p->org, scale, right, p_right); + glVertex3fv(p_right); + } + glEnd(); + } - glDepthMask (GL_TRUE); //johnfitz -- fix for particle z-buffer bug - glDisable (GL_BLEND); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glColor3f(1,1,1); + glDepthMask(GL_TRUE); //johnfitz -- fix for particle z-buffer bug + glDisable(GL_BLEND); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glColor3f(1, 1, 1); } @@ -942,77 +876,70 @@ void R_DrawParticles (void) R_DrawParticles_ShowTris -- johnfitz =============== */ -void R_DrawParticles_ShowTris (void) -{ - particle_t *p; - float scale; - vec3_t up, right, p_up, p_right, p_upright; - extern convar r_particles; +void R_DrawParticles_ShowTris(void) { + particle_t *p; + float scale; + vec3_t up, right, p_up, p_right, p_upright; + extern convar r_particles; - if (!r_particles.value) - return; + if (!r_particles.value) + return; - VectorScale (vup, 1.5, up); - VectorScale (vright, 1.5, right); + VectorScale(vup, 1.5, up); + VectorScale(vright, 1.5, right); - if (r_quadparticles.value) - { - for (p=active_particles ; p ; p=p->next) - { - glBegin (GL_TRIANGLE_FAN); + if (r_quadparticles.value) { + for (p = active_particles; p; p = p->next) { + glBegin(GL_TRIANGLE_FAN); - // hack a scale up to keep particles from disapearing - scale = (p->org[0] - r_origin[0]) * vpn[0] - + (p->org[1] - r_origin[1]) * vpn[1] - + (p->org[2] - r_origin[2]) * vpn[2]; - if (scale < 20) - scale = 1 + 0.08; //johnfitz -- added .08 to be consistent - else - scale = 1 + scale * 0.004; + // hack a scale up to keep particles from disapearing + scale = (p->org[0] - r_origin[0]) * vpn[0] + + (p->org[1] - r_origin[1]) * vpn[1] + + (p->org[2] - r_origin[2]) * vpn[2]; + if (scale < 20) + scale = 1 + 0.08; //johnfitz -- added .08 to be consistent + else + scale = 1 + scale * 0.004; - scale /= 2.0; //quad is half the size of triangle + scale /= 2.0; //quad is half the size of triangle - scale *= texturescalefactor; //compensate for apparent size of different particle textures + scale *= texturescalefactor; //compensate for apparent size of different particle textures - glVertex3fv (p->org); + glVertex3fv(p->org); - VectorMA (p->org, scale, up, p_up); - glVertex3fv (p_up); + VectorMA(p->org, scale, up, p_up); + glVertex3fv(p_up); - VectorMA (p_up, scale, right, p_upright); - glVertex3fv (p_upright); + VectorMA(p_up, scale, right, p_upright); + glVertex3fv(p_upright); - VectorMA (p->org, scale, right, p_right); - glVertex3fv (p_right); + VectorMA(p->org, scale, right, p_right); + glVertex3fv(p_right); - glEnd (); - } - } - else - { - glBegin (GL_TRIANGLES); - for (p=active_particles ; p ; p=p->next) - { - // hack a scale up to keep particles from disapearing - scale = (p->org[0] - r_origin[0]) * vpn[0] - + (p->org[1] - r_origin[1]) * vpn[1] - + (p->org[2] - r_origin[2]) * vpn[2]; - if (scale < 20) - scale = 1 + 0.08; //johnfitz -- added .08 to be consistent - else - scale = 1 + scale * 0.004; + glEnd(); + } + } else { + glBegin(GL_TRIANGLES); + for (p = active_particles; p; p = p->next) { + // hack a scale up to keep particles from disapearing + scale = (p->org[0] - r_origin[0]) * vpn[0] + + (p->org[1] - r_origin[1]) * vpn[1] + + (p->org[2] - r_origin[2]) * vpn[2]; + if (scale < 20) + scale = 1 + 0.08; //johnfitz -- added .08 to be consistent + else + scale = 1 + scale * 0.004; - scale *= texturescalefactor; //compensate for apparent size of different particle textures + scale *= texturescalefactor; //compensate for apparent size of different particle textures - glVertex3fv (p->org); + glVertex3fv(p->org); - VectorMA (p->org, scale, up, p_up); - glVertex3fv (p_up); + VectorMA(p->org, scale, up, p_up); + glVertex3fv(p_up); - VectorMA (p->org, scale, right, p_right); - glVertex3fv (p_right); - } - glEnd (); - } + VectorMA(p->org, scale, right, p_right); + glVertex3fv(p_right); + } + glEnd(); + } } - diff --git a/Quake/server.hpp b/Quake/server.hpp index 5a55026..155ceb2 100644 --- a/Quake/server.hpp +++ b/Quake/server.hpp @@ -66,15 +66,12 @@ typedef struct // be used to reference the world ent server_state_t state; // some actions are only valid during load - sizebuf_t datagram; - byte datagram_buf[MAX_DATAGRAM]; + net::msg datagram; - sizebuf_t reliable_datagram; // copied to all clients at end of frame - byte reliable_datagram_buf[MAX_DATAGRAM]; + net::msg reliable_datagram; // copied to all clients at end of frame - sizebuf_t *signon; - int num_signon_buffers; - sizebuf_t *signon_buffers[MAX_SIGNON_BUFFERS]; + std::vector signons; + size_t current_signon; unsigned protocol; //johnfitz unsigned protocolflags; @@ -108,7 +105,7 @@ typedef struct client_s usercmd_t cmd; // movement vec3_t wishdir; // intended motion calced from cmd - sizebuf_t message; // can be added to at any time, + net::msg message; // can be added to at any time, // copied and clear once per frame byte msgbuf[MAX_MSGLEN]; edict_t *edict; // EDICT_NUM(clientnum+1) @@ -235,7 +232,7 @@ void SV_Physics (void); bool SV_CheckBottom (edict_t *ent); bool SV_movestep (edict_t *ent, vec3_t move, bool relink); -void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg); +void SV_WriteClientdataToMessage (edict_t *ent, net::msg &msg); void SV_MoveToGoal (void); diff --git a/Quake/sv_main.cpp b/Quake/sv_main.cpp index 0926a6c..858e078 100644 --- a/Quake/sv_main.cpp +++ b/Quake/sv_main.cpp @@ -146,22 +146,22 @@ Make sure the event gets sent to all clients void SV_StartParticle(vec3_t org, vec3_t dir, int color, int count) { int i, v; - if (sv.datagram.cursize > MAX_DATAGRAM - 18) + if (sv.datagram.size() > MAX_DATAGRAM - 18) return; - MSG_WriteByte(&sv.datagram, svc_particle); - MSG_WriteCoord(&sv.datagram, org[0], sv.protocolflags); - MSG_WriteCoord(&sv.datagram, org[1], sv.protocolflags); - MSG_WriteCoord(&sv.datagram, org[2], sv.protocolflags); + sv.datagram.write_byte(svc_particle); + sv.datagram.write_coord(org[0], static_cast(sv.protocolflags)); + sv.datagram.write_coord(org[1], static_cast(sv.protocolflags)); + sv.datagram.write_coord(org[2], static_cast(sv.protocolflags)); for (i = 0; i < 3; i++) { v = dir[i] * 16; if (v > 127) v = 127; else if (v < -128) v = -128; - MSG_WriteChar(&sv.datagram, v); + sv.datagram.write_char(v); } - MSG_WriteByte(&sv.datagram, count); - MSG_WriteByte(&sv.datagram, color); + sv.datagram.write_byte(count); + sv.datagram.write_byte(color); } /* @@ -192,7 +192,7 @@ void SV_StartSound(edict_t *entity, int channel, const char *sample, int volume, if (channel < 0 || channel > 7) Host_Error("SV_StartSound: channel = %i", channel); - if (sv.datagram.cursize > MAX_DATAGRAM - 21) + if (sv.datagram.size() > MAX_DATAGRAM - 21) return; // find precache number for sound @@ -227,32 +227,32 @@ void SV_StartSound(edict_t *entity, int channel, const char *sample, int volume, } //johnfitz - if (sv.datagram.cursize > MAX_DATAGRAM - 21) + if (sv.datagram.size() > MAX_DATAGRAM - 21) return; // directed messages go only to the entity the are targeted on - MSG_WriteByte(&sv.datagram, svc_sound); - MSG_WriteByte(&sv.datagram, field_mask); + sv.datagram.write_byte(svc_sound); + sv.datagram.write_byte(field_mask); if (field_mask & SND_VOLUME) - MSG_WriteByte(&sv.datagram, volume); + sv.datagram.write_byte(volume); if (field_mask & SND_ATTENUATION) - MSG_WriteByte(&sv.datagram, attenuation * 64); + sv.datagram.write_byte(attenuation * 64); //johnfitz -- PROTOCOL_FITZQUAKE if (field_mask & SND_LARGEENTITY) { - MSG_WriteShort(&sv.datagram, ent); - MSG_WriteByte(&sv.datagram, channel); + sv.datagram.write_short(ent); + sv.datagram.write_byte(channel); } else - MSG_WriteShort(&sv.datagram, (ent << 3) | channel); + sv.datagram.write_short((ent << 3) | channel); if (field_mask & SND_LARGESOUND) - MSG_WriteShort(&sv.datagram, sound_num); + sv.datagram.write_short(sound_num); else - MSG_WriteByte(&sv.datagram, sound_num); + sv.datagram.write_byte(sound_num); //johnfitz for (i = 0; i < 3; i++) - MSG_WriteCoord(&sv.datagram, entity->v.origin[i] + 0.5 * (entity->v.mins[i] + entity->v.maxs[i]), - sv.protocolflags); + sv.datagram.write_coord(entity->v.origin[i] + 0.5 * (entity->v.mins[i] + entity->v.maxs[i]), + static_cast(sv.protocolflags)); } /* @@ -279,15 +279,16 @@ void SV_LocalSound(client_t *client, const char *sample) { field_mask = SND_LARGESOUND; } - if (client->message.cursize > client->message.maxsize - 4) - return; + // TODO: replace maxsize call? + // if (client->message.size() > client->message.maxsize - 4) + // return; - MSG_WriteByte(&client->message, svc_localsound); - MSG_WriteByte(&client->message, field_mask); + client->message.write_byte(svc_localsound); + client->message.write_byte(field_mask); if (field_mask & SND_LARGESOUND) - MSG_WriteShort(&client->message, sound_num); + client->message.write_short(sound_num); else - MSG_WriteByte(&client->message, sound_num); + client->message.write_byte(sound_num); } /* @@ -315,51 +316,51 @@ void SV_SendServerinfo(client_t *client) { char message[2048]; int i; //johnfitz - MSG_WriteByte(&client->message, svc_print); + client->message.write_byte(svc_print); sprintf(message, "%c\nFITZQUAKE %1.2f SERVER (%i CRC)\n", 2, FITZQUAKE_VERSION, pr_crc); //johnfitz -- include fitzquake version - MSG_WriteString(&client->message, message); + client->message.write_string(message); - MSG_WriteByte(&client->message, svc_serverinfo); - MSG_WriteLong(&client->message, sv.protocol); //johnfitz -- sv.protocol instead of PROTOCOL_VERSION + client->message.write_byte(svc_serverinfo); + client->message.write_long(sv.protocol); //johnfitz -- sv.protocol instead of PROTOCOL_VERSION if (sv.protocol == PROTOCOL_RMQ) { // mh - now send protocol flags so that the client knows the protocol features to expect - MSG_WriteLong(&client->message, sv.protocolflags); + client->message.write_long(sv.protocolflags); } - MSG_WriteByte(&client->message, svs.maxclients); + client->message.write_byte(svs.maxclients); if (!coop.value && deathmatch.value) - MSG_WriteByte(&client->message, GAME_DEATHMATCH); + client->message.write_byte(GAME_DEATHMATCH); else - MSG_WriteByte(&client->message, GAME_COOP); + client->message.write_byte(GAME_COOP); - MSG_WriteString(&client->message, PR_GetString(sv.edicts->v.message)); + client->message.write_string(PR_GetString(sv.edicts->v.message)); //johnfitz -- only send the first 256 model and sound precaches if protocol is 15 for (i = 1, s = sv.model_precache + 1; *s; s++, i++) if (sv.protocol != PROTOCOL_NETQUAKE || i < 256) - MSG_WriteString(&client->message, *s); - MSG_WriteByte(&client->message, 0); + client->message.write_string(*s); + client->message.write_byte(0); for (i = 1, s = sv.sound_precache + 1; *s; s++, i++) if (sv.protocol != PROTOCOL_NETQUAKE || i < 256) - MSG_WriteString(&client->message, *s); - MSG_WriteByte(&client->message, 0); + client->message.write_string(*s); + client->message.write_byte(0); //johnfitz // send music - MSG_WriteByte(&client->message, svc_cdtrack); - MSG_WriteByte(&client->message, sv.edicts->v.sounds); - MSG_WriteByte(&client->message, sv.edicts->v.sounds); + client->message.write_byte(svc_cdtrack); + client->message.write_byte(sv.edicts->v.sounds); + client->message.write_byte(sv.edicts->v.sounds); // set view - MSG_WriteByte(&client->message, svc_setview); - MSG_WriteShort(&client->message, NUM_FOR_EDICT(client->edict)); + client->message.write_byte(svc_setview); + client->message.write_short(NUM_FOR_EDICT(client->edict)); - MSG_WriteByte(&client->message, svc_signonnum); - MSG_WriteByte(&client->message, 1); + client->message.write_byte(svc_signonnum); + client->message.write_byte(1); client->sendsignon = PRESPAWN_FLUSH; client->spawned = false; // need prespawn, spawn, etc @@ -401,9 +402,7 @@ void SV_ConnectClient(int clientnum) { client->active = true; client->spawned = false; client->edict = ent; - client->message.data = client->msgbuf; - client->message.maxsize = sizeof(client->msgbuf); - client->message.allowoverflow = true; // we can catch it + client->message = net::msg{}; if (sv.loadgame) memcpy(client->spawn_parms, spawn_parms, sizeof(spawn_parms)); @@ -468,7 +467,7 @@ SV_ClearDatagram ================== */ void SV_ClearDatagram(void) { - SZ_Clear(&sv.datagram); + sv.datagram.clear(); } /* @@ -571,7 +570,7 @@ SV_WriteEntitiesToClient ============= */ -void SV_WriteEntitiesToClient(edict_t *clent, sizebuf_t *msg) { +void SV_WriteEntitiesToClient(edict_t *clent, net::msg &msg) { int e, i; int bits; byte *pvs; @@ -616,15 +615,15 @@ void SV_WriteEntitiesToClient(edict_t *clent, sizebuf_t *msg) { // assumed here. And, for protocol 85 the max size is actually 24 bytes. // For float coords and angles the limit is 40. // FIXME: Use tighter limit according to protocol flags and send bits. - if (msg->cursize + 40 > msg->maxsize) { - //johnfitz -- less spammy overflow message - if (!dev_overflows.packetsize || dev_overflows.packetsize + CONSOLE_RESPAM_TIME < realtime) { - console::info("Packet overflow!\n"); - dev_overflows.packetsize = realtime; - } - goto stats; - //johnfitz - } + // if (msg->cursize + 40 > msg->maxsize) { + // //johnfitz -- less spammy overflow message + // if (!dev_overflows.packetsize || dev_overflows.packetsize + CONSOLE_RESPAM_TIME < realtime) { + // console::info("Packet overflow!\n"); + // dev_overflows.packetsize = realtime; + // } + // goto stats; + // //johnfitz + // } // send an update bits = 0; @@ -702,66 +701,66 @@ void SV_WriteEntitiesToClient(edict_t *clent, sizebuf_t *msg) { // // write the message // - MSG_WriteByte(msg, bits | U_SIGNAL); + msg.write_byte(bits | U_SIGNAL); if (bits & U_MOREBITS) - MSG_WriteByte(msg, bits >> 8); + msg.write_byte(bits >> 8); //johnfitz -- PROTOCOL_FITZQUAKE if (bits & U_EXTEND1) - MSG_WriteByte(msg, bits >> 16); + msg.write_byte(bits >> 16); if (bits & U_EXTEND2) - MSG_WriteByte(msg, bits >> 24); + msg.write_byte(bits >> 24); //johnfitz if (bits & U_LONGENTITY) - MSG_WriteShort(msg, e); + msg.write_short(e); else - MSG_WriteByte(msg, e); + msg.write_byte(e); if (bits & U_MODEL) - MSG_WriteByte(msg, ent->v.modelindex); + msg.write_byte(ent->v.modelindex); if (bits & U_FRAME) - MSG_WriteByte(msg, ent->v.frame); + msg.write_byte(ent->v.frame); if (bits & U_COLORMAP) - MSG_WriteByte(msg, ent->v.colormap); + msg.write_byte(ent->v.colormap); if (bits & U_SKIN) - MSG_WriteByte(msg, ent->v.skin); + msg.write_byte(ent->v.skin); if (bits & U_EFFECTS) - MSG_WriteByte(msg, (int) ent->v.effects & pr_effects_mask); + msg.write_byte((int) ent->v.effects & pr_effects_mask); if (bits & U_ORIGIN1) - MSG_WriteCoord(msg, ent->v.origin[0], sv.protocolflags); + msg.write_coord(ent->v.origin[0], static_cast(sv.protocolflags)); if (bits & U_ANGLE1) - MSG_WriteAngle(msg, ent->v.angles[0], sv.protocolflags); + msg.write_angle(ent->v.angles[0], static_cast(sv.protocolflags)); if (bits & U_ORIGIN2) - MSG_WriteCoord(msg, ent->v.origin[1], sv.protocolflags); + msg.write_coord(ent->v.origin[1], static_cast(sv.protocolflags)); if (bits & U_ANGLE2) - MSG_WriteAngle(msg, ent->v.angles[1], sv.protocolflags); + msg.write_angle(ent->v.angles[1], static_cast(sv.protocolflags)); if (bits & U_ORIGIN3) - MSG_WriteCoord(msg, ent->v.origin[2], sv.protocolflags); + msg.write_coord(ent->v.origin[2], static_cast(sv.protocolflags)); if (bits & U_ANGLE3) - MSG_WriteAngle(msg, ent->v.angles[2], sv.protocolflags); + msg.write_angle(ent->v.angles[2], static_cast(sv.protocolflags)); //johnfitz -- PROTOCOL_FITZQUAKE if (bits & U_ALPHA) - MSG_WriteByte(msg, ent->alpha); + msg.write_byte(ent->alpha); if (bits & U_SCALE) - MSG_WriteByte(msg, ent->scale); + msg.write_byte(ent->scale); if (bits & U_FRAME2) - MSG_WriteByte(msg, (int) ent->v.frame >> 8); + msg.write_byte((int) ent->v.frame >> 8); if (bits & U_MODEL2) - MSG_WriteByte(msg, (int) ent->v.modelindex >> 8); + msg.write_byte((int) ent->v.modelindex >> 8); if (bits & U_LERPFINISH) - MSG_WriteByte(msg, (byte) (Q_rint((ent->v.nextthink-sv.time)*255))); + msg.write_byte((byte) (Q_rint((ent->v.nextthink-sv.time)*255))); //johnfitz } //johnfitz -- devstats stats: - if (msg->cursize > 1024 && dev_peakstats.packetsize <= 1024) - console::dev_warn("%i byte packet exceeds standard limit of 1024 (max = %d).\n", msg->cursize, msg->maxsize); - dev_stats.packetsize = msg->cursize; - dev_peakstats.packetsize = std::max(msg->cursize, dev_peakstats.packetsize); + if (msg.size() > 1024 && dev_peakstats.packetsize <= 1024) + console::dev_warn("%i byte packet exceeds standard limit of 1024 (max = %d).\n", msg.size(), 1024); // TODO + dev_stats.packetsize = msg.size(); + dev_peakstats.packetsize = std::max(static_cast(msg.size()), dev_peakstats.packetsize); //johnfitz } @@ -787,7 +786,7 @@ SV_WriteClientdataToMessage ================== */ -void SV_WriteClientdataToMessage(edict_t *ent, sizebuf_t *msg) { +void SV_WriteClientdataToMessage(edict_t *ent, net::msg &msg) { int bits; int i; edict_t *other; @@ -799,11 +798,11 @@ void SV_WriteClientdataToMessage(edict_t *ent, sizebuf_t *msg) { // if (ent->v.dmg_take || ent->v.dmg_save) { other = PROG_TO_EDICT(ent->v.dmg_inflictor); - MSG_WriteByte(msg, svc_damage); - MSG_WriteByte(msg, ent->v.dmg_save); - MSG_WriteByte(msg, ent->v.dmg_take); + msg.write_byte(svc_damage); + msg.write_byte(ent->v.dmg_save); + msg.write_byte(ent->v.dmg_take); for (i = 0; i < 3; i++) - MSG_WriteCoord(msg, other->v.origin[i] + 0.5 * (other->v.mins[i] + other->v.maxs[i]), sv.protocolflags); + msg.write_coord(other->v.origin[i] + 0.5 * (other->v.mins[i] + other->v.maxs[i]), static_cast(sv.protocolflags)); ent->v.dmg_take = 0; ent->v.dmg_save = 0; @@ -816,9 +815,9 @@ void SV_WriteClientdataToMessage(edict_t *ent, sizebuf_t *msg) { // a fixangle might get lost in a dropped packet. Oh well. if (ent->v.fixangle) { - MSG_WriteByte(msg, svc_setangle); + msg.write_byte(svc_setangle); for (i = 0; i < 3; i++) - MSG_WriteAngle(msg, ent->v.angles[i], sv.protocolflags); + msg.write_angle(ent->v.angles[i], static_cast(sv.protocolflags)); ent->v.fixangle = 0; } @@ -882,50 +881,50 @@ void SV_WriteClientdataToMessage(edict_t *ent, sizebuf_t *msg) { // send the data - MSG_WriteByte(msg, svc_clientdata); - MSG_WriteShort(msg, bits); + msg.write_byte(svc_clientdata); + msg.write_short(bits); //johnfitz -- PROTOCOL_FITZQUAKE - if (bits & SU_EXTEND1) MSG_WriteByte(msg, bits >> 16); - if (bits & SU_EXTEND2) MSG_WriteByte(msg, bits >> 24); + if (bits & SU_EXTEND1) msg.write_byte(bits >> 16); + if (bits & SU_EXTEND2) msg.write_byte(bits >> 24); //johnfitz if (bits & SU_VIEWHEIGHT) - MSG_WriteChar(msg, ent->v.view_ofs[2]); + msg.write_char(ent->v.view_ofs[2]); if (bits & SU_IDEALPITCH) - MSG_WriteChar(msg, ent->v.idealpitch); + msg.write_char(ent->v.idealpitch); for (i = 0; i < 3; i++) { if (bits & (SU_PUNCH1 << i)) - MSG_WriteChar(msg, ent->v.punchangle[i]); + msg.write_char(ent->v.punchangle[i]); if (bits & (SU_VELOCITY1 << i)) - MSG_WriteChar(msg, ent->v.velocity[i] / 16); + msg.write_char(ent->v.velocity[i] / 16); } // [always sent] if (bits & SU_ITEMS) - MSG_WriteLong(msg, items); + msg.write_long(items); if (bits & SU_WEAPONFRAME) - MSG_WriteByte(msg, ent->v.weaponframe); + msg.write_byte(ent->v.weaponframe); if (bits & SU_ARMOR) - MSG_WriteByte(msg, ent->v.armorvalue); + msg.write_byte(ent->v.armorvalue); if (bits & SU_WEAPON) - MSG_WriteByte(msg, SV_ModelIndex(PR_GetString(ent->v.weaponmodel))); + msg.write_byte(SV_ModelIndex(PR_GetString(ent->v.weaponmodel))); - MSG_WriteShort(msg, ent->v.health); - MSG_WriteByte(msg, ent->v.currentammo); - MSG_WriteByte(msg, ent->v.ammo_shells); - MSG_WriteByte(msg, ent->v.ammo_nails); - MSG_WriteByte(msg, ent->v.ammo_rockets); - MSG_WriteByte(msg, ent->v.ammo_cells); + msg.write_short(ent->v.health); + msg.write_byte(ent->v.currentammo); + msg.write_byte(ent->v.ammo_shells); + msg.write_byte(ent->v.ammo_nails); + msg.write_byte(ent->v.ammo_rockets); + msg.write_byte(ent->v.ammo_cells); if (standard_quake) { - MSG_WriteByte(msg, ent->v.weapon); + msg.write_byte(ent->v.weapon); } else { for (i = 0; i < 32; i++) { if (((int) ent->v.weapon) & (1 << i)) { - MSG_WriteByte(msg, i); + msg.write_byte(i); break; } } @@ -933,23 +932,23 @@ void SV_WriteClientdataToMessage(edict_t *ent, sizebuf_t *msg) { //johnfitz -- PROTOCOL_FITZQUAKE if (bits & SU_WEAPON2) - MSG_WriteByte(msg, SV_ModelIndex(PR_GetString(ent->v.weaponmodel)) >> 8); + msg.write_byte(SV_ModelIndex(PR_GetString(ent->v.weaponmodel)) >> 8); if (bits & SU_ARMOR2) - MSG_WriteByte(msg, (int) ent->v.armorvalue >> 8); + msg.write_byte((int) ent->v.armorvalue >> 8); if (bits & SU_AMMO2) - MSG_WriteByte(msg, (int) ent->v.currentammo >> 8); + msg.write_byte((int) ent->v.currentammo >> 8); if (bits & SU_SHELLS2) - MSG_WriteByte(msg, (int) ent->v.ammo_shells >> 8); + msg.write_byte((int) ent->v.ammo_shells >> 8); if (bits & SU_NAILS2) - MSG_WriteByte(msg, (int) ent->v.ammo_nails >> 8); + msg.write_byte((int) ent->v.ammo_nails >> 8); if (bits & SU_ROCKETS2) - MSG_WriteByte(msg, (int) ent->v.ammo_rockets >> 8); + msg.write_byte((int) ent->v.ammo_rockets >> 8); if (bits & SU_CELLS2) - MSG_WriteByte(msg, (int) ent->v.ammo_cells >> 8); + msg.write_byte((int) ent->v.ammo_cells >> 8); if (bits & SU_WEAPONFRAME2) - MSG_WriteByte(msg, (int) ent->v.weaponframe >> 8); + msg.write_byte((int) ent->v.weaponframe >> 8); if (bits & SU_WEAPONALPHA) - MSG_WriteByte(msg, ent->alpha); //for now, weaponalpha = client entity alpha + msg.write_byte(ent->alpha); //for now, weaponalpha = client entity alpha //johnfitz } @@ -960,31 +959,27 @@ SV_SendClientDatagram */ bool SV_SendClientDatagram(client_t *client) { byte buf[MAX_DATAGRAM]; - sizebuf_t msg; - - msg.data = buf; - msg.maxsize = sizeof(buf); - msg.cursize = 0; + net::msg msg{}; //johnfitz -- if client is nonlocal, use smaller max size so packets aren't fragmented - if (std::strcmp(NET_QSocketGetAddressString(client->netconnection), "LOCAL") != 0) - msg.maxsize = DATAGRAM_MTU; + // if (std::strcmp(NET_QSocketGetAddressString(client->netconnection), "LOCAL") != 0) + // msg.maxsize = DATAGRAM_MTU; //johnfitz - MSG_WriteByte(&msg, svc_time); - MSG_WriteFloat(&msg, sv.time); + msg.write_byte(svc_time); + msg.write_float(sv.time); // add the client specific data to the datagram - SV_WriteClientdataToMessage(client->edict, &msg); + SV_WriteClientdataToMessage(client->edict, msg); - SV_WriteEntitiesToClient(client->edict, &msg); + SV_WriteEntitiesToClient(client->edict, msg); // copy the server datagram if there is space - if (msg.cursize + sv.datagram.cursize < msg.maxsize) - SZ_Write(&msg, sv.datagram.data, sv.datagram.cursize); + // if (msg.cursize + sv.datagram.cursize < msg.maxsize) + msg.write(sv.datagram.data(), sv.datagram.size()); // send the datagram - if (NET_SendUnreliableMessage(client->netconnection, &msg) == -1) { + if (NET_SendUnreliableMessage(client->netconnection, msg) == -1) { SV_DropClient(true); // if the message couldn't send, kick off return false; } @@ -1007,9 +1002,9 @@ void SV_UpdateToReliableMessages(void) { for (j = 0, client = svs.clients; j < svs.maxclients; j++, client++) { if (!client->active) continue; - MSG_WriteByte(&client->message, svc_updatefrags); - MSG_WriteByte(&client->message, i); - MSG_WriteShort(&client->message, host_client->edict->v.frags); + client->message.write_byte(svc_updatefrags); + client->message.write_byte(i); + client->message.write_short(host_client->edict->v.frags); } host_client->old_frags = host_client->edict->v.frags; @@ -1019,10 +1014,10 @@ void SV_UpdateToReliableMessages(void) { for (j = 0, client = svs.clients; j < svs.maxclients; j++, client++) { if (!client->active) continue; - SZ_Write(&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize); + client->message.write(sv.reliable_datagram.data(), sv.reliable_datagram.size()); } - SZ_Clear(&sv.reliable_datagram); + sv.reliable_datagram.clear(); } @@ -1035,16 +1030,11 @@ message buffer ======================= */ void SV_SendNop(client_t *client) { - sizebuf_t msg; - byte buf[4]; + net::msg msg{}; - msg.data = buf; - msg.maxsize = sizeof(buf); - msg.cursize = 0; + msg.write_char(svc_nop); - MSG_WriteChar(&msg, svc_nop); - - if (NET_SendUnreliableMessage(client->netconnection, &msg) == -1) + if (NET_SendUnreliableMessage(client->netconnection, msg) == -1) SV_DropClient(true); // if the message couldn't send, kick off client->last_message = realtime; } @@ -1081,11 +1071,12 @@ void SV_SendClientMessages(void) { } if (host_client->sendsignon == PRESPAWN_SIGNONBUFS) { bool local = SV_IsLocalClient(host_client); - while (host_client->signonidx < sv.num_signon_buffers) { - sizebuf_t *signon = sv.signon_buffers[host_client->signonidx]; - if (host_client->message.cursize + signon->cursize > host_client->message.maxsize) - break; - SZ_Write(&host_client->message, signon->data, signon->cursize); + while (host_client->signonidx < sv.signons.size()) { + auto signon = sv.signons[host_client->signonidx]; + // if (host_client->message.cursize + signon->cursize > host_client->message.maxsize) + // break; + + host_client->message.write(signon.data(), signon.size()); host_client->signonidx++; // only send multiple buffers at once when playing locally, // otherwise we send one signon at a time to avoid overflowing @@ -1093,28 +1084,28 @@ void SV_SendClientMessages(void) { if (!local) break; } - if (host_client->signonidx == sv.num_signon_buffers) + if (host_client->signonidx == sv.signons.size()) host_client->sendsignon = PRESPAWN_SIGNONMSG; } if (host_client->sendsignon == PRESPAWN_SIGNONMSG) { - if (host_client->message.cursize + 2 < host_client->message.maxsize) { - MSG_WriteByte(&host_client->message, svc_signonnum); - MSG_WriteByte(&host_client->message, 2); + //if (host_client->message.siz + 2 < host_client->message.maxsize) { + host_client->message.write_byte(svc_signonnum); + host_client->message.write_byte(2); host_client->sendsignon = PRESPAWN_FLUSH; - } + //} } } // check for an overflowed message. Should only happen // on a very fucked up connection that backs up a lot, then // changes level - if (host_client->message.overflowed) { - SV_DropClient(true); - host_client->message.overflowed = false; - continue; - } + // if (host_client->message.overflowed) { + // SV_DropClient(true); + // host_client->message.overflowed = false; + // continue; + // } - if (host_client->message.cursize || host_client->dropasap) { + if (!host_client->message.empty() || host_client->dropasap) { if (!NET_CanSendMessage(host_client->netconnection)) { // I_Printf ("can't write\n"); continue; @@ -1124,9 +1115,9 @@ void SV_SendClientMessages(void) { SV_DropClient(false); // went to another level else { if (NET_SendMessage(host_client->netconnection - , &host_client->message) == -1) + , host_client->message) == -1) SV_DropClient(true); // if the message couldn't send, kick off - SZ_Clear(&host_client->message); + host_client->message.clear(); host_client->last_message = realtime; if (host_client->sendsignon == PRESPAWN_FLUSH) host_client->sendsignon = PRESPAWN_DONE; @@ -1156,15 +1147,11 @@ SV_AddSignonBuffer ================ */ static void SV_AddSignonBuffer(void) { - sizebuf_t *sb; - if (sv.num_signon_buffers >= MAX_SIGNON_BUFFERS) + if (sv.signons.size() >= MAX_SIGNON_BUFFERS) Host_Error("SV_AddSignonBuffer overflow\n"); - sb = (sizebuf_t *) Hunk_AllocName(sizeof(sizebuf_t) + SIGNON_SIZE, "signon"); - sb->data = (byte *) (sb + 1); - sb->maxsize = SIGNON_SIZE; - sv.signon_buffers[sv.num_signon_buffers++] = sb; - sv.signon = sb; + sv.signons.emplace_back(); + sv.current_signon = sv.signons.size() - 1; } /* @@ -1173,7 +1160,7 @@ SV_ReserveSignonSpace ================ */ void SV_ReserveSignonSpace(int numbytes) { - if (sv.signon->cursize + numbytes > sv.signon->maxsize) + if (sv.signons[sv.current_signon].size() + numbytes > SIGNON_SIZE) SV_AddSignonBuffer(); } @@ -1271,42 +1258,42 @@ void SV_CreateBaseline(void) { //johnfitz -- PROTOCOL_FITZQUAKE if (bits) - MSG_WriteByte(sv.signon, svc_spawnbaseline2); + sv.signons[sv.current_signon].write_byte(svc_spawnbaseline2); else - MSG_WriteByte(sv.signon, svc_spawnbaseline); + sv.signons[sv.current_signon].write_byte(svc_spawnbaseline); //johnfitz - MSG_WriteShort(sv.signon, entnum); + sv.signons[sv.current_signon].write_short(entnum); //johnfitz -- PROTOCOL_FITZQUAKE if (bits) - MSG_WriteByte(sv.signon, bits); + sv.signons[sv.current_signon].write_byte(bits); if (bits & B_LARGEMODEL) - MSG_WriteShort(sv.signon, svent->baseline.modelindex); + sv.signons[sv.current_signon].write_short(svent->baseline.modelindex); else - MSG_WriteByte(sv.signon, svent->baseline.modelindex); + sv.signons[sv.current_signon].write_byte(svent->baseline.modelindex); if (bits & B_LARGEFRAME) - MSG_WriteShort(sv.signon, svent->baseline.frame); + sv.signons[sv.current_signon].write_short(svent->baseline.frame); else - MSG_WriteByte(sv.signon, svent->baseline.frame); + sv.signons[sv.current_signon].write_byte(svent->baseline.frame); //johnfitz - MSG_WriteByte(sv.signon, svent->baseline.colormap); - MSG_WriteByte(sv.signon, svent->baseline.skin); + sv.signons[sv.current_signon].write_byte(svent->baseline.colormap); + sv.signons[sv.current_signon].write_byte(svent->baseline.skin); for (i = 0; i < 3; i++) { - MSG_WriteCoord(sv.signon, svent->baseline.origin[i], sv.protocolflags); - MSG_WriteAngle(sv.signon, svent->baseline.angles[i], sv.protocolflags); + sv.signons[sv.current_signon].write_coord(svent->baseline.origin[i], static_cast(sv.protocolflags)); + sv.signons[sv.current_signon].write_angle(svent->baseline.angles[i], static_cast(sv.protocolflags)); } //johnfitz -- PROTOCOL_FITZQUAKE if (bits & B_ALPHA) - MSG_WriteByte(sv.signon, svent->baseline.alpha); + sv.signons[sv.current_signon].write_byte(svent->baseline.alpha); //johnfitz if (bits & B_SCALE) - MSG_WriteByte(sv.signon, svent->baseline.scale); + sv.signons[sv.current_signon].write_byte(svent->baseline.scale); } } @@ -1320,15 +1307,11 @@ Tell all the clients that the server is changing levels */ void SV_SendReconnect(void) { byte data[128]; - sizebuf_t msg; + net::msg msg{}; - msg.data = data; - msg.cursize = 0; - msg.maxsize = sizeof(data); - - MSG_WriteChar(&msg, svc_stufftext); - MSG_WriteString(&msg, "reconnect\n"); - NET_SendToAll(&msg, 5.0); + msg.write_char( svc_stufftext); + msg.write_string("reconnect\n"); + NET_SendToAll(msg, 5.0); if (!isDedicated) command::execute_string("reconnect\n", command::source::command); @@ -1427,13 +1410,8 @@ void SV_SpawnServer(const char *server) { sv.max_edicts = std::clamp((int) max_edicts.value,MIN_EDICTS, MAX_EDICTS); //johnfitz -- max_edicts cvar sv.edicts = (edict_t *) malloc(sv.max_edicts * pr_edict_size); // ericw -- sv.edicts switched to use malloc() - sv.datagram.maxsize = sizeof(sv.datagram_buf); - sv.datagram.cursize = 0; - sv.datagram.data = sv.datagram_buf; - - sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf); - sv.reliable_datagram.cursize = 0; - sv.reliable_datagram.data = sv.reliable_datagram_buf; + sv.datagram = net::msg{}; + sv.reliable_datagram = net::msg{}; SV_AddSignonBuffer(); @@ -1510,8 +1488,8 @@ void SV_SpawnServer(const char *server) { SV_CreateBaseline(); //johnfitz -- warn if signon buffer larger than standard server can handle - for (i = 0, signonsize = 0; i < sv.num_signon_buffers; i++) - signonsize += sv.signon_buffers[i]->cursize; + for (i = 0, signonsize = 0; i < sv.signons.size(); i++) + signonsize += sv.signons[i].size(); if (signonsize > 64000 - 2) console::dev_warn("%i byte signon buffer exceeds QS limit of 63998.\n", signonsize); else if (signonsize > 8000 - 2) diff --git a/Quake/sv_user.cpp b/Quake/sv_user.cpp index cd24c5b..a1dc55e 100644 --- a/Quake/sv_user.cpp +++ b/Quake/sv_user.cpp @@ -23,25 +23,25 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.hpp" -edict_t *sv_player; +edict_t *sv_player; -extern convar sv_friction; -convar sv_edgefriction{"edgefriction", "2"}; -extern convar sv_stopspeed; +extern convar sv_friction; +convar sv_edgefriction{"edgefriction", "2"}; +extern convar sv_stopspeed; -static vec3_t forward, right, up; +static vec3_t forward, right, up; // world -float *angles; -float *origin; -float *velocity; +float *angles; +float *origin; +float *velocity; -bool onground; +bool onground; -usercmd_t cmd; +usercmd_t cmd; -convar sv_idealpitchscale{"sv_idealpitchscale","0.8"}; -convar sv_altnoclip{"sv_altnoclip","1",{.archive = true}}; //johnfitz +convar sv_idealpitchscale{"sv_idealpitchscale", "0.8"}; +convar sv_altnoclip{"sv_altnoclip", "1", {.archive = true}}; //johnfitz /* =============== @@ -49,66 +49,63 @@ SV_SetIdealPitch =============== */ #define MAX_FORWARD 6 -void SV_SetIdealPitch (void) -{ - float angleval, sinval, cosval; - trace_t tr; - vec3_t top, bottom; - float z[MAX_FORWARD]; - int i, j; - int step, dir, steps; - if (!((int)sv_player->v.flags & FL_ONGROUND)) - return; +void SV_SetIdealPitch(void) { + float angleval, sinval, cosval; + trace_t tr; + vec3_t top, bottom; + float z[MAX_FORWARD]; + int i, j; + int step, dir, steps; - angleval = sv_player->v.angles[YAW] * M_PI*2 / 360; - sinval = sin(angleval); - cosval = cos(angleval); + if (!((int) sv_player->v.flags & FL_ONGROUND)) + return; - for (i=0 ; iv.origin[0] + cosval*(i+3)*12; - top[1] = sv_player->v.origin[1] + sinval*(i+3)*12; - top[2] = sv_player->v.origin[2] + sv_player->v.view_ofs[2]; + angleval = sv_player->v.angles[YAW] * M_PI * 2 / 360; + sinval = sin(angleval); + cosval = cos(angleval); - bottom[0] = top[0]; - bottom[1] = top[1]; - bottom[2] = top[2] - 160; + for (i = 0; i < MAX_FORWARD; i++) { + top[0] = sv_player->v.origin[0] + cosval * (i + 3) * 12; + top[1] = sv_player->v.origin[1] + sinval * (i + 3) * 12; + top[2] = sv_player->v.origin[2] + sv_player->v.view_ofs[2]; - tr = SV_Move (top, vec3_origin, vec3_origin, bottom, 1, sv_player); - if (tr.allsolid) - return; // looking at a wall, leave ideal the way is was + bottom[0] = top[0]; + bottom[1] = top[1]; + bottom[2] = top[2] - 160; - if (tr.fraction == 1) - return; // near a dropoff + tr = SV_Move(top, vec3_origin, vec3_origin, bottom, 1, sv_player); + if (tr.allsolid) + return; // looking at a wall, leave ideal the way is was - z[i] = top[2] + tr.fraction*(bottom[2]-top[2]); - } + if (tr.fraction == 1) + return; // near a dropoff - dir = 0; - steps = 0; - for (j=1 ; j -ON_EPSILON && step < ON_EPSILON) - continue; + z[i] = top[2] + tr.fraction * (bottom[2] - top[2]); + } - if (dir && ( step-dir > ON_EPSILON || step-dir < -ON_EPSILON ) ) - return; // mixed changes + dir = 0; + steps = 0; + for (j = 1; j < i; j++) { + step = z[j] - z[j - 1]; + if (step > -ON_EPSILON && step < ON_EPSILON) + continue; - steps++; - dir = step; - } + if (dir && (step - dir > ON_EPSILON || step - dir < -ON_EPSILON)) + return; // mixed changes - if (!dir) - { - sv_player->v.idealpitch = 0; - return; - } + steps++; + dir = step; + } - if (steps < 2) - return; - sv_player->v.idealpitch = -dir * sv_idealpitchscale.value; + if (!dir) { + sv_player->v.idealpitch = 0; + return; + } + + if (steps < 2) + return; + sv_player->v.idealpitch = -dir * sv_idealpitchscale.value; } @@ -118,44 +115,43 @@ SV_UserFriction ================== */ -void SV_UserFriction (void) -{ - float *vel; - float speed, newspeed, control; - vec3_t start, stop; - float friction; - trace_t trace; +void SV_UserFriction(void) { + float *vel; + float speed, newspeed, control; + vec3_t start, stop; + float friction; + trace_t trace; - vel = velocity; + vel = velocity; - speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]); - if (!speed) - return; + speed = sqrt(vel[0] * vel[0] + vel[1] * vel[1]); + if (!speed) + return; -// if the leading edge is over a dropoff, increase friction - start[0] = stop[0] = origin[0] + vel[0]/speed*16; - start[1] = stop[1] = origin[1] + vel[1]/speed*16; - start[2] = origin[2] + sv_player->v.mins[2]; - stop[2] = start[2] - 34; + // if the leading edge is over a dropoff, increase friction + start[0] = stop[0] = origin[0] + vel[0] / speed * 16; + start[1] = stop[1] = origin[1] + vel[1] / speed * 16; + start[2] = origin[2] + sv_player->v.mins[2]; + stop[2] = start[2] - 34; - trace = SV_Move (start, vec3_origin, vec3_origin, stop, true, sv_player); + trace = SV_Move(start, vec3_origin, vec3_origin, stop, true, sv_player); - if (trace.fraction == 1.0) - friction = sv_friction.value*sv_edgefriction.value; - else - friction = sv_friction.value; + if (trace.fraction == 1.0) + friction = sv_friction.value * sv_edgefriction.value; + else + friction = sv_friction.value; -// apply friction - control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed; - newspeed = speed - host_frametime*control*friction; + // apply friction + control = speed < sv_stopspeed.value ? sv_stopspeed.value : speed; + newspeed = speed - host_frametime * control * friction; - if (newspeed < 0) - newspeed = 0; - newspeed /= speed; + if (newspeed < 0) + newspeed = 0; + newspeed /= speed; - vel[0] = vel[0] * newspeed; - vel[1] = vel[1] * newspeed; - vel[2] = vel[2] * newspeed; + vel[0] = vel[0] * newspeed; + vel[1] = vel[1] * newspeed; + vel[2] = vel[2] * newspeed; } /* @@ -163,57 +159,55 @@ void SV_UserFriction (void) SV_Accelerate ============== */ -convar sv_maxspeed{"sv_maxspeed", "320", {.notify = true,. server_info = true}}; -convar sv_accelerate{"sv_accelerate", "10"}; -void SV_Accelerate (float wishspeed, const vec3_t wishdir) -{ - int i; - float addspeed, accelspeed, currentspeed; +convar sv_maxspeed{"sv_maxspeed", "320", {.notify = true, . server_info = true}}; +convar sv_accelerate{"sv_accelerate", "10"}; - currentspeed = DotProduct (velocity, wishdir); - addspeed = wishspeed - currentspeed; - if (addspeed <= 0) - return; - accelspeed = sv_accelerate.value*host_frametime*wishspeed; - if (accelspeed > addspeed) - accelspeed = addspeed; +void SV_Accelerate(float wishspeed, const vec3_t wishdir) { + int i; + float addspeed, accelspeed, currentspeed; - for (i=0 ; i<3 ; i++) - velocity[i] += accelspeed*wishdir[i]; + currentspeed = DotProduct(velocity, wishdir); + addspeed = wishspeed - currentspeed; + if (addspeed <= 0) + return; + accelspeed = sv_accelerate.value * host_frametime * wishspeed; + if (accelspeed > addspeed) + accelspeed = addspeed; + + for (i = 0; i < 3; i++) + velocity[i] += accelspeed * wishdir[i]; } -void SV_AirAccelerate (float wishspeed, vec3_t wishveloc) -{ - int i; - float addspeed, wishspd, accelspeed, currentspeed; +void SV_AirAccelerate(float wishspeed, vec3_t wishveloc) { + int i; + float addspeed, wishspd, accelspeed, currentspeed; - wishspd = VectorNormalize (wishveloc); - if (wishspd > 30) - wishspd = 30; - currentspeed = DotProduct (velocity, wishveloc); - addspeed = wishspd - currentspeed; - if (addspeed <= 0) - return; -// accelspeed = sv_accelerate.value * host_frametime; - accelspeed = sv_accelerate.value*wishspeed * host_frametime; - if (accelspeed > addspeed) - accelspeed = addspeed; + wishspd = VectorNormalize(wishveloc); + if (wishspd > 30) + wishspd = 30; + currentspeed = DotProduct(velocity, wishveloc); + addspeed = wishspd - currentspeed; + if (addspeed <= 0) + return; + // accelspeed = sv_accelerate.value * host_frametime; + accelspeed = sv_accelerate.value * wishspeed * host_frametime; + if (accelspeed > addspeed) + accelspeed = addspeed; - for (i=0 ; i<3 ; i++) - velocity[i] += accelspeed*wishveloc[i]; + for (i = 0; i < 3; i++) + velocity[i] += accelspeed * wishveloc[i]; } -void DropPunchAngle (void) -{ - float len; +void DropPunchAngle(void) { + float len; - len = VectorNormalize (sv_player->v.punchangle); + len = VectorNormalize(sv_player->v.punchangle); - len -= 10*host_frametime; - if (len < 0) - len = 0; - VectorScale (sv_player->v.punchangle, len, sv_player->v.punchangle); + len -= 10 * host_frametime; + if (len < 0) + len = 0; + VectorScale(sv_player->v.punchangle, len, sv_player->v.punchangle); } /* @@ -222,76 +216,70 @@ SV_WaterMove =================== */ -void SV_WaterMove (void) -{ - int i; - vec3_t wishvel; - float speed, newspeed, wishspeed, addspeed, accelspeed; +void SV_WaterMove(void) { + int i; + vec3_t wishvel; + float speed, newspeed, wishspeed, addspeed, accelspeed; -// -// user intentions -// - AngleVectors (sv_player->v.v_angle, forward, right, up); + // + // user intentions + // + AngleVectors(sv_player->v.v_angle, forward, right, up); - for (i=0 ; i<3 ; i++) - wishvel[i] = forward[i]*cmd.forwardmove + right[i]*cmd.sidemove; + for (i = 0; i < 3; i++) + wishvel[i] = forward[i] * cmd.forwardmove + right[i] * cmd.sidemove; - if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove) - wishvel[2] -= 60; // drift towards bottom - else - wishvel[2] += cmd.upmove; + if (!cmd.forwardmove && !cmd.sidemove && !cmd.upmove) + wishvel[2] -= 60; // drift towards bottom + else + wishvel[2] += cmd.upmove; - wishspeed = VectorLength(wishvel); - if (wishspeed > sv_maxspeed.value) - { - VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel); - wishspeed = sv_maxspeed.value; - } - wishspeed *= 0.7; + wishspeed = VectorLength(wishvel); + if (wishspeed > sv_maxspeed.value) { + VectorScale(wishvel, sv_maxspeed.value / wishspeed, wishvel); + wishspeed = sv_maxspeed.value; + } + wishspeed *= 0.7; -// -// water friction -// - speed = VectorLength (velocity); - if (speed) - { - newspeed = speed - host_frametime * speed * sv_friction.value; - if (newspeed < 0) - newspeed = 0; - VectorScale (velocity, newspeed/speed, velocity); - } - else - newspeed = 0; + // + // water friction + // + speed = VectorLength(velocity); + if (speed) { + newspeed = speed - host_frametime * speed * sv_friction.value; + if (newspeed < 0) + newspeed = 0; + VectorScale(velocity, newspeed / speed, velocity); + } else + newspeed = 0; -// -// water acceleration -// - if (!wishspeed) - return; + // + // water acceleration + // + if (!wishspeed) + return; - addspeed = wishspeed - newspeed; - if (addspeed <= 0) - return; + addspeed = wishspeed - newspeed; + if (addspeed <= 0) + return; - VectorNormalize (wishvel); - accelspeed = sv_accelerate.value * wishspeed * host_frametime; - if (accelspeed > addspeed) - accelspeed = addspeed; + VectorNormalize(wishvel); + accelspeed = sv_accelerate.value * wishspeed * host_frametime; + if (accelspeed > addspeed) + accelspeed = addspeed; - for (i=0 ; i<3 ; i++) - velocity[i] += accelspeed * wishvel[i]; + for (i = 0; i < 3; i++) + velocity[i] += accelspeed * wishvel[i]; } -void SV_WaterJump (void) -{ - if (sv.time > sv_player->v.teleport_time - || !sv_player->v.waterlevel) - { - sv_player->v.flags = (int)sv_player->v.flags & ~FL_WATERJUMP; - sv_player->v.teleport_time = 0; - } - sv_player->v.velocity[0] = sv_player->v.movedir[0]; - sv_player->v.velocity[1] = sv_player->v.movedir[1]; +void SV_WaterJump(void) { + if (sv.time > sv_player->v.teleport_time + || !sv_player->v.waterlevel) { + sv_player->v.flags = (int) sv_player->v.flags & ~FL_WATERJUMP; + sv_player->v.teleport_time = 0; + } + sv_player->v.velocity[0] = sv_player->v.movedir[0]; + sv_player->v.velocity[1] = sv_player->v.movedir[1]; } /* @@ -301,20 +289,18 @@ SV_NoclipMove -- johnfitz new, alternate noclip. old noclip is still handled in SV_AirMove =================== */ -void SV_NoclipMove (void) -{ - AngleVectors (sv_player->v.v_angle, forward, right, up); +void SV_NoclipMove(void) { + AngleVectors(sv_player->v.v_angle, forward, right, up); - velocity[0] = forward[0]*cmd.forwardmove + right[0]*cmd.sidemove; - velocity[1] = forward[1]*cmd.forwardmove + right[1]*cmd.sidemove; - velocity[2] = forward[2]*cmd.forwardmove + right[2]*cmd.sidemove; - velocity[2] += cmd.upmove*2; //doubled to match running speed + velocity[0] = forward[0] * cmd.forwardmove + right[0] * cmd.sidemove; + velocity[1] = forward[1] * cmd.forwardmove + right[1] * cmd.sidemove; + velocity[2] = forward[2] * cmd.forwardmove + right[2] * cmd.sidemove; + velocity[2] += cmd.upmove * 2; //doubled to match running speed - if (VectorLength (velocity) > sv_maxspeed.value) - { - VectorNormalize (velocity); - VectorScale (velocity, sv_maxspeed.value, velocity); - } + if (VectorLength(velocity) > sv_maxspeed.value) { + VectorNormalize(velocity); + VectorScale(velocity, sv_maxspeed.value, velocity); + } } /* @@ -322,51 +308,46 @@ void SV_NoclipMove (void) SV_AirMove =================== */ -void SV_AirMove (void) -{ - int i; - vec3_t wishvel, wishdir; - float wishspeed; - float fmove, smove; +void SV_AirMove(void) { + int i; + vec3_t wishvel, wishdir; + float wishspeed; + float fmove, smove; - AngleVectors (sv_player->v.angles, forward, right, up); + AngleVectors(sv_player->v.angles, forward, right, up); - fmove = cmd.forwardmove; - smove = cmd.sidemove; + fmove = cmd.forwardmove; + smove = cmd.sidemove; -// hack to not let you back into teleporter - if (sv.time < sv_player->v.teleport_time && fmove < 0) - fmove = 0; + // hack to not let you back into teleporter + if (sv.time < sv_player->v.teleport_time && fmove < 0) + fmove = 0; - for (i=0 ; i<3 ; i++) - wishvel[i] = forward[i]*fmove + right[i]*smove; + for (i = 0; i < 3; i++) + wishvel[i] = forward[i] * fmove + right[i] * smove; - if ( (int)sv_player->v.movetype != MOVETYPE_WALK) - wishvel[2] = cmd.upmove; - else - wishvel[2] = 0; + if ((int) sv_player->v.movetype != MOVETYPE_WALK) + wishvel[2] = cmd.upmove; + else + wishvel[2] = 0; - VectorCopy (wishvel, wishdir); - wishspeed = VectorNormalize(wishdir); - if (wishspeed > sv_maxspeed.value) - { - VectorScale (wishvel, sv_maxspeed.value/wishspeed, wishvel); - wishspeed = sv_maxspeed.value; - } + VectorCopy(wishvel, wishdir); + wishspeed = VectorNormalize(wishdir); + if (wishspeed > sv_maxspeed.value) { + VectorScale(wishvel, sv_maxspeed.value / wishspeed, wishvel); + wishspeed = sv_maxspeed.value; + } - if ( sv_player->v.movetype == MOVETYPE_NOCLIP) - { // noclip - VectorCopy (wishvel, velocity); - } - else if ( onground ) - { - SV_UserFriction (); - SV_Accelerate (wishspeed, wishdir); - } - else - { // not on ground, so little effect on velocity - SV_AirAccelerate (wishspeed, wishvel); - } + if (sv_player->v.movetype == MOVETYPE_NOCLIP) { + // noclip + VectorCopy(wishvel, velocity); + } else if (onground) { + SV_UserFriction(); + SV_Accelerate(wishspeed, wishdir); + } else { + // not on ground, so little effect on velocity + SV_AirAccelerate(wishspeed, wishvel); + } } /* @@ -377,56 +358,53 @@ the move fields specify an intended velocity in pix/sec the angle fields specify an exact angular motion in degrees =================== */ -void SV_ClientThink (void) -{ - vec3_t v_angle; +void SV_ClientThink(void) { + vec3_t v_angle; - if (sv_player->v.movetype == MOVETYPE_NONE) - return; + if (sv_player->v.movetype == MOVETYPE_NONE) + return; - onground = (int)sv_player->v.flags & FL_ONGROUND; + onground = (int) sv_player->v.flags & FL_ONGROUND; - origin = sv_player->v.origin; - velocity = sv_player->v.velocity; + origin = sv_player->v.origin; + velocity = sv_player->v.velocity; - DropPunchAngle (); + DropPunchAngle(); -// -// if dead, behave differently -// - if (sv_player->v.health <= 0) - return; + // + // if dead, behave differently + // + if (sv_player->v.health <= 0) + return; -// -// angles -// show 1/3 the pitch angle and all the roll angle - cmd = host_client->cmd; - angles = sv_player->v.angles; + // + // angles + // show 1/3 the pitch angle and all the roll angle + cmd = host_client->cmd; + angles = sv_player->v.angles; - VectorAdd (sv_player->v.v_angle, sv_player->v.punchangle, v_angle); - angles[ROLL] = V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4; - if (!sv_player->v.fixangle) - { - angles[PITCH] = -v_angle[PITCH]/3; - angles[YAW] = v_angle[YAW]; - } + VectorAdd(sv_player->v.v_angle, sv_player->v.punchangle, v_angle); + angles[ROLL] = V_CalcRoll(sv_player->v.angles, sv_player->v.velocity) * 4; + if (!sv_player->v.fixangle) { + angles[PITCH] = -v_angle[PITCH] / 3; + angles[YAW] = v_angle[YAW]; + } - if ( (int)sv_player->v.flags & FL_WATERJUMP ) - { - SV_WaterJump (); - return; - } -// -// walk -// - //johnfitz -- alternate noclip - if (sv_player->v.movetype == MOVETYPE_NOCLIP && sv_altnoclip.value) - SV_NoclipMove (); - else if (sv_player->v.waterlevel >= 2 && sv_player->v.movetype != MOVETYPE_NOCLIP) - SV_WaterMove (); - else - SV_AirMove (); - //johnfitz + if ((int) sv_player->v.flags & FL_WATERJUMP) { + SV_WaterJump(); + return; + } + // + // walk + // + //johnfitz -- alternate noclip + if (sv_player->v.movetype == MOVETYPE_NOCLIP && sv_altnoclip.value) + SV_NoclipMove(); + else if (sv_player->v.waterlevel >= 2 && sv_player->v.movetype != MOVETYPE_NOCLIP) + SV_WaterMove(); + else + SV_AirMove(); + //johnfitz } @@ -435,41 +413,39 @@ void SV_ClientThink (void) SV_ReadClientMove =================== */ -void SV_ReadClientMove (usercmd_t *move) -{ - int i; - vec3_t angle; - int bits; +void SV_ReadClientMove(usercmd_t *move) { + int i; + vec3_t angle; + int bits; -// read ping time - host_client->ping_times[host_client->num_pings%NUM_PING_TIMES] - = sv.time - MSG_ReadFloat (); - host_client->num_pings++; + // read ping time + host_client->ping_times[host_client->num_pings % NUM_PING_TIMES] + = sv.time - net_message.read_float().value(); + host_client->num_pings++; -// read current angles - for (i=0 ; i<3 ; i++) - //johnfitz -- 16-bit angles for PROTOCOL_FITZQUAKE - if (sv.protocol == PROTOCOL_NETQUAKE) - angle[i] = MSG_ReadAngle (sv.protocolflags); - else - angle[i] = MSG_ReadAngle16 (sv.protocolflags); - //johnfitz + // read current angles + for (i = 0; i < 3; i++) + //johnfitz -- 16-bit angles for PROTOCOL_FITZQUAKE + if (sv.protocol == PROTOCOL_NETQUAKE) + angle[i] = net_message.read_angle(static_cast(cl.protocolflags)).value(); + else + angle[i] = net_message.read_angle_16(static_cast(cl.protocolflags)).value(); + //johnfitz - VectorCopy (angle, host_client->edict->v.v_angle); + VectorCopy(angle, host_client->edict->v.v_angle); -// read movement - move->forwardmove = MSG_ReadShort (); - move->sidemove = MSG_ReadShort (); - move->upmove = MSG_ReadShort (); + // read movement + move->forwardmove = net_message.read_short().value(); + move->sidemove = net_message.read_short().value(); + move->upmove = net_message.read_short().value(); -// read buttons - bits = MSG_ReadByte (); - host_client->edict->v.button0 = bits & 1; - host_client->edict->v.button2 = (bits & 2)>>1; + // read buttons + bits = net_message.read_byte().value(); + host_client->edict->v.button0 = bits & 1; + host_client->edict->v.button2 = (bits & 2) >> 1; - i = MSG_ReadByte (); - if (i) - host_client->edict->v.impulse = i; + if (const auto imp = net_message.read_byte(); imp.has_value()) + host_client->edict->v.impulse = imp.value(); } /* @@ -479,114 +455,110 @@ SV_ReadClientMessage Returns false if the client should be killed =================== */ -bool SV_ReadClientMessage (void) -{ - int ret; - int ccmd; - const char *s; +bool SV_ReadClientMessage(void) { + int ret; + int ccmd; - do - { -nextmsg: - ret = NET_GetMessage (host_client->netconnection); - if (ret == -1) - { - Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n"); - return false; - } - if (!ret) - return true; + do { + nextmsg: + ret = NET_GetMessage(host_client->netconnection); + if (ret == -1) { + Sys_Printf("SV_ReadClientMessage: NET_GetMessage failed\n"); + return false; + } + if (!ret) + return true; - MSG_BeginReading (); + net_message.begin_reading(); - while (1) - { - if (!host_client->active) - return false; // a command caused an error + while (true) { + if (!host_client->active) + return false; // a command caused an error - if (msg_badread) - { - Sys_Printf ("SV_ReadClientMessage: badread\n"); - return false; - } + if (net_message.eos()) { + Sys_Printf("SV_ReadClientMessage: badread\n"); + return false; + } - ccmd = MSG_ReadChar (); + auto ccmdv = net_message.read_char(); - switch (ccmd) - { - case -1: - goto nextmsg; // end of message + if (!ccmdv.has_value()) { + goto nextmsg; + } - default: - Sys_Printf ("SV_ReadClientMessage: unknown command char\n"); - return false; + ccmd = ccmdv.value(); - case clc_nop: -// Sys_Printf ("clc_nop\n"); - break; + switch (ccmd) { + default: + Sys_Printf("SV_ReadClientMessage: unknown command char\n"); + return false; - case clc_stringcmd: - s = MSG_ReadString (); - ret = 0; - if (q_strncasecmp(s, "status", 6) == 0) - ret = 1; - else if (q_strncasecmp(s, "god", 3) == 0) - ret = 1; - else if (q_strncasecmp(s, "notarget", 8) == 0) - ret = 1; - else if (q_strncasecmp(s, "fly", 3) == 0) - ret = 1; - else if (q_strncasecmp(s, "name", 4) == 0) - ret = 1; - else if (q_strncasecmp(s, "noclip", 6) == 0) - ret = 1; - else if (q_strncasecmp(s, "setpos", 6) == 0) - ret = 1; - else if (q_strncasecmp(s, "say", 3) == 0) - ret = 1; - else if (q_strncasecmp(s, "say_team", 8) == 0) - ret = 1; - else if (q_strncasecmp(s, "tell", 4) == 0) - ret = 1; - else if (q_strncasecmp(s, "color", 5) == 0) - ret = 1; - else if (q_strncasecmp(s, "kill", 4) == 0) - ret = 1; - else if (q_strncasecmp(s, "pause", 5) == 0) - ret = 1; - else if (q_strncasecmp(s, "spawn", 5) == 0) - ret = 1; - else if (q_strncasecmp(s, "begin", 5) == 0) - ret = 1; - else if (q_strncasecmp(s, "prespawn", 8) == 0) - ret = 1; - else if (q_strncasecmp(s, "kick", 4) == 0) - ret = 1; - else if (q_strncasecmp(s, "ping", 4) == 0) - ret = 1; - else if (q_strncasecmp(s, "give", 4) == 0) - ret = 1; - else if (q_strncasecmp(s, "ban", 3) == 0) - ret = 1; + case clc_nop: + // Sys_Printf ("clc_nop\n"); + break; - if (ret == 1) - command::execute_string (s, command::source::client); - else - console::debug("%s tried to %s\n", host_client->name, s); - break; + case clc_stringcmd: { + auto s = net_message.read_string().value(); + ret = 0; + if (q_strncasecmp(s.c_str(), "status", 6) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "god", 3) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "notarget", 8) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "fly", 3) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "name", 4) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "noclip", 6) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "setpos", 6) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "say", 3) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "say_team", 8) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "tell", 4) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "color", 5) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "kill", 4) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "pause", 5) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "spawn", 5) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "begin", 5) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "prespawn", 8) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "kick", 4) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "ping", 4) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "give", 4) == 0) + ret = 1; + else if (q_strncasecmp(s.c_str(), "ban", 3) == 0) + ret = 1; - case clc_disconnect: - // Sys_Printf ("SV_ReadClientMessage: client disconnected\n"); - return false; + if (ret == 1) + command::execute_string(s.c_str(), command::source::client); + else + console::debug("%s tried to %s\n", host_client->name, s.c_str()); + break; + } + case clc_disconnect: + // Sys_Printf ("SV_ReadClientMessage: client disconnected\n"); + return false; - case clc_move: - SV_ReadClientMove (&host_client->cmd); - break; - } - } - } while (ret == 1); + case clc_move: + SV_ReadClientMove(&host_client->cmd); + break; + } + } + } while (ret == 1); - return true; + return true; } @@ -595,33 +567,28 @@ nextmsg: SV_RunClients ================== */ -void SV_RunClients (void) -{ - int i; +void SV_RunClients(void) { + int i; - for (i=0, host_client = svs.clients ; iactive) - continue; + for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { + if (!host_client->active) + continue; - sv_player = host_client->edict; + sv_player = host_client->edict; - if (!SV_ReadClientMessage ()) - { - SV_DropClient (false); // client misbehaved... - continue; - } + if (!SV_ReadClientMessage()) { + SV_DropClient(false); // client misbehaved... + continue; + } - if (!host_client->spawned) - { - // clear client movement until a new packet is received - memset (&host_client->cmd, 0, sizeof(host_client->cmd)); - continue; - } + if (!host_client->spawned) { + // clear client movement until a new packet is received + memset(&host_client->cmd, 0, sizeof(host_client->cmd)); + continue; + } -// always pause in single player if in console or menus - if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) ) - SV_ClientThink (); - } + // always pause in single player if in console or menus + if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game)) + SV_ClientThink(); + } } - diff --git a/Quake/view.cpp b/Quake/view.cpp index eacba27..21d2a68 100644 --- a/Quake/view.cpp +++ b/Quake/view.cpp @@ -259,10 +259,10 @@ void V_ParseDamage(void) { float side; float count; - armor = MSG_ReadByte(); - blood = MSG_ReadByte(); + armor = net_message.read_byte().value(); + blood = net_message.read_byte().value(); for (i = 0; i < 3; i++) - from[i] = MSG_ReadCoord(cl.protocolflags); + from[i] = net_message.read_coord(static_cast(cl.protocolflags)).value(); count = blood * 0.5 + armor * 0.5; if (count < 10)