home *** CD-ROM | disk | FTP | other *** search
- head 1.4;
- branch ;
- access ;
- symbols ;
- locks ; strict;
- comment @ * @;
-
-
- 1.4
- date 93.10.21.16.21.23; author Torsten; state Rel;
- branches ;
- next 1.3;
-
- 1.3
- date 93.10.19.18.05.15; author Torsten; state Exp;
- branches ;
- next 1.2;
-
- 1.2
- date 93.10.12.18.03.27; author Torsten; state Exp;
- branches ;
- next 1.1;
-
- 1.1
- date 93.10.12.00.04.41; author Torsten; state Exp;
- branches ;
- next ;
-
-
- desc
- @Michael Warner's PUBLIC DOMAIN version of the classic game SNAKE.
- @
-
-
- 1.4
- log
- @Pause key changed from 'P' to spacebar.
- @
- text
- @/*
- * This is a modified version of Michael Warner's
- * Public Domain Workbench SNAKE game.
- *
- * This (improved?) version by Torsten Poulin
- * is, of course, still in the public domain.
- *
- * Torsten Poulin
- * Banebrinken 99, 2, 77
- * DK-2400 Copenhagen NV
- * DENMARK
- *
- * THIS IS A WB 2+ PROGRAM!!!
- *
- * $Log: Orm.c,v $
- * Revision 1.3 93/10/19 18:05:15 Torsten
- * Localized. Defaults to builtin Danish strings if
- * locale.library isn't available; thus, it still runs
- * under AmigaDOS 2.04 (V37).
- *
- * Revision 1.2 93/10/12 18:03:27 Torsten
- * Added function prototypes.
- * Can be compiled with either DICE or SAS/C 5.10.
- * Tries to be smart about the screen display mode.
- * Obstacles disabled on two-colour screens.
- * Knows about window border sizes.
- *
- * Revision 1.1 93/10/12 00:04:41 Torsten
- * This is my first version of Michael's game.
- *
- */
-
- char RCSid[] = "$Id: Orm.c,v 1.3 93/10/19 18:05:15 Torsten Exp Locker: Torsten $";
- char version[] = "$VER: Orm 1.4 (21.10.93)";
- char copyright[] = "$COPYRIGHT:*PUBLIC DOMAIN* - M. Warner & T. Poulin$";
-
- #include <exec/types.h>
- #include <intuition/intuition.h>
- #include <libraries/locale.h>
- #include <clib/exec_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/graphics_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/locale_protos.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <sys/types.h>
- #include "orm.h"
-
- /*
- * Compiler dependencies
- */
-
- #if defined(__SASC) | defined(LATTICE) | defined(_DCC)
-
- #ifdef _DCC
- void fool_DICE(void) { _waitwbmsg(); }
- #define main _main
- #define __far
- #define __stdargs
-
- #else /* __SASC | LATTICE */
- #ifdef __SASC_510
- #include <pragmas/exec_pragmas.h>
- #include <pragmas/intuition_pragmas.h>
- #include <pragmas/graphics_pragmas.h>
- #include <pragmas/locale_pragmas.h>
- extern struct DosLibrary *DOSBase;
- #include <pragmas/dos_pragmas.h>
- #endif /* __SASC_510 */
- void main(void);
- void _main(char *dummy) { main(); }
- #endif /* __SASC | LATTICE */
-
- #else /* something else */
- #define __far
- #define __stdargs
- #endif
-
- #define XSIZE 40
- #define YSIZE 40
- #define BSIZE 3
- #define NFROGS 4
- #define PRIORITY 0
-
- #define CLEAR 1
- #define SNAKE 0
- #define BRICK 3
- #define FROG 2
-
- #define KEY_ESC 69
- #define KEY_Pause 64
- #define KEY_UP 76
- #define KEY_DOWN 77
- #define KEY_RIGHT 78
- #define KEY_LEFT 79
-
- extern UWORD __stdargs RangeRand(ULONG maxValue);
- void exit_prog(int num);
- void open_window(void);
- void close_window(void);
- void notify(char *str);
- void set_point(int x, int y, int item);
- void clear_grid(void);
- void draw_score(void);
- void pause(void);
- void create_frog(int n);
- void check_frogs(void);
- void replace_frog(int x, int y);
- void setup_game(void);
- BOOL play_one_game(void);
- void play_game(void);
- void randomize(void);
- char *getcatalogstr(struct Catalog *, LONG, char *);
-
- APTR IntuitionBase, GfxBase;
- struct LocaleBase *LocaleBase;
- struct Catalog *catalog;
- struct Window *win;
- struct RastPort *rp;
- LONG xstretch, ystretch;
- LONG border_left, border_top;
-
- UBYTE grid[XSIZE][YSIZE];
- UBYTE snx[1500], sny[1500], frx[NFROGS], fry[NFROGS];
- int dir, length, head, tail, grow, frogtime[NFROGS];
- UBYTE key;
- BOOL has_obsta = FALSE;
-
-
- void exit_prog(int num)
- {
- if (LocaleBase) CloseLibrary((struct Library *) LocaleBase);
- if (GfxBase) CloseLibrary((struct Library *) GfxBase);
- if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
- exit(num);
- }
-
-
- void open_window(void)
- {
- struct Screen *scr;
- struct DrawInfo *scr_di;
- char *title;
-
- if (!(scr = LockPubScreen(NULL)))
- exit_prog(20);
-
- if (scr_di = GetScreenDrawInfo(scr)) {
- has_obsta = scr_di->dri_Depth > 1;
- FreeScreenDrawInfo(scr, scr_di);
- }
-
- /*
- * Determine stretch factors heuristically.
- * Please note that this is a hack and the result
- * will not be pleasing in the VGA-ExtraLores modes :-(
- */
- if (scr->Width <= 500)
- xstretch = 1; /* Lores */
- else if (scr->Width <= 800)
- xstretch = 2; /* Hires */
- else
- xstretch = 4; /* SuperHires or A2024 */
-
- if (scr->Height <= 300)
- ystretch = 1; /* Not interlaced */
- else if (scr->Height <= 700)
- ystretch = 2; /* Productivity or interlaced */
- else
- ystretch = 4; /* Interlaced Productivity or A2024 */
-
- if (has_obsta)
- title = getcatalogstr(catalog, MSG_SCRTITLE1, MSG_SCRTITLE1_STR);
- else
- title = getcatalogstr(catalog, MSG_SCRTITLE2, MSG_SCRTITLE2_STR);
-
- win = OpenWindowTags(NULL,
- WA_InnerWidth, XSIZE*BSIZE*xstretch+2*xstretch,
- WA_InnerHeight, YSIZE*BSIZE*ystretch+2*ystretch+1,
- WA_IDCMP, CLOSEWINDOW | RAWKEY,
- WA_ScreenTitle, title,
- WA_Flags, ACTIVATE
- | WINDOWCLOSE
- | WINDOWDRAG
- | RMBTRAP
- | WINDOWDEPTH
- | SMART_REFRESH
- | NOCAREREFRESH,
- TAG_DONE);
-
- UnlockPubScreen(NULL, scr);
-
- if (!win)
- exit_prog(20);
- rp = win->RPort;
- border_left = win->BorderLeft + xstretch;
- border_top = win->BorderTop + ystretch;
- }
-
-
- void close_window(void)
- {
- CloseWindow(win);
- }
-
-
- void notify(char *str)
- {
- struct EasyStruct es;
-
- es.es_StructSize = sizeof(struct EasyStruct);
- es.es_Flags = 0;
- es.es_Title = getcatalogstr(catalog,
- MSG_EASYREQ_RESULT_TITLE,
- MSG_EASYREQ_RESULT_TITLE_STR);
- es.es_TextFormat = str;
- es.es_GadgetFormat = getcatalogstr(catalog,
- MSG_EASYREQ_RESULT_GAD,
- MSG_EASYREQ_RESULT_GAD_STR);
- EasyRequestArgs(NULL, &es, NULL, NULL);
- }
-
-
- #define bleft (border_left + (x * BSIZE * xstretch))
- #define bwidth (border_left + (x * BSIZE * xstretch) + BSIZE * xstretch - 1)
- #define btop (border_top + (y * BSIZE * ystretch))
- #define bheight (border_top + (y * BSIZE * ystretch) + BSIZE * ystretch - 1)
-
- void set_point(int x, int y, int item)
- {
- grid[x][y] = item;
-
- SetAPen(rp, item);
- RectFill(rp, bleft, btop, bwidth, bheight);
- }
-
-
- void clear_grid(void)
- {
- int x, y;
-
- for (x = 0; x < XSIZE; x++)
- for (y = 0; y < YSIZE; y++)
- grid[x][y] = CLEAR;
- SetAPen(rp, CLEAR);
- RectFill(rp,
- border_left, border_top,
- border_left + XSIZE * BSIZE * xstretch,
- border_top + YSIZE * BSIZE * ystretch);
- }
-
-
- void draw_score(void)
- {
- static char str[20];
- char *fmtstr;
-
- fmtstr = getcatalogstr(catalog, MSG_WINDOWTITLE, MSG_WINDOWTITLE_STR);
- sprintf(str, fmtstr, length);
- SetWindowTitles(win, str, (APTR) -1);
- }
-
-
- void pause(void)
- {
- struct IntuiMessage *msg;
-
- for (;;) {
- msg = (struct IntuiMessage *) WaitPort(win->UserPort);
- if (msg->Class == RAWKEY && msg->Code & 0x80)
- ReplyMsg(GetMsg(win->UserPort));
- else
- return;
- }
- }
-
-
- void create_frog(int n)
- {
- int x, y;
-
- do {
- x = RangeRand(XSIZE);
- y = RangeRand(YSIZE);
- } while (grid[x][y] != CLEAR);
- frx[n] = x;
- fry[n] = y;
- set_point(x, y, FROG);
- frogtime[n] = 20 + RangeRand(50);
- }
-
-
- void check_frogs(void)
- {
- int n, x, y;
-
- for (n = 0; n < NFROGS; n++) {
- if (!frogtime[n]--) {
- x = frx[n];
- y = fry[n];
- create_frog(n);
- if (has_obsta)
- set_point(x, y, RangeRand(10) ? CLEAR : BRICK);
- else
- set_point(x, y, CLEAR);
- }
- }
- }
-
-
- void replace_frog(int x, int y)
- {
- int n;
-
- for (n = 0; n < NFROGS; n++) {
- if (frx[n] == x && fry[n] == y)
- create_frog(n);
- }
- set_point(x, y, CLEAR);
- }
-
-
- void setup_game(void)
- {
- int i;
-
- clear_grid();
- length = 1;
- draw_score();
- head = tail = 0;
- snx[0] = XSIZE / 2;
- sny[0] = YSIZE / 2;
- set_point(snx[0], sny[0], SNAKE);
- key = KEY_RIGHT;
- for (i = 0; i < NFROGS; i++)
- create_frog(i);
- draw_score();
- }
-
-
- BOOL play_one_game(void)
- {
- int item;
- struct IntuiMessage *msg;
- ULONG class;
- USHORT code;
- int x, y, nx, ny;
-
- for (;;) {
- Delay(4L);
- WaitTOF();
- while (msg = (struct IntuiMessage *) GetMsg(win->UserPort)) {
- class = msg->Class;
- code = msg->Code;
- ReplyMsg((struct Message *) msg);
- switch (class) {
- case CLOSEWINDOW:
- return (TRUE);
- case RAWKEY:
- if (!(code & 0x80))
- switch (code) {
- case KEY_ESC:
- return TRUE;
- case KEY_Pause:
- pause();
- break;
- case KEY_UP:
- case KEY_DOWN:
- case KEY_LEFT:
- case KEY_RIGHT:
- key = code;
- }
- }
- }
-
- x = snx[head];
- y = sny[head];
- switch (key) {
- case KEY_UP:
- nx = x;
- ny = y - 1;
- break;
- case KEY_DOWN:
- nx = x;
- ny = y + 1;
- break;
- case KEY_LEFT:
- nx = x - 1;
- ny = y;
- break;
- case KEY_RIGHT:
- nx = x + 1;
- ny = y;
- break;
- }
- if (nx < 0 || ny < 0 || nx >= XSIZE || ny >= YSIZE)
- return FALSE;
- item = grid[nx][ny];
- switch (item) {
- case FROG:
- grow += 2 + RangeRand(10);
- replace_frog(nx, ny);
- case CLEAR:
- head = (head + 1) % 1500;
- snx[head] = nx;
- sny[head] = ny;
- set_point(nx, ny, SNAKE);
- if (grow) {
- grow--;
- length++;
- draw_score();
- } else {
- set_point(snx[tail], sny[tail], CLEAR);
- tail = (tail + 1) % 1500;
- }
- break;
- case SNAKE:
- case BRICK:
- return (FALSE);
- }
- check_frogs();
- }
- }
-
-
- void play_game(void)
- {
- char str[80], *fmtstr;
-
- for (;;) {
- setup_game();
- WaitPort(win->UserPort);
- if (play_one_game())
- return;
- fmtstr = getcatalogstr(catalog,
- MSG_EASYREQ_RESULT,
- MSG_EASYREQ_RESULT_STR);
- sprintf(str, fmtstr, length);
- notify(str);
- }
- }
-
-
- void randomize(void)
- {
- extern ULONG __far RangeSeed;
- time_t t;
-
- time(&t);
- RangeSeed = (ULONG) t;
- }
-
-
- char *getcatalogstr(struct Catalog *catalog, LONG strNum, char *defStr)
- {
- if (LocaleBase)
- return (char *) GetCatalogStr(catalog, strNum, defStr);
- else
- return defStr;
- }
-
-
- void main(void)
- {
- BYTE old_pri;
- struct TagItem OC_tags[2];
-
- OC_tags[0].ti_Tag = OC_BuiltInLanguage;
- OC_tags[0].ti_Data = (ULONG) "dansk";
- OC_tags[1].ti_Tag = TAG_DONE;
- OC_tags[1].ti_Data = NULL;
-
- if (IntuitionBase = OpenLibrary("intuition.library", 37L))
- if (GfxBase = OpenLibrary("graphics.library", 37L)) {
- LocaleBase = (struct LocaleBase *) OpenLibrary("locale.library", 38L);
- if (LocaleBase)
- catalog = OpenCatalogA(NULL, "Orm.catalog", OC_tags);
- randomize();
- open_window();
- old_pri = SetTaskPri(FindTask(0), PRIORITY);
- play_game();
- close_window();
- SetTaskPri(FindTask(0), old_pri);
- if (LocaleBase)
- CloseCatalog(catalog);
- }
- exit_prog(0);
- }
- @
-
-
- 1.3
- log
- @Localized. Defaults to builtin Danish strings if
- locale.library isn't available; thus, it still runs
- under AmigaDOS 2.04 (V37).
- @
- text
- @d16 5
- d33 2
- a34 2
- char RCSid[] = "$Id: Orm.c,v 1.2 93/10/12 18:03:27 Torsten Exp Locker: Torsten $";
- char version[] = "$VER: Orm 1.3 (19.10.93)";
- d93 1
- a93 1
- #define KEY_P 25
- d366 1
- a366 1
- case KEY_P:
- @
-
-
- 1.2
- log
- @Added function prototypes.
- Can be compiled with either DICE or SAS/C 5.10.
- Tries to be smart about the screen display mode.
- Obstacles disabled on two-colour screens.
- Knows about window border sizes.
- @
- text
- @d16 7
- d28 2
- a29 2
- char RCSid[] = "$Id: Orm.c,v 1.1 93/10/12 00:04:41 Torsten Exp Locker: Torsten $";
- char version[] = "$VER: Orm 1.2 (12.10.93)";
- d34 1
- d39 1
- d44 1
- d63 1
- a75 2
- #define SCR_TITLE1 "Orm til Amiga - 1993"
- #define SCR_TITLE2 "Orm til Amiga - 1993 (uden forhindringer)"
- d110 1
- a110 1
-
- d112 3
- a114 1
- extern APTR IntuitionBase, GfxBase;
- d129 1
- d140 1
- d169 5
- d178 1
- a178 1
- WA_ScreenTitle, (has_obsta?SCR_TITLE1:SCR_TITLE2),
- d210 3
- a212 1
- es.es_Title = "Resultat";
- d214 3
- a216 1
- es.es_GadgetFormat = "OK";
- d253 1
- d255 2
- a256 1
- sprintf(str, "Længde: %d", length);
- d425 1
- a425 1
- char str[80];
- d432 4
- a435 1
- sprintf(str, "Ormen blev %d led lang", length);
- d451 9
- d463 1
- d465 5
- d472 3
- d481 3
- a483 1
- }
- @
-
-
- 1.1
- log
- @This is my first version of Michael's game.
- @
- text
- @d15 4
- a18 1
- * $Log$
- d21 4
- a26 1
- #include <graphics/gfx.h>
- d29 1
- d31 10
- a40 1
- #include <clib/alib_protos.h>
- d42 7
- d52 1
- d55 6
- a60 1
- #else
- d62 1
- d65 2
- a66 5
- char RCSid[] = "$Id$";
- char version[] = "$VER: Orm 1.1 (11.10.93)";
- char copyright[] = "$COPYRIGHT:*PUBLIC DOMAIN* - M. Warner & T. Poulin$";
-
- #define SCR_TITLE "Orm til Amiga - 1993"
- d69 1
- d85 18
- a103 1
-
- d106 9
- a114 1
- BOOL stretch;
- d127 9
- d137 18
- a154 3
- scr = LockPubScreen(NULL);
- stretch = scr->Height >= 400; /* this is a hack */
- UnlockPubScreen(NULL, scr);
- d157 2
- a158 2
- WA_Width, 251,
- WA_Height, stretch ? 256 : 135,
- d160 1
- a160 1
- WA_ScreenTitle, SCR_TITLE,
- d169 3
- d173 1
- a173 1
- exit_prog(10);
- d175 2
- d192 1
- a192 1
- es.es_Title = "Orm: Resultat";
- d199 4
- a202 4
- UBYTE grid[XSIZE][YSIZE];
- UBYTE snx[1500], sny[1500], frx[NFROGS], fry[NFROGS];
- int dir, length, head, tail, grow, frogtime[NFROGS];
- UBYTE key;
- d209 1
- a209 4
- if (stretch)
- RectFill(rp, 5 + x * 6, 12 + y * 6, 5 + x * 6 + 5, 12 + y * 6 + 5);
- else
- RectFill(rp, 5 + x * 6, 12 + y * 3, 5 + x * 6 + 5, 12 + y * 3 + 2);
- d221 4
- a224 1
- RectFill(rp, 5, 12, 5 + XSIZE * 6, 12 + YSIZE * (stretch ? 6 : 3));
- d275 4
- a278 1
- set_point(x, y, RangeRand(10) ? CLEAR : BRICK);
- d408 1
- a408 1
- sprintf(str, "Du blev %d led lang", length);
- d417 1
- a417 1
- int clk[2];
- d419 2
- a420 2
- timer(clk);
- RangeSeed = clk[0] + clk[1];
- @
-