public void initializeCannon(Village v, GameObject cannonPrefab)
    {
        Tile tileAt = v.getLocatedAt ();
        GameObject newCannon = Network.Instantiate(cannonPrefab, new Vector3(tileAt.point.x, 0.15f, tileAt.point.y), tileAt.transform.rotation, 0) as GameObject;
        Unit u = newCannon.GetComponent<Unit>();

        Tile toplace = null;
        foreach (Tile a in tileAt.getNeighbours())
        {
            if(a.prefab == null && a.getOccupyingUnit() == null && a.getColor() == tileAt.getColor())
            {
                toplace = a;
            }
        }
        if(toplace == null)
        {
            toplace = tileAt;
        }
        gameObject.networkView.RPC ("moveUnitPrefabNet",RPCMode.AllBuffered,u.networkView.viewID,new Vector3(toplace.point.x, 0.15f, toplace.point.y));
        u.networkView.RPC ("setLocationNet", RPCMode.AllBuffered, toplace.networkView.viewID);
        u.networkView.RPC ("setUnitTypeNet", RPCMode.AllBuffered, (int)UnitType.CANNON);
        u.networkView.RPC ("setVillageNet", RPCMode.AllBuffered, v.networkView.viewID);
        u.networkView.RPC ("setActionNet", RPCMode.AllBuffered, (int)UnitActionType.ReadyForOrders);
        u.getLocation ().networkView.RPC ("setOccupyingUnitNet", RPCMode.AllBuffered, u.networkView.viewID);
        v.gameObject.networkView.RPC("addGoldNet", RPCMode.AllBuffered, -35);
        v.gameObject.networkView.RPC("addWoodNet", RPCMode.AllBuffered, -12);
        v.gameObject.networkView.RPC("addUnitNet", RPCMode.AllBuffered, newCannon.networkView.viewID);
    }
    public void initializeUnit(Village v,GameObject unitPrefab, UnitType type)
    {
        Tile tileAt = v.getLocatedAt ();
        GameObject newUnit = Network.Instantiate(unitPrefab, new Vector3(tileAt.point.x, 0.15f, tileAt.point.y), tileAt.transform.rotation, 0) as GameObject;
        Unit u = newUnit.GetComponent<Unit>();

        Tile toplace = null;
        foreach (Tile a in tileAt.getNeighbours())
        {
            if(a.prefab == null && a.getOccupyingUnit() == null && a.getColor() == tileAt.getColor())
            {
                toplace = a;
            }
        }
        if(toplace == null)
        {
            toplace = tileAt;
        }
        gameObject.networkView.RPC ("moveUnitPrefabNet",RPCMode.AllBuffered,u.networkView.viewID,new Vector3(toplace.point.x, 0.15f, toplace.point.y));
        //		locatedAt = toplace;
        u.networkView.RPC ("setLocationNet", RPCMode.AllBuffered, toplace.networkView.viewID);
        //		myType = unitType;
        u.networkView.RPC ("setUnitTypeNet", RPCMode.AllBuffered, (int)type);
        u.networkView.RPC ("switchUnitPrefabNet", RPCMode.AllBuffered, (int)type);
        //		myVillage = v;
        u.networkView.RPC ("setVillageNet", RPCMode.AllBuffered, v.networkView.viewID);
        //		myAction = UnitActionType.ReadyForOrders;
        u.networkView.RPC ("setActionNet", RPCMode.AllBuffered, (int)UnitActionType.ReadyForOrders);
        u.getLocation ().networkView.RPC ("setOccupyingUnitNet", RPCMode.AllBuffered, u.networkView.viewID);
        print ((int)type);
        print ((int)type+1);
        int unitCost = 10 *((int)type+1);
        v.gameObject.networkView.RPC("addGoldNet", RPCMode.AllBuffered, -unitCost);
        v.gameObject.networkView.RPC("addUnitNet", RPCMode.AllBuffered, newUnit.networkView.viewID);
    }
    private void splitRegion(Tile splitTile, Village villageToSplit)
    {
        List<List<Tile>> lstRegions = new List<List<Tile>>();
        int oldWood = villageToSplit.getWood ();
        int oldGold = villageToSplit.getGold ();
        Tile oldLocation = villageToSplit.getLocatedAt ();
        Player p = villageToSplit.getPlayer();

        // prep for BFS
        foreach (Tile t in villageToSplit.getControlledRegion()) {
            t.setVisited(false);
        }
        // build connected regions
        foreach (Tile t in splitTile.getNeighbours()) {
            if (t.getVillage()==villageToSplit && !t.getVisited()){
                List<Tile> newRegion = new List<Tile>();
                t.setVisited(true);
                newRegion.Add (t);
                splitBFS (t,villageToSplit,newRegion);
                if (newRegion.Count<3){
                    Neutralize (newRegion);
                } else{
                    lstRegions.Add (newRegion);
                }
            }
        }
        print ("after the bfs");

        if (lstRegions.Count <= 0) {
            Debug.Log ("Inside Regions <= 0");
            oldLocation.gameObject.networkView.RPC ("setLandTypeNet",RPCMode.AllBuffered,(int)LandType.Meadow);
            GameObject meadow = Network.Instantiate (meadowPrefab, new Vector3 (oldLocation.point.x, 0, oldLocation.point.y), meadowPrefab.transform.rotation,0) as GameObject;
            villageToSplit.retireAllUnits();
            Debug.Log ("after retire all units");
            p.networkView.RPC ("removeVillageNet",RPCMode.AllBuffered,villageToSplit.networkView.viewID,p.getColor());
            oldLocation.networkView.RPC ("replaceTilePrefabNet",RPCMode.AllBuffered,meadow.networkView.viewID);
            Debug.Log ("after lstRegions <= 0");
            return; //stop here if no region is big enough
        }

        int splitWood = oldWood/lstRegions.Count;
        int splitGold = oldGold/lstRegions.Count;

        //create new villages
        foreach(List<Tile> region in lstRegions)
        {
            print ("creating new village");
            Vector3 hovelLocation;
            Tile tileLocation;

            if (region.Contains (oldLocation)){
                tileLocation = oldLocation;
                hovelLocation = new Vector3(tileLocation.point.x, 0.1f, tileLocation.point.y);
            } else {
                tileLocation = getTileForRespawn(region);
                hovelLocation = new Vector3(tileLocation.point.x, 0.1f, tileLocation.point.y);
            }

            GameObject newTown = Network.Instantiate(hovelPrefab, hovelLocation, hovelPrefab.transform.rotation, 0) as GameObject;
            Village v = newTown.GetComponent<Village>();
            //tileLocation.replace (newTown);
            tileLocation.networkView.RPC ("switchTilePrefabNet",RPCMode.AllBuffered,newTown.networkView.viewID);
            v.addRegion (region); //adds T<>V and any U<>V

        //			v.setLocation (tileLocation);
            v.gameObject.networkView.RPC ("setLocationNet",RPCMode.AllBuffered,tileLocation.networkView.viewID);
        //			p.addVillage(v);
            p.gameObject.networkView.RPC ("addVillageNet",RPCMode.AllBuffered,v.networkView.viewID,p.getColor ());
        //			v.setControlledBy(p);
            GameManager GM = GameObject.Find ("preserveGM").GetComponent<GameManager>();
            int playerIndex = GM.findPlayerIndex(p);
            v.gameObject.networkView.RPC ("setControlledByNet",RPCMode.AllBuffered,playerIndex);

            if (region.Contains (oldLocation)){
                VillageType vType = villageToSplit.getMyType();
                //v.setMyType(vType);
                v.gameObject.networkView.RPC ("setVillageTypeNet",RPCMode.AllBuffered,(int)vType);
                if (vType == VillageType.Hovel)
                {
        //					newTown.transform.FindChild("Hovel").gameObject.SetActive (true);
        //					newTown.transform.FindChild("Town").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Fort").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Castle").gameObject.SetActive (false);
                    newTown.gameObject.networkView.RPC ("switchPrefabNet",RPCMode.AllBuffered,(int)vType);
                }
                else if (vType == VillageType.Town)
                {
        //					newTown.transform.FindChild("Hovel").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Town").gameObject.SetActive (true);
        //					newTown.transform.FindChild("Fort").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Castle").gameObject.SetActive (false);
                    newTown.gameObject.networkView.RPC ("switchPrefabNet",RPCMode.AllBuffered,(int)vType);

                }
                else if (vType == VillageType.Fort)
                {
        //					newTown.transform.FindChild("Hovel").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Town").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Fort").gameObject.SetActive (true);
        //					newTown.transform.FindChild("Castle").gameObject.SetActive (false);
                    newTown.gameObject.networkView.RPC ("switchPrefabNet",RPCMode.AllBuffered,(int)vType);

                }
                else if (vType == VillageType.Castle)
                {
        //					newTown.transform.FindChild("Hovel").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Town").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Fort").gameObject.SetActive (false);
        //					newTown.transform.FindChild("Castle").gameObject.SetActive (true);
                    newTown.gameObject.networkView.RPC ("switchPrefabNet",RPCMode.AllBuffered,(int)vType);
                }
            }

            //v.addGold(splitGold);
            v.gameObject.networkView.RPC ("addGoldNet",RPCMode.AllBuffered,splitGold);
        //			v.addWood(splitWood);
            v.gameObject.networkView.RPC ("addWoodNet",RPCMode.AllBuffered,splitWood);

        }
        Debug.Log ("villageToSplit: "+villageToSplit.getPlayer ().getName ());
        p.networkView.RPC ("removeVillageNet", RPCMode.AllBuffered, villageToSplit.networkView.viewID, p.getColor ());
        gameObject.networkView.RPC ("destroyVillageNet", RPCMode.AllBuffered, villageToSplit.networkView.viewID);

        GameManager GMA = GameObject.Find ("preserveGM").gameObject.GetComponent<GameManager>();
        GMA.checkLoss (p);
        GMA.checkWin ();
    }