DEADSOFTWARE

Add Haiku OS support
[d2df-sdl.git] / src / game / g_game.pas
index fd0daf24a2f4282f5ded133f0e036e6fbd726bac..8444232487b70404ff6b6575e96c4778e65fa51e 100644 (file)
@@ -1,4 +1,4 @@
-(* Copyright (C)  DooM 2D:Forever Developers
+(* Copyright (C)  Doom 2D: Forever Developers
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,7 +22,7 @@ uses
   SysUtils, Classes,
   MAPDEF,
   g_basic, g_player, e_graphics, g_res_downloader,
-  g_sound, g_gui, wadreader, md5, xprofiler;
+  g_sound, g_gui, utils, md5, xprofiler;
 
 type
   TGameSettings = record
@@ -123,10 +123,10 @@ procedure g_FatalError(Text: String);
 procedure g_SimpleError(Text: String);
 function  g_Game_IsTestMap(): Boolean;
 procedure g_Game_DeleteTestMap();
-procedure GameCVars(P: SArray);
-procedure GameCommands(P: SArray);
-procedure GameCheats(P: SArray);
-procedure DebugCommands(P: SArray);
+procedure GameCVars(P: SSArray);
+procedure GameCommands(P: SSArray);
+procedure GameCheats(P: SSArray);
+procedure DebugCommands(P: SSArray);
 procedure g_Game_Process_Params;
 procedure g_Game_SetLoadingText(Text: String; Max: Integer; reWrite: Boolean);
 procedure g_Game_StepLoading();
@@ -344,9 +344,13 @@ uses
   e_input, e_log, g_console, g_items, g_map, g_panel,
   g_playermodel, g_gfx, g_options, g_weapons, Math,
   g_triggers, g_monsters, e_sound, CONFIG,
-  g_language, g_net, SDL,
+  g_language, g_net,
   ENet, e_msg, g_netmsg, g_netmaster, GL, GLExt,
-  utils, sfs, g_holmes;
+  sfs, wadreader, g_holmes;
+
+
+var
+  hasPBarGfx: Boolean = false;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -513,6 +517,7 @@ type
     ShowCount: Integer;
     Msgs: Array of String;
     NextMsg: Word;
+    PBarWasHere: Boolean; // did we draw a progress bar for this message?
   end;
 
   TParamStrValue = record
@@ -540,7 +545,7 @@ var
   EndingGameCounter: Byte = 0;
   MessageText: String;
   MessageTime: Word;
-  MapList: SArray = nil;
+  MapList: SSArray = nil;
   MapIndex: Integer = -1;
   MegaWAD: record
     info: TMegaWADInfo;
@@ -563,7 +568,7 @@ var
   end;
   //InterPic: String;
   InterText: record
-    lines: SArray;
+    lines: SSArray;
     img: String;
     cur_line: Integer;
     cur_char: Integer;
@@ -1538,8 +1543,8 @@ begin
       Exit;
   end;
 
-// ×èòàåì êëàâèàòóðó è äæîéñòèê, åñëè îêíî àêòèâíî:
-  e_PollInput();
+  // ×èòàåì êëàâèàòóðó è äæîéñòèê, åñëè îêíî àêòèâíî
+  // no need to, as we'll do it in event handler
 
 // Îáíîâëÿåì êîíñîëü (äâèæåíèå è ñîîáùåíèÿ):
   g_Console_Update();
@@ -2043,6 +2048,11 @@ begin
 end;
 
 procedure g_Game_LoadData();
+var
+  wl, hl: Integer;
+  wr, hr: Integer;
+  wb, hb: Integer;
+  wm, hm: Integer;
 begin
   if DataLoaded then Exit;
 
@@ -2054,14 +2064,37 @@ begin
   g_Texture_CreateWADEx('TEXTURE_PLAYER_HUDJET', GameWAD+':TEXTURES\JETBAR');
   g_Texture_CreateWADEx('TEXTURE_PLAYER_HUDBG', GameWAD+':TEXTURES\HUDBG');
   g_Texture_CreateWADEx('TEXTURE_PLAYER_ARMORHUD', GameWAD+':TEXTURES\ARMORHUD');
-  g_Texture_CreateWADEx('TEXTURE_PLAYER_REDFLAG', GameWAD+':TEXTURES\FLAGHUD_RB', GameWAD+':TEXTURES\FLAGHUD_R_BASE');
-  g_Texture_CreateWADEx('TEXTURE_PLAYER_REDFLAG_S', GameWAD+':TEXTURES\FLAGHUD_RS', GameWAD+':TEXTURES\FLAGHUD_R_STOLEN');
-  g_Texture_CreateWADEx('TEXTURE_PLAYER_REDFLAG_D', GameWAD+':TEXTURES\FLAGHUD_RD', GameWAD+':TEXTURES\FLAGHUD_R_DROP');
-  g_Texture_CreateWADEx('TEXTURE_PLAYER_BLUEFLAG', GameWAD+':TEXTURES\FLAGHUD_BB', GameWAD+':TEXTURES\FLAGHUD_B_BASE');
-  g_Texture_CreateWADEx('TEXTURE_PLAYER_BLUEFLAG_S', GameWAD+':TEXTURES\FLAGHUD_BS', GameWAD+':TEXTURES\FLAGHUD_B_STOLEN');
-  g_Texture_CreateWADEx('TEXTURE_PLAYER_BLUEFLAG_D', GameWAD+':TEXTURES\FLAGHUD_BD', GameWAD+':TEXTURES\FLAGHUD_B_DROP');
+  g_Texture_CreateWADEx('TEXTURE_PLAYER_REDFLAG', GameWAD+':TEXTURES\FLAGHUD_R_BASE');
+  g_Texture_CreateWADEx('TEXTURE_PLAYER_REDFLAG_S', GameWAD+':TEXTURES\FLAGHUD_R_STOLEN');
+  g_Texture_CreateWADEx('TEXTURE_PLAYER_REDFLAG_D', GameWAD+':TEXTURES\FLAGHUD_R_DROP');
+  g_Texture_CreateWADEx('TEXTURE_PLAYER_BLUEFLAG', GameWAD+':TEXTURES\FLAGHUD_B_BASE');
+  g_Texture_CreateWADEx('TEXTURE_PLAYER_BLUEFLAG_S', GameWAD+':TEXTURES\FLAGHUD_B_STOLEN');
+  g_Texture_CreateWADEx('TEXTURE_PLAYER_BLUEFLAG_D', GameWAD+':TEXTURES\FLAGHUD_B_DROP');
   g_Texture_CreateWADEx('TEXTURE_PLAYER_TALKBUBBLE', GameWAD+':TEXTURES\TALKBUBBLE');
   g_Texture_CreateWADEx('TEXTURE_PLAYER_INVULPENTA', GameWAD+':TEXTURES\PENTA');
+
+  hasPBarGfx := true;
+  if not g_Texture_CreateWADEx('UI_GFX_PBAR_LEFT', GameWAD+':TEXTURES\LLEFT') then hasPBarGfx := false;
+  if not g_Texture_CreateWADEx('UI_GFX_PBAR_MARKER', GameWAD+':TEXTURES\LMARKER') then hasPBarGfx := false;
+  if not g_Texture_CreateWADEx('UI_GFX_PBAR_MIDDLE', GameWAD+':TEXTURES\LMIDDLE') then hasPBarGfx := false;
+  if not g_Texture_CreateWADEx('UI_GFX_PBAR_RIGHT', GameWAD+':TEXTURES\LRIGHT') then hasPBarGfx := false;
+
+  if hasPBarGfx then
+  begin
+    g_Texture_GetSize('UI_GFX_PBAR_LEFT', wl, hl);
+    g_Texture_GetSize('UI_GFX_PBAR_RIGHT', wr, hr);
+    g_Texture_GetSize('UI_GFX_PBAR_MIDDLE', wb, hb);
+    g_Texture_GetSize('UI_GFX_PBAR_MARKER', wm, hm);
+    if (wl > 0) and (hl > 0) and (wr > 0) and (hr = hl) and (wb > 0) and (hb = hl) and (wm > 0) and (hm > 0) and (hm <= hl) then
+    begin
+      // yay!
+    end
+    else
+    begin
+      hasPBarGfx := false;
+    end;
+  end;
+
   g_Frames_CreateWAD(nil, 'FRAMES_TELEPORT', GameWAD+':TEXTURES\TELEPORT', 64, 64, 10, False);
   g_Sound_CreateWADEx('SOUND_GAME_TELEPORT', GameWAD+':SOUNDS\TELEPORT');
   g_Sound_CreateWADEx('SOUND_GAME_NOTELEPORT', GameWAD+':SOUNDS\NOTELEPORT');
@@ -2182,7 +2215,8 @@ var
 begin
   e_TextureFontGetSize(gStdFont, ww2, hh2);
 
-  e_PollInput();
+  g_ProcessMessages();
+
   if e_KeyPressed(IK_TAB) then
   begin
     if not gStatsPressed then
@@ -2503,13 +2537,124 @@ begin
 end;
 
 procedure DrawLoadingStat();
+  procedure drawRect (x, y, w, h: Integer);
+  begin
+    if (w < 1) or (h < 1) then exit;
+    glBegin(GL_QUADS);
+      glVertex2f(x+0.375, y+0.375);
+      glVertex2f(x+w+0.375, y+0.375);
+      glVertex2f(x+w+0.375, y+h+0.375);
+      glVertex2f(x+0.375, y+h+0.375);
+    glEnd();
+  end;
+
+  function drawPBar (cur, total: Integer; washere: Boolean): Boolean;
+  var
+    rectW, rectH: Integer;
+    x0, y0: Integer;
+    wdt: Integer;
+    wl, hl: Integer;
+    wr, hr: Integer;
+    wb, hb: Integer;
+    wm, hm: Integer;
+    idl, idr, idb, idm: LongWord;
+    f, my: Integer;
+  begin
+    result := false;
+    if (total < 1) then exit;
+    if (cur < 1) then exit; // don't blink
+    if (not washere) and (cur >= total) then exit; // don't blink
+    //if (cur < 0) then cur := 0;
+    //if (cur > total) then cur := total;
+    result := true;
+
+    if (hasPBarGfx) then
+    begin
+      g_Texture_Get('UI_GFX_PBAR_LEFT', idl);
+      g_Texture_GetSize('UI_GFX_PBAR_LEFT', wl, hl);
+      g_Texture_Get('UI_GFX_PBAR_RIGHT', idr);
+      g_Texture_GetSize('UI_GFX_PBAR_RIGHT', wr, hr);
+      g_Texture_Get('UI_GFX_PBAR_MIDDLE', idb);
+      g_Texture_GetSize('UI_GFX_PBAR_MIDDLE', wb, hb);
+      g_Texture_Get('UI_GFX_PBAR_MARKER', idm);
+      g_Texture_GetSize('UI_GFX_PBAR_MARKER', wm, hm);
+
+      //rectW := gScreenWidth-360;
+      rectW := trunc(624.0*gScreenWidth/1024.0);
+      rectH := hl;
+
+      x0 := (gScreenWidth-rectW) div 2;
+      y0 := gScreenHeight-rectH-64;
+      if (y0 < 2) then y0 := 2;
+
+      glEnable(GL_SCISSOR_TEST);
+
+      // left and right
+      glScissor(x0, gScreenHeight-y0-rectH, rectW, rectH);
+      e_DrawSize(idl, x0, y0, 0, true, false, wl, hl);
+      e_DrawSize(idr, x0+rectW-wr, y0, 0, true, false, wr, hr);
+
+      // body
+      glScissor(x0+wl, gScreenHeight-y0-rectH, rectW-wl-wr, rectH);
+      f := x0+wl;
+      while (f < x0+rectW) do
+      begin
+        e_DrawSize(idb, f, y0, 0, true, false, wb, hb);
+        f += wb;
+      end;
+
+      // filled part
+      wdt := (rectW-wl-wr)*cur div total;
+      if (wdt > rectW-wl-wr) then wdt := rectW-wr-wr;
+      if (wdt > 0) then
+      begin
+        my := y0; // don't be so smart, ketmar: +(rectH-wm) div 2;
+        glScissor(x0+wl, gScreenHeight-my-rectH, wdt, hm);
+        f := x0+wl;
+        while (wdt > 0) do
+        begin
+          e_DrawSize(idm, f, y0, 0, true, false, wm, hm);
+          f += wm;
+          wdt -= wm;
+        end;
+      end;
+
+      glScissor(0, 0, gScreenWidth, gScreenHeight);
+    end
+    else
+    begin
+      rectW := gScreenWidth-64;
+      rectH := 16;
+
+      x0 := (gScreenWidth-rectW) div 2;
+      y0 := gScreenHeight-rectH-64;
+      if (y0 < 2) then y0 := 2;
+
+      glDisable(GL_BLEND);
+      glDisable(GL_TEXTURE_2D);
+
+      //glClearColor(0, 0, 0, 0);
+      //glClear(GL_COLOR_BUFFER_BIT);
+
+      glColor4ub(127, 127, 127, 255);
+      drawRect(x0-2, y0-2, rectW+4, rectH+4);
+
+      glColor4ub(0, 0, 0, 255);
+      drawRect(x0-1, y0-1, rectW+2, rectH+2);
+
+      glColor4ub(127, 127, 127, 255);
+      wdt := rectW*cur div total;
+      if (wdt > rectW) then wdt := rectW;
+      drawRect(x0, y0, wdt, rectH);
+    end;
+  end;
+
 var
   ww, hh: Word;
   xx, yy, i: Integer;
   s: String;
 begin
-  if Length(LoadingStat.Msgs) = 0 then
-    Exit;
+  if (Length(LoadingStat.Msgs) = 0) then exit;
 
   e_CharFont_GetSize(gMenuFont, _lc[I_MENU_LOADING], ww, hh);
   yy := (gScreenHeight div 3);
@@ -2517,16 +2662,19 @@ begin
   xx := (gScreenWidth div 3);
 
   with LoadingStat do
+  begin
     for i := 0 to NextMsg-1 do
-      begin
-        if (i = (NextMsg-1)) and (MaxValue > 0) then
-          s := Format('%s:  %d/%d', [Msgs[i], CurValue, MaxValue])
-        else
-          s := Msgs[i];
+    begin
+      if (i = (NextMsg-1)) and (MaxValue > 0) then
+        s := Format('%s:  %d/%d', [Msgs[i], CurValue, MaxValue])
+      else
+        s := Msgs[i];
 
-        e_CharFont_PrintEx(gMenuSmallFont, xx, yy, s, _RGB(255, 0, 0));
-        yy := yy + LOADING_INTERLINE;
-      end;
+      e_CharFont_PrintEx(gMenuSmallFont, xx, yy, s, _RGB(255, 0, 0));
+      yy := yy + LOADING_INTERLINE;
+      PBarWasHere := drawPBar(CurValue, MaxValue, PBarWasHere);
+    end;
+  end;
 end;
 
 procedure DrawMinimap(p: TPlayer; RenderRect: e_graphics.TRect);
@@ -3101,15 +3249,21 @@ begin
 
   //glTranslatef(a, b+p.IncCam, 0);
 
-  if (p = gPlayer1) then g_Holmes_plrViewSize(sWidth, sHeight);
+  //if (p = gPlayer1) and (g_dbg_scale >= 1.0) then g_Holmes_plrViewSize(sWidth, sHeight);
 
+  //conwritefln('OLD: (%s,%s)-(%s,%s)', [sX, sY, sWidth, sHeight]);
   fixViewportForScale();
+  //conwritefln('     (%s,%s)-(%s,%s)', [sX, sY, sWidth, sHeight]);
   p.viewPortX := sX;
   p.viewPortY := sY;
   p.viewPortW := sWidth;
   p.viewPortH := sHeight;
 
-  if (p = gPlayer1) then g_Holmes_plrViewPos(sX, sY);
+  if (p = gPlayer1) then
+  begin
+    g_Holmes_plrViewPos(sX, sY);
+    g_Holmes_plrViewSize(sWidth, sHeight);
+  end;
 
   renderMapInternal(-c, -d, true);
 
@@ -3299,6 +3453,9 @@ begin
         e_DrawLine(2, 0, gScreenHeight div 2, gScreenWidth, gScreenHeight div 2, 0, 0, 0);
     end;
 
+    // draw inspector
+    if (g_holmes_enabled) then g_Holmes_Draw();
+
     if MessageText <> '' then
     begin
       w := 0;
@@ -3489,9 +3646,6 @@ begin
     g_ActiveWindow.Draw();
   end;
 
-  // draw inspector
-  if (g_holmes_enabled) then g_Holmes_Draw();
-
   g_Console_Draw();
 
   if g_debug_Sounds and gGameOn then
@@ -3728,6 +3882,7 @@ end;
 procedure g_Game_StartSingle(Map: String; TwoPlayers: Boolean; nPlayers: Byte);
 var
   i, nPl: Integer;
+  tmps: AnsiString;
 begin
   g_Game_Free();
 
@@ -3783,7 +3938,8 @@ begin
 // Çàãðóçêà è çàïóñê êàðòû:
   if not g_Game_StartMap(MAP, True) then
   begin
-    g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + MAP]));
+    if (Pos(':\', Map) > 0) or (Pos(':/', Map) > 0) then tmps := Map else tmps := gGameSettings.WAD + ':\' + MAP;
+    g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [tmps]));
     Exit;
   end;
 
