~ubuntu-branches/ubuntu/trusty/netrek-client-cow/trusty

« back to all changes in this revision

Viewing changes to findslot.c

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese
  • Date: 2009-06-16 15:27:20 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090616152720-whngeaf1w8l23u14
Tags: 3.3.0-1
[ Barry deFreese ]
* New upstream release.
* Install upstream desktop file. (Closes: #522587).
* Add quilt patch system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* findslot.c
2
 
 * 
 
2
 *
3
3
 * Kevin Smith 03/23/88
4
 
 *
5
4
 */
6
5
#include "config.h"
7
6
#include "copyright2.h"
8
7
 
9
8
#include <stdio.h>
10
9
#include <sys/types.h>
11
 
#include <errno.h>
12
 
#include <pwd.h>
13
 
#include <string.h>
14
 
#include <ctype.h>
 
10
 
15
11
#include "Wlib.h"
16
12
#include "defs.h"
17
13
#include "struct.h"
18
14
#include "data.h"
19
15
#include "packets.h"
20
16
 
 
17
#include "cowmain.h"
21
18
#include "defaults.h"
22
19
#include "findslot.h"
23
20
#include "newwin.h"
24
21
#include "socket.h"
25
22
 
26
 
#define WAITMOTD
27
 
 
28
 
#define WAITWIDTH 180
29
 
#define WAITHEIGHT 60
30
 
#define WAITTITLE 15 /* height of title for wait window */
31
 
 
32
 
extern void terminate(int error);
33
 
 
34
 
static void mapWaitWin(W_Window waitWin);
35
 
static void mapWaitQuit(W_Window qwin);
36
 
static void mapWaitCount(W_Window waitWin, W_Window countWin, int count);
37
 
static void mapWaitMotdButton(W_Window motdButtonWin);
38
 
 
 
23
/*
 
24
 
 
25
   window hierarchy diagram
 
26
 
 
27
   w                    wait queue window
 
28
    \_ quit             button
 
29
    \_ count            button (not reactive)
 
30
    \_ motd             button
 
31
 
 
32
   w_motd               message of the day window
 
33
 
 
34
*/
 
35
 
 
36
/* wait queue window constants, in pixels */
 
37
#define WQ_W 405        /* window width */
 
38
#define WQ_H 100        /* window height */
 
39
#define WQ_HEAD 20      /* heading height */
 
40
#define BM 10           /* button margin */
 
41
 
 
42
/* calculate an X coordinate for centering given text */
 
43
static int xc(char *s)
 
44
{
 
45
  return WQ_W / 6 - BM - strlen(s) * W_Textwidth / 2;
 
46
}
 
47
 
 
48
/* draw or redraw the heading in the wait queue window */
 
49
static void draw_heading(W_Window w)
 
50
{
 
51
  char    buf[60];
 
52
 
 
53
  sprintf(buf, "You are on the queue at %s, please wait.", serverName);
 
54
  W_WriteText(w, 10, 10, textColor, buf, strlen(buf), W_RegularFont);
 
55
}
 
56
 
 
57
/* draw or redraw the quit button */
 
58
static void draw_b_quit(W_Window w)
 
59
{
 
60
  char   *s = "Quit";
 
61
 
 
62
  W_WriteText(w, xc(s), 25, textColor, s, strlen(s), W_RegularFont);
 
63
}
 
64
 
 
65
/* draw or redraw the count button */
 
66
static void draw_b_count(W_Window ww, W_Window cw, int count)
 
67
{
 
68
  char   *s1 = "queue";
 
69
  char   *s2 = "position";
 
70
  char    buf[80];
 
71
 
 
72
  W_WriteText(cw, xc(s1),  5, textColor, s1, strlen(s1), W_RegularFont);
 
73
  W_WriteText(cw, xc(s2), 15, textColor, s2, strlen(s2), W_RegularFont);
 
74
  sprintf(buf, "   %d   ", count);
 
75
  if (count == -1) strcpy(buf, "   ?   ");
 
76
  W_WriteText(cw, xc(buf), 35, textColor, buf, strlen(buf), W_RegularFont);
 
77
 
 
78
  /* update the window title bar for the window manager to display */
 
79
  sprintf(buf, "Q%d @ %s", count, serverName);
 
80
  W_SetWindowName(ww, buf);
 
81
}
 
82
 
 
83
/* draw or redraw the motd button */
 
84
static void draw_b_motd(W_Window w)
 
85
{
 
86
  char   *s1 = "show server";
 
87
  char   *s2 = "welcome message";
 
88
 
 
89
  W_WriteText(w, xc(s1), 20, textColor, s1, strlen(s1), W_RegularFont);
 
90
  W_WriteText(w, xc(s2), 30, textColor, s2, strlen(s2), W_RegularFont);
 
91
}
 
92
 
 
93
/* find a server slot, returns the slot number once it is known, but
 
94
if the server reports we are placed in a queue then display the wait
 
95
queue window and maintain the count while optionally showing the
 
96
server message of the day */
39
97
int findslot(void)
40
98
{
41
99
  int     oldcount = -1;
42
 
  W_Window waitWin, qwin, countWin, motdButtonWin;
 
100
  W_Window w, b_quit, b_count, b_motd;
43
101
 
44
 
  W_Window motdWin;
45
 
  extern int MaxMotdLine;
 
102
  W_Window w_motd;
 
103
  extern int motd_last;
46
104
  int     WaitMotdLine = 0;
47
 
  int     mapMotd = booleanDefault("showMotd", 1);
 
105
  int     mapMotd = booleanDefault("showMotd", 0);
48
106
  W_Event event;
49
107
 
50
108
  /* Wait for some kind of indication about in/not in */
51
 
  while (queuePos == -1)
52
 
    {
53
 
      socketPauseNoUser();
54
 
      if (isServerDead())
55
 
        {
56
 
 
 
109
  while (queuePos == -1) {
 
110
    socketPauseNoUser();
 
111
    if (isServerDead()) {
57
112
#if defined(SOUND)
58
 
          Exit_Sound();
 
113
      Exit_Sound();
59
114
#endif
60
 
 
61
 
          fprintf(stderr, "server connection lost, during pre-queue\n");
62
 
          terminate(0);
63
 
        }
64
 
      readFromServer(NULL);
65
 
      if (me != NULL)
66
 
        {
67
 
          /* We are in! */
68
 
          ANNOUNCESOCKET;
69
 
          return (me->p_no);
70
 
        }
71
 
    }
 
115
      fprintf(stderr, "server connection lost, during pre-queue\n");
 
116
      terminate(0);
 
117
    }
 
118
    readFromServer(NULL);
 
119
    if (me != NULL) {
 
120
      /* We are in! */
 
121
      return me->p_no;
 
122
    }
 
123
  }
72
124
 
73
125
  /* We have to wait.  Make appropriate windows, etc... */
74
126
 
75
 
  waitWin = W_MakeWindow("wait", 0, 0, WAITWIDTH, WAITHEIGHT, NULL, 2,
76
 
                         foreColor);
77
 
  countWin = W_MakeWindow("count", WAITWIDTH / 3, WAITTITLE, WAITWIDTH / 3,
78
 
                          WAITHEIGHT - WAITTITLE, waitWin, 1, foreColor);
79
 
  qwin = W_MakeWindow("waitquit", 0, WAITTITLE, WAITWIDTH / 3,
80
 
                      WAITHEIGHT - WAITTITLE, waitWin, 1, foreColor);
81
 
  motdButtonWin = W_MakeWindow("motdbutton", 2 * WAITWIDTH / 3, WAITTITLE,
82
 
                             WAITWIDTH / 3, WAITHEIGHT - WAITTITLE, waitWin,
83
 
                               1, foreColor);
84
 
  W_MapWindow(waitWin);
85
 
  W_MapWindow(countWin);
86
 
  W_MapWindow(motdButtonWin);
87
 
  W_MapWindow(qwin);
88
 
  if (mapMotd)
89
 
    {
90
 
      motdWin = W_MakeWindow("waitmotd", 1, WAITWIDTH + 1, TWINSIDE,
91
 
                             TWINSIDE, 0, 2, foreColor);
92
 
      W_MapWindow(motdWin);
93
 
      showMotd(motdWin, WaitMotdLine);
94
 
    }
95
 
 
96
 
  for (;;)
97
 
    {
98
 
      socketPause();
99
 
      readFromServer(NULL);
100
 
      if (isServerDead())
101
 
        {
102
 
 
103
 
#if defined(SOUND)
104
 
          Exit_Sound();
105
 
#endif
106
 
 
107
 
          fprintf(stderr, "server connection lost, during queue wait\n");
108
 
          terminate(0);
109
 
        }
110
 
      while (W_EventsPending())
111
 
        {
112
 
          W_NextEvent(&event);
113
 
          switch ((int) event.type)
114
 
            {
115
 
            case W_EV_BUTTON:
116
 
            case W_EV_KEY:
117
 
              if (mapMotd && event.Window == motdWin)
118
 
                {
119
 
                  if (event.key == ' ' || event.key == 'q')
120
 
                    {
121
 
                      W_DestroyWindow(motdWin);
122
 
                      mapMotd = !mapMotd;
123
 
                    }
124
 
                  else
125
 
                    {
126
 
                      if (event.key == 'b')
127
 
                        {
128
 
                          WaitMotdLine -= 28;
129
 
                          WaitMotdLine = MAX(WaitMotdLine, 0);
130
 
                        }
131
 
                      else
132
 
                        {
133
 
                          WaitMotdLine += 28;
134
 
                          /* scroll to start if it goes over */
135
 
                          if (WaitMotdLine > MaxMotdLine)
136
 
                            WaitMotdLine = 0;
137
 
                        }
138
 
                      W_ClearWindow(motdWin);
139
 
                      showMotd(motdWin, WaitMotdLine);
140
 
                      break;
141
 
                    }
142
 
                }
143
 
              else if (event.Window == motdButtonWin)
144
 
                {
145
 
                  if (mapMotd)
146
 
                    {
147
 
                      W_DestroyWindow(motdWin);
148
 
                    }
149
 
                  else
150
 
                    {
151
 
                      motdWin = W_MakeWindow("waitmotd", 1, WAITWIDTH + 1,
152
 
                                             TWINSIDE, TWINSIDE, 0, 2,
153
 
                                             foreColor);
154
 
                      W_MapWindow(motdWin);
155
 
                      showMotd(motdWin, WaitMotdLine);
156
 
                    }
157
 
                  mapMotd = !mapMotd;
158
 
                }
159
 
              else if (event.Window == qwin)
160
 
                {
161
 
#if defined(SOUND)
162
 
                  Exit_Sound();
163
 
#endif
164
 
                  fprintf(stderr, "you selected quit\n");
165
 
                  terminate(0);
166
 
                }
167
 
              break;
168
 
            case W_EV_EXPOSE:
169
 
              if (event.Window == waitWin)
170
 
                {
171
 
                  mapWaitWin(waitWin);
172
 
                }
173
 
              else if (event.Window == motdWin)
174
 
                {
175
 
                  showMotd(motdWin, WaitMotdLine);
176
 
                }
177
 
              else if (event.Window == qwin)
178
 
                {
179
 
                  mapWaitQuit(qwin);
180
 
                }
181
 
              else if (event.Window == countWin)
182
 
                {
183
 
                  mapWaitCount(waitWin, countWin, queuePos);
184
 
                }
185
 
              else if (event.Window == motdButtonWin)
186
 
                {
187
 
                  mapWaitMotdButton(motdButtonWin);
188
 
                }
189
 
              break;
190
 
            case W_EV_CLOSED:
191
 
              if (event.Window == waitWin) {
192
 
                fprintf(stderr,
193
 
                        "you quit, by closing the wait window\n");
194
 
                terminate(0);
195
 
              }
196
 
              break;
197
 
            default:
198
 
              break;
199
 
            }
200
 
        }
201
 
      if (queuePos != oldcount)
202
 
        {
203
 
          mapWaitCount(waitWin, countWin, queuePos);
204
 
          oldcount = queuePos;
205
 
        }
206
 
      if (me != NULL)
207
 
        {
208
 
          W_DestroyWindow(waitWin);
209
 
          if (mapMotd)
210
 
            {
211
 
              W_DestroyWindow(motdWin);
212
 
            }
213
 
          ANNOUNCESOCKET;
214
 
          W_Beep();
215
 
          W_Beep();
216
 
          return (me->p_no);
217
 
        }
218
 
    }
219
 
}
220
 
 
221
 
static void mapWaitWin(W_Window waitWin)
222
 
{
223
 
  char   *s;
224
 
  char    buf[60];
225
 
 
226
 
  sprintf(buf, "Game full at %s", serverName);
227
 
  s = buf;
228
 
 
229
 
  W_WriteText(waitWin, 15, 5, textColor, s, strlen(s), W_RegularFont);
230
 
}
231
 
 
232
 
static void mapWaitQuit(W_Window qwin)
233
 
{
234
 
  char   *s = "Quit";
235
 
 
236
 
  W_WriteText(qwin, 10, 15, textColor, s, strlen(s), W_RegularFont);
237
 
}
238
 
 
239
 
static void mapWaitCount(W_Window waitWin, W_Window countWin, int count)
240
 
{
241
 
  char   *s1 = "Wait";
242
 
  char   *s2 = "Queue";
243
 
  char    buf[80];
244
 
 
245
 
  W_WriteText(countWin, 17, 5, textColor, s1, strlen(s1), W_RegularFont);
246
 
  W_WriteText(countWin, 15, 15, textColor, s2, strlen(s2), W_RegularFont);
247
 
  sprintf(buf, "%d    ", count);
248
 
  if (count == -1)
249
 
    STRNCPY(buf, "?", 2);
250
 
  W_WriteText(countWin, WAITWIDTH / 6 - strlen(buf) * W_Textwidth / 2, 25,
251
 
              textColor, buf, strlen(buf), W_RegularFont);
252
 
  sprintf(buf, "Q%d @ %s", count, serverName);
253
 
  W_SetWindowName(waitWin, buf);
254
 
}
255
 
 
256
 
static void mapWaitMotdButton(W_Window motdButtonWin)
257
 
{
258
 
  char   *s = "Motd";
259
 
 
260
 
  W_WriteText(motdButtonWin, 10, 15, textColor, s, strlen(s), W_RegularFont);
 
127
  w       = W_MakeWindow("wait", 0, 0, WQ_W, WQ_H, NULL, 2,
 
128
                         foreColor);
 
129
  b_quit  = W_MakeWindow("waitquit", BM, WQ_HEAD + BM,
 
130
                         WQ_W / 3 - BM * 2, WQ_H - WQ_HEAD - 2 - BM * 2,
 
131
                         w, 2, W_Red);
 
132
  b_count = W_MakeWindow("count", WQ_W / 3 + BM, WQ_HEAD + BM,
 
133
                         WQ_W / 3 - BM * 2, WQ_H - WQ_HEAD - 2 - BM * 2,
 
134
                         w, 2, W_Yellow);
 
135
  b_motd  = W_MakeWindow("motdbutton", 2 * WQ_W / 3 + BM, WQ_HEAD + BM,
 
136
                         WQ_W / 3 - BM * 2, WQ_H - WQ_HEAD - 2 - BM * 2,
 
137
                         w, 2, W_Green);
 
138
  W_MapWindow(w);
 
139
  W_MapWindow(b_count);
 
140
  W_MapWindow(b_motd);
 
141
  W_MapWindow(b_quit);
 
142
  if (mapMotd) {
 
143
    w_motd = W_MakeWindow("waitmotd", 1, WQ_W + 1, TWINSIDE,
 
144
                           TWINSIDE, 0, 2, foreColor);
 
145
    W_MapWindow(w_motd);
 
146
    showMotd(w_motd, WaitMotdLine);
 
147
  }
 
148
 
 
149
  /* a local event loop, exits when the player is given a slot, or if
 
150
     the server disconnects, or if the user choses to quit */
 
151
  for (;;) {
 
152
    socketPause();
 
153
    readFromServer(NULL);
 
154
    if (isServerDead()) {
 
155
#if defined(SOUND)
 
156
      Exit_Sound();
 
157
#endif
 
158
      fprintf(stderr, "server connection lost, during queue wait\n");
 
159
      terminate(0);
 
160
    }
 
161
    while (W_EventsPending()) {
 
162
      W_NextEvent(&event);
 
163
      switch ((int) event.type) {
 
164
      case W_EV_BUTTON:
 
165
      case W_EV_KEY:
 
166
        if (mapMotd && event.Window == w_motd) {
 
167
          if (event.key == ' ' || event.key == 'q') {
 
168
            W_DestroyWindow(w_motd);
 
169
            mapMotd = !mapMotd;
 
170
          } else {
 
171
            if (event.key == 'b') {
 
172
              WaitMotdLine -= 28;
 
173
              WaitMotdLine = MAX(WaitMotdLine, 0);
 
174
            } else {
 
175
              WaitMotdLine += 28;
 
176
              if (WaitMotdLine > motd_last)
 
177
                WaitMotdLine = 0;
 
178
            }
 
179
            W_ClearWindow(w_motd);
 
180
            showMotd(w_motd, WaitMotdLine);
 
181
            break;
 
182
          }
 
183
        } else if (event.Window == b_motd) {
 
184
          if (mapMotd) {
 
185
            W_DestroyWindow(w_motd);
 
186
          } else {
 
187
            w_motd = W_MakeWindow("waitmotd", 1, WQ_W + 1,
 
188
                                   TWINSIDE, TWINSIDE, 0, 2,
 
189
                                   foreColor);
 
190
            W_MapWindow(w_motd);
 
191
            WaitMotdLine = 0;
 
192
            showMotd(w_motd, WaitMotdLine);
 
193
          }
 
194
          mapMotd = !mapMotd;
 
195
        } else if (event.Window == b_quit || event.key == 'q') {
 
196
#if defined(SOUND)
 
197
          Exit_Sound();
 
198
#endif
 
199
          fprintf(stderr, "you selected quit\n");
 
200
          terminate(0);
 
201
        }
 
202
        break;
 
203
      case W_EV_EXPOSE:
 
204
        if (event.Window == w) {
 
205
          draw_heading(w);
 
206
        } else if (event.Window == w_motd) {
 
207
          showMotd(w_motd, WaitMotdLine);
 
208
        } else if (event.Window == b_quit) {
 
209
          draw_b_quit(b_quit);
 
210
        } else if (event.Window == b_count) {
 
211
          draw_b_count(w, b_count, queuePos);
 
212
        } else if (event.Window == b_motd) {
 
213
          draw_b_motd(b_motd);
 
214
        }
 
215
        break;
 
216
      case W_EV_CLOSED:
 
217
        if (event.Window == w) {
 
218
          fprintf(stderr, "you quit, by closing the wait window\n");
 
219
          terminate(0);
 
220
        } else if (event.Window == w_motd) {
 
221
          W_DestroyWindow(w_motd);
 
222
          mapMotd = 0;
 
223
        }
 
224
        break;
 
225
      default:
 
226
        break;
 
227
      }
 
228
    }
 
229
 
 
230
    /* keep the count button up to date based on the server responses */
 
231
    if (queuePos != oldcount) {
 
232
      draw_b_count(w, b_count, queuePos);
 
233
      oldcount = queuePos;
 
234
    }
 
235
 
 
236
    /* if we have a slot now, destroy the windows and begin playing */
 
237
    if (me != NULL) {
 
238
      W_DestroyWindow(w);
 
239
      if (mapMotd) {
 
240
        W_DestroyWindow(w_motd);
 
241
      }
 
242
      W_Beep();
 
243
      W_Beep();
 
244
      return me->p_no;
 
245
    }
 
246
  }
261
247
}