DEADSOFTWARE

do not bilinear anything except sky
[d2df-sdl.git] / src / game / g_map.pas
index e976be49c616045a92b12fd0b2c4844fd9227be2..1b92a967f0c31115b5101b3db1be3c003c1ae070 100644 (file)
@@ -65,7 +65,7 @@ procedure g_Map_Update();
 
 function g_Map_PanelByGUID (aguid: Integer): TPanel; inline;
 
-procedure g_Map_DrawPanels (PanelType: Word); // unaccelerated
+procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor); // unaccelerated
 procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
 
 procedure g_Map_DrawBack(dx, dy: Integer);
@@ -242,7 +242,7 @@ var
 implementation
 
 uses
-  e_input, g_main, e_log, SysUtils, g_items, g_gfx, g_console,
+  e_input, g_main, e_log, e_texture, SysUtils, g_items, g_gfx, g_console,
   GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
   g_options, g_triggers, g_player,
   Math, g_monsters, g_saveload, g_language, g_netmsg,
@@ -1338,22 +1338,9 @@ begin
     trigPanelGUID := atrigpanid;
     //trigShotPanelId := ashotpanid;
     //Data.Default := Trigger.DATA;
-    if (Trigger.trigRec = nil) then
-    begin
-      trigDataRec := nil;
-      //HACK!
-      if (TriggerType <> TRIGGER_SECRET) then
-      begin
-        e_LogWritefln('trigger of type %s has no triggerdata; wtf?!', [TriggerType], MSG_WARNING);
-      end;
-    end
-    else
-    begin
-      trigDataRec := Trigger.trigRec.clone(nil);
-    end;
   end;
 
-  result := Integer(g_Triggers_Create(_trigger));
+  result := Integer(g_Triggers_Create(_trigger, Trigger));
 end;
 
 procedure CreateMonster(monster: TDynRecord);
@@ -1600,17 +1587,13 @@ type
     //TexturePanel: Integer;
     tnum: Integer;
     id: Integer;
-    texPanIdx: Integer;
-    LiftPanelIdx: Integer;
-    DoorPanelIdx: Integer;
-    ShotPanelIdx: Integer;
-    MPlatPanelIdx: Integer;
     trigrec: TDynRecord;
-    texPan: TDynRecord;
-    liftPan: TDynRecord;
-    doorPan: TDynRecord;
-    shotPan: TDynRecord;
-    mplatPan: TDynRecord;
+    // texture pane;
+    texPanelIdx: Integer;
+    texPanel: TDynRecord;
+    // "action" panel
+    actPanelIdx: Integer;
+    actPanel: TDynRecord;
   end;
 var
   WAD: TWADFile;
@@ -1637,6 +1620,7 @@ var
   moveSpeed{, moveStart, moveEnd}: TDFPoint;
   //moveActive: Boolean;
   pan: TPanel;
+  mapOk: Boolean = false;
 begin
   mapGrid.Free();
   mapGrid := nil;
@@ -1688,7 +1672,7 @@ begin
     e_LogWritefln('Loading map: %s', [mapResName], MSG_NOTIFY);
     g_Game_SetLoadingText(_lc[I_LOAD_MAP], 0, False);
 
-    stt := curTimeMicro();
+    stt := getTimeMicro();
 
     try
       mapReader := g_Map_ParseMap(Data, Len);
@@ -1707,6 +1691,8 @@ begin
       exit;
     end;
 
+    gCurrentMap := mapReader;
+
     generateExternalResourcesList(mapReader);
     mapTextureList := mapReader['texture'];
     // get all other lists here too
@@ -1795,6 +1781,8 @@ begin
         begin
           e_WriteLog('error loading map: invalid texture index for panel', MSG_FATALERROR);
           result := false;
+          gCurrentMap := nil;
+          gCurrentMapFileName := '';
           exit;
         end;
       end;
@@ -1807,49 +1795,23 @@ begin
       //SetLength(TriggersTable, triggers.count);
       g_Game_SetLoadingText(_lc[I_LOAD_TRIGGERS_TABLE], triggers.count-1, False);
 
+      SetLength(TriggersTable, triggers.count);
+      trignum := -1;
       for rec in triggers do
       begin
-        SetLength(TriggersTable, Length(TriggersTable)+1);
-        pttit := @TriggersTable[High(TriggersTable)];
+        Inc(trignum);
+        pttit := @TriggersTable[trignum];
         pttit.trigrec := rec;
         // Ñìåíà òåêñòóðû (âîçìîæíî, êíîïêè)