@@ -4130,6 +4286,7 @@ begin
           enet_packet_destroy(NetEvent.packet);
       end
       else
+      begin
         if (NetEvent.kind = ENET_EVENT_TYPE_DISCONNECT) then
         begin
           State := 0;
@@ -4139,12 +4296,11 @@ begin
           OuterLoop := False;
           Break;
         end;
+      end;
     end;
 
     ProcessLoading(true);
 
-    e_PollInput();
-
     if e_KeyPressed(IK_ESCAPE) or e_KeyPressed(IK_SPACE) then
     begin
       State := 0;
@@ -4597,7 +4753,7 @@ var
   WadName: string;
 {
   WAD: TWADFile;
-  MapList: SArray;
+  MapList: SSArray;
   time: Integer;
 }
 begin
@@ -4650,7 +4806,7 @@ begin
   if gTempDelete then DeleteFile(WadName);
 end;
 
-procedure GameCVars(P: SArray);
+procedure GameCVars(P: SSArray);
 var
   a, b: Integer;
   stat: TPlayerStatArray;
@@ -5197,7 +5353,7 @@ begin
   e_LogWriteLn ('^=======================^');
 end;
 
-procedure DebugCommands(P: SArray);
+procedure DebugCommands(P: SSArray);
 var
   a, b: Integer;
   cmd: string;
