public void AddLinkedProvince(ProvinceMarker m) { if (m_linked == null) { m_linked = new List <ProvinceMarker>(); } if (m_linked.Contains(m)) { return; } m_linked.Add(m); SetNode(m.Node); }
static float mountain_count(ProvinceMarker m) { float res = 0f; foreach (Connection c in m.Node.Connections) { if (c.ConnectionType == ConnectionType.MOUNTAIN) { res += 1f; } else if (c.ConnectionType == ConnectionType.MOUNTAINPASS) { res += 0.5f; } } return res; }
private static float mountain_count(ProvinceMarker m) { var res = 0f; foreach (var c in m.Node.Connections) { if (c.ConnectionType == ConnectionType.MOUNTAIN) { res += 1f; } else if (c.ConnectionType == ConnectionType.MOUNTAINPASS) { res += 0.5f; } } return(res); }
void MakeProvinceIdTexture(ProvinceMarker pm, Vector3 offset, Transform t) { Bounds cam_bounds = CaptureCam.Bounds(offset); var pn = pm.ProvinceNumber; Color c = new Color32(0, (byte)(pn / 256), (byte)(pn % 256), 255); Debug.Assert(pn != 0); { var province_mesh = Instantiate(province_id_mesh_prefab); province_mesh.GetComponent <MeshFilter>().mesh = pm.MeshFilter.sharedMesh; var new_mat = Instantiate(province_id_mesh_prefab.sharedMaterial); new_mat.color = c; province_mesh.GetComponent <MeshRenderer>().material = new_mat; province_mesh.transform.position = offset; var bounds = province_mesh.bounds; province_mesh.transform.SetParent(t); BorderOverlap.Duplicate(province_mesh.gameObject, province_mesh.bounds, cam_bounds); } }
public void SetProvince(ProvinceMarker p) { ConnectionManager.s_connection_manager.Deselect(); if (!gameObject.activeSelf) { gameObject.SetActive(true); } if (m_current != null) { m_current.SetSelected(false); } m_current = p; clear_checkboxes(); update_checkboxes(p.Node.ProvinceData.Terrain); GetComponent <AudioSource>().PlayOneShot(Audio); }
List <GameObject> create_connection_wraps(ProvinceMarker pm, Vector3 offset) { List <GameObject> objs = new List <GameObject>(); if (IsDummy) { return(objs); } foreach (ConnectionMarker m in pm.Connections) { GameObject obj = m.CreateWrapMesh(m.PolyBorder, offset); if (obj != null) { objs.Add(obj); } } return(objs); }
private string generate_custom_name(List <ProvinceMarker> all, ProvinceMarker marker) { var format = m_name_formats.GetRandom(marker.Node.ProvinceData.Terrain); var name = string.Empty; foreach (var id in format.Strings) { var str = m_name_data.GetRandomString(id, marker.Node.ProvinceData.Terrain, marker.Node.ProvinceData.IsPlains); if (str == null) { Debug.LogError($"Unable to find custom name data with id: {id}, terrain: {marker.Node.ProvinceData.Terrain}"); } else { if (id == "SPACE") { name += " "; } else { name += str; } } } name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(name); name = name.Replace(" Of ", " of "); if (all.Any(x => x.Node.ProvinceData.CustomName != string.Empty && (x.Node.ProvinceData.CustomName.Contains(name) || name.Contains(x.Node.ProvinceData.CustomName)))) { return(string.Empty); } return(name); }
public void SetConnectionType(int ct) { if (m_current == null) { return; } var c = (ConnectionType)ct; var cur = m_current.Connection.ConnectionType; if (c == cur) { return; } m_current.UpdateConnection(c); if (m_current.LinkedConnection != null) { m_current.LinkedConnection.UpdateConnection(c); } var provs = new List <ProvinceMarker>(); var conns = new List <ConnectionMarker>(); ProvinceMarker pm = null; if (m_current.Prov1.IsDummy) { pm = m_current.Prov2; } else { pm = m_current.Prov1; } provs.Add(pm); foreach (var m in pm.ConnectedProvinces) { if (m.IsDummy) { provs.AddRange(m.LinkedProvinces); } else { provs.Add(m); } } conns.AddRange(pm.Connections); foreach (var p in provs) { foreach (var m in p.Connections) { if (provs.Contains(m.Prov1) && provs.Contains(m.Prov2) && !conns.Contains(m)) { conns.Add(m); } } } GetComponent <AudioSource>().PlayOneShot(AudioApply); GenerationManager.s_generation_manager.RegenerateElements(provs, conns, m_layout); }
public void SetParent(ProvinceMarker m) { m_parent = m; }
public void SetDummy(bool b, ProvinceMarker owner) { m_is_dummy = b; m_dummy_offset = (transform.position - owner.transform.position); }
public IEnumerator GenerateElements(List <Node> nodes, List <Connection> conns, NodeLayout layout) { if (m_generated != null) { foreach (GameObject o in m_generated) { GameObject.Destroy(o); } m_generated = new List <GameObject>(); } m_generated = new List <GameObject>(); m_provinces = new List <ProvinceMarker>(); m_connections = new List <ConnectionMarker>(); Vector3 min = Vector3.zero - new Vector3(m_size_x, m_size_y); Vector3 max = new Vector3(m_size_x * (layout.X - 1), m_size_y * (layout.Y - 1)); Vector3 min_top = new Vector3(min.x, max.y); Vector3 max_bot = new Vector3(max.x, min.y); float dist_up = Vector3.Distance(min, min_top); float dist_horz = Vector3.Distance(min, max_bot); MapBorder.SetBorders(min, max); foreach (Node n in nodes) // create basic provinces { Vector3 pos = new Vector3(n.X * m_size_x, n.Y * m_size_y, 0); Vector3 randpos = pos - new Vector3(m_edge_tolerance * m_size_x + (UnityEngine.Random.Range(0f, m_size_x - (m_edge_tolerance * 2 * m_size_x))), m_edge_tolerance * m_size_y + (UnityEngine.Random.Range(0f, m_size_y - (m_edge_tolerance * 2 * m_size_y)))); GameObject g = GameObject.Instantiate(ProvinceMarker); g.transform.position = randpos; ProvinceMarker m = g.GetComponent <ProvinceMarker>(); GameObject w = GameObject.Instantiate(ProvinceWidget); //w.transform.position = randpos + new Vector3(500f, 0f, 0f); ProvinceWidget widget = w.GetComponent <ProvinceWidget>(); widget.SetParent(m); m.SetWidget(widget); m.SetNode(n); m_provinces.Add(m); m_generated.Add(g); m_generated.Add(w); if (Util.ShouldYield()) { yield return(null); } } adjust_province_positions(); foreach (ProvinceMarker m in m_provinces) { m.OffsetWidget(); // properly position widget if (Util.ShouldYield()) { yield return(null); } } List <ProvinceMarker> dummies = new List <ProvinceMarker>(); foreach (ProvinceMarker m in m_provinces) // create dummy provinces { if (m.Node.X == 0 && m.Node.Y == 0) { Vector3 pos_up = new Vector3(m.transform.position.x, m.transform.position.y + dist_up); Vector3 pos_right = new Vector3(m.transform.position.x + dist_horz, m.transform.position.y); Vector3 pos_diag = new Vector3(m.transform.position.x + dist_horz, m.transform.position.y + dist_up); GameObject g1 = GameObject.Instantiate(ProvinceMarker); g1.transform.position = pos_up; ProvinceMarker m1 = g1.GetComponent <ProvinceMarker>(); m1.AddLinkedProvince(m); m.AddLinkedProvince(m1); m1.SetDummy(true, m); GameObject g2 = GameObject.Instantiate(ProvinceMarker); g2.transform.position = pos_right; ProvinceMarker m2 = g2.GetComponent <ProvinceMarker>(); m2.AddLinkedProvince(m); m.AddLinkedProvince(m2); m2.SetDummy(true, m); GameObject g3 = GameObject.Instantiate(ProvinceMarker); g3.transform.position = pos_diag; ProvinceMarker m3 = g3.GetComponent <ProvinceMarker>(); m3.AddLinkedProvince(m); m.AddLinkedProvince(m3); m3.SetDummy(true, m); dummies.Add(m1); dummies.Add(m2); dummies.Add(m3); m_generated.Add(g1); m_generated.Add(g2); m_generated.Add(g3); } else if (m.Node.X == 0) { Vector3 pos_right = new Vector3(m.transform.position.x + dist_horz, m.transform.position.y); GameObject g1 = GameObject.Instantiate(ProvinceMarker); g1.transform.position = pos_right; ProvinceMarker m1 = g1.GetComponent <ProvinceMarker>(); m1.AddLinkedProvince(m); m.AddLinkedProvince(m1); m1.SetDummy(true, m); dummies.Add(m1); m_generated.Add(g1); } else if (m.Node.Y == 0) { Vector3 pos_up = new Vector3(m.transform.position.x, m.transform.position.y + dist_up); GameObject g1 = GameObject.Instantiate(ProvinceMarker); g1.transform.position = pos_up; ProvinceMarker m1 = g1.GetComponent <ProvinceMarker>(); m1.AddLinkedProvince(m); m.AddLinkedProvince(m1); m1.SetDummy(true, m); dummies.Add(m1); m_generated.Add(g1); } if (Util.ShouldYield()) { yield return(null); } } m_provinces.AddRange(dummies); Dictionary <Node, ProvinceMarker> real_province = new Dictionary <Node, ProvinceMarker>(); Dictionary <Node, List <ProvinceMarker> > dummy_province = new Dictionary <Node, List <ProvinceMarker> >(); foreach (var pm in m_provinces) { if (!dummy_province.ContainsKey(pm.Node)) { dummy_province[pm.Node] = new List <ProvinceMarker> { } } ; if (pm.IsDummy) { dummy_province[pm.Node].Add(pm); } else { real_province[pm.Node] = pm; } } foreach (Connection c in conns) // create connection markers { ProvinceMarker prov1 = real_province[c.Node1]; ProvinceMarker prov2 = real_province[c.Node2]; List <ProvinceMarker> pd1 = dummy_province[c.Node1]; List <ProvinceMarker> pd2 = dummy_province[c.Node2]; if (pd1.Any() || pd2.Any()) // edge case { if (pd1.Any() && pd2.Any()) // both have an edge clone { if (pd1.Any(x => x.Node.X == 0 && x.Node.Y == 0)) { pd1.Add(prov1); pd1 = pd1.OrderBy(x => Vector3.Distance(x.transform.position, prov2.transform.position)).ToList(); prov1 = pd1[0]; } else if (pd2.Any(x => x.Node.X == 0 && x.Node.Y == 0)) { pd2.Add(prov2); pd2 = pd2.OrderBy(x => Vector3.Distance(x.transform.position, prov1.transform.position)).ToList(); prov2 = pd2[0]; } else if (pd1.Any(x => x.Node.X == layout.X - 1 || x.Node.Y == layout.Y - 1)) { pd2.Add(prov2); pd2 = pd2.OrderBy(x => Vector3.Distance(x.transform.position, prov1.transform.position)).ToList(); prov2 = pd2[0]; } else if (pd2.Any(x => x.Node.X == layout.X - 1 || x.Node.Y == layout.Y - 1)) { pd1.Add(prov1); pd1 = pd1.OrderBy(x => Vector3.Distance(x.transform.position, prov2.transform.position)).ToList(); prov1 = pd1[0]; } } else if (pd1.Any()) { pd1.Add(prov1); pd1 = pd1.OrderBy(x => Vector3.Distance(x.transform.position, prov2.transform.position)).ToList(); prov1 = pd1[0]; } else { pd2.Add(prov2); pd2 = pd2.OrderBy(x => Vector3.Distance(x.transform.position, prov1.transform.position)).ToList(); prov2 = pd2[0]; } GameObject g = GameObject.Instantiate(ConnectionMarker); ConnectionMarker m = g.GetComponent <ConnectionMarker>(); Vector3 p1 = prov1.transform.position; Vector3 p2 = prov2.transform.position; prov1.AddConnection(m); prov2.AddConnection(m); Vector3 center = get_weighted_center(p1, p2, prov1.Node, prov2.Node); g.transform.position = center; GameObject w = GameObject.Instantiate(ConnectionWidget); w.transform.position = center + new Vector3(500f, 0f, 0f); m.SetEdgeConnection(true); m.SetProvinces(prov1, prov2); m.SetEndPoints(p1, p2); ConnectionWidget widget = w.GetComponent <ConnectionWidget>(); widget.SetParent(m); m.SetWidget(widget); m.SetConnection(c); m_connections.Add(m); m_generated.Add(g); m_generated.Add(w); } else // base case { GameObject g = GameObject.Instantiate(ConnectionMarker); ConnectionMarker m = g.GetComponent <ConnectionMarker>(); Vector3 p1 = prov1.transform.position; Vector3 p2 = prov2.transform.position; prov1.AddConnection(m); prov2.AddConnection(m); Vector3 center = get_weighted_center(p1, p2, prov1.Node, prov2.Node); g.transform.position = center; GameObject w = GameObject.Instantiate(ConnectionWidget); w.transform.position = center + new Vector3(500f, 0f, 0f); m.SetProvinces(prov1, prov2); m.SetEndPoints(p1, p2); ConnectionWidget widget = w.GetComponent <ConnectionWidget>(); widget.SetParent(m); m.SetWidget(widget); m.SetConnection(c); m_connections.Add(m); m_generated.Add(g); m_generated.Add(w); } if (Util.ShouldYield()) { yield return(null); } } number_provinces(); yield return(StartCoroutine(ArtManager.s_art_manager.GenerateElements(m_provinces, m_connections, layout, layout.X * m_size_x, layout.Y * m_size_y))); } void adjust_province_positions() // we need to ensure that no province centers are on the same horizontal line { int ypos = 0; var row = m_provinces.Where(x => x.Node.Y == ypos && !x.IsDummy).ToList(); while (row.Any()) { foreach (ProvinceMarker m in row) { Vector3 pos = m.transform.position; float add = 0.02f; if (UnityEngine.Random.Range(0, 2) == 0) { add = -0.02f; } while (row.Any(x => x != m && Mathf.Abs(x.transform.position.y - pos.y) < 0.02f)) { pos.y += add; } m.transform.position = pos; } ypos++; row = m_provinces.Where(x => x.Node.Y == ypos).ToList(); } } void number_provinces() // assign province numbers based on X and Y position, this is how dominions does it { var valid = m_provinces.Where(x => !x.IsDummy).OrderBy(x => x.gameObject.transform.position.y).ThenBy(x => x.gameObject.transform.position.x); int num = 1; foreach (ProvinceMarker pm in valid) { pm.Node.SetID(num); num++; } } Vector3 get_mirrored_pos(Vector3 min, Vector3 max, Vector3 vec) { Vector3 diff = vec - min; if (Mathf.Abs(vec.x - min.x) < 0.01f) { vec.x = max.x; } else if (Mathf.Abs(vec.x - max.x) < 0.01f) { vec.x = min.x; } if (Mathf.Abs(vec.y - min.y) < 0.01f) { vec.y = max.y; } else if (Mathf.Abs(vec.y - max.y) < 0.01f) { vec.y = min.y; } return(vec); } Vector3 get_weighted_center(Vector3 p1, Vector3 p2, Node n1, Node n2) { Vector3 dir1 = (p1 - p2).normalized; Vector3 dir2 = (p2 - p1).normalized; Vector3 center = (p1 + p2) / 2; float dist = Vector3.Distance(center, p1); if (n1.ProvinceData.Terrain.IsFlagSet(Terrain.LARGEPROV) || n1.Connections.Count == 4) { center += (dir2 * (dist * 0.16f)); } if (n2.ProvinceData.Terrain.IsFlagSet(Terrain.LARGEPROV) || n2.Connections.Count == 4) { center += (dir1 * (dist * 0.16f)); } if (n1.ProvinceData.Terrain.IsFlagSet(Terrain.SMALLPROV)) { center += (dir1 * (dist * 0.20f)); } if (n2.ProvinceData.Terrain.IsFlagSet(Terrain.SMALLPROV)) { center += (dir2 * (dist * 0.20f)); } return(center); } }
public void SetProvinces(ProvinceMarker m1, ProvinceMarker m2) { m_p1 = m1; m_p2 = m2; }
public bool TouchingProvince(ProvinceMarker pm) { return(Vector3.Distance(pm.transform.position, Endpoint1) < 0.01f || Vector3.Distance(pm.transform.position, Endpoint2) < 0.01f); }