-        pttit.texPan := mapReader.panel[rec.TexturePanel];
-        pttit.liftPan := nil;
-        pttit.doorPan := nil;
-        pttit.shotPan := nil;
-        pttit.mplatPan := nil;
-        pttit.texPanIdx := -1;
-        pttit.LiftPanelIdx := -1;
-        pttit.DoorPanelIdx := -1;
-        pttit.ShotPanelIdx := -1;
-        pttit.MPlatPanelIdx := -1;
-        // Ëèôòû
-        if rec.TriggerType in [TRIGGER_LIFTUP, TRIGGER_LIFTDOWN, TRIGGER_LIFT] then
-        begin
-          pttit.liftPan := mapReader.panel[rec.trigRec.tgPanelID];
-        end;
-        // Äâåðè
-        if rec.TriggerType in [TRIGGER_OPENDOOR, TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5, TRIGGER_CLOSETRAP, TRIGGER_TRAP] then
-        begin
-          pttit.doorPan := mapReader.panel[rec.trigRec.tgPanelID];
-        end;
-        // Òóðåëü
-        if (rec.TriggerType = TRIGGER_SHOT) then
-        begin
-          pttit.shotPan := mapReader.panel[rec.trigRec.tgShotPanelID];
-        end;
-        //
-        if rec.TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
-        begin
-          pttit.mplatPan := mapReader.panel[rec.trigRec.tgPanelID];
-        end;
-
-        if (pttit.texPan <> nil) then pttit.texPan.userPanelTrigRef := true;
-        if (pttit.liftPan <> nil) then pttit.liftPan.userPanelTrigRef := true;
-        if (pttit.doorPan <> nil) then pttit.doorPan.userPanelTrigRef := true;
-        if (pttit.shotPan <> nil) then pttit.shotPan.userPanelTrigRef := true;
-        if (pttit.mplatPan <> nil) then pttit.mplatPan.userPanelTrigRef := true;
-
+        pttit.texPanelIdx := -1; // will be fixed later
+        pttit.texPanel := rec.TexturePanelRec;
+        // action panel
+        pttit.actPanelIdx := -1;
+        if (rec.trigRec <> nil) then pttit.actPanel := rec.trigRec.tgPanelRec else pttit.actPanel := nil;
+        // set flag
+        if (pttit.texPanel <> nil) then pttit.texPanel.userPanelTrigRef := true;
+        if (pttit.actPanel <> nil) then pttit.actPanel.userPanelTrigRef := true;
+        // update progress
         g_Game_StepLoading();
       end;
     end;
@@ -2038,11 +2000,8 @@ begin
     // ×èíèì ID'û ïàíåëåé, êîòîðûå èñïîëüçóþòñÿ â òðèããåðàõ
     for b := 0 to High(TriggersTable) do
     begin
-      if (TriggersTable[b].texPan <> nil) then TriggersTable[b].texPanIdx := TriggersTable[b].texPan.userPanelId;
-      if (TriggersTable[b].liftPan <> nil) then TriggersTable[b].LiftPanelIdx := TriggersTable[b].liftPan.userPanelId;
-      if (TriggersTable[b].doorPan <> nil) then TriggersTable[b].DoorPanelIdx := TriggersTable[b].doorPan.userPanelId;
-      if (TriggersTable[b].shotPan <> nil) then TriggersTable[b].ShotPanelIdx := TriggersTable[b].shotPan.userPanelId;
-      if (TriggersTable[b].mplatPan <> nil) then TriggersTable[b].MPlatPanelIdx := TriggersTable[b].mplatPan.userPanelId;
+      if (TriggersTable[b].texPanel <> nil) then TriggersTable[b].texPanelIdx := TriggersTable[b].texPanel.userPanelId;
+      if (TriggersTable[b].actPanel <> nil) then TriggersTable[b].actPanelIdx := TriggersTable[b].actPanel.userPanelId;
     end;
 
     // create map grid, init other grids (for monsters, for example)
@@ -2059,17 +2018,13 @@ begin
       for rec in triggers do
       begin
         Inc(trignum);
-        if (TriggersTable[trignum].texPan <> nil) then b := TriggersTable[trignum].texPan.PanelType else b := 0;
-        if (TriggersTable[trignum].shotPan <> nil) then c := TriggersTable[trignum].shotPan.PanelType else c := 0;
+        if (TriggersTable[trignum].texPanel <> nil) then b := TriggersTable[trignum].texPanel.PanelType else b := 0;
+        if (TriggersTable[trignum].actPanel <> nil) then c := TriggersTable[trignum].actPanel.PanelType else c := 0;
         // we can have only one of those
-             if (TriggersTable[trignum].LiftPanelIdx <> -1) then tgpid := TriggersTable[trignum].LiftPanelIdx
-        else if (TriggersTable[trignum].DoorPanelIdx <> -1) then tgpid := TriggersTable[trignum].DoorPanelIdx
-        else if (TriggersTable[trignum].ShotPanelIdx <> -1) then tgpid := TriggersTable[trignum].ShotPanelIdx
-        else if (TriggersTable[trignum].MPlatPanelIdx <> -1) then tgpid := TriggersTable[trignum].MPlatPanelIdx
-        else tgpid := -1;
+        tgpid := TriggersTable[trignum].actPanelIdx;
         //e_LogWritefln('creating trigger #%s; texpantype=%s; shotpantype=%s (%d,%d)', [trignum, b, c, TriggersTable[trignum].texPanIdx, TriggersTable[trignum].ShotPanelIdx]);
         TriggersTable[trignum].tnum := trignum;