@@ -5333,7 +5489,7 @@ begin
 end;
 
 
-procedure GameCheats(P: SArray);
+procedure GameCheats(P: SSArray);
 var
   cmd: string;
   f, a: Integer;
@@ -5548,7 +5704,7 @@ begin
   end;
 end;
 
-procedure GameCommands(P: SArray);
+procedure GameCommands(P: SSArray);
 var
   a, b: Integer;
   s, pw: String;
@@ -6002,7 +6158,7 @@ begin
         if P[2] = '' then
           g_Console_Add(Format(_lc[I_MSG_NO_MAPS], [P[1]]))
         else
-          g_Console_Add(Format(_lc[I_MSG_NO_MAP], [UpperCase(P[2])]));
+          g_Console_Add(Format(_lc[I_MSG_NO_MAP_FALLBACK], [UpperCase(P[2]), P[1]]));
     end else
       g_Console_Add(Format(_lc[I_MSG_NO_WAD], [P[1]]));
   end
@@ -6056,7 +6212,7 @@ begin
         if P[4] = '' then
           g_Console_Add(Format(_lc[I_MSG_NO_MAPS], [P[3]]))
         else
-          g_Console_Add(Format(_lc[I_MSG_NO_MAP], [UpperCase(P[4])]));
+          g_Console_Add(Format(_lc[I_MSG_NO_MAP_FALLBACK], [UpperCase(P[4]), P[3]]));
     end else
       g_Console_Add(Format(_lc[I_MSG_NO_WAD], [P[3]]));
   end
