⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.pas

📁 雷神之锤2(Quake2)Delphi源码
💻 PAS
📖 第 1 页 / 共 4 页
字号:
  end
  else if (bits and $00ff0000) <> 0 then
  begin
    MSG_WriteByte(msg, (bits shr 8) and 255);
    MSG_WriteByte(msg, (bits shr 16) and 255);
  end
  else if (bits and $0000ff00) <> 0 then
  begin
    MSG_WriteByte(msg, (bits shr 8) and 255);
  end;

  //----------

  if (bits and U_NUMBER16) <> 0 then
    MSG_WriteShort(msg, to_.number)
  else
    MSG_WriteByte(msg, to_.number);

  if (bits and U_MODEL) <> 0 then
    MSG_WriteByte(msg, to_.modelindex);
  if (bits and U_MODEL2) <> 0 then
    MSG_WriteByte(msg, to_.modelindex2);
  if (bits and U_MODEL3) <> 0 then
    MSG_WriteByte(msg, to_.modelindex3);
  if (bits and U_MODEL4) <> 0 then
    MSG_WriteByte(msg, to_.modelindex4);

  if (bits and U_FRAME8) <> 0 then
    MSG_WriteByte(msg, to_.frame);
  if (bits and U_FRAME16) <> 0 then
    MSG_WriteShort(msg, to_.frame);

  if ((bits and U_SKIN8) and (bits and U_SKIN16)) <> 0 then	//used for laser colors
    MSG_WriteLong(msg, to_.skinnum)
  else if (bits and U_SKIN8) <> 0 then
    MSG_WriteByte(msg, to_.skinnum)
  else if (bits and U_SKIN16) <> 0 then
    MSG_WriteShort(msg, to_.skinnum);


  if (bits and (U_EFFECTS8 or U_EFFECTS16)) = (U_EFFECTS8 or U_EFFECTS16) then
    MSG_WriteLong(msg, to_.effects)
  else if (bits and U_EFFECTS8) <> 0 then
    MSG_WriteByte(msg, to_.effects)
  else if (bits and U_EFFECTS16) <> 0 then
    MSG_WriteShort(msg, to_.effects);

  if (bits and (U_RENDERFX8 or U_RENDERFX16)) = (U_RENDERFX8 or U_RENDERFX16) then
    MSG_WriteLong(msg, to_.renderfx)
  else if (bits and U_RENDERFX8) <> 0 then
    MSG_WriteByte(msg, to_.renderfx)
  else if (bits and U_RENDERFX16) <> 0 then
    MSG_WriteShort(msg, to_.renderfx);

  if (bits and U_ORIGIN1) <> 0 then
    MSG_WriteCoord (msg, to_.origin[0]);
  if (bits and U_ORIGIN2) <> 0 then
    MSG_WriteCoord (msg, to_.origin[1]);
  if (bits and U_ORIGIN3) <> 0 then
    MSG_WriteCoord (msg, to_.origin[2]);

  if (bits and U_ANGLE1) <> 0 then
    MSG_WriteAngle(msg, to_.angles[0]);
  if (bits and U_ANGLE2) <> 0 then
    MSG_WriteAngle(msg, to_.angles[1]);
  if (bits and U_ANGLE3) <> 0 then
    MSG_WriteAngle(msg, to_.angles[2]);

  if (bits and U_OLDORIGIN) <> 0 then
  begin
    MSG_WriteCoord(msg, to_.old_origin[0]);
    MSG_WriteCoord(msg, to_.old_origin[1]);
    MSG_WriteCoord(msg, to_.old_origin[2]);
  end;

  if (bits and U_SOUND) <> 0 then
    MSG_WriteByte (msg, to_.sound);
  if (bits and U_EVENT) <> 0 then
    MSG_WriteByte (msg, Integer(to_.event));
  if (bits and U_SOLID) <> 0 then
    MSG_WriteShort (msg, to_.solid);
end;


//============================================================

//
// reading functions
//

procedure MSG_BeginReading(var sb: sizebuf_t);
begin
  sb.readcount := 0;
end;

