Files
2026-03-04 10:03:45 +08:00

1215 lines
36 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Gaia.FullSerializer;
using GaiaCommon1;
using UnityEngine;
namespace Gaia
{
[ExecuteInEditMode]
public class GaiaSessionManager : MonoBehaviour
{
public IEnumerator m_updateSessionCoroutine;
public IEnumerator m_updateOperationCoroutine;
private bool m_cancelPlayback;
public GaiaSession m_session;
public bool m_focusSceneView = true;
public bool m_genShowRandomGenerator;
public bool m_genShowTerrainHelper;
public GaiaConstants.GeneratorBorderStyle m_genBorderStyle = GaiaConstants.GeneratorBorderStyle.Water;
public int m_genGridSize = 3;
public int m_genNumStampsToGenerate = 10;
public float m_genScaleWidth = 10f;
public float m_genScaleHeight = 4f;
public float m_genChanceOfHills = 0.7f;
public float m_genChanceOfIslands;
public float m_genChanceOfLakes;
public float m_genChanceOfMesas = 0.1f;
public float m_genChanceOfMountains = 0.1f;
public float m_genChanceOfPlains;
public float m_genChanceOfRivers = 0.1f;
public float m_genChanceOfValleys;
public float m_genChanceOfVillages;
public float m_genChanceOfWaterfalls;
[fsIgnore]
public Stamper m_currentStamper;
[fsIgnore]
public Spawner m_currentSpawner;
[fsIgnore]
public DateTime m_lastUpdateDateTime = DateTime.Now;
[fsIgnore]
public ulong m_progress;
private List<string> m_genHillStamps = new List<string>();
private List<string> m_genIslandStamps = new List<string>();
private List<string> m_genLakeStamps = new List<string>();
private List<string> m_genMesaStamps = new List<string>();
private List<string> m_genMountainStamps = new List<string>();
private List<string> m_genPlainsStamps = new List<string>();
private List<string> m_genRiverStamps = new List<string>();
private List<string> m_genValleyStamps = new List<string>();
private List<string> m_genVillageStamps = new List<string>();
private List<string> m_genWaterfallStamps = new List<string>();
public static GaiaSessionManager GetSessionManager(bool pickupExistingTerrain = false)
{
GameObject gameObject = GameObject.Find("Gaia");
if (gameObject == null)
{
gameObject = new GameObject("Gaia");
}
GaiaSessionManager gaiaSessionManager = null;
GameObject gameObject2 = GameObject.Find("Session Manager");
if (gameObject2 == null)
{
gameObject2 = new GameObject("Session Manager");
gaiaSessionManager = gameObject2.AddComponent<GaiaSessionManager>();
gaiaSessionManager.CreateSession(pickupExistingTerrain);
gameObject2.transform.parent = gameObject.transform;
gameObject2.transform.position = TerrainHelper.GetActiveTerrainCenter();
}
else
{
gaiaSessionManager = gameObject2.GetComponent<GaiaSessionManager>();
}
return gaiaSessionManager;
}
public bool IsLocked()
{
if (m_session == null)
{
CreateSession();
}
return m_session.m_isLocked;
}
public bool LockSession()
{
if (m_session == null)
{
CreateSession();
}
bool isLocked = m_session.m_isLocked;
m_session.m_isLocked = true;
if (!isLocked)
{
SaveSession();
}
return isLocked;
}
public bool UnLockSession()
{
if (m_session == null)
{
CreateSession();
}
bool isLocked = m_session.m_isLocked;
m_session.m_isLocked = false;
if (isLocked)
{
SaveSession();
}
return isLocked;
}
public void AddOperation(GaiaOperation operation)
{
if (IsLocked())
{
Debug.Log("Cant add operation on locked session");
return;
}
m_session.m_operations.Add(operation);
SaveSession();
}
public GaiaOperation GetOperation(int operationIdx)
{
if (m_session == null)
{
CreateSession();
}
if (operationIdx < 0 || operationIdx >= m_session.m_operations.Count)
{
return null;
}
return m_session.m_operations[operationIdx];
}
public void RemoveOperation(int operationIdx)
{
if (IsLocked())
{
Debug.Log("Cant remove operation on locked session");
}
else if (operationIdx >= 0 && operationIdx < m_session.m_operations.Count)
{
m_session.m_operations.RemoveAt(operationIdx);
SaveSession();
}
}
public void AddResource(GaiaResource resource)
{
if (IsLocked())
{
Debug.Log("Cant add resource on locked session");
}
else if (resource != null)
{
m_session.m_resources.ContainsKey(resource.m_resourcesID + resource.name);
}
}
public void AddDefaults(GaiaDefaults defaults)
{
if (IsLocked())
{
Debug.Log("Cant add defaults on locked session");
}
else
{
_ = defaults != null;
}
}
public void AddPreviewImage(Texture2D image)
{
if (IsLocked())
{
Debug.Log("Cant add preview on locked session");
return;
}
m_session.m_previewImageWidth = image.width;
m_session.m_previewImageHeight = image.height;
m_session.m_previewImageBytes = image.GetRawTextureData();
SaveSession();
}
public bool HasPreviewImage()
{
if (m_session.m_previewImageWidth > 0 && m_session.m_previewImageHeight > 0 && m_session.m_previewImageBytes.GetLength(0) > 0)
{
return true;
}
return false;
}
public void RemovePreviewImage()
{
if (IsLocked())
{
Debug.Log("Cant remove preview on locked session");
return;
}
m_session.m_previewImageWidth = 0;
m_session.m_previewImageHeight = 0;
m_session.m_previewImageBytes = new byte[0];
SaveSession();
}
public Texture2D GetPreviewImage()
{
if (m_session.m_previewImageBytes.GetLength(0) == 0)
{
return null;
}
Texture2D texture2D = new Texture2D(m_session.m_previewImageWidth, m_session.m_previewImageHeight, TextureFormat.ARGB32, mipChain: false);
texture2D.LoadRawTextureData(m_session.m_previewImageBytes);
texture2D.Apply();
texture2D.name = m_session.m_name;
return texture2D;
}
public void SaveSession()
{
}
public void StartEditorUpdates()
{
}
public void StopEditorUpdates()
{
m_currentSpawner = null;
m_currentStamper = null;
m_updateOperationCoroutine = null;
m_updateSessionCoroutine = null;
}
private void EditorUpdate()
{
if (m_cancelPlayback)
{
if (m_currentSpawner != null)
{
m_currentSpawner.CancelSpawn();
}
if (m_currentStamper != null)
{
m_currentStamper.CancelStamp();
}
StopEditorUpdates();
}
else if (m_updateSessionCoroutine == null && m_updateOperationCoroutine == null)
{
StopEditorUpdates();
}
else if (m_updateOperationCoroutine != null)
{
m_updateOperationCoroutine.MoveNext();
}
else
{
m_updateSessionCoroutine.MoveNext();
}
}
public GaiaSession CreateSession(bool pickupExistingTerrain = false)
{
m_session = ScriptableObject.CreateInstance<GaiaSession>();
m_session.m_description = "Rocking out at Creativity Central! If you like Gaia please consider rating it :)";
GaiaSettings gaiaSettings = GaiaUtils.GetGaiaSettings();
if (gaiaSettings != null && gaiaSettings.m_currentDefaults != null)
{
m_session.m_seaLevel = gaiaSettings.m_currentDefaults.m_seaLevel;
}
Terrain activeTerrain = TerrainHelper.GetActiveTerrain();
if (activeTerrain != null)
{
m_session.m_terrainWidth = (int)activeTerrain.terrainData.size.x;
m_session.m_terrainDepth = (int)activeTerrain.terrainData.size.z;
m_session.m_terrainHeight = (int)activeTerrain.terrainData.size.y;
if (pickupExistingTerrain)
{
GaiaDefaults gaiaDefaults = ScriptableObject.CreateInstance<GaiaDefaults>();
gaiaDefaults.UpdateFromTerrain();
GaiaResource gaiaResource = ScriptableObject.CreateInstance<GaiaResource>();
gaiaResource.UpdatePrototypesFromTerrain();
gaiaResource.ChangeSeaLevel(m_session.m_seaLevel);
AddDefaults(gaiaDefaults);
AddResource(gaiaResource);
AddOperation(gaiaDefaults.GetTerrainCreationOperation(gaiaResource));
}
}
else if (gaiaSettings != null && gaiaSettings.m_currentDefaults != null)
{
m_session.m_terrainWidth = gaiaSettings.m_currentDefaults.m_terrainSize;
m_session.m_terrainDepth = gaiaSettings.m_currentDefaults.m_terrainHeight;
m_session.m_terrainHeight = gaiaSettings.m_currentDefaults.m_terrainSize;
}
float num = 0.0048828125f;
float num2 = 0.001953125f;
float num3 = 0.024414062f;
float num4 = m_session.m_terrainWidth;
float num5 = m_session.m_terrainHeight;
m_genScaleWidth = Mathf.Clamp(num4 * num, 0.1f, 40f);
m_genScaleHeight = Mathf.Clamp(num5 * num2, 0.1f, 16f);
m_session.m_seaLevel = Mathf.Clamp(num5 * num3, 0f, 150f);
return m_session;
}
public void SetSeaLevel(float seaLevel)
{
m_session.m_seaLevel = seaLevel;
}
public float GetSeaLevel()
{
if (m_session != null)
{
return m_session.m_seaLevel;
}
GaiaSettings gaiaSettings = GaiaUtils.GetGaiaSettings();
if (gaiaSettings != null)
{
return gaiaSettings.m_currentDefaults.m_seaLevel;
}
return 50f;
}
public void ResetSession()
{
if (m_session == null)
{
Debug.LogError("Can not erase the session as there is no existing session!");
}
else if (m_session.m_isLocked)
{
Debug.LogError("Can not erase the session as it is locked!");
}
else if (m_session.m_operations.Count > 1)
{
GaiaOperation gaiaOperation = m_session.m_operations[0];
m_session.m_operations.Clear();
if (gaiaOperation.m_operationType == GaiaOperation.OperationType.CreateTerrain)
{
AddOperation(gaiaOperation);
}
}
}
public void RandomiseStamps()
{
if (m_session == null)
{
Debug.LogError("Can not randomise stamps as there is no existing session!");
return;
}
if (m_session.m_isLocked)
{
Debug.LogError("Can not randomise stamps as the existing session is locked!");
return;
}
Terrain activeTerrain = TerrainHelper.GetActiveTerrain();
if (activeTerrain == null)
{
GaiaSettings gaiaSettings = (GaiaSettings)GaiaUtils.GetAssetScriptableObject("GaiaSettings");
if (gaiaSettings == null)
{
Debug.LogError("Can not randomise stamps as we are missing the terrain and settings!");
return;
}
GaiaDefaults currentDefaults = gaiaSettings.m_currentDefaults;
GaiaResource currentResources = gaiaSettings.m_currentResources;
if (currentDefaults == null || currentResources == null)
{
Debug.LogError("Can not randomise stamps as we are missing the terrain defaults or resources!");
return;
}
currentDefaults.CreateTerrain(currentResources);
activeTerrain = TerrainHelper.GetActiveTerrain();
}
Bounds bounds = default(Bounds);
TerrainHelper.GetTerrainBounds(activeTerrain, ref bounds);
GameObject gameObject = GameObject.Find("Gaia");
if (gameObject == null)
{
gameObject = new GameObject("Gaia");
}
Stamper stamper = null;
GameObject gameObject2 = GameObject.Find("Stamper");
if (gameObject2 == null)
{
gameObject2 = new GameObject("Stamper");
gameObject2.transform.parent = gameObject.transform;
stamper = gameObject2.AddComponent<Stamper>();
}
else
{
stamper = gameObject2.GetComponent<Stamper>();
}
float num = 1f;
if (m_genNumStampsToGenerate > 1)
{
num = Mathf.Sqrt((float)m_genNumStampsToGenerate - 1f);
if (!GaiaUtils.Math_ApproximatelyEqual(0f, num % 1f))
{
m_genNumStampsToGenerate = Mathf.CeilToInt(num);
m_genNumStampsToGenerate = m_genNumStampsToGenerate * m_genNumStampsToGenerate + 1;
}
num = Mathf.Sqrt((float)m_genNumStampsToGenerate - 1f);
}
for (int i = 0; i < m_genNumStampsToGenerate; i++)
{
string imagePreviewPath = "";
GaiaConstants.FeatureType featureType = GaiaConstants.FeatureType.Hills;
stamper.LoadStamp(imagePreviewPath);
stamper.FitToTerrain();
stamper.HidePreview();
if (m_session.m_operations.Count <= 1)
{
float width = stamper.m_width;
PositionStamp(bounds, stamper, featureType);
stamper.m_rotation = 0f;
stamper.m_x = 0f;
stamper.m_z = 0f;
stamper.m_width = width;
if (m_genBorderStyle == GaiaConstants.GeneratorBorderStyle.Mountains)
{
stamper.m_distanceMask = new AnimationCurve(new Keyframe(1f, 1f), new Keyframe(1f, 1f));
stamper.m_areaMaskMode = GaiaConstants.ImageFitnessFilterMode.ImageGreyScale;
stamper.m_imageMask = GaiaUtils.GetAsset("Island Mask 1.jpg", typeof(Texture2D)) as Texture2D;
stamper.m_imageMaskNormalise = true;
stamper.m_imageMaskInvert = true;
}
else
{
stamper.m_distanceMask = new AnimationCurve(new Keyframe(1f, 1f), new Keyframe(1f, 1f));
stamper.m_areaMaskMode = GaiaConstants.ImageFitnessFilterMode.ImageGreyScale;
stamper.m_imageMask = GaiaUtils.GetAsset("Island Mask 1.jpg", typeof(Texture2D)) as Texture2D;
stamper.m_imageMaskNormalise = true;
stamper.m_imageMaskInvert = false;
}
}
else
{
int num2 = (i - 1) % (int)num;
int num3 = (i - 1) / (int)num;
float num4 = 1f / num;
float num5 = num4 / 2f;
float suggestZ = num5 + num4 * (float)num2;
float suggestX = num5 + num4 * (float)num3;
PositionStampV2(bounds, stamper, featureType, suggestX, suggestZ, num5);
float num6 = UnityEngine.Random.Range(0f, 1f);
if (num6 < 0.1f)
{
stamper.m_stampOperation = GaiaConstants.FeatureOperation.LowerHeight;
stamper.m_invertStamp = true;
}
else if (num6 < 0.35f)
{
stamper.m_stampOperation = GaiaConstants.FeatureOperation.StencilHeight;
stamper.m_normaliseStamp = true;
if (featureType == GaiaConstants.FeatureType.Rivers || featureType == GaiaConstants.FeatureType.Lakes)
{
stamper.m_invertStamp = true;
stamper.m_stencilHeight = UnityEngine.Random.Range(-80f, -5f);
}
else if (UnityEngine.Random.Range(0f, 1f) < 0.5f)
{
stamper.m_invertStamp = true;
stamper.m_stencilHeight = UnityEngine.Random.Range(-80f, -5f);
}
else
{
stamper.m_invertStamp = false;
stamper.m_stencilHeight = UnityEngine.Random.Range(5f, 80f);
}
}
else
{
stamper.m_stampOperation = GaiaConstants.FeatureOperation.RaiseHeight;
stamper.m_invertStamp = false;
}
}
stamper.UpdateStamp();
stamper.AddToSession(GaiaOperation.OperationType.Stamp, "Stamping " + stamper.m_stampPreviewImage.name);
}
}
private void PositionStamp(Bounds bounds, Stamper stamper, GaiaConstants.FeatureType stampType)
{
float baseLevel = 0f;
float minHeight = 0f;
float maxHeight = 0f;
float num = stamper.m_height * 4f;
float num2 = 0f;
if ((float)m_session.m_terrainHeight > 0f)
{
num2 = m_session.m_seaLevel / (float)m_session.m_terrainHeight;
}
if (stamper.GetHeightRange(ref baseLevel, ref minHeight, ref maxHeight))
{
stamper.m_stampOperation = GaiaConstants.FeatureOperation.RaiseHeight;
stamper.m_invertStamp = false;
stamper.m_normaliseStamp = false;
stamper.m_rotation = UnityEngine.Random.Range(-179f, 179f);
stamper.m_width = UnityEngine.Random.Range(0.7f, 1.3f) * m_genScaleWidth;
stamper.m_height = UnityEngine.Random.Range(0.7f, 1.3f) * m_genScaleHeight;
float num3 = stamper.m_height / num * (float)m_session.m_terrainHeight;
float num4 = num3 / 2f;
float num5 = num2 * (float)m_session.m_terrainHeight;
stamper.m_stickBaseToGround = false;
stamper.m_y = num4 + num5 - baseLevel * num3;
float num6 = 1f;
if (m_genBorderStyle == GaiaConstants.GeneratorBorderStyle.None)
{
stamper.m_x = UnityEngine.Random.Range(0f - bounds.extents.x, bounds.extents.x);
stamper.m_z = UnityEngine.Random.Range(0f - bounds.extents.z, bounds.extents.z);
}
else if (m_genBorderStyle == GaiaConstants.GeneratorBorderStyle.Mountains)
{
num6 = 0.85f;
stamper.m_x = UnityEngine.Random.Range(0f - bounds.extents.x * num6, bounds.extents.x * num6);
stamper.m_z = UnityEngine.Random.Range(0f - bounds.extents.z * num6, bounds.extents.z * num6);
}
else
{
num6 = 0.75f;
stamper.m_x = UnityEngine.Random.Range(0f - bounds.extents.x * num6, bounds.extents.x * num6);
stamper.m_z = UnityEngine.Random.Range(0f - bounds.extents.z * num6, bounds.extents.z * num6);
}
stamper.m_distanceMask = new AnimationCurve(new Keyframe(0f, 1f), new Keyframe(1f, 0f));
stamper.m_areaMaskMode = GaiaConstants.ImageFitnessFilterMode.None;
stamper.m_imageMask = null;
}
}
private void PositionStampV2(Bounds bounds, Stamper stamper, GaiaConstants.FeatureType stampType, float suggestX, float suggestZ, float suggestJitter)
{
float baseLevel = 0f;
float minHeight = 0f;
float maxHeight = 0f;
float num = stamper.m_height * 4f;
float num2 = 0f;
if ((float)m_session.m_terrainHeight > 0f)
{
num2 = m_session.m_seaLevel / (float)m_session.m_terrainHeight;
}
if (stamper.GetHeightRange(ref baseLevel, ref minHeight, ref maxHeight))
{
stamper.m_stampOperation = GaiaConstants.FeatureOperation.RaiseHeight;
stamper.m_invertStamp = false;
stamper.m_normaliseStamp = false;
stamper.m_rotation = UnityEngine.Random.Range(-179f, 179f);
stamper.m_width = UnityEngine.Random.Range(0.7f, 1.3f) * m_genScaleWidth;
stamper.m_height = UnityEngine.Random.Range(0.7f, 1.3f) * m_genScaleHeight;
float num3 = stamper.m_height / num * (float)m_session.m_terrainHeight;
float num4 = num3 / 2f;
float num5 = num2 * (float)m_session.m_terrainHeight;
stamper.m_stickBaseToGround = false;
stamper.m_y = num4 + num5 - baseLevel * num3;
if (m_genBorderStyle == GaiaConstants.GeneratorBorderStyle.Water)
{
Vector2 a = new Vector2(suggestX, suggestZ);
Vector2 b = new Vector2(0.5f, 0.5f);
float num6 = Vector2.Distance(a, b) * 2f;
Vector2 vector = Vector2.Lerp(a, b, num6 / 2f);
suggestX = vector.x;
suggestZ = vector.y;
}
float x = bounds.size.x;
float z = bounds.size.z;
float num7 = x * suggestJitter;
float num8 = z * suggestJitter;
stamper.m_x = bounds.min.x + suggestX * x + UnityEngine.Random.Range(0f - num7, num7);
stamper.m_z = bounds.min.z + suggestZ * z + UnityEngine.Random.Range(0f - num8, num8);
stamper.m_distanceMask = new AnimationCurve(new Keyframe(0f, 1f), new Keyframe(1f, 0f));
stamper.m_areaMaskMode = GaiaConstants.ImageFitnessFilterMode.None;
stamper.m_imageMask = null;
}
}
private GaiaConstants.FeatureType GetWeightedRandomFeatureType()
{
float num = UnityEngine.Random.Range(0f, 1f);
float num2 = m_genChanceOfHills + m_genChanceOfIslands + m_genChanceOfLakes + m_genChanceOfMesas + m_genChanceOfMountains + m_genChanceOfPlains + m_genChanceOfRivers + m_genChanceOfValleys + m_genChanceOfVillages + m_genChanceOfWaterfalls;
if (num2 == 0f)
{
num2 = 1f;
}
float num3 = 0f;
float num4 = 0f;
num4 = num3 + m_genChanceOfHills / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Hills;
}
num3 = num4;
num4 = num3 + m_genChanceOfIslands / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Islands;
}
num3 = num4;
num4 = num3 + m_genChanceOfLakes / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Lakes;
}
num3 = num4;
num4 = num3 + m_genChanceOfMesas / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Mesas;
}
num3 = num4;
num4 = num3 + m_genChanceOfMountains / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Mountains;
}
num3 = num4;
num4 = num3 + m_genChanceOfPlains / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Plains;
}
num3 = num4;
num4 = num3 + m_genChanceOfRivers / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Rivers;
}
num3 = num4;
num4 = num3 + m_genChanceOfValleys / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Valleys;
}
num3 = num4;
num4 = num3 + m_genChanceOfVillages / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Villages;
}
num3 = num4;
num4 = num3 + m_genChanceOfWaterfalls / num2;
if (num >= num3 && num < num4)
{
return GaiaConstants.FeatureType.Waterfalls;
}
return (GaiaConstants.FeatureType)UnityEngine.Random.Range(2, 7);
}
public string GetRandomStampPath(GaiaConstants.FeatureType featureType)
{
switch (featureType)
{
case GaiaConstants.FeatureType.Adhoc:
return "";
case GaiaConstants.FeatureType.Bases:
return "";
case GaiaConstants.FeatureType.Hills:
if (m_genHillStamps.Count == 0)
{
m_genHillStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Hills);
}
if (m_genHillStamps.Count == 0)
{
return "";
}
return m_genHillStamps[UnityEngine.Random.Range(0, m_genHillStamps.Count - 1)];
case GaiaConstants.FeatureType.Islands:
if (m_genIslandStamps.Count == 0)
{
m_genIslandStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Islands);
}
if (m_genIslandStamps.Count == 0)
{
return "";
}
return m_genIslandStamps[UnityEngine.Random.Range(0, m_genIslandStamps.Count - 1)];
case GaiaConstants.FeatureType.Lakes:
if (m_genLakeStamps.Count == 0)
{
m_genLakeStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Lakes);
}
if (m_genLakeStamps.Count == 0)
{
return "";
}
return m_genLakeStamps[UnityEngine.Random.Range(0, m_genLakeStamps.Count - 1)];
case GaiaConstants.FeatureType.Mesas:
if (m_genMesaStamps.Count == 0)
{
m_genMesaStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Mesas);
}
if (m_genMesaStamps.Count == 0)
{
return "";
}
return m_genMesaStamps[UnityEngine.Random.Range(0, m_genMesaStamps.Count - 1)];
case GaiaConstants.FeatureType.Mountains:
if (m_genMountainStamps.Count == 0)
{
m_genMountainStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Mountains);
}
if (m_genMountainStamps.Count == 0)
{
return "";
}
return m_genMountainStamps[UnityEngine.Random.Range(0, m_genMountainStamps.Count - 1)];
case GaiaConstants.FeatureType.Plains:
if (m_genPlainsStamps.Count == 0)
{
m_genPlainsStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Plains);
}
if (m_genPlainsStamps.Count == 0)
{
return "";
}
return m_genPlainsStamps[UnityEngine.Random.Range(0, m_genPlainsStamps.Count - 1)];
case GaiaConstants.FeatureType.Rivers:
if (m_genRiverStamps.Count == 0)
{
m_genRiverStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Rivers);
}
if (m_genRiverStamps.Count == 0)
{
return "";
}
return m_genRiverStamps[UnityEngine.Random.Range(0, m_genRiverStamps.Count - 1)];
case GaiaConstants.FeatureType.Rocks:
return "";
case GaiaConstants.FeatureType.Valleys:
if (m_genValleyStamps.Count == 0)
{
m_genValleyStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Valleys);
}
if (m_genValleyStamps.Count == 0)
{
return "";
}
return m_genValleyStamps[UnityEngine.Random.Range(0, m_genValleyStamps.Count - 1)];
case GaiaConstants.FeatureType.Villages:
if (m_genVillageStamps.Count == 0)
{
m_genVillageStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Villages);
}
if (m_genVillageStamps.Count == 0)
{
return "";
}
return m_genVillageStamps[UnityEngine.Random.Range(0, m_genVillageStamps.Count - 1)];
case GaiaConstants.FeatureType.Waterfalls:
if (m_genWaterfallStamps.Count == 0)
{
m_genWaterfallStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Waterfalls);
}
if (m_genWaterfallStamps.Count == 0)
{
return "";
}
return m_genWaterfallStamps[UnityEngine.Random.Range(0, m_genWaterfallStamps.Count - 1)];
default:
return "";
}
}
public string GetRandomMountainFieldPath()
{
if (m_genMountainStamps.Count == 0)
{
m_genMountainStamps = GaiaUtils.GetGaiaStampsList(GaiaConstants.FeatureType.Mountains);
}
if (m_genMountainStamps.Count == 0)
{
return "";
}
int num = 0;
int num2 = 0;
for (num = 0; num < m_genMountainStamps.Count; num++)
{
string text = m_genMountainStamps[num];
if (text.Contains("Field"))
{
num2++;
}
}
int num3 = 0;
int num4 = UnityEngine.Random.Range(0, num2 - 1);
for (num = 0; num < m_genMountainStamps.Count; num++)
{
string text = m_genMountainStamps[num];
if (text.Contains("Field"))
{
if (num3 == num4)
{
return text;
}
num3++;
}
}
return "";
}
public GameObject Apply(int operationIdx)
{
if (operationIdx < 0 || operationIdx >= m_session.m_operations.Count)
{
Debug.LogWarning($"Can not Apply operation because the index {operationIdx} is out of bounds.");
return null;
}
GaiaOperation gaiaOperation = m_session.m_operations[operationIdx];
GameObject gameObject = FindOrCreateObject(gaiaOperation);
if (gameObject == null)
{
return gameObject;
}
Stamper component = gameObject.GetComponent<Stamper>();
if (component != null && gaiaOperation.m_operationType == GaiaOperation.OperationType.Stamp)
{
component.DeSerialiseJson(gaiaOperation.m_operationDataJson[0]);
component.m_resources = GaiaUtils.GetAsset(ScriptableObjectWrapper.GetSessionedFileName(m_session.GetSessionFileName(), component.m_resourcesPath), typeof(GaiaResource)) as GaiaResource;
if (component.m_resources == null)
{
ExportSessionResource(component.m_resourcesPath);
component.m_resources = GaiaUtils.GetAsset(ScriptableObjectWrapper.GetSessionedFileName(m_session.GetSessionFileName(), component.m_resourcesPath), typeof(GaiaResource)) as GaiaResource;
}
component.m_seaLevel = m_session.m_seaLevel;
}
Spawner component2 = gameObject.GetComponent<Spawner>();
if (component2 != null && gaiaOperation.m_operationType == GaiaOperation.OperationType.Spawn)
{
component2.DeSerialiseJson(gaiaOperation.m_operationDataJson[0]);
component2.m_resources = GaiaUtils.GetAsset(ScriptableObjectWrapper.GetSessionedFileName(m_session.GetSessionFileName(), component2.m_resourcesPath), typeof(GaiaResource)) as GaiaResource;
if (component2.m_resources == null)
{
ExportSessionResource(component2.m_resourcesPath);
component2.m_resources = GaiaUtils.GetAsset(ScriptableObjectWrapper.GetSessionedFileName(m_session.GetSessionFileName(), component2.m_resourcesPath), typeof(GaiaResource)) as GaiaResource;
}
if (component2.m_resources == null)
{
Debug.LogError("Unable to get resources file for " + component2.name);
}
else
{
component2.AssociateAssets();
int[] missingResources = component2.GetMissingResources();
if (missingResources.GetLength(0) > 0)
{
component2.AddResourcesToTerrain(missingResources);
}
component2.m_resources.ChangeSeaLevel(m_session.m_seaLevel);
}
}
return gameObject;
}
public void PlaySession()
{
m_cancelPlayback = false;
ExportSessionResources();
StartCoroutine(PlaySessionCoRoutine());
}
public IEnumerator PlaySessionCoRoutine()
{
m_progress = 0uL;
if (Application.isPlaying)
{
for (int idx = 0; idx < m_session.m_operations.Count; idx++)
{
if (!m_cancelPlayback && m_session.m_operations[idx].m_isActive)
{
yield return StartCoroutine(PlayOperationCoRoutine(idx));
}
}
}
else
{
for (int idx = 0; idx < m_session.m_operations.Count; idx++)
{
if (!m_cancelPlayback && m_session.m_operations[idx].m_isActive)
{
m_updateOperationCoroutine = PlayOperationCoRoutine(idx);
yield return new WaitForSeconds(0.2f);
}
}
}
Debug.Log("Finished playing session " + m_session.m_name);
m_updateSessionCoroutine = null;
}
public void PlayOperation(int opIdx)
{
m_cancelPlayback = false;
StartCoroutine(PlayOperationCoRoutine(opIdx));
}
public IEnumerator PlayOperationCoRoutine(int operationIdx)
{
if (operationIdx < 0 || operationIdx >= m_session.m_operations.Count)
{
Debug.LogWarning($"Operation index {operationIdx} is out of bounds.");
m_updateOperationCoroutine = null;
yield break;
}
if (!m_session.m_operations[operationIdx].m_isActive)
{
Debug.LogWarning($"Operation '{m_session.m_operations[operationIdx].m_description}' is not active. Ignoring.");
m_updateOperationCoroutine = null;
yield break;
}
bool lockState = m_session.m_isLocked;
m_session.m_isLocked = true;
GaiaOperation gaiaOperation = m_session.m_operations[operationIdx];
GameObject gameObject = Apply(operationIdx);
Stamper stamper = null;
Spawner spawner = null;
if (gameObject != null)
{
stamper = gameObject.GetComponent<Stamper>();
spawner = gameObject.GetComponent<Spawner>();
}
switch (gaiaOperation.m_operationType)
{
case GaiaOperation.OperationType.CreateTerrain:
if (TerrainHelper.GetActiveTerrainCount() == 0 && m_session.m_defaults != null && m_session.m_defaults.m_content.GetLength(0) <= 0)
{
}
break;
case GaiaOperation.OperationType.FlattenTerrain:
if (stamper != null)
{
stamper.FlattenTerrain();
}
break;
case GaiaOperation.OperationType.SmoothTerrain:
if (stamper != null)
{
stamper.SmoothTerrain();
}
break;
case GaiaOperation.OperationType.ClearDetails:
if (stamper != null)
{
stamper.ClearDetails();
}
break;
case GaiaOperation.OperationType.ClearTrees:
if (stamper != null)
{
stamper.ClearTrees();
}
break;
case GaiaOperation.OperationType.Stamp:
if (!(stamper != null))
{
break;
}
m_currentStamper = stamper;
m_currentSpawner = null;
if (!Application.isPlaying)
{
stamper.HidePreview();
stamper.Stamp();
while (stamper.IsStamping())
{
if ((DateTime.Now - m_lastUpdateDateTime).Milliseconds > 250)
{
m_lastUpdateDateTime = DateTime.Now;
m_progress++;
}
yield return new WaitForSeconds(0.2f);
}
}
else
{
yield return StartCoroutine(stamper.ApplyStamp());
}
break;
case GaiaOperation.OperationType.StampUndo:
if (stamper != null)
{
stamper.Undo();
}
break;
case GaiaOperation.OperationType.StampRedo:
if (stamper != null)
{
stamper.Redo();
}
break;
case GaiaOperation.OperationType.Spawn:
if (!(spawner != null))
{
break;
}
m_currentStamper = null;
m_currentSpawner = spawner;
if (Application.isPlaying)
{
break;
}
spawner.RunSpawnerIteration();
while (spawner.IsSpawning())
{
if ((DateTime.Now - m_lastUpdateDateTime).Milliseconds > 250)
{
m_lastUpdateDateTime = DateTime.Now;
m_progress++;
}
yield return new WaitForSeconds(0.2f);
}
break;
}
m_session.m_isLocked = lockState;
m_updateOperationCoroutine = null;
}
public void CancelPlayback()
{
m_cancelPlayback = true;
if (m_currentStamper != null)
{
m_currentStamper.CancelStamp();
}
if (m_currentSpawner != null)
{
m_currentSpawner.CancelSpawn();
}
}
public void ExportSessionResources()
{
string text = "Assets/GaiaSessions/";
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
text = Path.Combine(text, GaiaCommon1.Utils.FixFileName(m_session.m_name));
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
if (m_session != null && m_session.m_defaults != null && m_session.m_defaults.m_content != null && m_session.m_defaults.m_content.GetLength(0) > 0)
{
GaiaCommon1.Utils.WriteAllBytes(Path.Combine(text, ScriptableObjectWrapper.GetSessionedFileName(m_session.m_name, m_session.m_defaults.m_fileName)), m_session.m_defaults.m_content);
}
foreach (KeyValuePair<string, ScriptableObjectWrapper> resource in m_session.m_resources)
{
GaiaCommon1.Utils.WriteAllBytes(Path.Combine(text, ScriptableObjectWrapper.GetSessionedFileName(m_session.m_name, resource.Value.m_fileName)), resource.Value.m_content);
}
}
public void ExportSessionDefaults()
{
string text = "Assets/GaiaSessions/";
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
text = Path.Combine(text, GaiaCommon1.Utils.FixFileName(m_session.m_name));
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
if (m_session != null && m_session.m_defaults != null && m_session.m_defaults.m_content != null && m_session.m_defaults.m_content.GetLength(0) > 0)
{
GaiaCommon1.Utils.WriteAllBytes(Path.Combine(text, ScriptableObjectWrapper.GetSessionedFileName(m_session.m_name, m_session.m_defaults.m_fileName)), m_session.m_defaults.m_content);
}
}
public void ExportSessionResource(string resourcePath)
{
string text = "Assets/GaiaSessions/";
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
text = Path.Combine(text, GaiaCommon1.Utils.FixFileName(m_session.m_name));
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
foreach (KeyValuePair<string, ScriptableObjectWrapper> resource in m_session.m_resources)
{
if (Path.GetFileName(resourcePath).ToLower() == Path.GetFileName(resource.Value.m_fileName).ToLower())
{
GaiaCommon1.Utils.WriteAllBytes(Path.Combine(text, ScriptableObjectWrapper.GetSessionedFileName(m_session.m_name, resource.Value.m_fileName)), resource.Value.m_content);
}
}
}
private void OnDrawGizmosSelected()
{
if (m_session != null)
{
Bounds bounds = default(Bounds);
if (TerrainHelper.GetTerrainBounds(base.transform.position, ref bounds))
{
Gizmos.color = Color.white;
Gizmos.DrawWireCube(bounds.center, bounds.size);
bounds.center = new Vector3(bounds.center.x, m_session.m_seaLevel, bounds.center.z);
bounds.size = new Vector3(bounds.size.x, 0.05f, bounds.size.z);
Gizmos.color = new Color(Color.blue.r, Color.blue.g, Color.blue.b, Color.blue.a / 4f);
Gizmos.DrawCube(bounds.center, bounds.size);
}
}
}
private GameObject FindOrCreateObject(GaiaOperation operation)
{
if (operation.m_generatedByType == "Gaia.Stamper")
{
Stamper[] array = UnityEngine.Object.FindObjectsOfType<Stamper>();
for (int i = 0; i < array.GetLength(0); i++)
{
if (array[i].m_stampID == operation.m_generatedByID && array[i].name == operation.m_generatedByName)
{
return array[i].gameObject;
}
}
return ShowStamper(operation.m_generatedByName, operation.m_generatedByID);
}
if (operation.m_generatedByType == "Gaia.Spawner")
{
Spawner[] array2 = UnityEngine.Object.FindObjectsOfType<Spawner>();
for (int j = 0; j < array2.GetLength(0); j++)
{
if (array2[j].m_spawnerID == operation.m_generatedByID && array2[j].name == operation.m_generatedByName)
{
return array2[j].gameObject;
}
}
return CreateSpawner(operation.m_generatedByName, operation.m_generatedByID);
}
return null;
}
private GameObject ShowStamper(string name, string id)
{
GameObject gameObject = GameObject.Find("Gaia");
if (gameObject == null)
{
gameObject = new GameObject("Gaia");
}
GameObject gameObject2 = GameObject.Find(name);
if (gameObject2 == null)
{
gameObject2 = new GameObject(name);
gameObject2.transform.parent = gameObject.transform;
Stamper stamper = gameObject2.AddComponent<Stamper>();
stamper.m_stampID = id;
stamper.HidePreview();
stamper.m_seaLevel = m_session.m_seaLevel;
}
return gameObject2;
}
private GameObject CreateSpawner(string name, string id)
{
GameObject gameObject = GameObject.Find("Gaia");
if (gameObject == null)
{
gameObject = new GameObject("Gaia");
}
GameObject obj = new GameObject(name);
obj.transform.parent = gameObject.transform;
obj.AddComponent<Spawner>().m_spawnerID = id;
return obj;
}
}
}