Roguelike Item creationg and optimization

I'm creating a roguelike/ cave exploration game, and I've created a lot of it, but I am having problems with item creation. The item is supposed to be the '*'. I know what the problem is (I'm setting Dmap to map after creating the item, but before outputting Dmap), but I don't know how to fix it.

Also, I don't really know how code optimization works, so any tips would be great.

I'll split my code between this post and the comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

using namespace std;

#define on , // So I can use the function - void text(text_color on background_color)
             // To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 15 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color+(paper_color*16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );
}


int MapSizeX = 400;
int MapSizeY = 400;
char map[400][400] = {};
char Dmap[400][400] = {};
char wall = 178;
int ViewDistance = 15;
char Dir = 'N';
int x = 25;
int y = 25;
int Groom = 1;
int RoomArea = 2;
int InitRandX = (rand() % (400 / RoomArea)) * RoomArea;
int InitRandY = (rand() % (400 / RoomArea)) * RoomArea;

void Init()
{
       
     map[MapSizeY][MapSizeX];
     for(int a = 0; a < MapSizeY; a++) //Fill ALL the squares!!!!1!
     {
             for(int b = 0; b < MapSizeX; b++)
             {
                     map[b][a] = wall;
             }
     } 
     
   for(int c = RoomArea; c < MapSizeX; c = c + RoomArea) //Creating Initial Rooms
     {
            for(int d = RoomArea * (ViewDistance + 1); d < MapSizeY; d = d + RoomArea)
            {
                  int Groom = (rand() % 100);
                  if(Groom <= 45)
                  {
                           for(int a = 0; a < RoomArea; a++)
                           {
                                   for(int b = 0; b < RoomArea; b++)
                                   {
                                       map[a + c][b + d] = ' ';  
                                   }   
                           }        
                  }
            }
     }
     int Total = 0;
   for (int a = 0; a < 5; a++) //Cellular Automata Method
   {
       for(int c = RoomArea; c < MapSizeX; c = c + RoomArea)
     {
            for(int d = RoomArea; d < MapSizeY; d = d + RoomArea)
            {
                    Total = 0;
                    if (map[d][c] == wall)
                    {
                       if(map[d][c + RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d][c - RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d + RoomArea][c] == wall)
                       {
                          Total++;
                       }
                       if(map[d - RoomArea][c] == wall)
                       {
                          Total++;
                       }
                       if(map[d + RoomArea][c + RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d - RoomArea][c - RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d - RoomArea][c + RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d + RoomArea][c - RoomArea] == wall)
                       {
                          Total++;
                       }
                       if (Total >= 4)
                       {
                       for(int a = 0; a < RoomArea; a++)
                           {
                                   for(int b = 0; b < RoomArea; b++)
                                   {
                                       map[a + d][b + c] = wall;  
                                   }   
                           }    
                       }
                       else if (Total < 3)
                       {
                           for(int a = 0; a < RoomArea; a++)
                           {
                                   for(int b = 0; b < RoomArea; b++)
                                   {
                                       map[a + d][b + c] = ' ';  
                                   }   
                           }   
                       }
                    }
                    else if (map[d][c] == ' ')
                    {
                       if(map[d][c + RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d][c - RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d + RoomArea][c] == wall)
                       {
                          Total++;
                       }
                       if(map[d - RoomArea][c] == wall)
                       {
                          Total++;
                       }
                       if(map[d + RoomArea][c + RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d - RoomArea][c - RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d - RoomArea][c + RoomArea] == wall)
                       {
                          Total++;
                       }
                       if(map[d + RoomArea][c - RoomArea] == wall)
                       {
                          Total++;
                       }
                       if (Total >= 5)
                       {
                          for(int a = 0; a < RoomArea; a++)
                           {
                                   for(int b = 0; b < RoomArea; b++)
                                   {
                                       map[a + d][b + c] = ' ';  
                                   }   
                           }  
                       }
                    }

            }
     }
   }    
     for (int a = 0; a < MapSizeY; a++)
     {
          for (int b = 0; b < (RoomArea * ViewDistance) + 1  ; b++)
          {
             map[a][b] = wall;
          }   
     }
     for (int a = 0; a < MapSizeY; a++)
     {
          for (int b = MapSizeX; b > MapSizeX -(RoomArea * ViewDistance) + 1  ; b--)
          {
             map[a][b] = wall;
          }   
     }
     for (int a = 0; a < MapSizeX; a++)
     {
          for (int b = 0; b < (RoomArea * ViewDistance) + 1  ; b++)
          {
             map[b][a] = wall;
          }   
     }
     for (int a = 0; a < MapSizeX; a++)
     {
          for (int b = MapSizeY; b > MapSizeY -(RoomArea * ViewDistance) + 1  ; b--)
          {
             map[b][a] = wall;
          }   
     }
     
     while (map[InitRandY][InitRandX] != ' ' || InitRandX < ((ViewDistance + 1) * RoomArea) + (RoomArea * 2)|| InitRandY < ((ViewDistance + 1) * RoomArea) + (RoomArea * 2)) //Spawning Character
       {
        InitRandX = (rand() % (400 / RoomArea)) * RoomArea;
        InitRandY = (rand() % (400 / RoomArea)) * RoomArea;
       }
          x = InitRandX;
          y = InitRandY;
     
}


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
void Draw()
{
     for(int a = 0; a < MapSizeY; a++)
     {
             for(int b = 0; b < MapSizeX; b++)
             {
                     Dmap[b][a] = map[b][a];
             }
     }
     Dmap[y][x] = '@';
     
     for(int i = 0; i <= ViewDistance; i++)
     {
     text(white on white);
    cout << "__";
    }
    cout << "_" << endl;
     for (int c=x-ViewDistance; c<x+ViewDistance; c++)
     {
         text(white on white);
         cout << "|";
         text(white on black);
         for (int d=y-ViewDistance; d<y+ViewDistance+1; d++)
         {
             if (Dmap[d][c] == ' ')
             {
                  text(black on light_blue);
                  cout << Dmap[d][c];
                  }
             else if (Dmap[d][c] == 'G')
             {
                  text(light_red on light_blue);
                  cout << Dmap[d][c];
                  }
             else if (Dmap[d][c] == map[y][x])
             {
                  text(light_gray on light_blue);
                  cout << Dmap[d][c];
                  }
             else if (Dmap[d][c] == '*')
             {
                  text(light_gray on light_red);
                  cout << Dmap[d][c];
                  }
             else
             {
                 text(white on light_gray);
                 cout << Dmap[d][c];
                 }
         }
         text(white on white);
         cout << "|" << endl;
     }
     text(white on white);
     for(int i = 0; i <= ViewDistance; i++)
     {
    cout << "--";
    }
    cout << "-" << endl;
     }
     int RandDrop = rand() % 100;
     int DropRate = 100;
     void Destruct()
     {    
          
          if (Dir == 'S' && map[y][x+1] == wall)
          {
                  map[y][x+1] = ' ';
                  RandDrop = rand() % 100;
                  if (RandDrop < DropRate)
                  {
                      Dmap[y][x+1] = '*';
                  }
          
          }
          
          if (Dir == 'N' && map[y][x-1] == wall)
          {
                  map[y][x-1] = ' ';
                  RandDrop = rand() % 100;
                  if (RandDrop < DropRate)
                  {
                      Dmap[y][x-1] = '*';
                  }
          }
          
          if (Dir == 'E' && map[y+1][x] == wall)
          {
                  map[y+1][x] = ' ';
                  RandDrop = rand() % 100;
                  if (RandDrop < DropRate)
                  {
                      Dmap[y+1][x] = '*'; 
                  }
          }
          
          if (Dir == 'W' && map[y-1][x] == wall)
          {
                  map[y-1][x] = ' ';
                  RandDrop = rand() % 100;
                  if (RandDrop < DropRate)
                  {
                      Dmap[y-1][x] = '*';
                  }
          }
     }
     
