Skip to content

Commit

Permalink
Update to
Browse files Browse the repository at this point in the history
  • Loading branch information
tyronx committed Nov 15, 2023
1 parent e78624b commit 7886bb9
Show file tree
Hide file tree
Showing 44 changed files with 770 additions and 427 deletions.
1 change: 1 addition & 0 deletions Client/Audio/SoundParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum EnumSoundType
Music,
Ambient,
Weather,
Entity,
MusicGlitchunaffected,
AmbientGlitchunaffected,
SoundGlitchunaffected
Expand Down
9 changes: 6 additions & 3 deletions Client/Render/PerceptionEffects/DrunkPerceptionEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ public override void ApplyToFpHand(Matrixf modelMat)

public override void ApplyToTpPlayer(EntityPlayer entityPlr, float[] modelMatrix, float? playerIntensity = null)
{
var rplr = entityPlr.Player as IClientPlayer;
if (rplr.CameraMode == EnumCameraMode.FirstPerson && !rplr.ImmersiveFpMode) return;

float inten = playerIntensity == null ? Intensity : (float)playerIntensity;

Mat4f.RotateX(modelMatrix, modelMatrix, GameMath.Sin(accum) / 6f * inten);
Mat4f.RotateZ(modelMatrix, modelMatrix, GameMath.Sin(accum * 1.2f) / 6f * inten);
var pos = entityPlr.AnimManager.Animator.GetPosebyName("root");
pos.degOffX = GameMath.Sin(accum) / 5f * inten * GameMath.RAD2DEG;
pos.degOffZ = GameMath.Sin(accum * 1.2f) / 5f * inten * GameMath.RAD2DEG;
}

public override void NowActive(float intensity)
{
base.NowActive(intensity);

capi.Render.ShaderUniforms.PerceptionEffectId = 2;

}


Expand Down
18 changes: 16 additions & 2 deletions Client/Texture/CompositeTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public CompositeTexture()
{
}



