Debian Codegraph @

src/level.cc

raw symbols SCIP
1 // (_||_/
2 // ( )
3 // ( o 0 )
4 //-OOO°--(_)---°OOO---------------------------------------
5 // Copyright (C) 2006 By Dominique Roux-Serret
6 // .OOOo oOOO. [email protected]
7 //-( )------( )---------------------------------------
8 // ( ( ) / Copyright (C) 2023 By Johnny Jazeix
9 // (_) (_/ [email protected]
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 or version 3 of the License.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19
20 // You should have received a copy of the GNU General Public License along
21 // with this program; if not, write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 #include "preference.h"
25 #include "utils.h"
26 #include "level.h"
27
28 /*** Constructeurs ***/
29 /*********************/
30 Level::Level()
31 {
32 int i;
33 for (i = 0; i < MAX_N_LEVEL_IN_MEMORY; i++) {
34 Clear(i);
35 }
36 }
37
38 /*** Charge les tableaux ***/
39 /***************************/
40 bool Level::Load()
41 {
42 unsigned char *Buf;
43 long L, P = 2;
44 int i, j;
45 char NameLevelFile[512] = "levels.dat";
46
47 Utils::GetPath(NameLevelFile);
48 L = Utils::LoadFile(NameLevelFile, Buf);
49 if (L <= 0) {
50 return false;
51 }
52
53 // Charge les tableaux
54 N = (int)(Buf[0]) * 256 + (int)(Buf[1]);
55
56 for (i = 0; i < N; i++) {
57 for (j = 0; j < LT * HT; j++) {
58 T[i].T[j] = Buf[P++];
59 }
60 T[i].DepX = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
61 T[i].DepY = (int)(Buf[P + 2]) * 256 + (int)(Buf[P + 3]);
62 T[i].DepDir = (int)(Buf[P + 4]) * 256 + (int)(Buf[P + 5]);
63 T[i].NDeco = (int)(Buf[P + 6]) * 256 + (int)(Buf[P + 7]);
64 P += 8;
65 for (j = 0; j < T[i].NDeco; j++) {
66 T[i].Deco[j].NumSpr = (int)(Buf[P]) * 256 + (int)(Buf[P + 1]);
67 T[i].Deco[j].x = (int)(Buf[P + 2]) * 256 + (int)(Buf[P + 3]);
68 T[i].Deco[j].y = (int)(Buf[P + 4]) * 256 + (int)(Buf[P + 5]);
69 P += 6;
70 }
71 }
72
73 delete[] Buf;
74
75 return true;
76 }
77
78 /*** Sauve les tableaux ***/
79 /**************************/
80 bool Level::Save()
81 {
82 unsigned char *Buf;
83 long P = 2;
84 int i, j;
85 char NameLevelFile[512] = "levels.dat";
86
87 // Alloue la mémoire
88 Buf = new unsigned char[sizeof(s_Level) * N + sizeof(int) + 1];
89 if (Buf == nullptr) {
90 return false;
91 }
92
93 // Charge les tableaux
94 Buf[0] = N / 256;
95 Buf[1] = N % 256;
96
97 for (i = 0; i < N; i++) {
98 for (j = 0; j < LT * HT; j++) {
99 Buf[P++] = T[i].T[j];
100 }
101 Buf[P] = T[i].DepX / 256;
102 Buf[P + 1] = T[i].DepX % 256;
103 Buf[P + 2] = T[i].DepY / 256;
104 Buf[P + 3] = T[i].DepY % 256;
105 Buf[P + 4] = T[i].DepDir / 256;
106 Buf[P + 5] = T[i].DepDir % 256;
107 Buf[P + 6] = T[i].NDeco / 256;
108 Buf[P + 7] = T[i].NDeco % 256;
109
110 P += 8;
111 for (j = 0; j < T[i].NDeco; j++) {
112 Buf[P] = T[i].Deco[j].NumSpr / 256;
113 Buf[P + 1] = T[i].Deco[j].NumSpr % 256;
114 Buf[P + 2] = T[i].Deco[j].x / 256;
115 Buf[P + 3] = T[i].Deco[j].x % 256;
116 Buf[P + 4] = T[i].Deco[j].y / 256;
117 Buf[P + 5] = T[i].Deco[j].y % 256;
118 P += 6;
119 }
120 }
121
122 // Sauve les tableaux
123 Utils::GetPath(NameLevelFile);
124 if (Utils::SaveFile(NameLevelFile, (char *)Buf, P) == false) {
125 delete[] Buf;
126 return false;
127 }
128
129 delete[] Buf;
130 return true;
131 }
132
133 /*** Efface un Tableau ***/
134 /*************************/
135 void Level::Del(int Num)
136 {
137 int i, j;
138
139 if (Num < N) {
140 for (i = Num; i < N - 1; i++) {
141 for (j = 0; j < LT * HT; j++) {
142 T[i].T[j] = T[i + 1].T[j];
143 }
144 T[i].DepX = T[i + 1].DepX;
145 T[i].DepY = T[i + 1].DepY;
146 T[i].DepDir = T[i + 1].DepDir;
147 T[i].NDeco = T[i + 1].NDeco;
148 for (j = 0; j < T[i].NDeco; j++) {
149 T[i].Deco[j].x = T[i + 1].Deco[j].x;
150 T[i].Deco[j].y = T[i + 1].Deco[j].y;
151 T[i].Deco[j].NumSpr = T[i + 1].Deco[j].NumSpr;
152 }
153 }
154 N--;
155 }
156 }
157
158 /*** Insert un Tableau ***/
159 /*************************/
160 void Level::Ins(int Num)
161 {
162 int i, j;
163
164 if (Num < N) {
165 for (i = N; i > Num; i--) {
166 for (j = 0; j < LT * HT; j++) {
167 T[i].T[j] = T[i - 1].T[j];
168 }
169 T[i].DepX = T[i - 1].DepX;
170 T[i].DepY = T[i - 1].DepY;
171 T[i].DepDir = T[i - 1].DepDir;
172 T[i].NDeco = T[i - 1].NDeco;
173 for (j = 0; j < T[i].NDeco; j++) {
174 T[i].Deco[j].x = T[i - 1].Deco[j].x;
175 T[i].Deco[j].y = T[i - 1].Deco[j].y;
176 T[i].Deco[j].NumSpr = T[i - 1].Deco[j].NumSpr;
177 }
178 }
179
180 Clear(Num);
181 N++;
182 }
183 }
184
185 /*** Vide un tableau ***/
186 /***********************/
187 void Level::Clear(int Num)
188 {
189 int i;
190
191 for (i = 0; i < LT * HT; i++) {
192 T[Num].T[i] = C_None;
193 }
194 T[Num].DepX = LT / 2;
195 T[Num].DepY = HT / 2;
196 T[Num].NDeco = 0;
197 }