Example #1
0
    public void GenerateCurrentPositionsList()
    {
        List <SwitchMove> switchList = new List <SwitchMove>();

        foreach (var station in layout.layoutState.StationStates)
        {
            foreach (var car in station.CarsPresent)
            {
                SwitchMove move      = new SwitchMove();
                var        movingCar = new MovingCar();
                move.CarNumber       = car;
                move.CurrentLocation = station.Name;
                move.Cargo           = "―――";
                move.TargetLocation  = "―――";
                switchList.Add(move);
            }
        }
        GenerateSwitchListFile(switchList);
    }
Example #2
0
        /*
         * Metod prepares model and players for the next move 
         * and notifies players that move has been switched.
         * If next move is impossible, method notifies players 
         * about the end of the game.
         */
        private void FinishTurnAndMoveOn()
        {
            currentAllowedCells = board.GetAllowedCells(turnHolder.CurrentTurnColor);

            if (currentAllowedCells.Count == 0)
            {
                Pass(turnHolder.CurrentTurnColor);
                return;
            }

            playerHasPassed[Color.White] = false;
            playerHasPassed[Color.Black] = false;

            SwitchMove?.Invoke(
                this, 
                new SwitchMoveEventArgs 
                    { 
                        AllowedCells = currentAllowedCells, 
                        CurrentPlayerColor = turnHolder.CurrentTurnColor 
                    }
                );
        }
Example #3
0
    public void GenerateNewSwitchlist()
    {
        print("STARTING NEW DAY");
        List <SwitchMove> switchList = new List <SwitchMove>();

        Random.InitState(layout.layoutState.Seed);

        //Get all the cars on the layout
        var allCars = new List <MovingCar>();

        foreach (var station in layout.layoutState.StationStates)
        {
            foreach (var car in station.CarsPresent)
            {
                var movingCar = new MovingCar {
                    CarName = car, PreviousStation = station.Name
                };
                allCars.Add(movingCar);
            }
        }

        //Work out which cars are actually moving today
        List <MovingCar> stayingCars = new List <MovingCar>();

        allCars = GetMovingCars(allCars, out stayingCars);
        //print("Cars moving: " + allCars);
        //Clear moving cars from their current stations (so they're not counted as present)
        foreach (var item in allCars)
        {
            layout.layoutState.StationStates.FirstOrDefault(b => b.Name == item.PreviousStation).CarsPresent.Remove(item.CarName);
        }

        //Send the cars to their new destination
        foreach (var car in allCars)
        {
            print(car.CarName + " is starting move logic.");
            //Create the switchmove object
            SwitchMove move = new SwitchMove {
                CurrentLocation = car.PreviousStation, CarNumber = car.CarName
            };

            var stationRef = layout.layout.Stations.FirstOrDefault(b => b.Name == car.PreviousStation);

            //Jumble the list of exported cargo
            System.Random rnd = new System.Random();
            string[]      randomExportCargo = stationRef.CargoExport.OrderBy(x => rnd.Next()).ToArray();
            string        cargo             = "";
            string        targetStation     = "";
            foreach (var rndCargo in randomExportCargo)
            {
                print(car.CarName + " is testing export for " + rndCargo);
                //Check if the car takes this cargo
                if (GetValidCarTypeCargo(layout.layout.Cars.FirstOrDefault(b => b.Number == car.CarName).Type).Contains(rndCargo))
                {
                    //Find a random station that will recieve this cargo
                    targetStation = GetValidStation(rndCargo, true);

                    //if there's no recieving stations, skip to the next cargo
                    if (targetStation == "")
                    {
                        continue;
                    }

                    //Otherwise, we're good to go, so let's proceed with that cargo and station
                    cargo      = rndCargo;
                    move.Cargo = rndCargo;
                    print(car.CarName + " selected " + rndCargo);
                    break;
                }
            }



            //If we've failed to find a station that will take valid cargo, find some other station that exports valid cargo for a logistics move
            if (cargo == "" || targetStation == "")
            {
                print(car.CarName + " has no valid export. Starting logistics move search.");
                //Randomly sort the cargo this car carries
                string[] validCargo = GetValidCarTypeCargo(layout.layout.Cars.FirstOrDefault(b => b.Number == car.CarName).Type).OrderBy(x => rnd.Next()).ToArray();

                //Check the cargo for free stations
                foreach (var item in validCargo)
                {
                    print(car.CarName + " is testing logistics move for " + item);
                    targetStation = GetValidStation(item, false);

                    if (targetStation != "" && targetStation != car.PreviousStation)
                    {
                        print(car.CarName + " found logistics move to   " + targetStation);
                        move.Cargo = "―――";
                        break;
                    }
                    else
                    {
                        print(car.CarName + " found no valid station for logistics move that exports  " + item);
                    }
                }
            }

            move.TargetLocation = targetStation;
            if (targetStation == "")
            {
                Debug.LogError(car.CarName + " totally failed to find a move and is staying put.");
                MoveCar(car.CarName, car.PreviousStation);
                move.Cargo          = "―――";
                move.TargetLocation = "―――";
                switchList.Add(move);
                continue;
            }

            MoveCar(car.CarName, targetStation);
            Debug.LogWarning($"{move.CarNumber} FROM {move.CurrentLocation} TO {move.TargetLocation} CARGO {move.Cargo}");
            switchList.Add(move);
        }
        foreach (var car in stayingCars)
        {
            SwitchMove move = new SwitchMove
            {
                CurrentLocation = car.PreviousStation,
                CarNumber       = car.CarName,
                Cargo           = "―――",
                TargetLocation  = "―――"
            };
            switchList.Add(move);
        }

        GenerateSwitchListFile(switchList);
        layout.layoutState.Seed++;
    }