/// <summary> /// loads scenario from json file and returns whether successful /// </summary> private bool scnOpen(string path, int user, bool multiplayer) { Hashtable json; ArrayList jsonA; bool b = false; // ISSUE #17: in multiplayer games, host should load file & send data to other players, otherwise json double parsing may not match if (!File.Exists(path)) return false; json = (Hashtable)Procurios.Public.JSON.JsonDecode(File.ReadAllText(path), ref b); if (!b) return false; // base scenario g = new Sim { // not stored in scenario file selUser = user, networkView = multiplayer ? networkView : null, events = new List<SimEvt>(), cmdPending = new List<SimEvt>(), checksum = 0, synced = true, timeSim = 0, timeUpdateEvt = long.MinValue, maxSpeed = 0, deleteLines = new List<MoveLine>(), keepLines = new List<MoveLine>(), alternatePaths = new List<Path>(), // stored in scenario file mapSize = jsonFP(json, "mapSize"), updateInterval = (long)jsonDouble(json, "updateInterval"), tileInterval = (long)jsonDouble (json, "tileInterval"), visRadius = jsonFP(json, "visRadius"), camSpeed = jsonFP(json, "camSpeed"), zoom = (float)jsonDouble(json, "zoom"), zoomMin = (float)jsonDouble(json, "zoomMin"), zoomMax = (float)jsonDouble(json, "zoomMax"), zoomSpeed = (float)jsonDouble (json, "zoomSpeed"), zoomMouseWheelSpeed = (float)jsonDouble (json, "zoomMouseWheelSpeed"), uiBarHeight = (float)jsonDouble (json, "uiBarHeight"), healthBarSize = jsonVector2(json, "healthBarSize"), healthBarYOffset = (float)jsonDouble(json, "healthBarYOffset"), stackRadius = jsonFP(json, "stackRadius"), stackRotSpeed = (float)jsonDouble(json, "stackRotSpeed"), backCol = jsonColor(json, "backCol"), borderCol = jsonColor(json, "borderCol"), noVisCol = jsonColor(json, "noVisCol"), playerVisCol = jsonColor(json, "playerVisCol"), unitVisCol = jsonColor(json, "unitVisCol"), exclusiveCol = jsonColor(json, "exclusiveCol"), waypointCol = jsonColor (json, "waypointCol"), pathCol = jsonColor(json, "pathCol"), healthBarBackCol = jsonColor(json, "healthBarBackCol"), healthBarFullCol = jsonColor(json, "healthBarFullCol"), healthBarEmptyCol = jsonColor(json, "healthBarEmptyCol"), rscNames = new string[0], players = new Player[0], unitT = new UnitType[0], units = new List<Unit>(), paths = new List<Path>(), }; if (g.updateInterval > 0) g.events.addEvt(new UpdateEvt(0)); if (g.tileInterval > 0) g.events.addEvt (new TileUpdateEvt(0)); g.camPos = jsonFPVector(json, "camPos", new FP.Vector(g.mapSize / 2, g.mapSize / 2)); // resources jsonA = jsonArray(json, "resources"); if (jsonA != null) { foreach (string rscName in jsonA) { Array.Resize(ref g.rscNames, g.rscNames.Length + 1); g.rscNames[g.rscNames.Length - 1] = rscName; } } // players jsonA = jsonArray(json, "players"); if (jsonA != null) { foreach (Hashtable jsonO in jsonA) { Hashtable jsonO2 = jsonObject(jsonO, "startRsc"); Player player = new Player { g = this.g, id = g.players.Length, name = jsonString(jsonO, "name"), isUser = jsonBool(jsonO, "isUser"), user = (int)jsonDouble(jsonO, "user"), populationLimit = (int)jsonDouble (jsonO, "populationLimit", -1), startRsc = new long[g.rscNames.Length], mapHack = jsonBool (jsonO, "mapHack"), hasNonLivePaths = false, timeGoLiveFailedAttempt = long.MinValue, goLiveStackPaths = new Dictionary<int, HashSet<Path>>(), unseenTiles = g.tileLen () * g.tileLen (), }; for (int i = 0; i < g.rscNames.Length; i++) { player.startRsc[i] = (jsonO2 != null) ? jsonFP(jsonO2, g.rscNames[i]) : 0; } Array.Resize(ref g.players, g.players.Length + 1); g.players[g.players.Length - 1] = player; } foreach (Hashtable jsonO in jsonA) { ArrayList jsonA2 = jsonArray(jsonO, "canAttack"); Player player = g.playerNamed(jsonString(jsonO, "name")); player.canAttack = new bool[g.players.Length]; for (int i = 0; i < g.players.Length; i++) { player.canAttack[i] = false; } if (jsonA2 != null) { foreach (string s in jsonA2) { if (g.playerNamed(s) != null) { player.canAttack[g.playerNamed(s).id] = true; } } } } foreach (Player player in g.players) { player.immutable = player.calcImmutable(); } } // unit types jsonA = jsonArray(json, "unitTypes"); if (jsonA != null) { foreach (Hashtable jsonO in jsonA) { Hashtable jsonO2 = jsonObject(jsonO, "rscCost"); Hashtable jsonO3 = jsonObject(jsonO, "rscCollectRate"); UnitType unitT = new UnitType { id = g.unitT.Length, name = jsonString(jsonO, "name"), imgPath = jsonString(jsonO, "imgPath"), imgOffset = jsonFPVector (jsonO, "imgOffset"), imgHalfHeight = jsonFP (jsonO, "imgHalfHeight"), maxHealth = (int)jsonDouble(jsonO, "maxHealth"), speed = jsonFP(jsonO, "speed"), reload = (long)jsonDouble(jsonO, "reload"), range = jsonFP(jsonO, "range"), tightFormationSpacing = jsonFP(jsonO, "tightFormationSpacing"), seePrecedence = (int)jsonDouble (jsonO, "seePrecedence"), makeUnitMinDist = jsonFP(jsonO, "makeUnitMinDist"), makeUnitMaxDist = jsonFP(jsonO, "makeUnitMaxDist"), makePathMinDist = jsonFP(jsonO, "makePathMinDist"), makePathMaxDist = jsonFP(jsonO, "makePathMaxDist"), rscCost = new long[g.rscNames.Length], rscCollectRate = new long[g.rscNames.Length], }; unitT.selMinPos = jsonFPVector (jsonO, "selMinPos", new FP.Vector(unitT.imgOffset.x - unitT.imgHalfHeight, unitT.imgOffset.y - unitT.imgHalfHeight)); unitT.selMaxPos = jsonFPVector (jsonO, "selMaxPos", new FP.Vector(unitT.imgOffset.x + unitT.imgHalfHeight, unitT.imgOffset.y + unitT.imgHalfHeight)); unitT.laserPos = jsonFPVector (jsonO, "laserPos", unitT.imgOffset); if (unitT.speed > g.maxSpeed) g.maxSpeed = unitT.speed; for (int i = 0; i < g.rscNames.Length; i++) { unitT.rscCost[i] = (jsonO2 != null) ? jsonFP(jsonO2, g.rscNames[i]) : 0; unitT.rscCollectRate[i] = (jsonO3 != null) ? jsonFP(jsonO3, g.rscNames[i]) : 0; } Array.Resize(ref g.unitT, g.unitT.Length + 1); g.unitT[g.unitT.Length - 1] = unitT; } foreach (Hashtable jsonO in jsonA) { Hashtable jsonO2 = jsonObject(jsonO, "damage"); ArrayList jsonA2 = jsonArray(jsonO, "canMake"); UnitType unitT = g.unitTypeNamed(jsonString(jsonO, "name")); unitT.makeOnUnitT = g.unitTypeNamed(jsonString(jsonO, "makeOnUnitT")); unitT.damage = new int[g.unitT.Length]; for (int i = 0; i < g.unitT.Length; i++) { unitT.damage[i] = (jsonO2 != null) ? (int)jsonDouble(jsonO2, g.unitT[i].name) : 0; } unitT.canMake = new bool[g.unitT.Length]; for (int i = 0; i < g.unitT.Length; i++) { unitT.canMake[i] = false; } if (jsonA2 != null) { foreach (string s in jsonA2) { if (g.unitTypeNamed(s) != null) { unitT.canMake[g.unitTypeNamed(s).id] = true; } } } } } // tiles g.tiles = new Tile[g.tileLen(), g.tileLen()]; for (int i = 0; i < g.tileLen(); i++) { for (int j = 0; j < g.tileLen(); j++) { g.tiles[i, j] = new Tile(g, i, j); } } foreach (Player player in g.players) { if (player.mapHack) { player.mapHack = false; new MapHackCmdEvt(g.timeSim, player.id, true).apply (g); } } // units jsonA = jsonArray(json, "units"); if (jsonA != null) { foreach (Hashtable jsonO in jsonA) { if (g.playerNamed(jsonString(jsonO, "player")) != null) { ArrayList jsonA2 = jsonArray (jsonO, "types"); List<Unit> units = new List<Unit>(); if (jsonA2 != null) { foreach (string type in jsonA2) { if (g.unitTypeNamed(type) != null) { Unit unit = new Unit(g, g.units.Count, g.unitTypeNamed(type), g.playerNamed(jsonString(jsonO, "player"))); g.units.Add (unit); units.Add (unit); } } } FP.Vector startPos = jsonFPVector(jsonO, "startPos", new FP.Vector((long)(UnityEngine.Random.value * g.mapSize), (long)(UnityEngine.Random.value * g.mapSize))); g.paths.Add (new Path(g, g.paths.Count, units, (long)jsonDouble(jsonO, "startTime"), startPos, false, int.MaxValue, g.tileAt(startPos).x, g.tileAt(startPos).y)); } } } g.nRootPaths = g.paths.Count; // start game loadUI (); g.timeGame = 0; timeSpeedChg = (long)(Time.time * 1000) - 1000; timeNow = (long)(Time.time * 1000); return true; }