~netrek-developers/netrek-client-cow/trunk

« back to all changes in this revision

Viewing changes to smessage.c

  • Committer: Collin Pruitt
  • Date: 2009-05-12 04:30:09 UTC
  • Revision ID: collinp111@gmail.com-20090512043009-3jsjojoyrk16oass
Initial upload - updated from http://james.tooraweenah.com/darcs/netrek-client-cow/ using darcs (hince the existnace of _darcs), fully patched.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * smessage.c
 
3
 */
 
4
#include "config.h"
 
5
#include "copyright.h"
 
6
 
 
7
#include <stdio.h>
 
8
#include <math.h>
 
9
#include <signal.h>
 
10
#include <ctype.h>
 
11
#include "Wlib.h"
 
12
#include "defs.h"
 
13
#include "struct.h"
 
14
#include "data.h"
 
15
#include "packets.h"
 
16
 
 
17
#include "dmessage.h"
 
18
#include "socket.h"
 
19
#include "tools.h"
 
20
#include "warning.h"
 
21
 
 
22
#include "smessage.h"
 
23
 
 
24
static int lcount;
 
25
static int HUDoffset;
 
26
static int talkative = 0;
 
27
static char buf[80];
 
28
static char cursor = '_';
 
29
static char mbuf[80];
 
30
char   *getaddr(char who), *getaddr2(int flags, int recip);
 
31
 
 
32
/* XFIX */
 
33
#define BLANKCHAR(col, n) W_ClearArea(messagew, 5+W_Textwidth*(col), 5, \
 
34
    W_Textwidth * (n), W_Textheight);
 
35
#define DRAWCURSOR(col) W_WriteText(messagew, 5+W_Textwidth*(col), 5, \
 
36
    textColor, &cursor, 1, W_RegularFont);
 
37
#define TESTCAP(c,s)    (c ? strcap(s) : s)
 
38
 
 
39
/* Routines to handle multi-window messaging */
 
40
void    DisplayMessage()
 
41
{
 
42
  int len = strlen(outmessage);
 
43
 
 
44
#ifdef XTRA_MESSAGE_UI
 
45
  if (HUDoffset)
 
46
    W_WriteText(w, 5, HUDoffset, textColor,
 
47
                outmessage, len, W_RegularFont);
 
48
#endif
 
49
 
 
50
  W_ClearWindow(messagew);
 
51
  if (len == 0 && talkative < 5 && !isServerDead()) {
 
52
    W_WriteText(messagew, 5, 5, W_Red,
 
53
                "Talk to everyone, click here, type, press enter.  "
 
54
                "Read and learn.", -1, W_RegularFont);
 
55
  } else {
 
56
    W_WriteText(messagew, 5, 5, textColor,
 
57
                outmessage, len, W_RegularFont);
 
58
  }
 
59
}
 
60
 
 
61
void    AddChar(char *twochar)
 
62
{
 
63
 
 
64
#ifdef XTRA_MESSAGE_UI
 
65
  if (HUDoffset)
 
66
    W_WriteText(w, 5 + W_Textwidth * lcount, HUDoffset, textColor,
 
67
                twochar, 2, W_RegularFont);
 
68
#endif
 
69
 
 
70
  W_WriteText(messagew, 5 + W_Textwidth * lcount, 5, textColor,
 
71
              twochar, 2, W_RegularFont);
 
72
}
 
73
void    BlankChar(int HUDoffsetcol, int len)
 
74
{
 
75
 
 
76
#ifdef XTRA_MESSAGE_UI
 
77
  if (HUDoffset)
 
78
    W_ClearArea(w, 5 + W_Textwidth * (HUDoffsetcol), HUDoffset,
 
79
                W_Textwidth * (len), W_Textheight);
 
80
#endif
 
81
 
 
82
  W_ClearArea(messagew, 5 + W_Textwidth * (HUDoffsetcol), 5,
 
83
              W_Textwidth * (len), W_Textheight);
 
84
}
 
85
void    DrawCursor(int col)
 
86
{
 
87
 
 
88
#ifdef XTRA_MESSAGE_UI
 
89
  if (HUDoffset)
 
90
    W_WriteText(w, 5 + W_Textwidth * (col), HUDoffset,
 
91
                textColor, &cursor, 1, W_RegularFont);
 
92
#endif
 
93
 
 
94
  W_WriteText(messagew, 5 + W_Textwidth * (col), 5,
 
95
              textColor, &cursor, 1, W_RegularFont);
 
96
}
 
