aboutsummaryrefslogtreecommitdiff
path: root/worm.c
blob: 67d4dac4715e524e7333d0c7ee6857be94a08cdf (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* GEM Worm
 * Copyright (C) 2009 Jeffrey Armstrong
 * http://jeff.rainbow-100.com
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 * A full version of the license terms is available in LICENSE.txt.
 */

#include <stdio.h>
/*#include <stdlib.h>*/
#include <portab.h>
#include <aes.h>
#include <vdi.h>

/* The proper resource header is dependent upon system */
#ifdef PCGEM
#include "wormpc.h"
#else
#include "wormst.h"
#endif

#include "field.h"
#include "player.h"

/* Sometimes you may need to compile in some missing global
 * arrays used within the gem libraries
 */
#ifdef PCGEM
#ifdef NEEDGSX
GLOBAL WORD   contrl[11];        /* control inputs                            */
GLOBAL WORD   intin[80];         /* max string length                         */
GLOBAL WORD   ptsin[256];        /* polygon fill points                       */
GLOBAL WORD   intout[45];        /* open workstation output                   */
GLOBAL WORD   ptsout[12];
#endif
#else
#define FAR
#endif

WORD	app_wflags = NAME | CLOSER | MOVER | SIZER;
WORD	app_accid;
WORD	app_appid;

WORD	app_wh = -1;	/* Border window handle */
WORD	app_vh;			/* VDI handle */

char	*app_title = "Worm";
GRECT	app_wdw;			/* xywh of working area */

OBJECT FAR *app_menu;
OBJECT FAR *about_box;

#ifndef WF_WXYWH
#ifdef WF_WORKXYWH
#define WF_WXYWH	WF_WORKXYWH
#endif
#endif

#ifndef WF_CXYWH
#ifdef WF_CURRXYWH
#define WF_CXYWH	WF_CURRXYWH
#endif
#endif

#define max(x,y)	x>y ? x : y
#define min(x,y)	x<y ? x : y

#ifdef PCGEM
#define RCS_FILE	"wormpc.rsc"
#else
#define RCS_FILE	"wormst.rsc"
#endif

#define MOVEDELAY	300

#define STARTWIDTH  640
#define STARTHEIGHT 480

int rc_intersect(GRECT *one, GRECT *two)
{
WORD tx,ty,tw,th;

	tw = min(one->g_x+one->g_w,two->g_x+two->g_w);
	th = min(one->g_y+one->g_h,two->g_y+two->g_h);
	tx = max(one->g_x,two->g_x);
	ty = max(one->g_y,two->g_y);
	two->g_x = tx;
	two->g_y = ty;
	two->g_w = tw-tx;
	two->g_h = th-ty;
	return ( (tw > tx) && (th > ty) );
}

void hndl_about()
{
GRECT box,origin;

	/* ob_xywh(app_menu, MDESK, &origin); */

	form_center(about_box, &box.g_x, &box.g_y, &box.g_w, &box.g_h);
	objc_draw(about_box,0,2,box.g_x, box.g_y, box.g_w, box.g_h);
	form_do(about_box,0);
	form_dial(FMD_FINISH,0,0,0,0,box.g_x, box.g_y, box.g_w, box.g_h);



	return;
}

int open_window(int new)
{
	graf_mouse(M_OFF,NULL);
	wind_update(BEG_UPDATE);

	app_wh = wind_create(app_wflags, 0, 0, 200, 200);
	if(app_wh<=0)
		return -1;

#ifdef PCGEM
	wind_set(app_wh, WF_NAME, FPOFF(app_title), FPSEG(app_title), 0, 0);
#else
    wind_set(app_wh, WF_NAME, app_title);
#endif

	if(new == 1) {
		/* Get the desktop */
		wind_get(0, WF_WXYWH, &app_wdw.g_x,
	                         &app_wdw.g_y,
	                         &app_wdw.g_w,
	                         &app_wdw.g_h);
	
		app_wdw.g_w = min(STARTWIDTH,app_wdw.g_w);
		app_wdw.g_h = min(STARTHEIGHT,app_wdw.g_h);
	}

	wind_open(app_wh, app_wdw.g_x, app_wdw.g_y, app_wdw.g_w, app_wdw.g_h);
	wind_get(app_wh, WF_WXYWH, &app_wdw.g_x,
	                           &app_wdw.g_y,
	                           &app_wdw.g_w,
	                           &app_wdw.g_h);


	wind_update(END_UPDATE);
	graf_mouse(M_ON,NULL);
	return 1;
}

int init_app()
{
WORD work_in[11];
WORD work_out[57];

WORD ww;

WORD i;
WORD txtwidth,txtheight;

	app_accid = appl_init();

	/* VDI handle... */
	app_vh = graf_handle(&txtwidth,&txtheight,&i,&i);
	for(i=0;i<10;i++) work_in[i] = 1;
	work_in[10] = 2;
	v_opnvwk(work_in,&app_vh,work_out);

	/* RSC load */
	if(rsrc_load(RCS_FILE) == 0) {
		form_alert(1,"[3][The resource file was not found][Exit]");
		return -1;
	}

	wind_update(BEG_UPDATE);
	rsrc_gaddr(R_TREE,RCMENU,&app_menu);
	menu_bar(app_menu,1);
	wind_update(END_UPDATE);

	rsrc_gaddr(R_TREE,ABOUT,&about_box);

	return open_window(1);
}

/* Not necessary yet... */
void do_redraw()
{
GRECT box, me;

WORD pts[4];

#ifdef DEBUG
	printf("REDRAW!\n");
#endif

	graf_mouse(M_OFF,NULL);
	wind_update(BEG_UPDATE);

	wind_get(app_wh, WF_WXYWH, &me.g_x, &me.g_y, &me.g_w, &me.g_h);
	wind_get(app_wh, WF_FIRSTXYWH, &box.g_x,
	                               &box.g_y,
	                               &box.g_w,
	                               &box.g_h);

	while(box.g_w || box.g_h) {
		if(rc_intersect(&me, &box)) {
			pts[0] = box.g_x;
			pts[1] = box.g_y;
			pts[2] = box.g_w+box.g_x-1;
			pts[3] = box.g_h+box.g_y-1;
      		vs_clip(app_vh, 1, pts);
			draw_field(app_vh, &app_wdw);
		}
		wind_get(app_wh, WF_NEXTXYWH, &box.g_x,
	                                 &box.g_y,
	                                 &box.g_w,
	                                 &box.g_h);
	}

	/* Turn off clipping */
	vs_clip(app_vh, 0, pts);
	wind_update(END_UPDATE);
	graf_mouse(M_ON,NULL);
}

void do_window_change(GRECT *rect)
{
	wind_set(app_wh, WF_CXYWH, rect->g_x, rect->g_y, rect->g_w, rect->g_h);
	wind_get(app_wh, WF_WXYWH, &app_wdw.g_x, &app_wdw.g_y, &app_wdw.g_w, &app_wdw.g_h);
}

void hndl_keys(WORD key, WPLAYER *player)
{
char asc;

	asc = key & 127;
	switch(asc) {
		case 'w':
		case 'W':
			player->dir = WUP;
			break;
		case 's':
		case 'S':
			player->dir = WDOWN;
			break;
		case 'a':
		case 'A':
			player->dir = WLEFT;
			break;
		case 'd':
		case 'D':
			player->dir = WRIGHT;
			break;
	}

}

int main(int argc, char *argv[])
{
WORD ret;
WORD msg[8];
int done;
WORD top;
WORD dummy;

WORD special_keys;
WORD key;

WPLAYER *player;

int playing = 0;
int palive, falive;

int tailx, taily;

    srand(time(NULL));

	if(init_app() > 0) {
		done = 0;

#ifdef DEBUG
		printf("AppID: %d\n",app_accid);
#endif
		player = init_player();
		field_init();
		food_init();

#ifdef DEBUG
		printf("Game init complete\n");
#endif

		msg[0] = AC_OPEN;
		msg[4] = app_accid;
		ret = MU_MESAG;
		done = -2;
		while(done <=0) {

			if(done == -2)
				done = -1;
			else {
				ret = evnt_multi(MU_KEYBD | MU_MESAG | MU_TIMER, /* Accept keypresses, messages, and timers */
						 0, 0, 0,			 								 /* Mouse clicks, which buttons, and states */
						 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,				 /* Mouse movement info */
						 msg,				 									 /* 16-byte message array */
						 (WORD)MOVEDELAY, 0,		 						 /* The low and high word of the timer in ms */
						 &dummy, &dummy,		 							 /* Mouse position @ mouse event */
						 &dummy,			 									 /* Mouse button status */
						 &special_keys,			 						 /* "Special" key statuses */
						 &key,				 								 /* Key pressed scancode */
						 &dummy);											 /* # of mouse button presses */

			}
#ifdef DEBUG
			printf("maybe MSG: %d\n",msg[0]);
#endif
			/* Key presses */
			if((ret & MU_KEYBD) && playing > 0) {
				hndl_keys(key,player);

			/* Timer - update game */
			} else if((ret & MU_TIMER) && playing > 0) {
				wind_update(BEG_UPDATE);
				palive = update_player(player, &tailx, &taily);
				falive = update_field(player);

				food_check(app_vh, &app_wdw, player);

				/* form_alert(1,"[1][Update][Ok]"); */
				/* draw_field(app_vh, &app_wdw); */
                incremental_draw(app_vh, &app_wdw, player->head->c_x, player->head->c_y);
                incremental_draw(app_vh, &app_wdw, tailx, taily);

				wind_update(END_UPDATE);

				if(palive == DEAD || falive == DEAD) {
					form_alert(1,"[3][You've Died!|Good try, though...][Reset]");
					reset_player(player);
					playing = 0;
				}

			/* Messages (menu, resizes, etc.) */
			} else if(ret & MU_MESAG) {
#ifdef DEBUG
				printf("MSG: %d\n",msg[0]);
#endif
				switch(msg[0]) {
					case AC_OPEN:
						if(msg[4] != app_accid)
							break;
						if(app_wh > 0) {
							wind_get(0,WF_TOP, &top, &dummy, &dummy, &dummy);
							if(top != app_wh) {
								wind_set(app_wh, WF_TOP, app_wh, 0, 0, 0);
								wind_set(app_wh, WF_CXYWH, 30,30,100,100);
							}
						} else
							open_window(0);
						break;

					case AC_CLOSE:
						if(msg[3] == app_accid)
							app_wh = -1;
						break;

					case WM_CLOSED:
						if(msg[3] != app_wh || app_wh < 0)
							break;
						graf_mouse(M_OFF,NULL);
						wind_close(app_wh);
						wind_delete(app_wh);
						graf_mouse(M_ON,NULL);
						app_wh = -1;
						done = 1;
						break;

					case WM_TOPPED:
						wind_set(app_wh,WF_TOP,app_wh,0,0,0);
						break;

					case WM_MOVED:
					case WM_SIZED:
						do_window_change((GRECT *)&msg[4]);
					case WM_REDRAW:
						update_field(player);
						do_redraw();
						break;

					case MN_SELECTED:
						switch(msg[4]) {
							case MQUIT:
								msg[0] = WM_CLOSED;
								msg[3] = app_wh;
								appl_write(app_accid,sizeof(msg),msg);
								break;
							case MNEW:
							   update_field(player);
							   draw_field(app_vh, &app_wdw);
								playing = 1;
								break;
							case MABOUT:
								hndl_about();
								break;
						}
						break;

				} /* switch */

			} /* if */

			if(app_wh <= 0 )
				continue;

			/* done = 0; */

		} /* while */

		v_clsvwk(app_vh);
		/* wind_close(app_wh); */
		/* wind_delete(app_wh); */
		appl_exit();

	}

	return 0;
}