comparison smessage.c @ 3:5a977ccbc7a9 default tip

Empty changelog
author darius
date Sat, 06 Dec 1997 05:41:29 +0000
parents
children
comparison
equal deleted inserted replaced
2:fba0b6e6cdc7 3:5a977ccbc7a9
1 /* $Id: smessage.c,v 1.1.1.1 1997/12/06 05:41:31 darius Exp $ */
2
3 /*
4 * smessage.c
5 */
6 #include "copyright.h"
7
8 #include <stdio.h>
9 #include <math.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include "Wlib.h"
14 #include "defs.h"
15 #include "struct.h"
16 #include "data.h"
17 #include "proto.h"
18 #include "gameconf.h"
19
20 #define ADDRLEN 10
21 #define M_XOFF 5
22 #ifndef AMIGA
23 #define M_YOFF 5
24 #else
25 #define M_YOFF 1
26 #endif
27
28 #define MSGLEN 80
29
30 /* XFIX */
31 #define BLANKCHAR(col, n) W_ClearArea(messagew, M_XOFF+W_Textwidth*(col), \
32 M_YOFF, W_Textwidth * (n), W_Textheight);
33 #define DRAWCURSOR(col) W_WriteText(messagew, M_XOFF+W_Textwidth*(col), \
34 M_YOFF, textColor, &cursor, 1, W_RegularFont);
35
36 static int lcount;
37 static char buf[MSGLEN];
38 static char cursor = '_';
39
40 char addr, *addr_str;
41
42 /* Prototypes */
43 static void smessage_first P((int ichar));
44 /*static*/ char *getaddr P((int who));
45 /*static*/ char *getaddr2 P((int flags, int recip));
46
47 void
48 message_expose()
49 {
50 if (!messpend)
51 return;
52
53 W_WriteText(messagew, M_XOFF, M_YOFF, textColor, addr_str,
54 strlen(addr_str), W_RegularFont);
55 W_WriteText(messagew, M_XOFF + ADDRLEN * W_Textwidth, M_YOFF, textColor,
56 buf, lcount - ADDRLEN, W_RegularFont);
57 DRAWCURSOR(lcount);
58 }
59
60 void
61 smessage_ahead(head, ichar)
62 char head;
63 char ichar;
64 {
65 if (messpend == 0) {
66 smessage_first(head);
67 }
68 smessage(ichar);
69 }
70
71 static void
72 smessage_first(ichar)
73 char ichar;
74 {
75 messpend = 1;
76
77 /* clear out message window in case messages went there */
78 W_ClearWindow(messagew);
79 if (mdisplayed) {
80 BLANKCHAR(0, lastcount);
81 mdisplayed = 0;
82 }
83 /* Put the proper recipient in the window */
84 if (/*(ichar == 't') || */
85 /* that's a player number! */
86 /* YUCK! */
87 (ichar == 'T'))
88 addr = teaminfo[me->p_teami].letter;
89 else
90 addr = ichar;
91 addr_str = getaddr(addr);
92 if (addr_str == 0) {
93 /* print error message */
94 messpend = 0;
95 #ifdef NOWARP
96 message_off();
97 #else
98 W_WarpPointer(NULL);
99 #endif
100 } else {
101 W_WriteText(messagew, M_XOFF, M_YOFF, textColor, addr_str,
102 strlen(addr_str), W_RegularFont);
103 lcount = ADDRLEN;
104 DRAWCURSOR(ADDRLEN);
105 }
106 }
107
108 void
109 smessage(ichar)
110 int ichar;
111 {
112 register int i;
113 char twochar[2], *delim;
114
115 if (messpend == 0) {
116 if(lowercaset && ichar == 't')
117 ichar='T';
118 else if(lowercaset && (ichar == 'T'))
119 ichar='t';
120 smessage_first(ichar);
121 return;
122 }
123 switch (ichar) {
124 case '\b': /* character erase */
125 case '\177':
126 if (--lcount < ADDRLEN) {
127 lcount = ADDRLEN;
128 break;
129 }
130 BLANKCHAR(lcount + 1, 1);
131 DRAWCURSOR(lcount);
132 break;
133
134 case 'w'+256: /* word erase */
135 i = 0;
136 /* back up over blanks */
137 while (--lcount >= ADDRLEN &&
138 isspace((unsigned char) buf[lcount - ADDRLEN] & ~(0x80)))
139 i++;
140 lcount++;
141 /* back up over non-blanks */
142 while (--lcount >= ADDRLEN &&
143 !isspace((unsigned char) buf[lcount - ADDRLEN] & ~(0x80)))
144 i++;
145 lcount++;
146
147 if (i > 0) {
148 BLANKCHAR(lcount, i + 1);
149 DRAWCURSOR(lcount);
150 }
151 break;
152
153 case 'u'+256: /* kill line */
154 case 'x'+256:
155 if (lcount > ADDRLEN) {
156 BLANKCHAR(ADDRLEN, lcount - ADDRLEN + 1);
157 lcount = ADDRLEN;
158 DRAWCURSOR(ADDRLEN);
159 }
160 break;
161
162 case '\033': /* abort message */
163 BLANKCHAR(0, lcount + 1);
164 mdisplayed = 0;
165 messpend = 0;
166 #ifdef NOWARP
167 message_off();
168 #else
169 W_WarpPointer(NULL);
170 #endif
171 break;
172
173 case '\r': /* send message */
174 buf[lcount - ADDRLEN] = '\0';
175 messpend = 0;
176 sendCharMessage(buf, addr);
177 BLANKCHAR(0, lcount + 1);
178 mdisplayed = 0;
179 lcount = 0;
180 break;
181
182 default: /* add character */
183 if (lcount >= 79) { /* send mesg and continue mesg */
184 if (addr == 'M')
185 if ((delim = strchr(buf, '>') + 1) == NULL) {
186 W_Beep();
187 break;
188 }
189 else {
190 i = delim - buf;
191 buf[lcount - ADDRLEN + 1] = '\0';
192 sendCharMessage(buf, addr);
193 memset(delim, '\0', sizeof(buf) - i);
194 BLANKCHAR(i, lcount + 1);
195 W_WriteText(messagew, M_XOFF, M_YOFF, textColor,
196 addr_str, strlen(addr_str), W_RegularFont);
197 W_WriteText(messagew, M_XOFF+(strlen(addr_str) +
198 1) * W_Textwidth,
199 M_YOFF, textColor, buf, strlen(buf), W_RegularFont);
200 lcount = i + ADDRLEN;
201 DRAWCURSOR(i + ADDRLEN);
202 }
203 else {
204 buf[lcount - ADDRLEN + 1] = '\0';
205 sendCharMessage(buf, addr);
206 BLANKCHAR(0, lcount + 1);
207 W_WriteText(messagew, M_XOFF, M_YOFF, textColor, addr_str,
208 strlen(addr_str), W_RegularFont);
209 lcount = ADDRLEN;
210 DRAWCURSOR(ADDRLEN);
211 }
212 }
213 if (ichar > 255)
214 break;
215 twochar[0] = ichar;
216 twochar[1] = cursor;
217 W_WriteText(messagew, M_XOFF + W_Textwidth * lcount, M_YOFF,
218 textColor, twochar, 2, W_RegularFont);
219 buf[(lcount++) - ADDRLEN] = ichar;
220 break;
221 }
222 }
223
224 void
225 sendCharMessage(buf, ch)
226 char *buf;
227 int ch;
228 {
229 char tmp[MSGLEN], *delim;
230 int i, count;
231
232 /* uses ch to find out what kind of message it is and then sends
233 it there */
234 switch ((char) ch) {
235 case 'A':
236 pmessage(buf, 0, MALL);
237 break;
238 case 'F':
239 pmessage(buf, FEDm, MTEAM);
240 break;
241 case 'R':
242 pmessage(buf, ROMm, MTEAM);
243 break;
244 case 'K':
245 pmessage(buf, KLIm, MTEAM);
246 break;
247 case 'O':
248 pmessage(buf, ORIm, MTEAM);
249 break;
250 case 'G':
251 pmessage(buf, 0, MGOD);
252 break;
253 #ifdef TOOLS
254 case '!':
255 pmessage(buf, 0, MTOOLS);
256 break;
257 #endif
258 case 'T':
259 pmessage(buf, idx_to_mask(me->p_teami), MTEAM);
260 break;
261 case 'M':
262 /* mcast format: hit M and list of target addresses followed */
263 /* by greater than(>) and message. the entire mesg including */
264 /* addresses will be sent to each address */
265 if((delim = strchr(buf, '>')) != NULL) {
266 count = delim - buf; /* number of addresses */
267 strncpy(tmp, buf, count);
268 if(strchr(tmp, ' ') == NULL)
269 for(i=0; i < count; i++) { /* dont do tmp[0] is M */
270 sendCharMessage(buf, tmp[i]);
271 }
272 }
273 break;
274 case '0':
275 case '1':
276 case '2':
277 case '3':
278 case '4':
279 case '5':
280 case '6':
281 case '7':
282 case '8':
283 case '9':
284 pmessage(buf, ch - '0', MINDIV);
285 break;
286 case 'a':
287 case 'b':
288 case 'c':
289 case 'd':
290 case 'e':
291 case 'f':
292 case 'g':
293 case 'h':
294 case 'i':
295 case 'j':
296 case 'k':
297 case 'l':
298 case 'm':
299 case 'n':
300 case 'o':
301 case 'p':
302 case 'q':
303 case 'r':
304 case 's':
305 case 't':
306 case 'u':
307 case 'v':
308 case 'w':
309 case 'x':
310 case 'y':
311 case 'z':
312 pmessage(buf, ch - 'a' + 10, MINDIV);
313 break;
314 default:
315 {
316 int i;
317 for (i = 0; i < number_of_teams; i++) {
318 if (ch == teaminfo[i].letter)
319 break;
320 }
321 if (i < number_of_teams) {
322 pmessage(buf, idx_to_mask(i), MTEAM);
323 break;
324 }
325 }
326 warning("Not legal recipient");
327 }
328 }
329
330 void
331 pmessage(str, recip, group)
332 char *str;
333 int recip;
334 int group;
335 {
336 char newbuf[100];
337
338 strcpy(lastMessage, str);
339 switch(group) {
340 #ifdef TOOLS
341 case MTOOLS:
342 sendTools(str);
343 break;
344 #endif
345 default:
346 sendMessage(str, group, recip);
347 }
348 if ((group == MTEAM && recip != idx_to_mask(me->p_teami)) ||
349 (group == MINDIV && recip != me->p_no)) {
350 sprintf(newbuf, "%s %s",
351 getaddr2(group, (group == MTEAM) ?
352 mask_to_idx(recip) : recip),
353 str);
354 newbuf[79] = 0;
355 dmessage(newbuf, group, me->p_no, recip);
356 }
357 #ifdef NOWARP
358 message_off();
359 #else
360 W_WarpPointer(NULL);
361 #endif
362 }
363
364 /*static */
365 char *
366 getaddr(who)
367 char who;
368 {
369 switch (who) {
370 case 'A':
371 return (getaddr2(MALL, 0));
372 case 'F':
373 return (getaddr2(MTEAM, FEDi));
374 case 'R':
375 return (getaddr2(MTEAM, ROMi));
376 case 'K':
377 return (getaddr2(MTEAM, KLIi));
378 case 'O':
379 return (getaddr2(MTEAM, ORIi));
380 case 'G':
381 return (getaddr2(MGOD, 0));
382 #ifdef TOOLS
383 case '!':
384 return (getaddr2(MTOOLS, 0));
385 #endif
386 case 'M':
387 return (getaddr2(MCAST, 0));;
388 case '0':
389 case '1':
390 case '2':
391 case '3':
392 case '4':
393 case '5':
394 case '6':
395 case '7':
396 case '8':
397 case '9':
398 if (isPlaying(&players[who - '0'])) {
399 return (getaddr2(MINDIV, who - '0'));
400 } else {
401 warning("Player is not in game");
402 return (0);
403 }
404 break;
405 case 'a':
406 case 'b':
407 case 'c':
408 case 'd':
409 case 'e':
410 case 'f':
411 case 'g':
412 case 'h':
413 case 'i':
414 case 'j':
415 case 'k':
416 case 'l':
417 case 'm':
418 case 'n':
419 case 'o':
420 case 'p':
421 case 'q':
422 case 'r':
423 case 's':
424 case 't':
425 case 'u':
426 case 'v':
427 case 'w':
428 case 'x':
429 case 'y':
430 case 'z':
431 if (who - 'a' + 10 > nplayers) {
432 warning("Invalid player number");
433 return (0);
434 } else if (isPlaying(&players[who - 'a' + 10])) {
435 return (getaddr2(MINDIV, who - 'a' + 10));
436 } else {
437 warning("Player is not in game");
438 return (0);
439 }
440 break;
441 default:
442 {
443 int i;
444 for (i = 0; i < number_of_teams; i++) {
445 if (addr == teaminfo[i].letter)
446 break;
447 }
448 if (i < number_of_teams) {
449 return getaddr2(MTEAM, i);
450 }
451 }
452 warning("Not legal recipient");
453 return (0);
454 }
455 }
456
457 /*static*/
458 char *
459 getaddr2(flags, recip)
460 int flags;
461 int recip;
462 {
463 static char addrmesg[ADDRLEN];
464
465 (void) sprintf(addrmesg, " %c%c->", teaminfo[me->p_teami].letter, shipnos[me->p_no]);
466 switch (flags) {
467 case MALL:
468 (void) sprintf(&addrmesg[5], "ALL");
469 break;
470 case MTEAM:
471 (void) sprintf(&addrmesg[5], teaminfo[recip].shortname);
472 break;
473 case MINDIV:
474 (void) sprintf(&addrmesg[5], "%c%c ",
475 teaminfo[players[recip].p_teami].letter, shipnos[recip]);
476 break;
477 case MGOD:
478 (void) sprintf(&addrmesg[5], "GOD");
479 break;
480 #ifdef TOOLS
481 case MTOOLS:
482 (void) sprintf(addrmesg, " Shell>");
483 break;
484 #endif
485 case MCAST:
486 (void) sprintf(&addrmesg[5], "MCAS");
487 }
488
489 return (addrmesg);
490 }
491
492 /* Used in NEWMACRO, useful elsewhere also */
493 int
494 getgroup(addr, recip)
495 char addr;
496 int *recip;
497 {
498 *recip = 0;
499
500 switch (addr) {
501 case 'A':
502 *recip = 0;
503 return (MALL);
504 break;
505 case 'T': /* had to add this...why didn't COW-lite need
506 it?? -JR */
507 *recip = idx_to_mask(me->p_teami);
508 return (MTEAM);
509 break;
510 case 'F':
511 *recip = FEDm;
512 return (MTEAM);
513 break;
514 case 'R':
515 *recip = ROMm;
516 return (MTEAM);
517 break;
518 case 'K':
519 *recip = KLIm;
520 return (MTEAM);
521 break;
522 case 'O':
523 *recip = ORIm;
524 return (MTEAM);
525 break;
526 case 'G':
527 *recip = 0;
528 return (MGOD);
529 break;
530 #ifdef TOOLS
531 case '!':
532 *recip = 0;
533 return (MTOOLS);
534 break;
535 #endif
536 case 'M':
537 *recip = 0;
538 return (MMOO);
539 break;
540 case '0':
541 case '1':
542 case '2':
543 case '3':
544 case '4':
545 case '5':
546 case '6':
547 case '7':
548 case '8':
549 case '9':
550 if (players[addr - '0'].p_status == PFREE) {
551 warning("That player left the game. message not sent.");
552 return 0;
553 }
554 *recip = addr - '0';
555 return (MINDIV);
556 break;
557 case 'a':
558 case 'b':
559 case 'c':
560 case 'd':
561 case 'e':
562 case 'f':
563 case 'g':
564 case 'h':
565 case 'i':
566 case 'j':
567 case 'k':
568 case 'l':
569 case 'm':
570 case 'n':
571 case 'o':
572 case 'p':
573 case 'q':
574 case 'r':
575 case 's':
576 case 't':
577 case 'u':
578 case 'v':
579 case 'w':
580 case 'x':
581 case 'y':
582 case 'z':
583 if (players[addr - 'a' + 10].p_status == PFREE) {
584 warning("That player left the game. message not sent.");
585 return 0;
586 }
587 *recip = addr - 'a' + 10;
588 return (MINDIV);
589 break;
590 default:
591 warning("Not legal recipient");
592 }
593 return 0;
594 }
595
596
597
598
599
600 /*-------------------------------EMERGENCY--------------------------------*/
601 /* This function sends a distress message out to the player's team. */
602 void
603 emergency()
604 {
605 char ebuf[120]; /* to sprintf into */
606 char buf2[20];
607
608 sprintf(ebuf, "Distress %c%c", /* get team and end */
609 teaminfo[me->p_teami].letter, shipnos[me->p_no]);
610 switch (myship->s_type) {
611 case STARBASE:
612 strcat(ebuf, " (Starbase): ");
613 break;
614 case WARBASE:
615 strcat(ebuf, " (Warbase): ");
616 break;
617 case JUMPSHIP:
618 strcat(ebuf, " (Jumpship): ");
619 break;
620 default:
621 strcat(ebuf, ": ");
622 }
623 if (me->p_damage != 0) {
624 if (me->p_damage > me->p_ship->s_maxdamage - 30)
625 sprintf(buf2, "DEAD MAN ");
626 else
627 sprintf(buf2, "damg: %d%% ", (100 * me->p_damage) / me->p_ship->s_maxdamage);
628 strcat(ebuf, buf2);
629 }
630 if (me->p_shield < me->p_ship->s_maxshield) {
631 if (me->p_shield < 5)
632 sprintf(buf2, "NO SHIELDS ");
633 else
634 sprintf(buf2, "shld: %d%% ", (100 * me->p_shield) / me->p_ship->s_maxshield);
635 strcat(ebuf, buf2);
636 }
637 if (me->p_wtemp > 0) {
638 if (me->p_flags & PFWEP)
639 sprintf(buf2, "WTEMP ");
640 else
641 sprintf(buf2, "W %d%% ", (100 * me->p_wtemp) / me->p_ship->s_maxwpntemp);
642 strcat(ebuf, buf2);
643 }
644 if (me->p_etemp > 0) {
645 if (me->p_flags & PFENG)
646 sprintf(buf2, "ETEMP ");
647 else
648 sprintf(buf2, "E %d%% ", (100 * me->p_etemp) / me->p_ship->s_maxegntemp);
649 strcat(ebuf, buf2);
650 }
651 if (me->p_fuel < me->p_ship->s_maxfuel) {
652 if (me->p_fuel < 400)
653 sprintf(buf2, "NO FUEL ");
654 else
655 sprintf(buf2, "F %d%% ", (100 * me->p_fuel) / me->p_ship->s_maxfuel);
656 strcat(ebuf, buf2);
657 }
658 if ((int) strlen(ebuf) < 12)
659 strcat(ebuf, " perfect health ");
660 if (me->p_armies > 0) {
661 sprintf(buf2, "%d ARMIES!", me->p_armies);
662 strcat(ebuf, buf2);
663 }
664 pmessage(ebuf, idx_to_mask(me->p_teami), MTEAM);
665 }
666
667
668
669 void
670 carry_report()
671 {
672 char ebuf[MSGLEN], *pntr;
673 double dist, closedist;
674 struct planet *k, *p = NULL;
675
676 closedist = blk_gwidth;
677
678 for (k = &planets[0]; k < &planets[nplanets]; k++) {
679 dist = hypot((double) (me->p_x - k->pl_x),
680 (double) (me->p_y - k->pl_y));
681 if (dist < closedist) {
682 p = k;
683 closedist = dist;
684 }
685 }
686 if (myship->s_type == STARBASE)
687 sprintf(ebuf, "Your Starbase is carrying %d armies. ", me->p_armies);
688 else
689 sprintf(ebuf, "I am carrying %d armies. ", me->p_armies);
690 for (pntr = ebuf; *pntr; pntr++);
691 if (paradise) {
692 sprintf(pntr, "Sector: %d-%d ", (me->p_x / GRIDSIZE) + 1,
693 (me->p_y / GRIDSIZE) + 1);
694 for (; *pntr; pntr++);
695 }
696 sprintf(pntr, "%sear %s", (me->p_flags & PFCLOAK) ? "Cloaked n" : "N",
697 p->pl_name);
698 pmessage(ebuf, idx_to_mask(me->p_teami), MTEAM);
699 }
700
701 #ifdef NOWARP
702 void
703 message_on()
704 {
705 if (warp) {
706 W_WarpPointer(messagew);
707 }
708 #ifdef TCURSORS
709 else {
710 messageon = 1;
711 W_DefineTextCursor(w);
712 W_DefineTextCursor(mapw);
713 }
714 #endif /* TCURSORS */
715 }
716
717 void
718 message_off()
719 {
720 #ifdef TCURSORS
721 if (!warp) {
722 messageon = 0;
723 W_RevertCursor(w);
724 W_RevertCursor(mapw);
725 }
726 #endif /* TCURSORS */
727 }
728 #endif /* NOWARP */