97
 
 
98
 
 
99
void    smessage(char ichar)
 
100
{
 
101
  int     i;
 
102
  char   *getaddr(char who);
 
103
  char    twochar[2];
 
104
  static char addr, *addr_str, *pm;
 
105
 
 
106
  if (messpend == 0)
 
107
    {
 
108
      messpend = 1;
 
109
 
 
110
#ifdef XTRA_MESSAGE_UI
 
111
      /* Figure out where to put the message on the local */
 
112
      switch (messageHUD)
 
113
        {
 
114
        case 1:
 
115
          HUDoffset = 5;
 
116
          break;
 
117
        case 2:
 
118
          HUDoffset = W_WindowHeight(w) - W_Textheight - 5;
 
119
          break;
 
120
        default:
 
121
          HUDoffset = 0;
 
122
        }
 
123
#endif
 
124
 
 
125
      /* Put the proper recipient in the window */
 
126
 
 
127
#ifdef TOOLS
 
128
      if (keys[0] != '\0')
 
129
        {
 
130
          if ((pm = INDEX((char *) keys, ichar)))
 
131
            ichar = macroKeys[((int) pm) - ((int) keys)].dest;
 
132
        }
 
133
#endif
 
134
 
 
135
      if ((ichar == 't') || (ichar == 'T'))
 
136
        addr = teamlet[me->p_team];
 
137
      else
 
138
        addr = ichar;
 
139
      addr_str = getaddr(addr);
 
140
      if (addr_str == 0)
 
141
        {
 
142
          /* print error message */
 
143
          messpend = 0;
 
144
          message_off();
 
145
          return;
 
146
        }
 
147
      strcat(outmessage, addr_str);
 
148
      lcount = ADDRLEN;
 
149
      DrawCursor(ADDRLEN);
 
150
      while (strlen(outmessage) < ADDRLEN)
 
151
        {
 
152
          strcat(outmessage, " ");
 
153
        }
 
154
      strcat(outmessage, "_");
 
155
 
 
156
      /* Display the header */
 
157
      DisplayMessage();
 
158
 
 
159
      return;
 
160
    }
 
161
 
 
162
  if (ichar == ((char) ('h' + 96)) || ichar == ((char) ('H' + 96)))
 
163
    ichar = '\b';
 
164
  else if (ichar == ((char) ('[' + 96)))
 
165
    ichar = '\033';
 
166
  else if (ichar == ((char) ('m' + 96)) || ichar == ((char) ('M' + 96)))
 
167
    ichar = '\r';
 
168
  else if (ichar == ((char) ('j' + 96)) || ichar == ((char) ('J' + 96)))
 
169
    ichar = '\r';
 
170
  else if (ichar == ((char) ('u' + 96)) || ichar == ((char) ('U' + 96)))
 
171
    ichar = 23;
 
172
 
 
173
  switch ((unsigned char) ichar & ~(0x80))
 
174
    {
 
175
    case '\b':                                   /* character erase */
 
176
    case '\177':
 
177
      if (--lcount < ADDRLEN)
 
178
        {
 
179
          lcount = ADDRLEN;
 
180
          break;
 
181
        }
 
182
      BlankChar(lcount + 1, 1);
 
183
      DrawCursor(lcount);
 
184
      outmessage[lcount + 1] = '\0';
 
185
      outmessage[lcount] = cursor;
 
186
      break;
 
187
 
 
188
    case '\033':                                 /* abort message */
 
189
      BlankChar(0, lcount + 1);
 
190
      mdisplayed = 0;
 
191
      messpend = 0;
 
192
      message_off();
 
193
      for (i = 0; i < 80; i++)
 
194
        {
 
195
          outmessage[i] = '\0';
 
196
        }
 
197
      DisplayMessage();
 
198
      break;
 
199
 
 
200
    case 23: /* erase to start, but continue in message mode */
 
201
      while (--lcount >= ADDRLEN)
 
202
        {
 
203
          BlankChar(lcount + 1, 1);
 
204
          DrawCursor(lcount);
 
205
          outmessage[lcount + 1] = '\0';
 
206
          outmessage[lcount] = cursor;
 
207
        }
 
208
      lcount = ADDRLEN;
 
209
      break;
 
210
    case '\r':                                   /* send message */
 
211
      talkative++;
 
212
      buf[lcount - ADDRLEN] = '\0';
 
213
      messpend = 0;
 
214
      for (i = 0; i < 80; i++)
 
215
        {
 
216
          outmessage[i] = '\0';
 
217
        }
 
218
      switch (addr)
 
219
        {
 
220
        case 'A':
 
221
          pmessage(buf, 0, MALL);
 
222
          break;
 
223
        case 'F':
 
224
          pmessage(buf, FED, MTEAM);
 
225
          break;
 
226
        case 'R':
 
227
          pmessage(buf, ROM, MTEAM);
 
228
          break;
 
229
        case 'K':
 
230
          pmessage(buf, KLI, MTEAM);
 
231
          break;
 
232
        case 'O':
 
233
          pmessage(buf, ORI, MTEAM);
 
234
          break;
 
235
        case 'G':
 
236
          pmessage(buf, 0, MGOD);
 
237
          break;
 
238
 
 
239
#ifdef TOOLS
 
240
        case '!':
 
241
          pmessage(buf, 0, MTOOLS);
 
242
          break;
 
243
#endif
 
244
 
 
245
        case 'M':
 
246
          pmessage(buf, 0, MMOO);
 
247
          break;
 
248
        case '0':
 
249
        case '1':
 
250
        case '2':
 
251
        case '3':
 
252
        case '4':
 
253
        case '5':
 
254
        case '6':
 
255
        case '7':
 
256
        case '8':
 
257
        case '9':
 
258
          if (players[addr - '0'].p_status == PFREE)
 
259
            {
 
260
              warning("That player left the game. message not sent.");
 
261
              return;
 
262
            }
 
263
          pmessage(buf, addr - '0', MINDIV);
 
264
          break;
 
265
        case 'a':
 
266
        case 'b':
 
267
        case 'c':
 
268
        case 'd':
 
269
        case 'e':
 
270
        case 'f':
 
271
        case 'g':
 
272
        case 'h':
 
273
        case 'i':
 
274
        case 'j':
 
275
        case 'k':
 
276
        case 'l':
 
277
        case 'm':
 
278
        case 'n':
 
279
        case 'o':
 
280
        case 'p':
 
281
        case 'q':
 
282
        case 'r':
 
283
        case 's':
 
284
        case 't':
 
285
        case 'u':
 
286
        case 'v':
 
287
        case 'w':
 
288
        case 'x':
 
289
        case 'y':
 
290
        case 'z':
 
291
          if (players[addr - 'a' + 10].p_status == PFREE)
 
292
            {
 
293
              warning("That player left the game. message not sent.");
 
294
              return;
 
295
            }
 
296
          pmessage(buf, addr - 'a' + 10, MINDIV);
 
297
          break;
 
298
        default:
 
299
          warning("Not legal recipient");
 
300
        }
 
301
      BlankChar(0, lcount + 1);
 
302
      DisplayMessage();
 
303
      mdisplayed = 0;
 
304
      lcount = 0;
 
305
      break;
 
306
 
 
307
    default:                                     /* add character */
 
308
      if (lcount >= 79)
 
309
        {
 
310
          W_Beep();
 
311
          break;
 
312
        }
 
313
      if (iscntrl((unsigned char) ichar & ~(0x80)))
 
314
        break;
 
315
      twochar[0] = ichar;
 
316
      twochar[1] = cursor;
 
317
      AddChar(twochar);
 
318
      outmessage[lcount] = ichar;
 
319
      outmessage[lcount + 1] = cursor;
 
320
      buf[(lcount++) - ADDRLEN] = ichar;
 
321
      break;
 
322
    }
 
323
}
 
324
 
 
325
void pmessage(char *str, int recip, int group)
 
326
{
 
327
  char    newbuf[100];
 
328
 
 
329
  /* message length failsafe and last message saving - jn 6/17/93 */
 
330
  lastMessage[0] = '\0';
 
331
  strncat(lastMessage, str, 79);
 
332
  str = lastMessage;
 
333
 
 
334
  switch (group)
 
335
    {
 
336
 
 
337
#ifdef TOOLS
 
338
    case MTOOLS:
 
339
      sendTools(str);
 
340
      break;
 
341
#endif
 
342
 
 
343
    case MMOO:
 
344
      strcpy(defaultsFile, str);
 
345
      sprintf(mbuf, "changing defaultsFile to %s", str);
 
346
      warning(mbuf);
 
347
      break;
 
348
    default:
 
349
      sendMessage(str, group, recip);
 
350
    }
 
351
 
 
352
  if ((group == MTEAM && recip != me->p_team) ||
 
353
      (group == MINDIV && recip != me->p_no))
 
354
    {
 
355
      sprintf(newbuf, "%s  %s", getaddr2(group, recip), str);
 
356
      newbuf[79] = 0;
 
357
      dmessage(newbuf, group, me->p_no, recip);
 
358
    }
 
359
  message_off();
 
360
}
 
