Mercurial > hg > early-roguelike
comparison rogue5/scmisc.c @ 33:f502bf60e6e4
Import Rogue 5.4 from the Roguelike Restoration Project (r1490)
author | elwin |
---|---|
date | Mon, 24 May 2010 20:10:59 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
32:2dcd75e6a736 | 33:f502bf60e6e4 |
---|---|
1 /* | |
2 * copies of several routines needed for score | |
3 * | |
4 * @(#)smisc.c 4.7 (Berkeley) 02/05/99 | |
5 * | |
6 * Rogue: Exploring the Dungeons of Doom | |
7 * Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman | |
8 * All rights reserved. | |
9 * | |
10 * See the file LICENSE.TXT for full copyright and licensing information. | |
11 */ | |
12 | |
13 # include <stdio.h> | |
14 # include <sys/types.h> | |
15 # include <sys/stat.h> | |
16 # include <ctype.h> | |
17 | |
18 # define TRUE 1 | |
19 # define FALSE 0 | |
20 # define MAXSTR 80 | |
21 # define when break;case | |
22 # define otherwise break;default | |
23 | |
24 typedef struct { | |
25 char *m_name; | |
26 } MONST; | |
27 | |
28 char *s_vowelstr(); | |
29 | |
30 char *lockfile = "/tmp/.fredlock"; | |
31 | |
32 char prbuf[MAXSTR]; /* buffer for sprintfs */ | |
33 | |
34 MONST monsters[] = { | |
35 { "aquator" }, { "bat" }, { "centaur" }, { "dragon" }, { "emu" }, | |
36 { "venus flytrap" }, { "griffin" }, { "hobgoblin" }, { "ice monster" }, | |
37 { "jabberwock" }, { "kobold" }, { "leprechaun" }, { "medusa" }, | |
38 { "nymph" }, { "orc" }, { "phantom" }, { "quasit" }, { "rattlesnake" }, | |
39 { "snake" }, { "troll" }, { "ur-vile" }, { "vampire" }, { "wraith" }, | |
40 { "xeroc" }, { "yeti" }, { "zombie" } | |
41 }; | |
42 | |
43 /* | |
44 * s_lock_sc: | |
45 * lock the score file. If it takes too long, ask the user if | |
46 * they care to wait. Return TRUE if the lock is successful. | |
47 */ | |
48 int | |
49 s_lock_sc(void) | |
50 { | |
51 int cnt; | |
52 struct stat sbuf; | |
53 | |
54 over: | |
55 close(8); /* just in case there are no files left */ | |
56 if (creat(lockfile, 0000) >= 0) | |
57 return TRUE; | |
58 for (cnt = 0; cnt < 5; cnt++) | |
59 { | |
60 md_sleep(1); | |
61 if (creat(lockfile, 0000) >= 0) | |
62 return TRUE; | |
63 } | |
64 if (stat(lockfile, &sbuf) < 0) | |
65 { | |
66 creat(lockfile, 0000); | |
67 return TRUE; | |
68 } | |
69 if (time(NULL) - sbuf.st_mtime > 10) | |
70 { | |
71 if (md_unlink(lockfile) < 0) | |
72 return FALSE; | |
73 goto over; | |
74 } | |
75 else | |
76 { | |
77 printf("The score file is very busy. Do you want to wait longer\n"); | |
78 printf("for it to become free so your score can get posted?\n"); | |
79 printf("If so, type \"y\"\n"); | |
80 (void) fgets(prbuf, MAXSTR, stdin); | |
81 if (prbuf[0] == 'y') | |
82 for (;;) | |
83 { | |
84 if (creat(lockfile, 0000) >= 0) | |
85 return TRUE; | |
86 if (stat(lockfile, &sbuf) < 0) | |
87 { | |
88 creat(lockfile, 0000); | |
89 return TRUE; | |
90 } | |
91 if (time(NULL) - sbuf.st_mtime > 10) | |
92 { | |
93 if (md_unlink(lockfile) < 0) | |
94 return FALSE; | |
95 } | |
96 md_sleep(1); | |
97 } | |
98 else | |
99 return FALSE; | |
100 } | |
101 } | |
102 | |
103 /* | |
104 * s_unlock_sc: | |
105 * Unlock the score file | |
106 */ | |
107 void | |
108 s_unlock_sc(void) | |
109 { | |
110 md_unlink(lockfile); | |
111 } | |
112 | |
113 /* | |
114 * s_encwrite: | |
115 * Perform an encrypted write | |
116 */ | |
117 void | |
118 s_encwrite(char *start, size_t size, FILE *outf) | |
119 { | |
120 char *e1, *e2, fb; | |
121 int temp; | |
122 | |
123 e1 = encstr; | |
124 e2 = statlist; | |
125 fb = 0; | |
126 | |
127 while (size--) | |
128 { | |
129 putc(*start++ ^ *e1 ^ *e2 ^ fb, outf); | |
130 temp = *e1++; | |
131 fb += temp * *e2++; | |
132 if (*e1 == '\0') | |
133 e1 = encstr; | |
134 if (*e2 == '\0') | |
135 e2 = statlist; | |
136 } | |
137 } | |
138 | |
139 /* | |
140 * s_encread: | |
141 * Perform an encrypted read | |
142 */ | |
143 | |
144 s_encread(char *start, size_t size, int inf) | |
145 { | |
146 char *e1, *e2, fb; | |
147 int temp; | |
148 int read_size; | |
149 | |
150 fb = 0; | |
151 | |
152 if ((read_size = read(inf, start, size)) == 0 || read_size == -1) | |
153 return; | |
154 | |
155 e1 = encstr; | |
156 e2 = statlist; | |
157 | |
158 while (size--) | |
159 { | |
160 *start++ ^= *e1 ^ *e2 ^ fb; | |
161 temp = *e1++; | |
162 fb += temp * *e2++; | |
163 if (*e1 == '\0') | |
164 e1 = encstr; | |
165 if (*e2 == '\0') | |
166 e2 = statlist; | |
167 } | |
168 } | |
169 | |
170 /* | |
171 * s_killname: | |
172 * Convert a code to a monster name | |
173 */ | |
174 char * | |
175 s_killname(int monst, int doart) | |
176 { | |
177 char *sp; | |
178 int article; | |
179 | |
180 article = TRUE; | |
181 switch (monst) | |
182 { | |
183 case 'a': | |
184 sp = "arrow"; | |
185 when 'b': | |
186 sp = "bolt"; | |
187 when 'd': | |
188 sp = "dart"; | |
189 when 's': | |
190 sp = "starvation"; | |
191 article = FALSE; | |
192 when 'h': | |
193 sp = "hypothermia"; | |
194 article = FALSE; | |
195 otherwise: | |
196 if (isupper(monst)) | |
197 sp = monsters[monst-'A'].m_name; | |
198 else | |
199 { | |
200 sp = "God"; | |
201 article = FALSE; | |
202 } | |
203 } | |
204 if (doart && article) | |
205 sprintf(prbuf, "a%s ", s_vowelstr(sp)); | |
206 else | |
207 prbuf[0] = '\0'; | |
208 strcat(prbuf, sp); | |
209 return prbuf; | |
210 } | |
211 | |
212 /* | |
213 * s_vowelstr: | |
214 * For printfs: if string starts with a vowel, return "n" for an | |
215 * "an". | |
216 */ | |
217 char * | |
218 s_vowelstr(char *str) | |
219 { | |
220 switch (*str) | |
221 { | |
222 case 'a': case 'A': | |
223 case 'e': case 'E': | |
224 case 'i': case 'I': | |
225 case 'o': case 'O': | |
226 case 'u': case 'U': | |
227 return "n"; | |
228 default: | |
229 return ""; | |
230 } | |
231 } |