using UnityEngine;
///
/// Simplified MapMaker2 - Basic map generation with team placement on towns/villages
/// No exploration or expansion systems - just a clean, working map
///
public class SimpleMapMaker2 : MonoBehaviour
{
[Header("Basic Map Settings")]
public int mapSize = 100;
public int seed = 12345;
[Header("Team Placement")]
public GameObject teamMarkerPrefab;
[Header("Visualization")]
public MapVisualizer mapVisualizer;
// Core components
private MapData mapData;
private Transform teamMarker;
void Start()
{
GenerateSimpleMap();
}
///
/// Generate a simple map and place team marker on a town/village
///
public void GenerateSimpleMap()
{
Debug.Log("πΊοΈ SimpleMapMaker2: Generating basic map...");
// Generate basic map data
CreateBasicMapData();
// Visualize the map
if (mapVisualizer != null)
{
mapVisualizer.VisualizeMap(mapData);
Debug.Log("β
Map visualization complete");
}
else
{
Debug.LogWarning("β οΈ MapVisualizer not assigned - creating simple cubes for visualization");
CreateSimpleCubeVisualization();
}
// Place team marker on a town/village
PlaceTeamMarkerOnTown();
Debug.Log($"π― SimpleMapMaker2: Map generation complete - Size: {mapSize}x{mapSize}");
}
///
/// Create basic map data with terrain and settlements
///
private void CreateBasicMapData()
{
Random.InitState(seed);
mapData = new MapData(mapSize, mapSize);
// Generate basic terrain
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
// Simple terrain generation
float noise = Mathf.PerlinNoise(x * 0.1f, y * 0.1f);
MapTile tile = mapData.GetTile(x, y);
if (tile != null)
{
if (noise < 0.3f)
{
tile.terrainType = TerrainType.Plains;
}
else if (noise < 0.6f)
{
tile.terrainType = TerrainType.Forest;
}
else if (noise < 0.8f)
{
tile.terrainType = TerrainType.Plains; // Use plains for hills
tile.height = 1f; // Use height for elevation
}
else
{
tile.terrainType = TerrainType.Mountain;
}
}
}
}
// Add some roads
AddBasicRoads();
// Add settlements (towns and villages)
AddSettlements();
Debug.Log($"π Basic map data created: {mapSize}x{mapSize}");
}
///
/// Add basic road network
///
private void AddBasicRoads()
{
// Horizontal road across the middle
int midY = mapSize / 2;
for (int x = 0; x < mapSize; x++)
{
MapTile tile = mapData.GetTile(x, midY);
if (tile != null && tile.terrainType != TerrainType.Mountain)
{
tile.featureType = FeatureType.Road;
}
}
// Vertical road across the middle
int midX = mapSize / 2;
for (int y = 0; y < mapSize; y++)
{
MapTile tile = mapData.GetTile(midX, y);
if (tile != null && tile.terrainType != TerrainType.Mountain)
{
tile.featureType = FeatureType.Road;
}
}
Debug.Log("π€οΈ Basic roads added");
}
///
/// Add towns and villages to the map
///
private void AddSettlements()
{
// Add a few towns and villages
Vector2Int[] settlementPositions = {
new Vector2Int(25, 25),
new Vector2Int(75, 25),
new Vector2Int(25, 75),
new Vector2Int(75, 75),
new Vector2Int(50, 50), // Center town
new Vector2Int(30, 60),
new Vector2Int(70, 40)
};
for (int i = 0; i < settlementPositions.Length; i++)
{
Vector2Int pos = settlementPositions[i];
// Make sure position is valid
if (pos.x >= 0 && pos.x < mapSize && pos.y >= 0 && pos.y < mapSize)
{
MapTile tile = mapData.GetTile(pos.x, pos.y);
if (tile != null)
{
// First three are towns, rest are villages
FeatureType settlementType = i < 3 ? FeatureType.Town : FeatureType.Village;
tile.featureType = settlementType;
// Give settlements names
if (settlementType == FeatureType.Town)
{
tile.name = $"Town_{i + 1}";
}
else
{
tile.name = $"Village_{i + 1}";
}
Debug.Log($"ποΈ Placed {settlementType} at ({pos.x}, {pos.y})");
}
}
}
}
///
/// Place team marker on a town or village
///
private void PlaceTeamMarkerOnTown()
{
// Find a suitable town or village
Vector2Int? townPosition = FindTownOrVillage();
if (townPosition.HasValue)
{
PlaceTeamMarkerAt(townPosition.Value);
}
else
{
// Fallback to center if no town found
Debug.LogWarning("β οΈ No town/village found, placing team marker at center");
PlaceTeamMarkerAt(new Vector2Int(mapSize / 2, mapSize / 2));
}
}
///
/// Find the first town or village on the map
///
private Vector2Int? FindTownOrVillage()
{
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
MapTile tile = mapData.GetTile(x, y);
if (tile != null && (tile.featureType == FeatureType.Town || tile.featureType == FeatureType.Village))
{
return new Vector2Int(x, y);
}
}
}
return null;
}
///
/// Place team marker at specific map coordinates
///
private void PlaceTeamMarkerAt(Vector2Int mapPos)
{
// Find existing team marker or create one
GameObject teamMarkerObj = GameObject.Find("TeamMarker");
if (teamMarkerObj == null)
{
// Create a simple team marker if none exists
if (teamMarkerPrefab != null)
{
teamMarkerObj = Instantiate(teamMarkerPrefab);
teamMarkerObj.name = "TeamMarkerSimpleMapMaker2";
Debug.Log("π― Created new TeamMarker from prefab");
}
else
{
// Create a simple sphere as team marker
teamMarkerObj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
teamMarkerObj.name = "TeamMarkerSimplMapMaker2";
teamMarkerObj.transform.localScale = new Vector3(2f, 2f, 2f);
// Make it green and easily visible
Renderer renderer = teamMarkerObj.GetComponent();
if (renderer != null)
{
renderer.material.color = Color.green;
}
Debug.Log("π― Created simple sphere TeamMarker");
}
}
if (teamMarkerObj != null)
{
// Convert map coordinates to world position
Vector3 worldPos = new Vector3(mapPos.x, 1f, mapPos.y); // Y=1 to be above ground
teamMarkerObj.transform.position = worldPos;
teamMarker = teamMarkerObj.transform;
Debug.Log($"π Team marker placed at map position ({mapPos.x}, {mapPos.y}) = world position {worldPos}");
// Log what type of settlement we're on
MapTile tile = mapData.GetTile(mapPos.x, mapPos.y);
if (tile != null)
{
Debug.Log($"ποΈ Team marker placed on: Terrain={tile.terrainType}, Feature={tile.featureType}");
if (!string.IsNullOrEmpty(tile.name))
{
Debug.Log($"ποΈ Settlement name: {tile.name}");
}
}
}
else
{
Debug.LogError("β Could not create TeamMarker!");
}
}
///
/// Context menu helper to regenerate map
///
[ContextMenu("Generate New Map")]
public void RegenerateMap()
{
seed = Random.Range(1, 99999);
GenerateSimpleMap();
}
///
/// Context menu helper to disable conflicting systems
///
[ContextMenu("Setup Simple System (Disable Conflicts)")]
public void SetupSimpleSystem()
{
Debug.Log("π§ Setting up simple system by disabling conflicting components...");
// Disable SimpleTeamPlacement if present
SimpleTeamPlacement teamPlacement = FindFirstObjectByType();
if (teamPlacement != null)
{
teamPlacement.enabled = false;
Debug.Log("π« Disabled SimpleTeamPlacement component");
}
// Disable old TravelUI if present
TravelUI oldTravelUI = FindFirstObjectByType();
if (oldTravelUI != null)
{
oldTravelUI.enabled = false;
Debug.Log("π« Disabled old TravelUI component");
}
// Disable TeamTravelSystem if present
TeamTravelSystem travelSystem = FindFirstObjectByType();
if (travelSystem != null)
{
travelSystem.enabled = false;
Debug.Log("π« Disabled TeamTravelSystem component");
}
// Disable ExplorationManager if present (search by component name)
MonoBehaviour[] allComponents = FindObjectsByType(FindObjectsSortMode.None);
foreach (var component in allComponents)
{
if (component.GetType().Name == "ExplorationManager")
{
component.enabled = false;
Debug.Log("π« Disabled ExplorationManager component");
break;
}
}
Debug.Log("β
Simple system setup complete - conflicting systems disabled");
// Regenerate the map to ensure clean state
GenerateSimpleMap();
}
///
/// Context menu helper to place team marker on different town
///
[ContextMenu("Move Team to Random Town")]
public void MoveTeamToRandomTown()
{
// Find all towns and villages
var settlements = new System.Collections.Generic.List();
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
MapTile tile = mapData.GetTile(x, y);
if (tile != null && (tile.featureType == FeatureType.Town || tile.featureType == FeatureType.Village))
{
settlements.Add(new Vector2Int(x, y));
}
}
}
if (settlements.Count > 0)
{
int randomIndex = Random.Range(0, settlements.Count);
PlaceTeamMarkerAt(settlements[randomIndex]);
}
else
{
Debug.LogWarning("β οΈ No settlements found on map");
}
}
///
/// Create simple cube visualization when MapVisualizer is not available
///
private void CreateSimpleCubeVisualization()
{
// Create a parent object for all tiles
GameObject mapContainer = GameObject.Find("MapContainer");
if (mapContainer == null)
{
mapContainer = new GameObject("MapContainer");
}
// Clear existing tiles
for (int i = mapContainer.transform.childCount - 1; i >= 0; i--)
{
DestroyImmediate(mapContainer.transform.GetChild(i).gameObject);
}
// Create simple cubes for visualization
for (int x = 0; x < mapSize; x++)
{
for (int y = 0; y < mapSize; y++)
{
MapTile tile = mapData.GetTile(x, y);
if (tile != null)
{
CreateTileVisualization(x, y, tile, mapContainer.transform);
}
}
}
Debug.Log($"β
Created simple cube visualization with {mapSize * mapSize} tiles");
}
///
/// Create a simple cube to represent a tile
///
private void CreateTileVisualization(int x, int y, MapTile tile, Transform parent)
{
// Create a cube for the tile
GameObject tileObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
tileObj.name = $"Tile_{x}_{y}";
tileObj.transform.parent = parent;
tileObj.transform.position = new Vector3(x, 0, y);
tileObj.transform.localScale = new Vector3(0.9f, 0.1f, 0.9f); // Flat tiles
// Get the renderer to change colors
Renderer renderer = tileObj.GetComponent();
if (renderer != null)
{
// Color based on terrain type
Color terrainColor = GetTerrainColor(tile.terrainType);
renderer.material.color = terrainColor;
}
// Add feature visualization on top if needed
if (tile.featureType != FeatureType.None)
{
CreateFeatureVisualization(x, y, tile, tileObj.transform);
}
}
///
/// Create feature visualization (towns, villages, roads)
///
private void CreateFeatureVisualization(int x, int y, MapTile tile, Transform tileParent)
{
GameObject featureObj = null;
switch (tile.featureType)
{
case FeatureType.Town:
featureObj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
featureObj.name = $"Town_{x}_{y}";
featureObj.transform.localScale = new Vector3(0.6f, 0.5f, 0.6f);
featureObj.GetComponent().material.color = Color.blue;
break;
case FeatureType.Village:
featureObj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
featureObj.name = $"Village_{x}_{y}";
featureObj.transform.localScale = new Vector3(0.4f, 0.3f, 0.4f);
featureObj.GetComponent().material.color = Color.cyan;
break;
case FeatureType.Road:
featureObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
featureObj.name = $"Road_{x}_{y}";
featureObj.transform.localScale = new Vector3(0.8f, 0.05f, 0.8f);
featureObj.GetComponent().material.color = Color.yellow;
break;
}
if (featureObj != null)
{
featureObj.transform.parent = tileParent;
featureObj.transform.localPosition = new Vector3(0, 0.6f, 0); // On top of tile
}
}
///
/// Get color for terrain type
///
private Color GetTerrainColor(TerrainType terrainType)
{
return terrainType switch
{
TerrainType.Plains => Color.green,
TerrainType.Forest => new Color(0f, 0.5f, 0f), // Dark green
TerrainType.Mountain => Color.gray,
TerrainType.Ocean => Color.blue,
TerrainType.Lake => new Color(0f, 0.5f, 1f), // Light blue
TerrainType.River => Color.cyan,
_ => Color.white
};
}
///
/// Get the current map data
///
public MapData GetMapData()
{
return mapData;
}
///
/// Get the team marker transform
///
public Transform GetTeamMarker()
{
return teamMarker;
}
}