// returns -1 if no more characters are available
function MSG_ReadChar(var msg_read: sizebuf_t): ShortInt;
begin
  if (msg_read.readcount+1 > msg_read.cursize) then
    Result:= -1
  else
    Result:= ShortInt(msg_read.data[msg_read.readcount]);

  Inc(msg_read.readcount);
end;

function MSG_ReadByte(var msg_read: sizebuf_t): Integer;
begin
  if (msg_read.readcount+1 > msg_read.cursize) then
    Result := -1
  else
    Result := Byte(msg_read.data[msg_read.readcount]);

  Inc(msg_read.readcount);
end;

function MSG_ReadShort(var msg_read: sizebuf_t): Integer;
begin
  if (msg_read.readcount+2 > msg_read.cursize) then
    Result := -1
  else
    Result := SmallInt(msg_read.data[msg_read.readcount] +
                (msg_read.data[msg_read.readcount+1] shl 8));

  Inc(msg_read.readcount, 2);
end;

function MSG_ReadLong(var msg_read: sizebuf_t): Integer;
begin
  if (msg_read.readcount+4 > msg_read.cursize) then
    Result := -1
  else
    Result := msg_read.data[msg_read.readcount] +
             (msg_read.data[msg_read.readcount+1] shl 8) +
             (msg_read.data[msg_read.readcount+2] shl 16) +
             (msg_read.data[msg_read.readcount+3] shl 24);

  Inc(msg_read.readcount, 4);
end;

function MSG_ReadFloat(var msg_read: sizebuf_t): Single;
type
  dat_ = packed record
    case Integer of
      0: (f: Single);
      1: (b: array[0..3] of Byte);
      2: (l: Integer);
  end;
var
  dat: dat_;
begin
  if (msg_read.readcount+4 > msg_read.cursize) then
    dat.f := -1
  else
  begin
    dat.b[0] :=	msg_read.data[msg_read.readcount];
    dat.b[1] :=	msg_read.data[msg_read.readcount+1];
    dat.b[2] :=	msg_read.data[msg_read.readcount+2];
    dat.b[3] :=	msg_read.data[msg_read.readcount+3];
  end;
  Inc(msg_read.readcount, 4);

  dat.l := LittleLong(dat.l);

  Result:= dat.f;
end;

function MSG_ReadString(var msg_read: sizebuf_t): PChar;
{$IFDEF COMPILER6_UP}{$WRITEABLECONST ON}{$ENDIF}
const
  string_: array [0..2048-1] of Char = #0;
{$IFDEF COMPILER6_UP}{$WRITEABLECONST OFF}{$ENDIF}
var
  l, c: Integer;
begin
  l := 0;
  repeat
    c := MSG_ReadChar(msg_read);
    if (c = -1) or (c = 0) then
      Break;
    string_[l] := Char(c);
    Inc(l);
  until (l >= SizeOf(string_)-1);
  //} while (l < sizeof(string)-1);

  string_[l] := #0;

  Result:= string_;
end;

function MSG_ReadStringLine(var msg_read: sizebuf_t): PChar;
{$IFDEF COMPILER6_UP}{$WRITEABLECONST ON}{$ENDIF}
const
  string_: array [0..2048-1] of Char = #0;
{$IFDEF COMPILER6_UP}{$WRITEABLECONST OFF}{$ENDIF}
var
  l, c: Integer;
begin
  l := 0;
  repeat
    c := MSG_ReadChar(msg_read);
    if (c = -1) or (c = 0) or (c = 10) then
      Break;
    string_[l] := Char(c);
    Inc(l);
  until (l >= SizeOf(string_)-1);
  //} while (l < sizeof(string)-1);

  string_[l] := #0;

  Result:= string_;
end;

function MSG_ReadCoord(var msg_read: sizebuf_t): Single;
begin
  Result:= MSG_ReadShort(msg_read) * (1.0/8);
end;

procedure MSG_ReadPos(var msg_read: sizebuf_t; var pos: vec3_t);
begin
  pos[0] := MSG_ReadShort(msg_read) * (1.0/8);
  pos[1] := MSG_ReadShort(msg_read) * (1.0/8);
  pos[2] := MSG_ReadShort(msg_read) * (1.0/8);