/// <summary>
/// Creates a new empty composite texture with given base texture
/// </summary>
Expand Down Expand Up @@ -355,8 +357,12 @@ static BakedCompositeTexture Bake(IAssetManager assetManager, CompositeTexture c
var tile = ct.Tiles[i];
if (tile.Base.EndsWithWildCard)
{
var assets = wildcardsCache[ct.Base] = assetManager.GetManyInCategory("textures", ct.Base.Path.Substring(0, ct.Base.Path.Length - 1), ct.Base.Domain);
var sortedassets = assets.OrderBy(asset => asset.Location.GetName().Split(".")[0].ToInt()).ToList();
// Fix borked windows sorting (i.e. 1, 10, 11, 12, ....)
var basePath = ct.Base.Path.Substring(0, ct.Base.Path.Length - 1);
var assets = wildcardsCache[ct.Base] = assetManager.GetManyInCategory("textures", basePath, ct.Base.Domain);
var len = "textures".Length + basePath.Length + "/".Length;
var sortedassets = assets.OrderBy(asset => asset.Location.Path.Substring(len).RemoveFileEnding().ToInt()).ToList();

for (int j = 0; j < sortedassets.Count; j++)
{
IAsset asset = sortedassets[j];
Expand Down Expand Up @@ -387,6 +393,14 @@ public override string ToString()
{
return Base.ToString() + "@" + Rotation + "a" + Alpha;
}
public void FillPlaceholder(string search, string replace)
{
Base.Path = Base.Path.Replace(search, replace);
if (BlendedOverlays != null) BlendedOverlays.Foreach((ov) => ov.Base.Path = ov.Base.Path.Replace(search, replace));
if (Alternates != null) Alternates.Foreach((alt) => alt.FillPlaceholder(search, replace));
if (Tiles != null) Tiles.Foreach((tile) => tile.FillPlaceholder(search, replace));
}

}


Expand Down
54 changes: 53 additions & 1 deletion Client/Texture/ITextureSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Vintagestory.API.MathTools;
using System.Collections.Generic;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;

namespace Vintagestory.API.Client
{
Expand All @@ -14,4 +16,54 @@ public interface ITexPositionSource
/// </summary>
Size2i AtlasSize { get; }
}


/// <summary>
/// Helper class for implementors of ITexPositionSource
/// </summary>
public class ContainedTextureSource : ITexPositionSource
{
ITextureAtlasAPI targetAtlas;
ICoreClientAPI capi;
public Size2i AtlasSize => targetAtlas.Size;

public Dictionary<string, AssetLocation> Textures = new Dictionary<string, AssetLocation>();
private string sourceForErrorLogging;

public ContainedTextureSource(ICoreClientAPI capi, ITextureAtlasAPI targetAtlas, Dictionary<string, AssetLocation> textures, string sourceForErrorLogging)
{
this.capi = capi;
this.targetAtlas = targetAtlas;
this.Textures = textures;
this.sourceForErrorLogging = sourceForErrorLogging;
}

public TextureAtlasPosition this[string textureCode]
{
get
{
return getOrCreateTexPos(Textures[textureCode]);
}
}

protected TextureAtlasPosition getOrCreateTexPos(AssetLocation texturePath)
{
TextureAtlasPosition texpos = targetAtlas[texturePath];

if (texpos == null)
{
IAsset texAsset = capi.Assets.TryGet(texturePath.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"));
if (texAsset != null)
{
targetAtlas.GetOrInsertTexture(texturePath, out _, out texpos, () => texAsset.ToBitmap(capi));
}
else
{
capi.World.Logger.Warning("{0}, require texture {1}, but no such texture found.", sourceForErrorLogging, texturePath);
}
}

return texpos;
}
}
}
9 changes: 8 additions & 1 deletion Client/Texture/TextureAtlasPosition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Vintagestory.API.Client
using System;

namespace Vintagestory.API.Client
{
/// <summary>
/// The position of a texture inside an atlas
Expand Down Expand Up @@ -45,5 +47,10 @@ public class TextureAtlasPosition
/// The y coordinate of the texture end point
/// </summary>
public float y2;

public TextureAtlasPosition Clone()
{
return new TextureAtlasPosition() { atlasTextureId = atlasTextureId, atlasNumber = atlasNumber, reloadIteration = reloadIteration, AvgColor = AvgColor, RndColors = RndColors, x1 = x1, y1 = y1, x2 = x2, y2 = y2 };
}
}
}
27 changes: 27 additions & 0 deletions Client/UI/IconUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class IconUtil
public IconRendererDelegate SvgIconSource(AssetLocation loc)
{
var asset = capi.Assets.TryGet(loc);
return SvgIconSource(asset);
}
public IconRendererDelegate SvgIconSource(IAsset asset)
{
return (ctx, x, y, w, h, rgba) =>
{
capi.Gui.DrawSvg(asset, ctx.GetTarget() as ImageSurface, x, y, (int)w, (int)h, ColorUtil.FromRGBADoubles(rgba));
Expand All @@ -36,6 +40,28 @@ public IconUtil(ICoreClientAPI capi)
CustomIcons["wpCross"] = (ctx, x, y, w, h, rgba) => { ctx.SetSourceRGBA(rgba); capi.Gui.Icons.DrawCross(ctx, x, y, 4, w); };
}

public LoadedTexture GenTexture(int width, int height, IAsset asset, double[] rgba = null)
{
var handler = SvgIconSource(asset);
ImageSurface surface = new ImageSurface(Format.Argb32, width, height);
Context ctx = new Context(surface);

handler(ctx,0, 0, width, height, rgba ?? ColorUtil.WhiteArgbDouble);

int textureId = capi.Gui.LoadCairoTexture(surface, true);

surface.Dispose();
ctx.Dispose();

return new LoadedTexture(capi)
{
TextureId = textureId,
Width = width,
Height = height
};
}


/// <summary>
/// Generates the texture.
/// </summary>
Expand Down Expand Up @@ -23391,6 +23417,7 @@ public void Drawlake_svg(Context cr, int x, int y, float width, float height, do
cr.Restore();
}





Expand Down
1 change: 0 additions & 1 deletion Common/API/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ namespace Vintagestory.API.Common



public delegate TResult Func<TResult>();
public delegate TResult Func<T1, TResult>(T1 t1);
public delegate TResult Func<T1, T2, TResult>(T1 t1, T2 t2);
public delegate TResult Func<T1, T2, T3, TResult>(T1 t1, T2 t2, T3 t3);
Expand Down
12 changes: 0 additions & 12 deletions Common/API/IBlockAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,6 @@ public interface IBlockAccessor
/// <returns></returns>
Block GetBlock(BlockPos pos);

/// <summary>
/// Tries to find a object that implements an interface at given position in the following order:<br/>
/// 1. Block implements T
/// 2. BlockBehavior implements T
/// 3. BlockEntity implements T
/// 4. BlockEntityBehavior implements T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pos"></param>
/// <returns></returns>
T GetInterface<T>(BlockPos pos);

/// <summary>
/// Get the block type of the block at the given world coordinate. Will never return null. For airblocks or invalid coordinates you'll get a block instance with block code "air" and id 0
/// <br/>Reads the block from the specified layer(s), see <see cref="BlockLayersAccess"/> documentation for details.
Expand Down
2 changes: 1 addition & 1 deletion Common/API/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void Error(Exception e)
/// <returns></returns>
public static string CleanStackTrace(string stackTrace)
{
return stackTrace.Replace(SourcePath,"");
return stackTrace?.Replace(SourcePath,"") ?? "No stack trace";
}

public void Fatal(string format, params object[] args)
Expand Down
5 changes: 4 additions & 1 deletion Common/API/IMapRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ public interface IMapRegion
/// <param name="key"></param>
/// <returns></returns>
T GetModdata<T>(string key);

/// <summary>
/// A thread-safe way to add a new GeneratedStructure, also marks DirtyForSaving = true
/// </summary>
void AddGeneratedStructure(GeneratedStructure generatedStructure);
}
}
4 changes: 4 additions & 0 deletions Common/API/IWorldChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public interface IChunkBlocks
/// Same as ClearBlocks but initialises the chunkdata palette, so that SetBlockUnsafe can be used (useful in worldgen)
/// </summary>
void ClearBlocksAndPrepare();

