home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / games / volume15 / gtetris / part02 < prev    next >
Encoding:
Internet Message Format  |  1993-01-26  |  10.5 KB

  1. Path: uunet!ogicse!zephyr.ens.tek.com!master!saab!billr
  2. From: billr@saab.CNA.TEK.COM (Bill Randle)
  3. Newsgroups: comp.sources.games
  4. Subject: v15i002:  gtetris -  Generic Tetris for X11, Part02/02
  5. Message-ID: <3646@master.CNA.TEK.COM>
  6. Date: 23 Sep 92 19:13:02 GMT
  7. Article-I.D.: master.3646
  8. Sender: news@master.CNA.TEK.COM
  9. Lines: 394
  10. Approved: billr@saab.CNA.TEK.COM
  11.  
  12. Submitted-by: "Qiang Alex Zhao" <azhao@cs.arizona.edu>
  13. Posting-number: Volume 15, Issue 2
  14. Archive-name: gtetris/Part02
  15. Environment: X11R4/5, Xlib
  16.  
  17.  
  18.  
  19. #! /bin/sh
  20. # This is a shell archive.  Remove anything before this line, then unpack
  21. # it by saving it into a file and typing "sh file".  To overwrite existing
  22. # files, type "sh file -c".  You can also feed this as standard input via
  23. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  24. # will see the following message at the end:
  25. #        "End of archive 2 (of 2)."
  26. # Contents:  Makefile die.c tscores.c
  27. # Wrapped by billr@saab on Wed Sep 23 12:10:55 1992
  28. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  29. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  30.   echo shar: Will not clobber existing file \"'Makefile'\"
  31. else
  32. echo shar: Extracting \"'Makefile'\" \(2093 characters\)
  33. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  34. X# GENERIC X-WINDOW-BASED TETRIS
  35. X#
  36. X#    Makefile
  37. X#
  38. X###
  39. X#
  40. X#  Copyright (C) 1992    Qiang Alex Zhao
  41. X#            Computer Science Dept, University of Arizona
  42. X#            azhao@cs.arizona.edu
  43. X#
  44. X#            All Rights Reserved
  45. X#
  46. X#  Permission to use, copy, modify, and distribute this software and
  47. X#  its documentation for any purpose and without fee is hereby granted,
  48. X#  provided that the above copyright notice appear in all copies and
  49. X#  that both that copyright notice and this permission notice appear in
  50. X#  supporting documentation, and that the name of the author not be
  51. X#  used in advertising or publicity pertaining to distribution of the
  52. X#  software without specific, written prior permission.
  53. X#
  54. X#  This program is distributed in the hope that it will be "playable",
  55. X#  but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. X#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  57. X#
  58. X
  59. X
  60. X### Configurable Settings
  61. X
  62. X# destination directory
  63. XDEST        = /r/cas/usr/azhao/pub
  64. X
  65. X# score file
  66. XSCOREFILE    = $(DEST)/.tetris.scores
  67. X
  68. X# path to X header files
  69. XXINCLUDE    = /usr/include/X11
  70. X
  71. X# path to X library files
  72. XXLIBS        = /usr/lib/X11
  73. X
  74. X# the C compiler, used as linker ;o)
  75. XCC        = cc
  76. X
  77. X# optimization flages
  78. XCFLAGS        = -O -I$(XINCLUDE) -DSCOREFILE='"$(SCOREFILE)"'
  79. XLDFLAGS        = -O -L$(XLIBS)
  80. X
  81. X### Fixed settings
  82. X
  83. XMAKEFILE    = Makefile
  84. X
  85. XPROGT        = tetris
  86. XPROGS        = tscores
  87. X
  88. XHDRS        = tetris.h \
  89. X          data.h
  90. X
  91. XLDLIBS        = -lX11 -lm
  92. X
  93. XSRCS        = tetris.c \
  94. X          die.c \
  95. X          tscores.c
  96. X
  97. XOBJT        = tetris.o \
  98. X          die.o
  99. X
  100. XOBJS        = tscores.o
  101. X
  102. XPRINT        = enscript -2rG
  103. X
  104. X
  105. X### Dependencies/Actions
  106. X
  107. Xall:        $(PROGT) $(PROGS)
  108. X
  109. X$(PROGT):    $(OBJT)
  110. X    @echo -n "Linking $(PROGT) ... "
  111. X    @$(CC) $(LDFLAGS) $(OBJT) $(LDLIBS) -o $(PROGT)
  112. X    @echo "Done."
  113. X
  114. X$(PROGS):    $(OBJS)
  115. X    @echo -n "Linking $(PROGS) ... "
  116. X    @$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(PROGS)
  117. X    @echo "Done."
  118. X
  119. Xclean:
  120. X    rm -rf *.o $(PROGT) $(PROGS)
  121. X
  122. Xinstall:    $(PROGT) $(PROGS)
  123. X    @echo Installing $(PROGT) and $(PROGS) in $(DEST)
  124. X    install -s $(PROGT) $(PROGS) $(DEST)
  125. X
  126. Xprint:
  127. X    $(PRINT) $(HDRS) $(SRCS)
  128. X
  129. X### dependencies for each object
  130. Xtetris.o:    tetris.c tetris.h data.h
  131. Xdie.o:        die.c tetris.h
  132. Xtscore.o:    tscore.c tetris.h
  133. X
  134. END_OF_FILE
  135. if test 2093 -ne `wc -c <'Makefile'`; then
  136.     echo shar: \"'Makefile'\" unpacked with wrong size!
  137. fi
  138. # end of 'Makefile'
  139. fi
  140. if test -f 'die.c' -a "${1}" != "-c" ; then 
  141.   echo shar: Will not clobber existing file \"'die.c'\"
  142. else
  143. echo shar: Extracting \"'die.c'\" \(4448 characters\)
  144. sed "s/^X//" >'die.c' <<'END_OF_FILE'
  145. X/*
  146. X# GENERIC X-WINDOW-BASED TETRIS
  147. X#
  148. X#    die.c
  149. X#
  150. X###
  151. X#
  152. X#  Copyright (C) 1992    Qiang Alex Zhao
  153. X#            Computer Science Dept, University of Arizona
  154. X#            azhao@cs.arizona.edu
  155. X#
  156. X#            All Rights Reserved
  157. X#
  158. X#  Permission to use, copy, modify, and distribute this software and
  159. X#  its documentation for any purpose and without fee is hereby granted,
  160. X#  provided that the above copyright notice appear in all copies and
  161. X#  that both that copyright notice and this permission notice appear in
  162. X#  supporting documentation, and that the name of the author not be
  163. X#  used in advertising or publicity pertaining to distribution of the
  164. X#  software without specific, written prior permission.
  165. X#
  166. X#  This program is distributed in the hope that it will be "playable",
  167. X#  but WITHOUT ANY WARRANTY; without even the implied warranty of
  168. X#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  169. X#
  170. X*/
  171. X
  172. X#include "tetris.h"
  173. X
  174. Xvoid
  175. Xbanner(field, str)
  176. X    field_t        *field;
  177. X    char           *str;
  178. X{
  179. X    int             mlen = strlen(str);
  180. X    int             x, y, w, ascent, descent;
  181. X
  182. X    ascent = field->tfont->ascent;
  183. X    descent = field->tfont->descent;
  184. X    w = XTextWidth(field->tfont, str, mlen);
  185. X    x = (field->winwidth - w) / 2;
  186. X    y = (field->winheight - ascent - descent) / 2;
  187. X
  188. X    XFillRectangle(disp, field->win, gc_w2, x - 5, y - ascent - 5,
  189. X    w + 10, ascent + descent + 10);
  190. X    XDrawString(disp, field->win, gc_ttx, x, y, str, mlen);
  191. X}
  192. X
  193. Xvoid
  194. Xdie(field)
  195. X    field_t        *field;
  196. X{
  197. X    time_t          tloc;
  198. X    score_t         myscore;
  199. X    struct passwd  *tmp;
  200. X
  201. X    time(&tloc);
  202. X
  203. X    banner(field, DIE_MESG);
  204. X    XFlush(disp);
  205. X    sleep(1);
  206. X
  207. X    gethostname(myscore.myhost, NAMELEN);
  208. X    myscore.myhost[NAMELEN-1] = '\0';
  209. X    setpwent();
  210. X    tmp = getpwuid(getuid());
  211. X    if (tmp == NULL)        /* should not occur */
  212. X    sprintf(myscore.myname, "%d", getuid());
  213. X    else
  214. X    strncpy(myscore.myname, tmp->pw_name, NAMELEN);
  215. X    endpwent();
  216. X    myscore.myname[NAMELEN-1] = '\0';
  217. X
  218. X    sprintf(myscore.score, "%9d", field->score);
  219. X    sprintf(myscore.level, "%3d", field->level);
  220. X    sprintf(myscore.lines, "%4d", field->lines);
  221. X    strcpy(myscore.mytime, asctime(localtime(&tloc)));
  222. X    fprintf(stderr, "\t%s", myscore.mytime);
  223. X    fprintf(stderr, "Your final score is %d, at level %d with %d raws.\n\n",
  224. X       field->score, field->level, field->lines);
  225. X
  226. X    addHighScore(&myscore, field->score);
  227. X    showHighScores(SHOWNSCORES);
  228. X
  229. X    XCloseDisplay(disp);
  230. X    exit(0);
  231. X}
  232. X
  233. Xvoid 
  234. XaddHighScore(myscore, snum)
  235. X    score_t        *myscore;
  236. X    int             snum;
  237. X{
  238. X    char            buff[2][SCORELEN];
  239. X    int             fd;
  240. X    int             tmp, ptmp, s1, s2;
  241. X    int             mycount = 0;
  242. X    Bool            saved = False, trickle = False;
  243. X
  244. X    fd = open(SCOREFILE, O_CREAT | O_RDWR, 0644);
  245. X    if (fd == -1) {
  246. X    fprintf(stderr, "Cannot write the score-file!\n");
  247. X    return;
  248. X    }
  249. X    flock(fd, LOCK_EX);
  250. X
  251. X    tmp = 0;
  252. X    ptmp = 1;
  253. X    bcopy((char *) myscore, buff[1], SCORELEN);
  254. X
  255. X    while (read(fd, buff[tmp], SCORELEN) == SCORELEN) {
  256. X    sscanf(((score_t *)buff[tmp])->score, " %d", &s1);
  257. X    if (!saved && (s1 <= snum)) {
  258. X        trickle = True;
  259. X        saved = True;
  260. X        mycount++;
  261. X    }
  262. X    if (!strncmp(myscore->myname,
  263. X        ((score_t *)buff[tmp])->myname, NAMELEN)) {
  264. X        mycount++;
  265. X    }
  266. X    /* Then check if we should trickle the score */
  267. X    if (trickle) {
  268. X        lseek(fd, (off_t) -SCORELEN, L_INCR);
  269. X        write(fd, buff[ptmp], SCORELEN);
  270. X        ptmp = tmp;
  271. X        tmp = (tmp + 1) % 2;
  272. X    }
  273. X    /*
  274. X     * As we trickle, we add up records owned by me. Once we hit max,
  275. X     * we throw it away, and stop trickling.
  276. X     */
  277. X    if ((mycount > MAXSCORES) || ((mycount == MAXSCORES) && !trickle)) {
  278. X        trickle = False;
  279. X        break;
  280. X    }
  281. X    }    /* while */
  282. X
  283. X    if (trickle) {
  284. X    write(fd, buff[ptmp], SCORELEN);
  285. X    }
  286. X    if (!saved && (mycount < MAXSCORES)) {
  287. X    write(fd, (char *) myscore, SCORELEN);
  288. X    }
  289. X
  290. X    flock(fd, LOCK_UN);
  291. X    close(fd);
  292. X}
  293. X
  294. Xvoid 
  295. XshowHighScores(ub)
  296. X    int             ub;
  297. X{
  298. X    int             fd, i;
  299. X    score_t         curs;
  300. X
  301. X    fd = open(SCOREFILE, O_RDONLY, 0644);
  302. X    if (fd == -1)
  303. X    return;
  304. X
  305. X    i = 0;
  306. X    fprintf(stderr, " # USER            SCORE   L    R  HOST         DATE\n");
  307. X
  308. X    while (read(fd, (char *) &curs, SCORELEN) == SCORELEN) {
  309. X    i++;
  310. X    if (i<= ub)
  311. X        fprintf(stderr, "%2d %-12s%9s %3s %4s  %-12s %-s",
  312. X        i, curs.myname, curs.score, curs.level, curs.lines,
  313. X        curs.myhost, curs.mytime);
  314. X    }
  315. X    close(fd);
  316. X    fprintf(stderr, "There are %d scores to date\n", i);
  317. X}
  318. X
  319. END_OF_FILE
  320. if test 4448 -ne `wc -c <'die.c'`; then
  321.     echo shar: \"'die.c'\" unpacked with wrong size!
  322. fi
  323. # end of 'die.c'
  324. fi
  325. if test -f 'tscores.c' -a "${1}" != "-c" ; then 
  326.   echo shar: Will not clobber existing file \"'tscores.c'\"
  327. else
  328. echo shar: Extracting \"'tscores.c'\" \(1474 characters\)
  329. sed "s/^X//" >'tscores.c' <<'END_OF_FILE'
  330. X/*
  331. X# GENERIC X-WINDOW-BASED TETRIS
  332. X#
  333. X#    tscores.c
  334. X#
  335. X###
  336. X#
  337. X#  Copyright (C) 1992    Qiang Alex Zhao
  338. X#            Computer Science Dept, University of Arizona
  339. X#            azhao@cs.arizona.edu
  340. X#
  341. X#            All Rights Reserved
  342. X#
  343. X#  Permission to use, copy, modify, and distribute this software and
  344. X#  its documentation for any purpose and without fee is hereby granted,
  345. X#  provided that the above copyright notice appear in all copies and
  346. X#  that both that copyright notice and this permission notice appear in
  347. X#  supporting documentation, and that the name of the author not be
  348. X#  used in advertising or publicity pertaining to distribution of the
  349. X#  software without specific, written prior permission.
  350. X#
  351. X#  This program is distributed in the hope that it will be "playable",
  352. X#  but WITHOUT ANY WARRANTY; without even the implied warranty of
  353. X#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  354. X#
  355. X*/
  356. X
  357. X#include "tetris.h"
  358. X
  359. Xvoid 
  360. Xmain()
  361. X{
  362. X    int             fd, i;
  363. X    score_t         curs;
  364. X
  365. X    fprintf(stderr, "\tGENERIC TETRIS HALL OF FAME\n\n");
  366. X    fd = open(SCOREFILE, O_RDONLY, 0644);
  367. X    if (fd == -1)
  368. X    return;
  369. X
  370. X    i = 0;
  371. X    fprintf(stderr, "   # USER            SCORE   L    R  HOST         DATE\n");
  372. X
  373. X    while (read(fd, (char *) &curs, SCORELEN) == SCORELEN) {
  374. X    i++;
  375. X    fprintf(stderr, "%4d %-12s%9s %3s %4s  %-12s %-s", i,
  376. X        curs.myname, curs.score, curs.level, curs.lines,
  377. X        curs.myhost, curs.mytime);
  378. X    }
  379. X    close(fd);
  380. X    fprintf(stderr, "There are %d scores to date\n", i);
  381. X}
  382. X
  383. END_OF_FILE
  384. if test 1474 -ne `wc -c <'tscores.c'`; then
  385.     echo shar: \"'tscores.c'\" unpacked with wrong size!
  386. fi
  387. # end of 'tscores.c'
  388. fi
  389. echo shar: End of archive 2 \(of 2\).
  390. cp /dev/null ark2isdone
  391. MISSING=""
  392. for I in 1 2 ; do
  393.     if test ! -f ark${I}isdone ; then
  394.     MISSING="${MISSING} ${I}"
  395.     fi
  396. done
  397. if test "${MISSING}" = "" ; then
  398.     echo You have unpacked both archives.
  399.     rm -f ark[1-9]isdone
  400. else
  401.     echo You still need to unpack the following archives:
  402.     echo "        " ${MISSING}
  403. fi
  404. ##  End of shell archive.
  405. exit 0
  406.