end;

function MSG_ReadAngle(var msg_read: sizebuf_t): Single;
begin
  Result:= MSG_ReadChar(msg_read) * (360.0/256);
end;

function MSG_ReadAngle16(var msg_read: sizebuf_t): Single;
begin
  Result:= SHORT2ANGLE(MSG_ReadShort(msg_read));
end;

procedure MSG_ReadDeltaUsercmd(var msg_read: sizebuf_t; const from: usercmd_t; var move: usercmd_t);
var
  bits: Integer;
begin
  System.Move(from, move, SizeOf(move));

  bits := MSG_ReadByte(msg_read);

// read current angles
  if (bits and CM_ANGLE1) <> 0 then
    move.angles[0] := MSG_ReadShort(msg_read);
  if (bits and CM_ANGLE2) <> 0 then
    move.angles[1] := MSG_ReadShort(msg_read);
  if (bits and CM_ANGLE3) <> 0 then
    move.angles[2] := MSG_ReadShort(msg_read);

// read movement
  if (bits and CM_FORWARD) <> 0 then
    move.forwardmove := MSG_ReadShort(msg_read);
  if (bits and CM_SIDE) <> 0 then
    move.sidemove := MSG_ReadShort(msg_read);
  if (bits and CM_UP) <> 0 then
    move.upmove := MSG_ReadShort(msg_read);

// read buttons
  if (bits and CM_BUTTONS) <> 0 then
    move.buttons := MSG_ReadByte(msg_read);

  if (bits and CM_IMPULSE) <> 0 then
    move.impulse := MSG_ReadByte(msg_read);

// read time to run command
  move.msec := MSG_ReadByte(msg_read);

// read the light level
  move.lightlevel := MSG_ReadByte(msg_read);
end;


procedure MSG_ReadData(var msg_read: sizebuf_t; data: Pointer; len: Integer);
var
  i: integer;
begin
  for i := 0 to len - 1 do
    PByteArray(data)[i] := MSG_ReadByte(msg_read);
end;


//===========================================================================

procedure SZ_Init(var buf: sizebuf_t; data: PByte; length: Integer);
begin
  FillChar(buf, SizeOf(buf), 0);
  buf.data := PByteArray(data);
  buf.maxsize := length;
end;

procedure SZ_Clear(var buf: sizebuf_t);
begin
  buf.cursize := 0;
  buf.overflowed := False;
end;