/// <summary>
/// Efficiently bulk-set a single block id in a chunk throughout a y-layer. lenX will be ignored (always treated as 32), the y-position is specified in index3d, the width lenZ will be respected
/// </summary>
void SetBlockBulk(int index3d, int lenX, int lenZ, int value);
/// <summary>
/// Not threadsafe, used only in cases where we know that the chunk already has a palette (e.g. in worldgen when replacing rock with other blocks)
Expand Down
10 changes: 8 additions & 2 deletions Common/Assets/AssetLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,21 @@ public virtual bool HasDomain()
public virtual string GetName()
{
var index = Path.LastIndexOf('/');
return (index >= 0) ? Path.Substring(index + 1) : Path;
return Path.Substring(index + 1); // Even if index is -1, this works :)
}

/// <summary>
/// Removes the file ending from the asset path.
/// </summary>
public virtual void RemoveEnding()
{
path = path.Substring(0, path.LastIndexOf('.'));
path = path[..path.LastIndexOf('.')];
}

public string PathOmittingPrefixAndSuffix(string prefix, string suffix)
{
int endpoint = path.EndsWith(suffix) ? path.Length - suffix.Length : path.Length;
return path[prefix.Length..endpoint];
}

/// <summary>
Expand Down
32 changes: 27 additions & 5 deletions Common/Collectible/Block/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace Vintagestory.API.Common
{
public delegate void BlockBehaviorDelegate(BlockBehavior behavior, ref EnumHandling handling);

public enum EnumRetentionType
{
Heat,
Sound,
Water
}

/// <summary>
/// Basic class for a placeable block
/// </summary>
Expand Down Expand Up @@ -1298,7 +1305,6 @@ public virtual void OnNeighbourBlockChange(IWorldAccessor world, BlockPos pos, B
{
world.BlockAccessor.SetBlock(notSnowCovered.Id, pos);
}

}
}