@@ -6090,9 +6246,9 @@ begin
               g_Game_ChangeMap(s);
           end else
           begin
-            g_Console_Add(Format(_lc[I_MSG_NO_MAP], [s]));
             // Òàêîé êàðòû íåò, èùåì WAD ôàéë
             P[1] := addWadExtension(P[1]);
+            g_Console_Add(Format(_lc[I_MSG_NO_MAP_FALLBACK], [s, P[1]]));
             if FileExists(MapsDir + P[1]) then
             begin
               // Ïàðàìåòðà êàðòû íåò, ïîýòîìó ñòàâèì ïåðâóþ èç ôàéëà
@@ -6179,9 +6335,9 @@ begin
               nm := True;
             end else
             begin
-              g_Console_Add(Format(_lc[I_MSG_NO_MAP], [s]));
               // Òàêîé êàðòû íåò, èùåì WAD ôàéë
               P[1] := addWadExtension(P[1]);
+              g_Console_Add(Format(_lc[I_MSG_NO_MAP_FALLBACK], [s, P[1]]));
               if FileExists(MapsDir + P[1]) then
               begin
                 // Ïàðàìåòðà êàðòû íåò, ïîýòîìó ñòàâèì ïåðâóþ èç ôàéëà
@@ -6945,6 +7101,7 @@ begin
     CurValue := 0;
     MaxValue := Max;
     ShowCount := 0;
+    PBarWasHere := false;
   end;
 
   g_ActiveWindow := nil;
@@ -6981,6 +7138,7 @@ begin
     for len := Low(Msgs) to High(Msgs) do
       Msgs[len] := '';
     NextMsg := 0;
+    PBarWasHere := false;
   end;
 end;
 
@@ -7132,6 +7290,11 @@ begin
     if (s <> '') then
       gMapOnce := True;
 
+  // Override map to test:
+    s := LowerCase(Find_Param_Value(pars, '-testmap'));
+    if s <> '' then
+      gTestMap := MapsDir + s;
+
   // Delete test map after play:
     s := Find_Param_Value(pars, '--testdelete');
     if (s <> '') then
@@ -7143,9 +7306,9 @@ begin
 
   // Delete temporary WAD after play:
     s := Find_Param_Value(pars, '--tempdelete');
-    if (s <> '') then
+    if (s <> '') and (gTestMap <> '') then
     begin
-      gMapToDelete := MapsDir + map;
+      gMapToDelete := gTestMap;
       gTempDelete := True;
     end;