DEADSOFTWARE

trigger data now cached on map loading (so it should be as fast as the previous trigg...
[d2df-sdl.git] / src / game / g_map.pas
index 6cfd0da3b578ccd8d20f6ca93fe46e1637dcca37..e976be49c616045a92b12fd0b2c4844fd9227be2 100644 (file)
@@ -290,7 +290,7 @@ begin
   begin
     for pan in panByGUID do
     begin
-      if pan.gncNeedSend then MH_SEND_PanelState(pan.panelType, pan.guid);
+      if pan.gncNeedSend then MH_SEND_PanelState(pan.guid);
     end;
   end;
 end;
@@ -358,8 +358,11 @@ begin
 
   try
     dfmapdef := TDynMapDef.Create(pr);
-  except on e: Exception do
-    raise Exception.Create(Format('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.line, pr.col, e.message]));
+  except
+    on e: TDynParseException do
+      raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
+    on e: Exception do
+      raise Exception.CreateFmt('ERROR in "mapdef.txt" at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
   end;
 
   st.Free();
@@ -371,7 +374,6 @@ end;
 function g_Map_ParseMap (data: Pointer; dataLen: Integer): TDynRecord;
 var
   wst: TSFSMemoryChunkStream = nil;
-  pr: TTextParser = nil;
 begin
   result := nil;
   if (dataLen < 4) then exit;
@@ -381,41 +383,25 @@ begin
   if (dfmapdef = nil) then raise Exception.Create('internal map loader error');
 
   wst := TSFSMemoryChunkStream.Create(data, dataLen);
-
-  if (PAnsiChar(data)[0] = 'M') and (PAnsiChar(data)[1] = 'A') and (PAnsiChar(data)[2] = 'P') and (PByte(data)[3] = 1) then
-  begin
-    // binary map
-    try
-      //e_LogWriteln('parsing binary map...');
-      result := dfmapdef.parseBinMap(wst);
-    except on e: Exception do
+  try
+    result := dfmapdef.parseMap(wst);
+  except
+    on e: TDynParseException do
       begin
-        e_LogWritefln('ERROR: %s', [e.message]);
+        e_LogWritefln('ERROR at (%s,%s): %s', [e.tokLine, e.tokCol, e.message]);
         wst.Free();
         result := nil;
         exit;
       end;
-    end;
-    wst.Free();
-  end
-  else
-  begin
-    // text map
-    pr := TFileTextParser.Create(wst);
-    try
-      //e_LogWriteln('parsing text map...');
-      result := dfmapdef.parseMap(pr);
-    except on e: Exception do
+    on e: Exception do
       begin
-        if (pr <> nil) then e_LogWritefln('ERROR at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message])
-        else e_LogWritefln('ERROR: %s', [e.message]);
-        pr.Free(); // will free `wst`
+        e_LogWritefln('ERROR: %s', [e.message]);
+        wst.Free();
         result := nil;
         exit;
       end;
-    end;
-    pr.Free(); // will free `wst`
   end;
+
   //e_LogWriteln('map parsed.');
 end;
 
@@ -821,7 +807,7 @@ begin
   PanelArray := nil;
 end;
 
-function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer; sav: Boolean): Integer;
+function CreatePanel (PanelRec: TDynRecord; AddTextures: TAddTextureArray; CurTex: Integer): Integer;
 var
   len: Integer;
   panels: ^TPanelArray;
@@ -856,7 +842,6 @@ begin
   pan.arrIdx := len;
   pan.proxyId := -1;
   pan.tag := panelTypeToTag(PanelRec.PanelType);
-  if sav then pan.SaveIt := True;
 
   PanelRec.user['panel_guid'] := pguid;
 
@@ -1355,7 +1340,8 @@ begin
     //Data.Default := Trigger.DATA;
     if (Trigger.trigRec = nil) then
     begin
-      trigData := nil;
+      trigDataRec := nil;
+      //HACK!
       if (TriggerType <> TRIGGER_SECRET) then
       begin
         e_LogWritefln('trigger of type %s has no triggerdata; wtf?!', [TriggerType], MSG_WARNING);
@@ -1363,7 +1349,7 @@ begin
     end
     else
     begin
-      trigData := Trigger.trigRec.clone(nil);
+      trigDataRec := Trigger.trigRec.clone(nil);
     end;
   end;
 
@@ -1389,7 +1375,7 @@ begin
         if gTriggers[a].TriggerType in [TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF] then
         begin
           //if (gTriggers[a].Data.MonsterID-1) = Integer(mon.StartID) then mon.AddTrigger(a);
-          if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
+          if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
         end;
       end;
     end;
@@ -1421,7 +1407,7 @@ procedure g_Map_ReAdd_DieTriggers();
           tw.Free();
         end;
         }
-        if (gTriggers[a].trigData.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
+        if (gTriggers[a].trigDataRec.trigMonsterId) = Integer(mon.StartID) then mon.AddTrigger(a);
       end;
     end;
   end;
@@ -1642,7 +1628,7 @@ var
   FileName, mapResName, s, TexName: String;
   Data: Pointer;
   Len: Integer;
-  ok, isAnim, trigRef: Boolean;
+  ok, isAnim: Boolean;
   CurTex, ntn: Integer;
   rec, texrec: TDynRecord;
   pttit: PTRec;
@@ -1881,7 +1867,6 @@ begin
         //e_LogWritefln('PANSTART: pannum=%s', [pannum]);
         texrec := nil;
         SetLength(AddTextures, 0);
-        trigRef := False;
         CurTex := -1;
         ok := false;
 
@@ -1898,21 +1883,9 @@ begin
           ok := false;
           if (TriggersTable <> nil) and (mapTextureList <> nil) then
           begin
-            {
-            for b := 0 to High(TriggersTable) do
-            begin
-              if (TriggersTable[b].texPan = rec) or (TriggersTable[b].shotPan = rec) then
-              begin
-                trigRef := True;
-                ok := True;
-                break;
-              end;
-            end;
-            }
             if rec.userPanelTrigRef then
             begin
               // e_LogWritefln('trigref for panel %s', [pannum]);
-              trigRef := True;
               ok := True;
             end;
           end;
@@ -2040,7 +2013,7 @@ begin
         //e_LogWritefln('PANADD: pannum=%s', [pannum]);
 
         // Ñîçäàåì ïàíåëü è çàïîìèíàåì åå GUID
-        PanelID := CreatePanel(rec, AddTextures, CurTex, trigRef);
+        PanelID := CreatePanel(rec, AddTextures, CurTex);
         //e_LogWritefln('panel #%s of type %s got guid #%s', [pannum, rec.PanelType, PanelID]);
         rec.userPanelId := PanelID; // remember game panel id, we'll fix triggers later
 
@@ -2048,7 +2021,7 @@ begin
         moveSpeed := rec.moveSpeed;
         //moveStart := rec.moveStart;
         //moveEnd := rec.moveEnd;
-        //moveActive := rec['move_active'].varvalue;
+        //moveActive := rec['move_active'].value;
         if not moveSpeed.isZero then
         begin
           SetLength(gMovingWallIds, Length(gMovingWallIds)+1);
@@ -2940,7 +2913,7 @@ begin
   //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true
   //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor);
 
-  //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState({gWalls[ID]}pan.PanelType, pguid);
+  //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
   // mark platform as interesting
   pan.setDirty();
 
@@ -2965,7 +2938,7 @@ begin
   mapGrid.proxyEnabled[pan.proxyId] := false;
   //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end;
 
-  //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, pguid);
+  //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
   // mark platform as interesting
   pan.setDirty();
 
@@ -2995,7 +2968,7 @@ begin
   }
 
   tp.NextTexture(AnimLoop);
-  if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(PanelType, pguid, AnimLoop);
+  if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelTexture(pguid, AnimLoop);
 end;
 
 
@@ -3024,7 +2997,7 @@ begin
       3: g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT);
     end;
 
-    //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, pguid);
+    //if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pguid);
     // mark platform as interesting
     pan.setDirty();
   end;
@@ -3147,20 +3120,15 @@ var
   var
     PAMem: TBinMemoryWriter;
     pan: TPanel;
+    count: Integer;
   begin
     // Ñîçäàåì íîâûé ñïèñîê ñîõðàíÿåìûõ ïàíåëåé
     PAMem := TBinMemoryWriter.Create((Length(panByGUID)+1) * 40);
 
-    for pan in panByGUID do
-    begin
-      if true{pan.SaveIt} then
-      begin
-        // ID ïàíåëè
-        //PAMem.WriteInt(i);
-        // Ñîõðàíÿåì ïàíåëü
-        pan.SaveState(PAMem);
-      end;
-    end;
+    // Ñîõðàíÿåì ïàíåëè
+    count := Length(panByGUID);
+    Mem.WriteInt(count);
+    for pan in panByGUID do pan.SaveState(PAMem);
 
     // Ñîõðàíÿåì ýòîò ñïèñîê ïàíåëåé
     PAMem.SaveToMemory(Mem);
@@ -3243,25 +3211,19 @@ var
   var
     PAMem: TBinMemoryReader;
     pan: TPanel;
+    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');
     for pan in panByGUID do
     begin
-      if true{pan.SaveIt} then
-      begin
-        // ID ïàíåëè:
-        //PAMem.ReadInt(id);
-        {
-        if id <> i then raise EBinSizeError.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel ID');
-        }
-        // Çàãðóæàåì ïàíåëü
-        pan.LoadState(PAMem);
-        //if (panels[i].arrIdx <> i) then raise Exception.Create('g_Map_LoadState: LoadPanelArray: Wrong Panel arrIdx');
-        if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
-      end;
+      pan.LoadState(PAMem);
+      if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled;
     end;
 
     // Ýòîò ñïèñîê ïàíåëåé çàãðóæåí