function  SZ_GetSpace(var buf: sizebuf_t; length: Integer): Pointer;
begin
  if (buf.cursize + length > buf.maxsize) then
  begin
    if (not buf.allowoverflow) then
      Com_Error(ERR_FATAL, 'SZ_GetSpace: overflow without allowoverflow set', []);

    if (length > buf.maxsize) then
      Com_Error(ERR_FATAL, 'SZ_GetSpace: %d is > full buffer size', [length]);

    Com_Printf('SZ_GetSpace: overflow'#10, []);
    SZ_Clear(buf);
    buf.overflowed := True;
  end;

  Result := Pointer(Integer(buf.data) + buf.cursize);
  buf.cursize := buf.cursize + length;
end;

procedure SZ_Write(var buf: sizebuf_t; data: Pointer; length: Integer);
begin
  Move(data^, SZ_GetSpace(buf, length)^, length);
end;

procedure SZ_Print(var buf: sizebuf_t; data: PChar);
var
  len: Integer;
begin
  len := StrLen(data)+1;

  if (buf.cursize <> 0) then
  begin
    if (buf.data[buf.cursize-1] <> 0) then
      Move(data^, SZ_GetSpace(buf, len)^, len) // no trailing 0
    else
      Move(data^, SZ_GetSpace(buf, len-1)^, len) // write over trailing 0
  end
  else
    Move(data^, SZ_GetSpace(buf, len)^, len) // no trailing 0
end;


//============================================================================


(*
================
COM_CheckParm

Returns the position (1 to argc-1) in the program's argument list
where the given parameter apears, or 0 if not present
================
*)
function COM_CheckParm(parm: PChar): Integer;
var
  i: Integer;
begin
  for i := 1 to com_argc_ - 1 do
  begin
    if (StrComp(parm, com_argv_[i]) = 0) then
    begin
      Result:= i;
      Exit;
    end;
  end;

  Result:= 0;
end;

function COM_Argc: Integer;
begin
  Result:= com_argc_;
end;

function COM_Argv(arg: Integer): PChar;
begin
  if (arg < 0) or (arg >= com_argc_) or (com_argv_[arg] = #0) then
    Result:= nil
  else
    Result:= com_argv_[arg];
end;

procedure COM_ClearArgv(arg: Integer);
begin
  if (arg < 0) or (arg >= com_argc_) or (com_argv_[arg] = #0) then
    Exit;
  com_argv_[arg] := '';
end;


(*
================
COM_InitArgv
================
*)
procedure COM_InitArgv(argc: Integer; argv: PComArgvArray); //todo: , char **argv);
var
  i: Integer;
begin
  if (argc > MAX_NUM_ARGVS) then
    Com_Error(ERR_FATAL, 'argc > MAX_NUM_ARGVS', []);
  com_argc_ := argc;
  for i := 0 to argc - 1 do
  begin
    if (argv[i] = #0) or (StrLen(argv[i]) >= MAX_TOKEN_CHARS) then
      com_argv_[i] := ''
    else
      com_argv_[i] := argv[i];
  end;
end;

(*
================
COM_AddParm

Adds the given string at the end of the current argument list
================
*)
procedure COM_AddParm(parm: PChar);
begin
  if (com_argc = MAX_NUM_ARGVS) then
    Com_Error(ERR_FATAL, 'COM_AddParm: MAX_NUM)ARGS', []);
  com_argv_[com_argc_] := parm;
  Inc(com_argc_);
end;




/// just for debugging
function MemSearch(start: PByte; count, search: Integer): Integer;
var
  i: Integer;
begin
  for i := 0 to count - 1 do
    if (PByteArray(start)[i] = search) then
    begin
      Result:= i;
      Exit;
    end;
  Result:= -1;
end;


function CopyString(in_: PChar): PChar;
var
  out: PChar;
begin
  out := Z_Malloc(StrLen(in_)+1);
  StrCopy(out, in_);
  Result := out;
end;



procedure Info_Print(s: PChar);
var
  key: array [0..512-1] of Char;
  value: array [0..512-1] of Char;
  o: PChar;
  l: Integer;
begin
  if (s^ = '\') then Inc(s);

  while (s^ <> #0) do
  begin
    o := key;
    // while (*s && *s != '\\')
    while (s^ <> #0) and (s^ <> '\') do
    begin
      // *o++ = *s++;
      o^ := s^; Inc(o); Inc(s);
    end;

    l := o - key;
    if (l < 20) then
    begin
      FillChar(o^, 20-l, ' ');
      key[20] := #0;
    end
    else
      o^ := #0;
    Com_Printf('%s', [key]);

    if (s^ = #0) then
    begin
      Com_Printf('MISSING VALUE'#10, []);
      Exit;
    end;

    o := value;
    Inc(s);
    // while (*s && *s != '\\')
    while (s^ <> #0) and (s^ <> '\') do
    begin
      // *o++ = *s++;
      o^ := s^; Inc(o); Inc(s);
    end;
    o^ := #0;

    if (s^ <> #0) then
      Inc(s);
    Com_Printf('%s'#10, [value]);
  end;
end;


(*
==============================================================================

						ZONE MEMORY ALLOCATION

just cleared malloc with counters now...

==============================================================================
*)

const
  Z_MAGIC       = $1d1d;

type
  zhead_p = ^zhead_t;
  zhead_s = record
    prev, next: zhead_p;
    magic: SmallInt;
    tag: SmallInt;		// for group free
    size: Integer;
  end;
  zhead_t = zhead_s;

var
  z_chain: zhead_t;
  z_count, z_bytes: Integer;

(*
========================
Z_Free
========================
*)
procedure Z_Free(ptr: Pointer);
var
  z: zhead_p;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -