public List <Entity> GetEntitiesInRegion(PatchRegion pr)
        {
            List <Entity> ents = new List <Entity>();

            //caching:
            PointF pLow  = new PointF(pr.XLow, pr.YLow);
            PointF pHigh = new PointF(pr.XHigh, pr.YHigh);

            foreach (Entity e in Entities)
            {
                if (e.IsInBetween(pLow, pHigh))
                {
                    if (e.Type == "XD_VillageCenter" || e.Type == "XD_StonePit1" || e.Type == "XD_IronPit1" || e.Type == "XD_ClayPit1" || e.Type == "XD_SulfurPit1")
                    {
                        ents.Insert(0, e);
                    }
                    else
                    {
                        ents.Add(e);
                    }
                }
            }
            return(ents);
        }
Example #2
0
        public void createRegionPatchFile(string region, string f, string regonname, bool entities, bool textures, bool heights, bool vertexColors, bool waterTypes, bool waterLevels)
        {
            if (!elist.PatchRegions.ContainsKey(region))
            {
                Console.WriteLine("region not found! " + region);
                Environment.Exit(1);
            }

            PatchRegion pr = elist.PatchRegions[region];

            StringBuilder sb         = new StringBuilder();
            Point         heightHigh = new Point((int)pr.XHigh / 100, (int)pr.YHigh / 100);
            Point         heightLow  = new Point((int)pr.XLow / 100, (int)pr.YLow / 100);
            Point         quadHigh   = new Point((int)pr.XHigh / 400, (int)pr.YHigh / 400);
            Point         quadLow    = new Point((int)pr.XLow / 400, (int)pr.YLow / 400);
            PointF        middlePos  = new PointF((pr.XHigh - pr.XLow) / 2 + pr.XLow, (pr.YHigh - pr.YLow) / 2 + pr.YLow);
            float         radius     = (float)Math.Sqrt(((pr.XHigh - pr.XLow) / 2) * ((pr.XHigh - pr.XLow) / 2) + ((pr.YHigh - pr.YLow) / 2) * ((pr.YHigh - pr.YLow) / 2));

            // region definition
            sb.Append(string.Format(@"getmetatable(terrainPatcher).{12} = function(_off, _subPatchPos)
local terrainData = {{
    heightBoundaries = {{
        high = {{ X = {0}, Y = {1} }},
        low = {{ X = {2}, Y = {3} }}
    }},
    quadBoundaries = {{
        high = {{ X = {4}, Y = {5} }},
        low = {{ X = {6}, Y = {7} }}
    }},
    posBoundaries = {{
        high = {{ X = {8}, Y = {9} }},
        low = {{ X = {10}, Y = {11} }},
    }},", heightHigh.X, heightHigh.Y, heightLow.X, heightLow.Y, quadHigh.X, quadHigh.Y, quadLow.X, quadLow.Y, pr.XHigh, pr.YHigh, pr.XLow, pr.YLow, regonname));

            // entities
            if (entities)
            {
                sb.Append("\n\t\tnewEntities = {");
                foreach (Entity ent in elist.GetEntitiesInRegion(pr))
                {
                    sb.Append(ent);
                    sb.Append(',');
                }
                sb.Append("},");
            }

            // textures
            if (textures)
            {
                sb.Append("\n\t\ttextures = {");

                XYLoop(quadLow, quadHigh, sb, delegate(int x, int y)
                {
                    sb.Append(tdata.Quads[y, x].TerrainType);
                    sb.Append(',');
                });
                sb.Append("},");
            }

            // water type
            if (waterTypes)
            {
                sb.Append("\n\t\twaterTypes = {");

                XYLoop(quadLow, quadHigh, sb, delegate(int x, int y)
                {
                    sb.Append(tdata.Quads[y, x].WaterType);
                    sb.Append(',');
                });
                sb.Append("},");
            }

            // water level
            if (waterLevels)
            {
                sb.Append("\n\t\twaterLevels = {");

                XYLoop(quadLow, quadHigh, sb, delegate(int x, int y)
                {
                    sb.Append(tdata.Quads[y, x].WaterLevel);
                    sb.Append(',');
                });
                sb.Append("},");
            }

            // terrain level
            if (heights)
            {
                sb.Append("\n\t\theights = {");

                XYLoop(heightLow, heightHigh, sb, delegate(int x, int y)
                {
                    sb.Append(tdata.Heights[y, x]);
                    sb.Append(',');
                });
                sb.Append("},");
            }

            // vertex colors
            if (vertexColors)
            {
                sb.Append("\n\t\tvertexColors = {");

                XYLoop(heightLow, heightHigh, sb, delegate(int x, int y)
                {
                    sb.Append("{");
                    sb.Append(tdata.VertexCols[y, x].R);
                    sb.Append(',');
                    sb.Append(tdata.VertexCols[y, x].G);
                    sb.Append(',');
                    sb.Append(tdata.VertexCols[y, x].B);
                    sb.Append("},");
                });
                sb.Append("},");
            }

            // generator script

            //score luaerr fix
            sb.Append(@"
}
local TB_Score_OnBuildingConstructionCompleteBkp = Score.OnBuildingConstructionComplete
Score.OnBuildingConstructionComplete = function() end");

            // calc offset
            sb.Append(@"
_off = _off or {X=0,Y=0}
_off.X = math.floor(_off.X/100)*100 -- align the offset with texture grid
_off.Y = math.floor(_off.Y/100)*100
local posOffX, posOffY, heightOffX, heightOffY = _off.X, _off.Y, math.floor(_off.X/100), math.floor(_off.Y/100)
if _subPatchPos then
    posOffX, posOffY, heightOffX, heightOffY = posOffX-terrainData.posBoundaries.low.X, posOffY-terrainData.posBoundaries.low.Y, heightOffX-math.floor(terrainData.posBoundaries.low.X/100), heightOffY-math.floor(terrainData.posBoundaries.low.Y/100)
end
");

            // remove old entities
            if (entities)
            {
                sb.Append(@"
terrainPatcher.removeEntitiesInRegion(terrainData.posBoundaries, _off)");
            }

            // terrain height & vertex color
            if (heights || vertexColors)
            {
                sb.Append(@"

for x = terrainData.heightBoundaries.low.X, terrainData.heightBoundaries.high.X do
  for y = terrainData.heightBoundaries.low.Y, terrainData.heightBoundaries.high.Y do");
                if (heights)
                {
                    sb.Append(@"
    Logic.SetTerrainNodeHeight(x+heightOffX, y+heightOffY, terrainData.heights[y-terrainData.heightBoundaries.low.Y+1][x-terrainData.heightBoundaries.low.X+1])");
                }
                if (vertexColors)
                {
                    sb.Append(@"
    Logic.SetTerrainVertexColor(x+heightOffX, y+heightOffY, 
    terrainData.vertexColors[y-terrainData.heightBoundaries.low.Y+1][x-terrainData.heightBoundaries.low.X+1][1], 
    terrainData.vertexColors[y-terrainData.heightBoundaries.low.Y+1][x-terrainData.heightBoundaries.low.X+1][2], 
    terrainData.vertexColors[y-terrainData.heightBoundaries.low.Y+1][x-terrainData.heightBoundaries.low.X+1][3])");
                }
                sb.Append(@"
  end
end");
            }

            // terrain texture & water height % water type
            if (textures || waterLevels || waterTypes)
            {
                sb.Append(@"

for x = terrainData.quadBoundaries.low.X, terrainData.quadBoundaries.high.X do
  for y = terrainData.quadBoundaries.low.Y, terrainData.quadBoundaries.high.Y do");
                if (textures)
                {
                    sb.Append(@"
    Logic.SetTerrainNodeType(x*4+heightOffX, y*4+heightOffY, terrainData.textures[y-terrainData.quadBoundaries.low.Y+1][x-terrainData.quadBoundaries.low.X+1]) ");
                }
                if (waterTypes)
                {
                    sb.Append(@"
    Logic.ExchangeWaterType(x*4+heightOffX, y*4+heightOffY, x*4+1+heightOffX, y*4+1+heightOffY, 0, terrainData.waterTypes[y-terrainData.quadBoundaries.low.Y+1][x-terrainData.quadBoundaries.low.X+1])");
                }
                if (waterLevels)
                {
                    sb.Append(@"
    Logic.WaterSetAbsoluteHeight(x*4+heightOffX, y*4+heightOffY, x*4+1+heightOffX, y*4+1+heightOffY, terrainData.waterLevels[y-terrainData.quadBoundaries.low.Y+1][x-terrainData.quadBoundaries.low.X+1]*4)");
                }
                sb.Append(@"
  end
end");
            }

            // blocking update
            sb.Append(@"

Logic.UpdateBlocking(terrainData.heightBoundaries.low.X+heightOffX, terrainData.heightBoundaries.low.Y+heightOffY, terrainData.heightBoundaries.high.X+heightOffX, terrainData.heightBoundaries.high.Y+heightOffY)");

            // entities
            if (entities)
            {
                sb.Append(@"

for i = 1, table.getn(terrainData.newEntities) do
  local eId = Logic.CreateEntity(terrainData.newEntities[i][1], terrainData.newEntities[i][2]+posOffX, terrainData.newEntities[i][3]+posOffY, terrainData.newEntities[i][4], terrainData.newEntities[i][5])
  Logic.SetEntityScriptingValue(eId, -33, terrainData.newEntities[i][6])
  if terrainData.newEntities[i][7] then
    if terrainData.newEntities[i][8] then
      Logic.SetEntityScriptingValue(eId, 9, terrainData.newEntities[i][8])
      Logic.SetEntityScriptingValue(eId, -8, terrainData.newEntities[i][9])
    end
    if terrainData.newEntities[i][7] ~= """" then
      Logic.SetEntityName(eId, terrainData.newEntities[i][7])
    end
  end
end");
            }

            // finishing generators
            sb.Append(@"

GUI.RebuildMinimapTerrain()
terrainData = nil
Score.OnBuildingConstructionComplete = TB_Score_OnBuildingConstructionCompleteBkp
CollectGarbage = function() collectgarbage(); return true; end
StartSimpleHiResJob(""CollectGarbage"")
end");

            // save
            FileStream   fs = File.Create(f);
            StreamWriter sw = new StreamWriter(fs);

            sw.Write(sb.ToString());
            sw.Close();
        }