aboutsummaryrefslogtreecommitdiff
path: root/engines/avalanche/chunkxf9.cpp
blob: f03674477ffc0a210ea141f71182a6ab884ee9c6 (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on the original source code of Lord Avalot d'Argent version 1.3.
 * Copyright (c) 1994-1995 Mike, Mark and Thomas Thurman.
 */

#include "graph.h"
/*#include "Tommys.h"*/
/*#include "Crt.h"*/

namespace Avalanche {

enum flavourtype {ch_ega, ch_bgi, last_flavourtype};

struct chunkblocktype {
	flavourtype flavour;
	integer x, y;
	integer xl, yl;
	longint size;
	boolean natural;

	boolean memorise; /* Hold it in memory? */
};

const array<1, 44, char> chunkheader =
    string("Chunk-type AVD file, for an Avvy game.") + '\32' + '\261' + '\x30' + '\x1' + '\x75' + '\261';

const integer arraysize = 12000;

array<1, 30, longint> offsets;
byte num_chunks, this_chunk;
integer gd, gm;
untyped_file f;
array<0, arraysize, byte> aa;

void finder() {
	char r;
	integer x, y;
	setfillstyle(0, 0);
	setcolor(14);
	x = 320;
	y = 100;
	setwritemode(xorput);
	do {
		bar(0, 0, 200, 10);
		outtextxy(0, 0, strf(x) + ',' + strf(y));
		line(x - 20, y, x + 20, y);
		line(x, y - 20, x, y + 20);
		do {
		} while (!keypressed());
		line(x - 20, y, x + 20, y);
		line(x, y - 20, x, y + 20);
		switch (readkey()) {
		case '\15':
			return;
			break;
		case '8':
			y -= 10;
			break;
		case '4':
			x -= 10;
			break;
		case '6':
			x += 10;
			break;
		case '2':
			y += 10;
			break;
		case '\0':
			switch (readkey()) {
			case cup:
				y -= 1;
				break;
			case cdown:
				y += 1;
				break;
			case cleft:
				x -= 1;
				break;
			case cright:
				x += 1;
				break;
			}
			break;
		}
	} while (!false);
}

void load() {
	byte a1 /*absolute $A400:800*/;
	byte bit;
	untyped_file f;

	assign(f, "place42.avd");
	reset(f, 1);
	seek(f, 177);
	for (bit = 0; bit <= 3; bit ++) {
		port[0x3c4] = 2;
		port[0x3ce] = 4;
		port[0x3c5] = 1 << bit;
		port[0x3cf] = bit;
		blockread(f, a1, 12080);
	}

	close(f);
	bit = getpixel(0, 0);
	setvisualpage(1);
	setactivepage(1);
	finder();
	setvisualpage(0);
	setactivepage(0);
}

void loadtemp() {
	byte a0 /*absolute $A000:800*/;
	byte bit;
	untyped_file f;

	assign(f, "a:chunkbi3.avd");
	reset(f, 1);
	seek(f, 177);
	for (bit = 0; bit <= 3; bit ++) {
		port[0x3c4] = 2;
		port[0x3ce] = 4;
		port[0x3c5] = 1 << bit;
		port[0x3cf] = bit;
		blockread(f, a0, 12080);
	}
	close(f);

	bit = getpixel(0, 0);

	finder();
}

void open_chunk() {
	assign(f, "chunk42.avd");
	rewrite(f, 1);
	blockwrite(f, chunkheader, sizeof(chunkheader));
	blockwrite(f, num_chunks, 1);
	blockwrite(f, offsets, num_chunks * 4);

	this_chunk = 0;
}

void close_chunk() {
	seek(f, 45);
	blockwrite(f, offsets, num_chunks * 4); /* make sure they're right! */
	close(f);
}

void mgrab(integer x1, integer y1, integer x2, integer y2, word size) {
	integer yy;
	word aapos;
	byte length, bit;
	if (size > arraysize) {
		restorecrtmode();
		output << "*** SORRY! *** Increase the arraysize constant to be greater" << NL;
		output << " than " << size << '.' << NL;
		exit(0);
	}

	aapos = 0;

	length = x2 - x1;

	for (bit = 0; bit <= 3; bit ++) {
		port[0x3c4] = 2;
		port[0x3ce] = 4;
		port[0x3c5] = 1 << bit;
		port[0x3cf] = bit;
		for (yy = y1; yy <= y2; yy ++) {
			move(mem[0xa400 * yy * 80 + x1], aa[aapos], length);
			aapos += length;
		}
	}
	bit = getpixel(0, 0);

}

void grab(integer x1, integer y1, integer x2, integer y2, integer realx, integer realy, flavourtype flav,
          boolean mem, boolean nat)
/* yes, I *do* know how to spell "really"! */
{
	word s;
	pointer p;
	chunkblocktype ch;
	/* rectangle(x1,y1,x2,y2); exit;*/
	this_chunk += 1;
	offsets[this_chunk] = filepos(f);


	s = imagesize(x1, y1, x2, y2);
	getmem(p, s);
	getimage(x1, y1, x2, y2, p);

	{
		ch.flavour = flav;
		ch.x = realx;
		ch.y = realy;

		ch.xl = x2 - x1;
		ch.yl = y2 - y1;
		ch.size = s;
		ch.memorise = mem;
		ch.natural = nat;
	}

	setvisualpage(1);
	setactivepage(1);
	input >> NL;
	putimage(ch.x, ch.y, p, 0);

	if (flav == ch_ega) {
		freemem(p, s);
		s = 4 * (((x2 / 8) - (x1 / 8)) + 2) * (y2 - y1 + 1);
		{
			ch.size = s;
			ch.x = ch.x / 8;
			ch.xl = (ch.xl / 8) + 2;
			mgrab(ch.x, ch.y, ch.x + ch.xl, ch.y + ch.yl, s);
		}
	} else
		/* For BGI pictures. */
	{
		ch.x = ch.x / 8;
		ch.xl = (ch.xl + 7) / 8;
		ch.size = imagesize(ch.x * 8, ch.y, (ch.x + ch.xl) * 8, ch.y + ch.yl);
	}

	input >> NL;
	setvisualpage(0);
	setactivepage(0);

	blockwrite(f, ch, sizeof(ch));

	switch (flav) {
	case ch_ega :
		if (! nat)  blockwrite(f, aa, s);
		break;
	case ch_bgi : {
		if (! nat)  blockwrite(f, p, s);
		freemem(p, s);
	}
	break;
	}
	/* rectangle(x1,y1,x2,y2);*/
}

int main(int argc, const char *argv[]) {
	pio_initialize(argc, argv);
	gd = 3;
	gm = 0;
	initgraph(gd, gm, "c:\\bp\\bgi");
	load();
	loadtemp();
	setwritemode(xorput);

	num_chunks = 4;

	open_chunk();

	grab(276, 36, 329, 73, 94, 97, ch_bgi, true, true); /*1 Not tied to the tree.*/
	grab(276, 36, 329, 73, 94, 97, ch_ega, false, false); /*2 Tied to the tree. */
	grab(182, 80, 226, 88, 311, 132, ch_ega, false, false); /*3 Mushrooms have gone! */
	grab(253, 75, 267, 82, 114, 98, ch_ega, false, false); /*4 Avalot blinks. */
	close_chunk();
	return EXIT_SUCCESS;
}

} // End of namespace Avalanche.