File: nPlay.c

package info (click to toggle)
conquest 8.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,984 kB
  • ctags: 3,086
  • sloc: ansic: 39,393; sh: 8,540; yacc: 446; makefile: 296; lex: 146
file content (316 lines) | stat: -rw-r--r-- 7,925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* 
 * play node.  Sets up for the cockpit (CP).
 *
 * $Id: nPlay.c,v 1.9 2004/12/14 20:42:54 jon Exp $
 *
 * Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
 */

#include "c_defs.h"
#include "context.h"
#include "global.h"
#include "datatypes.h"
#include "color.h"
#include "conf.h"
#include "conqcom.h"
#include "gldisplay.h"
#include "glmisc.h"
#include "node.h"
#include "client.h"
#include "record.h"

#include "nMenu.h"
#include "nCP.h"
#include "nPlay.h"
#include "cqkeys.h"

#define S_DONE       0          /* ready to play */
#define S_NSERR      1          /* _newship error */
#define S_SELSYS     2          /* select system */
#define S_MENU       3          /* go back to menu */

extern void processPacket(Unsgn8 *buf);

static int state;
static int fatal = FALSE;
static spAck_t *sack = NULL;
static spClientStat_t scstat = {};
static int shipinited = FALSE;   /* whether we've done _newship() yet */
static int owned[NUMPLAYERTEAMS]; 

static int nPlayDisplay(dspConfig_t *);
static int nPlayIdle(void);
static int nPlayInput(int ch);

static scrNode_t nPlayNode = {
  nPlayDisplay,               /* display */
  nPlayIdle,                  /* idle */
  nPlayInput,                  /* input */
  NULL                          /* next */
};

/* select a system to enter */
static void selectentry( Unsgn8 esystem )
{
  int i; 
  char cbuf[BUFFER_SIZE];
  
  /* First figure out which systems we can enter from. */
  for ( i = 0; i < NUMPLAYERTEAMS; i++ )
    if (esystem & (1 << i))
      {
	owned[i] = TRUE;
      }
    else
      owned[i] = FALSE;
  
  /* Prompt for a decision. */
  c_strcpy( "Enter which system", cbuf );
  for ( i = 0; i < NUMPLAYERTEAMS; i++ )
    if ( owned[i] )
      {
	appstr( ", ", cbuf );
	appstr( Teams[i].name, cbuf );
      }
  /* Change first comma to a colon. */
  i = c_index( cbuf, ',' );
  if ( i != ERR )
    cbuf[i] = ':';
  
  cprintf(12, 0, ALIGN_CENTER, cbuf);

  return;
}



/*  _newship - here we will await a ClientStat from the server (indicating
    our possibly new ship), or a NAK indicating a problem.
*/
static int _newship( int unum, int *snum )
{
  int pkttype;
  Unsgn8 buf[PKT_MAXSIZE];
  int sockl[2] = {cInfo.sock, cInfo.usock};

  /* here we will wait for ack's or a clientstat pkt. Acks indicate an
     error.  If the clientstat pkt's esystem is !0, we need to prompt
     for the system to enter and send it in a CP_COMMAND:CPCMD_ENTER
     pkt. */


  while (TRUE)
    {
      if ((pkttype = waitForPacket(PKT_FROMSERVER, sockl, PKT_ANYPKT,
				   buf, PKT_MAXSIZE, 60, NULL)) < 0)
	{
	  clog("nPlay: _newship: waitforpacket returned %d", pkttype);
          fatal = TRUE;
	  return FALSE;
	}
      
      switch (pkttype)
	{
	case 0:			/* timeout */
	  return FALSE;
	  break;
	  
	case SP_ACK:		/* bummer */
	  sack = (spAck_t *)buf;
          state = S_NSERR;
	  
	  return FALSE;		/* always a failure */
	  break;
	  
	case SP_CLIENTSTAT:
	  scstat = *(spClientStat_t *)buf; /* make a copy... */
	  /* first things first */
	  Context.unum = (int)ntohs(scstat.unum);
	  Context.snum = scstat.snum;
	  Ships[Context.snum].team = scstat.team;
	  
          return TRUE;
	  break;
	  
	  /* we might get other packets too */
	default:
	  processPacket(buf);
	  break;
	}
    }

  /* if we are here, something unexpected happened */
  return FALSE;			/* NOTREACHED */

  
}