361
 
 
362
char   *
 
363
        getaddr(char who)
 
364
{
 
365
 
 
366
  switch (who)
 
367
    {
 
368
    case 'A':
 
369
      return (getaddr2(MALL, 0));
 
370
    case 'F':
 
371
      return (getaddr2(MTEAM, FED));
 
372
    case 'R':
 
373
      return (getaddr2(MTEAM, ROM));
 
374
    case 'K':
 
375
      return (getaddr2(MTEAM, KLI));
 
376
    case 'O':
 
377
      return (getaddr2(MTEAM, ORI));
 
378
    case 'G':
 
379
      return (getaddr2(MGOD, 0));
 
380
    case 'M':
 
381
      return (getaddr2(MMOO, 0));
 
382
 
 
383
#ifdef TOOLS
 
384
    case '!':
 
385
      return (getaddr2(MTOOLS, 0));
 
386
#endif
 
387
 
 
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 (players[who - '0'].p_status == PFREE)
 
399
        {
 
400
          warning("Slot is not alive.");
 
401
          return 0;
 
402
        }
 
403
      return (getaddr2(MINDIV, who - '0'));
 
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 > MAXPLAYER)
 
432
        {
 
433
          warning("Player is not in game");
 
434
          return (0);
 
435
        }
 
436
      if (players[who - 'a' + 10].p_status == PFREE)
 
437
        {
 
438
          warning("Slot is not alive.");
 
439
          return 0;
 
440
        }
 
441
 
 
442
      return (getaddr2(MINDIV, who - 'a' + 10));
 
443
      break;
 
444
    default:
 
445
      warning("Not legal recipient");
 
446
      return (0);
 
447
    }
 
448
}
 
449
 
 
450
char *getaddr2(int flags, int recip)
 
451
{
 
452
  static char addrmesg[ADDRLEN];
 
453
 
 
454
  (void) sprintf(addrmesg, " %c%c->", teamlet[me->p_team], shipnos[me->p_no]);
 
455
  switch (flags)
 
456
    {
 
457
    case MALL:
 
458
      (void) sprintf(&addrmesg[5], "ALL");
 
459
      break;
 
460
    case MTEAM:
 
461
      (void) sprintf(&addrmesg[5], teamshort[recip]);
 
462
      break;
 
463
    case MINDIV:
 
464
      if (maskrecip)
 
465
        {
 
466
          (void) sprintf(&addrmesg[5], "?? ");
 
467
          maskrecip = 0;
 
468
        }
 
469
      else
 
470
        {
 
471
          /* printf("smessage:getaddr2 recip=%d\n",recip); */
 
472
          (void) sprintf(&addrmesg[5], "%c%c ",
 
473
                         teamlet[players[recip].p_team], shipnos[recip]);
 
474
        }
 
475
 
 
476
      break;
 
477
    case MGOD:
 
478
      (void) sprintf(&addrmesg[5], "GOD");
 
479
      break;
 
480
 
 
481
#ifdef TOOLS
 
482
    case MTOOLS:
 
483
      (void) sprintf(addrmesg, "COW: sh>");
 
484
      break;
 
485
#endif
 
486
 
 
487
    case MMOO:
 
488
      (void) sprintf(&addrmesg[5], "MOO");
 
489
      break;
 
490
    }
 
491
  return (addrmesg);
 
492
}
 
493
 
 
494
void message_on(void)
 
495
{
 
496
  messageon = 1;
 
497
  W_DefineTextCursor(w);
 
498
  W_DefineTextCursor(mapw);
 
499
 
 
500
#ifdef XTRA_MESSAGE_UI
 
501
  messMouseDelta = 0;
 
502
  if (lcount)
 
503
    DisplayMessage();
 
504
#endif
 
505
}
 