string input;
     
void Input()
{
    if (input == "H")
    {
        Dir = 'N';
        if ((x > 5) && (map[y][x-1] == ' '))
        {
             x = x - 1;
        }
    }
    if (input == "P")
    {
        Dir = 'S';
        if ((x < 394) && (map[y][x+1] == ' '))
        {
              x = x + 1;
        }
    }
    
    if (input == "K")
    {
        Dir = 'W';
        if ((y > 5) && (map[y-1][x] == ' '))  
        {
              y = y - 1;
        }
    }
       
    if (input == "M")
    {
        Dir = 'E';
        if ((y < 394) && (map[y+1][x] == ' '))
        {
              y = y + 1;
        }
    }
    
    if (input == "b")
    ViewDistance--;
    
    if (input == "n")
    ViewDistance++;     
    
    if (input == "6")
    Init();
    
    if (input == "9")
    {
              x = 360;
              y = 360;
    }
    if (input == "d")
    {
     Destruct();          
    }
     }

int main(int argc, char *argv[])
{
    Init(); 
    while (true)
    { 
    text(white on black);
    system("cls");
    Draw();
    text(white on black);
    cout<< "X: " << x << endl;
    cout<< "Y: " << y << endl;
    cout<< Dir << endl;
    input = getch();
    input = getch();
    Input();
    }
    
    text(white on black);
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.