示例#1
0
 public void AddLake(bool allow_disconnected_lakes)
 {
     bool trees = CoinFlip();
     while(true){
         Dungeon shape = new Dungeon(21,21);
         shape[10,10] = CellType.DeepWater;
         for(bool done=false;!done;){
             pos p = new pos(Roll(19),Roll(19));
             if(shape[p].Is(CellType.Wall)){
                 bool good = false;
                 foreach(int dir in FourDirections){
                     if(shape[p.PosInDir(dir)].Is(CellType.DeepWater)){
                         good = true;
                         break;
                     }
                 }
                 if(good){
                     if(p.ApproximateEuclideanDistanceFromX10(10,10) < 100){
                         if(PercentChance(100 - p.ApproximateEuclideanDistanceFromX10(10,10))){
                             shape[p] = CellType.DeepWater;
                             if(p.row == 1 || p.col == 1 || p.row == 19 || p.col == 19){
                                 done = true;
                             }
                         }
                     }
                 }
             }
         }
         shape.RemoveDeadEndCorridors();
         shape.ApplyCellularAutomataXYRule(5);
         for(int i=0;i<21;++i){
             for(int j=0;j<21;++j){
                 if(!shape[i,j].IsWall()){
                     bool done = false;
                     for(int s=0;s<21 && !done;++s){
                         for(int t=0;t<21 && !done;++t){
                             if(shape[s,t].IsWall() && new pos(i,j).ApproximateEuclideanDistanceFromX10(s,t) < 20){
                                 shape[i,j] = CellType.ShallowWater;
                                 done = true;
                             }
                             else{
                                 if(shape[s,t].IsWall() && new pos(i,j).ApproximateEuclideanDistanceFromX10(s,t) == 20){
                                     if(CoinFlip()){
                                         shape[i,j] = CellType.ShallowWater;
                                         done = true;
                                     }
                                 }
                             }
                         }
                     }
                     if(!done){
                         shape[i,j] = CellType.DeepWater;
                     }
                 }
             }
         }
         int start_r = 999; //these are for the position of the lake within the "shape" variable
         int start_c = 999;
         int end_r = -1;
         int end_c = -1;
         for(int i=0;i<21;++i){
             for(int j=0;j<21;++j){
                 if(!shape[i,j].IsWall()){
                     if(i < start_r){
                         start_r = i;
                     }
                     if(i > end_r){
                         end_r = i;
                     }
                     if(j < start_c){
                         start_c = j;
                     }
                     if(j > end_c){
                         end_c = j;
                     }
                 }
             }
         }
         int lake_h = (end_r - start_r) + 1;
         int lake_w = (end_c - start_c) + 1;
         CellType[,] old_cells = new CellType[lake_h,lake_w];
         for(int tries=0;tries<200;++tries){
             int rr = Roll(H - lake_h - 1);
             int rc = Roll(W - lake_w - 1);
             for(int i=0;i<lake_h;++i){
                 for(int j=0;j<lake_w;++j){
                     old_cells[i,j] = map[i+rr,j+rc];
                     if(shape[i+start_r,j+start_c].Is(CellType.ShallowWater,CellType.DeepWater) && !map[i+rr,j+rc].Is(CellType.DeepWater)){
                         map[i+rr,j+rc] = shape[i+start_r,j+start_c];
                     }
                 }
             }
             if(allow_disconnected_lakes || this.IsFullyConnected()){
                 for(int i=0;i<lake_h;++i){
                     for(int j=0;j<lake_w;++j){
                         if(map[i+rr,j+rc] == CellType.ShallowWater){
                             if(trees){
                                 map[i+rr,j+rc] = CellType.Tree;
                             }
                         }
                         if(map[i+rr,j+rc] == CellType.DeepWater){
                             if(trees){
                                 map[i+rr,j+rc] = CellType.RoomInterior;
                             }
                         }
                     }
                 }
                 return;
             }
             else{
                 for(int i=0;i<lake_h;++i){ //undo what we just did and try again
                     for(int j=0;j<lake_w;++j){
                         map[i+rr,j+rc] = old_cells[i,j];
                     }
                 }
             }
         }
     }
 }