506
 
 
507
void message_off(void)
 
508
{
 
509
  messageon = 0;
 
510
  W_DefineLocalcursor(w);
 
511
  W_DefineMapcursor(mapw);
 
512
}
 
513
 
 
514
#ifdef XTRA_MESSAGE_UI
 
515
void message_hold(void)
 
516
{
 
517
  char    twochar[2] =
 
518
  {'#', ' '};
 
519
 
 
520
  AddChar(twochar);
 
521
  message_off();
 
522
}
 
523
#endif
 
524
 
 
525
/* Used in NEWMACRO, useful elsewhere also */
 
526
int
 
527
        getgroup(char addr, int *recip)
 
528
{
 
529
  *recip = 0;
 
530
 
 
531
  switch (addr)
 
532
    {
 
533
    case 'A':
 
534
      *recip = 0;
 
535
      return (MALL);
 
536
      break;
 
537
    case 'F':
 
538
      *recip = FED;
 
539
      return (MTEAM);
 
540
      break;
 
541
    case 'R':
 
542
      *recip = ROM;
 
543
      return (MTEAM);
 
544
      break;
 
545
    case 'K':
 
546
      *recip = KLI;
 
547
      return (MTEAM);
 
548
      break;
 
549
    case 'O':
 
550
      *recip = ORI;
 
551
      return (MTEAM);
 
552
      break;
 
553
    case 'G':
 
554
      *recip = 0;
 
555
      return (MGOD);
 
556
      break;
 
557
 
 
558
#ifdef TOOLS
 
559
    case '!':
 
560
      *recip = 0;
 
561
      return (MTOOLS);
 
562
      break;
 
563
#endif
 
564
 
 
565
    case 'M':
 
566
      *recip = 0;
 
567
      return (MMOO);
 
568
      break;
 
569
    case '0':
 
570
    case '1':
 
571
    case '2':
 
572
    case '3':
 
573
    case '4':
 
574
    case '5':
 
575
    case '6':
 
576
    case '7':
 
577
    case '8':
 
578
    case '9':
 
579
      if (players[addr - '0'].p_status == PFREE)
 
580
        {
 
581
          warning("That player left the game. message not sent.");
 
582
          return 0;
 
583
        }
 
584
      *recip = addr - '0';
 
585
      return (MINDIV);
 
586
      break;
 
587
    case 'a':
 
588
    case 'b':
 
589
    case 'c':
 
590
    case 'd':
 
591
    case 'e':
 
592
    case 'f':
 
593
    case 'g':
 
594
    case 'h':
 
595
    case 'i':
 
596
    case 'j':
 
597
    case 'k':
 
598
    case 'l':
 
599
    case 'm':
 
600
    case 'n':
 
601
    case 'o':
 
602
    case 'p':
 
603
    case 'q':
 
604
    case 'r':
 
605
    case 's':
 
606
    case 't':
 
607
    case 'u':
 
608
    case 'v':
 
609
    case 'w':
 
610
    case 'x':
 
611
    case 'y':
 
612
    case 'z':
 
613
      if (players[addr - 'a' + 10].p_status == PFREE)
 
614
        {
 
615
          warning("That player left the game. message not sent.");
 
616
          return 0;
 
617
        }
 
618
      *recip = addr - 'a' + 10;
 
619
      return (MINDIV);
 
620
      break;
 
621
    default:
 
622
      warning("Not legal recipient");
 
623
    }
 
624
  return 0;
 
625
}
 
626
 
 
627
 
 
628
void pnbtmacro(int c)
 
629
{
 
630
  switch (macro[c].who)
 
631
    {
 
632
    case 'A':
 
633
      pmessage(macro[c].string, 0, MALL);
 
634
      break;
 
635
    case 'F':
 
636
      pmessage(macro[c].string, FED, MTEAM);
 
637
      break;
 
638
    case 'R':
 
639
      pmessage(macro[c].string, ROM, MTEAM);
 
640
      break;
 
641
    case 'K':
 
642
      pmessage(macro[c].string, KLI, MTEAM);
 
643
      break;
 
644
    case 'O':
 
645
      pmessage(macro[c].string, ORI, MTEAM);
 
646
      break;
 
647
    case 'T':
 
648
      pmessage(macro[c].string, me->p_team, MTEAM);
 
649
      break;
 
650
    }
 
651
}