Unit progdefs;

Interface

Uses pr_defs, math, strings, console;

{The variables shared between the game and external code:}
Type globalvars_tp = ^globalvars_t;
		 globalvars_t = Record
                      pad                 : Array[0..27] Of Longint;
                      self, other, world  : LongInt;
                      time                : float;
                      main                : LongInt;
                      crap                : Array[0..16096] Of float;
                    End;

{12-byte multi-datatype record for handling variables:}
Type eval_t = Record
                Case Integer Of
                  0: (nstring : string_t);
                  1: (_float : float);
                  2: (vector : vec3_t);
                  3: (func : func_t);
                  4: (_int : longint);
                  5: (edict : longint);
              End;
     eval_tp = ^eval_t;

Type  entvars_t = Record
                    origin, velocity  : vec3_t;
                    health            : float;
                    nextthink         : float;
                    think             : func_t;
                    enemy             : longint;
                    chain             : longint;
                  End;
      entvars_fa = Array[0..(SizeOf(entvars_t) Div 4)-1] Of Float;
      edict_t = Record
                  {Everything needed internally in the game.}
                  free      : Boolean;
                  {Space holder for external code's definitions:}
                  Case Integer Of
                    0: (v : entvars_fa);
                    1: (e : entvars_t);
                End;
      edict_tp = ^edict_t;

{Don't ask...}
Type zpr_functions      = Array[0..0] Of dfunction_t;
     zpr_strings        = Array[0..0] Of char;
     zpr_fielddefs      = Array[0..0] Of ddef_t;
     zpr_globaldefs     = Array[0..0] Of ddef_t;
     zpr_statements     = Array[0..0] Of dstatement_t;
     zpr_global_struct  = Array[0..0] Of globalvars_t;
     zedicts            = Array[0..0] Of edict_t;

     ppr_functions      = ^zpr_functions;
     ppr_strings        = ^zpr_strings;
     ppr_fielddefs      = ^zpr_fielddefs;
     ppr_globaldefs     = ^zpr_globaldefs;
     ppr_statements     = ^zpr_statements;
     ppr_global_struct  = ^zpr_global_struct;
     pedicts            = ^zedicts;

{Pointers to all the external code and data:}
Var  progs              : dprograms_tp;
     pr_functions       : ppr_functions;
     pr_strings         : ppr_strings;
     pr_fielddefs       : ppr_fielddefs;
     pr_globaldefs      : ppr_globaldefs;
     pr_statements      : ppr_statements;
     pr_global_struct   : ppr_global_struct;
     edicts             : pedicts;
     numedicts          : LongInt;
     pr_edictsize       : Longint;

Procedure PR_RunError (error : String);
Function Spawn : LongInt;
Procedure Remove(e : LongInt);
Function trim(s : string) : string;
Function tofloat(f : float) : String;
Function tovec(v : vec3_tp) : String;
Function itos(i : longint) : String;

Implementation

Procedure PR_RunError (error : String);
Begin
  Con_WriteLn(error);
  Halt(1);
End;

{Trim spaces off both sides of a string:}
Function trim(s : string) : string;
var p : string;
begin
  p := s;
  While p[1] = ' ' Do Delete(p,1,1);
  While p[length(p)] = ' ' Do Delete(p,length(p),1);
  trim := p;
end;

Function itos(i : longint) : String;
Var so : String;
Begin
  Str(i, so);
  itos := trim(so);
End;

Function tofloat(f : float) : String;
Var so : String;
Begin
  Str(f:_D:_N, so);
  tofloat := trim(so);
End;

Function tovec(v : vec3_tp) : String;
Var so,t : String;
Begin
  so := '''';
  Str(v^[0]:_D:_N, t);
  so := so + trim(t) + ' ';
  Str(v^[1]:_D:_N, t);
  so := so + trim(t) + ' ';
  Str(v^[2]:_D:_N, t);
  so := so + trim(t) + '''';
  tovec := so;
End;

Function Spawn : LongInt;
Var i : LongInt;
Begin
  For i := 1 To numedicts - 1 Do Begin
    If edicts^[i].free = True Then Begin
      edicts^[i].free := False;
      Spawn := i;
      Exit;
    End;
  End;
  PR_RunError('No free edicts.');
End;

Procedure Remove(e : LongInt);
Begin
  edicts^[e].free := True;
End;

End.