void nPlayInit(void)
{
  state = S_SELSYS;               /* default */
  shipinited = FALSE;
  /* let the server know our intentions */
  if (!sendCommand(CPCMD_ENTER, 0))
    fatal = TRUE;

  setNode(&nPlayNode);

  return;
}


static int nPlayDisplay(dspConfig_t *dsp)
{
  char cbuf[BUFFER_SIZE];
  int i, j;

  if (fatal)
    return NODE_EXIT;

  if (state == S_SELSYS)
    {                 
      if (!shipinited)
        {                       /* need to call _newship */
          shipinited = TRUE;
          if (!_newship( Context.unum, &Context.snum ))
            {
              state = S_NSERR;
              return NODE_OK;
            }
        }
          
      if (!scstat.esystem)
        {                       /* we are ready  */
          state = S_DONE;
          return NODE_OK;
        }
      else
        {                       /* need to display/get a selection */
          selectentry(scstat.esystem);
        }
    }
  else if (state == S_NSERR)
    {
      if (sack)
        {
          switch (sack->code)
            {
            case PERR_FLYING:
              sprintf(cbuf, "You're already playing on another ship.");
              cprintf(5,0,ALIGN_CENTER,"#%d#%s",InfoColor, cbuf);
              Ships[Context.snum].status = SS_RESERVED;
              break;
              
            case PERR_TOOMANYSHIPS:
              i = MSG_LIN2/2;
              cprintf(i, 0, ALIGN_CENTER, 
                      "I'm sorry, but you're playing on too many ships right now.");
              i = i + 1;
              c_strcpy( "You are only allowed to fly ", cbuf );
              j = Users[Context.unum].multiple;
              appint( j, cbuf );
              appstr( " ship", cbuf );
              if ( j != 1 )
                appchr( 's', cbuf );
              appstr( " at one time.", cbuf );
              cprintf(i, 0, ALIGN_CENTER, cbuf);
              break;
              
            default:
              clog("nPlayDisplay: _newship: unexpected ack code %d",
                   sack->code);
              break;
            }
        }
    }


  return NODE_OK;
}  
  
static int nPlayIdle(void)
{
  if (state == S_DONE)
    {
      Context.entship = TRUE;
      Ships[Context.snum].sdfuse = 0;       /* zero self destruct fuse */
      grand( &Context.msgrand );            /* initialize message timer */
      Context.leave = FALSE;                /* assume we won't want to bail */
      Context.redraw = TRUE;                /* want redraw first time */
      Context.msgok = TRUE;         /* ok to get messages */
      
      Context.display = TRUE;               /* ok to display */
      
      /* start recording if neccessary */
      if (Context.recmode == RECMODE_STARTING)
        {
          if (recordInitOutput(Context.unum, getnow(NULL, 0), Context.snum))
            {
              Context.recmode = RECMODE_ON;
              /* need to tell the server to resend all the crap it already
                 sent in menu so it can be recorded... sorry. */
              sendCommand(CPCMD_RELOAD, 0);
            }
          else
            Context.recmode = RECMODE_OFF;
        }

      nCPInit();            /* play */
    }
  else if (state == S_MENU)
    nMenuInit();

  return NODE_OK;
}

static int nPlayInput(int ch)
{
  int i;
  unsigned char c = CQ_CHAR(ch);

  switch (state)
    {
    case S_SELSYS:              /* we are selecting our system */
      {
        switch  ( ch )
          {
          case TERM_NORMAL:
          case TERM_ABORT:	/* doesn't like the choices ;-) */
            sendCommand(CPCMD_ENTER, 0);
            state = S_MENU;
            return NODE_OK;
            break;
          case TERM_EXTRA:
            /* Enter the home system. */
            sendCommand(CPCMD_ENTER, (Unsgn16)(1 << Ships[Context.snum].team));
            state = S_DONE;
            return NODE_OK;
            break;
          default:
            for ( i = 0; i < NUMPLAYERTEAMS; i++ )
              if ( Teams[i].teamchar == (char)toupper(c) && owned[i] )
                {
                  /* Found a good one. */
                  sendCommand(CPCMD_ENTER, (Unsgn16)(1 << i));
                  state = S_DONE;
                  return NODE_OK;
                }
            
            /* Didn't get a good one; complain and try again. */
            mglBeep();
            break;
          }
      }
      
      break;

    case S_NSERR:               /* any key to return */
      nMenuInit();

      return NODE_OK;
      break;
    }

  return NODE_OK;
}