Expand Down Expand Up @@ -1853,7 +1859,21 @@ public virtual float GetSnowLevel(BlockPos pos)
/// <param name="pos"></param>
/// <param name="facing"></param>
/// <returns></returns>
[Obsolete("Use GetRetention() instead")]
public virtual int GetHeatRetention(BlockPos pos, BlockFacing facing)
{
return GetRetention(pos, facing, EnumRetentionType.Heat);
}


/// <summary>
/// Return a positive integer if the block retains something, e.g. (for warm rooms or greenhouses) or a negative integer if something can pass through, e.g. cool for cellars
/// </summary>
/// <param name="pos"></param>
/// <param name="facing"></param>
/// <param name="type"></param>
/// <returns></returns>
public virtual int GetRetention(BlockPos pos, BlockFacing facing, EnumRetentionType type)
{
bool preventDefault = false;
int result = 0;
Expand All @@ -1862,7 +1882,7 @@ public virtual int GetHeatRetention(BlockPos pos, BlockFacing facing)
int bhresult;

EnumHandling handled = EnumHandling.PassThrough;
bhresult = behavior.GetHeatRetention(pos, facing, ref handled);
bhresult = behavior.GetRetention(pos, facing, type, ref handled);
if (handled != EnumHandling.PassThrough)
{
preventDefault = true;
Expand All @@ -1873,8 +1893,10 @@ public virtual int GetHeatRetention(BlockPos pos, BlockFacing facing)

if (preventDefault) return result;

if (/*SideSolid[facing.Opposite.Index] || - Tyron 24sep: Why the eff do we check the opposite side?? */ SideSolid[facing.Index])
if (SideSolid[facing.Index])
{
if (type == EnumRetentionType.Sound) return 10;

var mat = GetBlockMaterial(api.World.BlockAccessor, pos);
if (mat == EnumBlockMaterial.Ore || mat == EnumBlockMaterial.Stone || mat == EnumBlockMaterial.Soil || mat == EnumBlockMaterial.Ceramic)
{
Expand Down Expand Up @@ -2432,8 +2454,8 @@ public virtual T GetBEBehavior<T>(BlockPos pos) where T : BlockEntityBehavior

/// <summary>
/// Returns instance of class that implements this interface in the following order<br/>
/// 1. Block<br/>
/// 2. BlockBehavior<br/>
/// 1. Block (returns itself)<br/>
/// 2. BlockBehavior (returns on of our own behavior)<br/>
/// 3. BlockEntity<br/>
/// 4. BlockEntityBehavior
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion Common/Collectible/Block/BlockBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System;
using System.Text;
using Vintagestory.API.Client;
using Vintagestory.API.Common.Entities;
using Vintagestory.API.Datastructures;
Expand Down Expand Up @@ -350,11 +351,17 @@ public virtual void GetPlacedBlockName(StringBuilder sb, IWorldAccessor world, B

}

[Obsolete("Use GetRetention() instead")]
public virtual int GetHeatRetention(BlockPos pos, BlockFacing facing, ref EnumHandling handled)
{
return 0;
}

public virtual int GetRetention(BlockPos pos, BlockFacing facing, EnumRetentionType type, ref EnumHandling handled)
{
return 0;
}

public virtual float GetLiquidBarrierHeightOnSide(BlockFacing face, BlockPos pos, ref EnumHandling handled)
{
return 0;
Expand Down
2 changes: 2 additions & 0 deletions Common/Collectible/Block/BlockSchematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ public virtual void TransformWhilePacked(IWorldAccessor worldForResolve, EnumOri
EntitiesUnpacked.Clear();

angle = GameMath.Mod(angle, 360);
if (angle == 0) return;

if (EntranceRotation != -1)
{
EntranceRotation = GameMath.Mod(EntranceRotation + angle, 360);
Expand Down
Loading

0 comments on commit 7886bb9

Please sign in to comment.