private void ParseRuleMap() { string line = GetNextLine(); string[] lineSplit = SplitLine(line); if (lineSplit.Length == 1) { ThrowInvalidLine("ParseRuleMap()"); } SimRuleMap ruleMap = new SimRuleMap(); ruleMap.id = lineSplit[1]; line = GetNextLine(); List <SimRuleCommand> commands = new List <SimRuleCommand>(); while (line != "end") { lineSplit = SplitLine(line); switch (lineSplit[0]) { case "rate": ruleMap.rate = ParseInt(lineSplit[1]); break; case "randomTiles": ruleMap.randomTiles = ParseBool(lineSplit[1]); break; case "randomTilesPercent": ruleMap.randomTilesPercent = ParseInt(lineSplit[1]); break; default: SimRuleCommand command = ParseCommand(line); if (command == null) { ThrowInvalidLine("ParseRuleMap()"); } commands.Add(command); break; } line = GetNextLine(); } ruleMap.commands = commands.ToArray(); this.rules.Add(ruleMap.id, ruleMap); }
public void ExecuteRules() { ticks++; SimRuleMap[] rules = mapType.rules; for (int i = 0; i < rules.Length; i++) { SimRuleMap rule = rules[i]; if (ticks % rule.rate == 0) { if (rule.randomTiles) { int tilesAmount = (rule.randomTilesPercent * sizeX * sizeY) / 100; randomCoordinates.Init(sizeX, sizeY); for (int j = 0; j < tilesAmount; j++) { if (randomCoordinates.GetNextCoordinate(out context.mapPositionX, out context.mapPositionY)) { rule.Execute(context); } } } else { for (int x = 0; x < sizeX; x++) { context.mapPositionX = x; for (int y = 0; y < sizeY; y++) { context.mapPositionY = y; rule.Execute(context); } } } } } }
private SimulationDefinition CreateDefinition() { SimulationDefinition definition = new SimulationDefinition(); SimResource resourceWater = new SimResource(); resourceWater.id = "Water"; SimResource resourceGrass = new SimResource(); resourceGrass.id = "Grass"; definition.resourceTypes.Add(resourceWater.id, resourceWater); definition.resourceTypes.Add(resourceGrass.id, resourceGrass); //Add 2 of water every 10 ticks SimRuleValueMap valueMapWater = new SimRuleValueMap(); valueMapWater.mapId = "Water"; SimRuleCommandAdd commandAddWater = new SimRuleCommandAdd(); commandAddWater.amount = 2; commandAddWater.target = valueMapWater; SimRuleMap ruleMapAddWater = new SimRuleMap(); ruleMapAddWater.id = "AddWater"; ruleMapAddWater.rate = 10; ruleMapAddWater.randomTiles = true; ruleMapAddWater.randomTilesPercent = 25; ruleMapAddWater.commands = new SimRuleCommand[] { commandAddWater }; //Water Map SimMapType mapTypeWater = new SimMapType(); mapTypeWater.id = "Water"; mapTypeWater.color = 0x0000FF; mapTypeWater.capacity = 100; mapTypeWater.rules = new SimRuleMap[] { ruleMapAddWater }; definition.mapTypes.Add(mapTypeWater.id, mapTypeWater); //Add grass every 7 ticks if there is 10 of water available SimRuleValueMap valueMapGrass = new SimRuleValueMap(); valueMapGrass.mapId = "Grass"; SimRuleCommandRemove commandRemoveWater = new SimRuleCommandRemove(); commandRemoveWater.amount = 10; commandRemoveWater.target = valueMapWater; SimRuleCommandAdd commandAddGrass = new SimRuleCommandAdd(); commandAddGrass.amount = 1; commandAddGrass.target = valueMapGrass; SimRuleMap ruleMapCreateGrass = new SimRuleMap(); ruleMapCreateGrass.id = "CreateGrass"; ruleMapCreateGrass.rate = 7; ruleMapCreateGrass.commands = new SimRuleCommand[] { commandRemoveWater, commandAddGrass }; //Grass Map SimMapType mapTypeGrass = new SimMapType(); mapTypeGrass.id = "Grass"; mapTypeGrass.color = 0x00FF00; mapTypeGrass.capacity = 10; mapTypeGrass.rules = new SimRuleMap[] { ruleMapCreateGrass }; definition.mapTypes.Add(mapTypeGrass.id, mapTypeGrass); return(definition); }