-        TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanIdx, tgpid, Word(b), Word(c));
+        TriggersTable[trignum].id := CreateTrigger(trignum, rec, TriggersTable[trignum].texPanelIdx, tgpid, Word(b), Word(c));
       end;
     end;
 
@@ -2129,26 +2084,28 @@ begin
     mapReader := nil;
 
     // Çàãðóçêà íåáà
-    if gMapInfo.SkyName <> '' then
+    if (gMapInfo.SkyName <> '') then
     begin
       e_WriteLog('  Loading sky: ' + gMapInfo.SkyName, MSG_NOTIFY);
       g_Game_SetLoadingText(_lc[I_LOAD_SKY], 0, False);
       FileName := g_ExtractWadName(gMapInfo.SkyName);
 
-      if FileName <> '' then
-        FileName := GameDir+'/wads/'+FileName
-      else
-        begin
-          FileName := g_ExtractWadName(Res);
-        end;
+      if (FileName <> '') then FileName := GameDir+'/wads/'+FileName else FileName := g_ExtractWadName(Res);
 
-      s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
-      if g_Texture_CreateWAD(BackID, s) then
+      if gTextureFilter then TEXTUREFILTER := GL_LINEAR else TEXTUREFILTER := GL_NEAREST;
+      try
+        s := FileName+':'+g_ExtractFilePathName(gMapInfo.SkyName);
+        if g_Texture_CreateWAD(BackID, s) then
         begin
           g_Game_SetupScreenSize();
         end
-      else
-        g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
+        else
+        begin
+          g_FatalError(Format(_lc[I_GAME_ERROR_SKY], [s]));
+        end;
+      finally
+        TEXTUREFILTER := GL_NEAREST;
+      end;
     end;
 
     // Çàãðóçêà ìóçûêè
@@ -2204,12 +2161,18 @@ begin
       gMusic.SetByName('');
     end;
 
-    stt := curTimeMicro()-stt;
+    stt := getTimeMicro()-stt;
     e_LogWritefln('map loaded in %s.%s milliseconds', [Integer(stt div 1000), Integer(stt mod 1000)]);
+    mapOk := true;
   finally
     sfsGCEnable(); // enable releasing unused volumes
     mapReader.Free();
     e_ClearInputBuffer(); // why not?
+    if not mapOk then
+    begin
+      gCurrentMap := nil;
+      gCurrentMapFileName := '';
+    end;
   end;
 
   e_WriteLog('Done loading map.', MSG_NOTIFY);
@@ -2535,7 +2498,7 @@ end;
 
 
 // old algo
-procedure g_Map_DrawPanels (PanelType: Word);
+procedure g_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
 
   procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
   var
@@ -2546,7 +2509,7 @@ procedure g_Map_DrawPanels (PanelType: Word);
       // alas, no visible set
       for idx := 0 to High(panels) do
       begin
-        if not (drawDoors xor panels[idx].Door) then panels[idx].Draw();
+        if not (drawDoors xor panels[idx].Door) then panels[idx].Draw(hasAmbient, ambColor);
       end;
     end;
   end;
@@ -3120,14 +3083,12 @@ var
   var
     PAMem: TBinMemoryWriter;
     pan: TPanel;
-    count: Integer;
   begin
     // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé
     PAMem := TBinMemoryWriter.Create((Length(panByGUID)+1) * 40);
 
     // Ñîõðàíÿåì ïàíåëè
-    count := Length(panByGUID);
-    Mem.WriteInt(count);
+    //Mem.WriteInt(Length(panByGUID));
     for pan in panByGUID do pan.SaveState(PAMem);
 
     // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé
@@ -3211,15 +3172,16 @@ var
   var
     PAMem: TBinMemoryReader;
     pan: TPanel;
-    count: LongInt;
+    //count: LongInt;
   begin
     // Çàãðóæàåì òåêóùèé ñïèñîê ïàíåëåé
     PAMem := TBinMemoryReader.Create();
     PAMem.LoadFromMemory(Mem);
 
     // Çàãðóæàåì ïàíåëè
-    PAMem.ReadInt(count);
-    if (count <> Length(panByGUID)) then raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: invalid number of panels');
+    //PAMem.ReadInt(count);
+    //if (count <> Length(panByGUID)) then raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: invalid number of panels');
+    //if (count <> Length(panByGUID)) then raise EBinSizeError.Create(Format('g_Map_LoadState: LoadPanelArray: invalid number of panels (%d : %d)', [count, Length(panByGUID)]));
     for pan in panByGUID do
     begin
       pan.LoadState(PAMem);