-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mappa.cpp
executable file
·144 lines (142 loc) · 5.33 KB
/
Mappa.cpp
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
#include <stdio.h>
#include <math.h>
#include "functions.h"
#include "Mappa.h"
#define VISCOSITA 55
#define STABILITA 0.99997
Mappa::Mappa()
{
occupazione = new Oggetto*[MAP_WIDTH*MAP_HEIGHT];
smell2 = new float[MAP_WIDTH*MAP_HEIGHT];
smell1 = new float[MAP_WIDTH*MAP_HEIGHT];
for (int index = 0; index < MAP_WIDTH*MAP_HEIGHT; index++)
{
occupazione[index] = nullptr;
smell1[index] = 0;
smell2[index] = 0;
}
stable_smell = smell1;
unstable_smell = smell2;
}
void Mappa::add_smell(int x, int y, float in_smell)
{
if(x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT)
printf("ERROR: sei fuori dalla mappa 2! %d %d\n", x, y);
else
new_smell.push_back(A_smell{x+MAP_WIDTH*y, in_smell});
}
float Mappa::get_smell(int x, int y)
{
if(x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT)
return 0;
return stable_smell[x+MAP_WIDTH*y];
}
void Mappa::set_space(int x, int y, Oggetto *oggetto)
{
if(x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT)
printf("ERROR: sei fuori dalla mappa! %d %d\n", x, y);
else
occupazione[x+MAP_WIDTH*y] = oggetto;
}
Oggetto* Mappa::get_space(int x, int y)
{
if(x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT)
return nullptr;
return occupazione[x+MAP_WIDTH*y];
}
void Mappa::draw_1(unsigned char screen_color[SCREEN_HEIGHT][SCREEN_WIDTH][4], int player_posx, int player_posy)
{
if(draw_type == 0)
{
}
else
{
int x_min = int_max(0,-player_posx);
int x_max = int_min(SCREEN_WIDTH,MAP_WIDTH-player_posx);
int y_min = int_max(0,-player_posy);
int y_max = int_min(SCREEN_HEIGHT,MAP_HEIGHT-player_posy);
for (int screenx = x_min; screenx < x_max; screenx++)
for (int screeny = y_min; screeny < y_max; screeny++)
{
int x = screenx + player_posx;
int y = screeny + player_posy;
if (stable_smell[x+MAP_WIDTH*y] < 0)
{
int log_value = int(std::log(1-stable_smell[x+MAP_WIDTH*y])*15);
screen_color[screeny][screenx][2] = int_min(255, log_value);
screen_color[screeny][screenx][1] = int_min(255,int_max(0,log_value-255));
screen_color[screeny][screenx][0] = int_min(255,int_max(0,log_value-510));
}
else
{
int log_value = int(std::log(1+stable_smell[x+MAP_WIDTH*y])*15);
screen_color[screeny][screenx][2] = int_min(255,int_max(0,log_value-510));
screen_color[screeny][screenx][1] = int_min(255,int_max(0,log_value-255));
screen_color[screeny][screenx][0] = int_min(255, log_value);
}
}
}
}
void Mappa::draw(unsigned char screen_color[SCREEN_HEIGHT][SCREEN_WIDTH][4], float zoom_level, float player_posx, float player_posy)
{
if(draw_type == 0)
{
}
else
{
int x_min = int_max(0,int(-player_posx*zoom_level));
int x_max = int_min(SCREEN_WIDTH,int((MAP_WIDTH-player_posx)*zoom_level));
int y_min = int_max(0,int(-player_posy*zoom_level));
int y_max = int_min(SCREEN_HEIGHT,int((MAP_WIDTH-player_posy)*zoom_level));
for (int screenx = x_min; screenx < x_max; screenx++)
for (int screeny = y_min; screeny < y_max; screeny++)
{
int x = int(screenx / zoom_level + player_posx);
int y = int(screeny / zoom_level + player_posy);
if (stable_smell[x+MAP_WIDTH*y] < 0)
{
int log_value = int(std::log(1-stable_smell[x+MAP_WIDTH*y])*40);
screen_color[screeny][screenx][2] = int_min(255, log_value);
screen_color[screeny][screenx][1] = int_min(255,int_max(0,log_value-255));
screen_color[screeny][screenx][0] = int_min(255,int_max(0,log_value-510));
}
else
{
int log_value = int(std::log(1+stable_smell[x+MAP_WIDTH*y])*40);
screen_color[screeny][screenx][2] = int_min(255,int_max(0,log_value-510));
screen_color[screeny][screenx][1] = int_min(255,int_max(0,log_value-255));
screen_color[screeny][screenx][0] = int_min(255, log_value);
}
}
}
}
void Mappa::compute_diffusion()
{
for (int x = 1; x < MAP_WIDTH-1; x++)
for (int y = 1; y < MAP_HEIGHT-1; y++)
unstable_smell[x+MAP_WIDTH*y] = stable_smell[x+MAP_WIDTH*y]+(stable_smell[x-1+MAP_WIDTH*y]+stable_smell[x+MAP_WIDTH*(y-1)]+stable_smell[x+1+MAP_WIDTH*y]+stable_smell[x+MAP_WIDTH*(y+1)]-4*stable_smell[x+MAP_WIDTH*y])/VISCOSITA;
for (int x = 1; x < MAP_WIDTH-1; x++)
for (int y = 1; y < MAP_HEIGHT-1; y++)
unstable_smell[x+MAP_WIDTH*y] = unstable_smell[x+MAP_WIDTH*y]*STABILITA;
}
void Mappa::confirm_diffusion()
{
float *temp = unstable_smell;
for (A_smell a_smell : new_smell)
unstable_smell[a_smell.index] += a_smell.intensity;
new_smell.clear();
unstable_smell = stable_smell;
stable_smell = temp;
}
void Mappa::print()
{
for (int y = 0; y < MAP_HEIGHT; y++)
{
for (int x = 0; x < MAP_WIDTH; x++)
if(occupazione[x+MAP_WIDTH*y])
printf("%d ", occupazione[x+MAP_WIDTH*y]->get_type());
else
printf("0 ");
printf("\n");
}
}