source: abuse/tags/pd/abuse/src/netcfg.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 16.1 KB
Line 
1#include "netcfg.hpp"
2#include "input.hpp"
3#include "cache.hpp"
4#include "sock.hpp"
5#include "timing.hpp"
6#include "light.hpp"
7
8extern char *get_login();
9extern window_manager *eh;
10net_configuration *main_net_cfg=NULL;
11extern char lsf[256];
12char *symbol_str(char *name);
13
14extern net_protocol *prot;
15
16net_configuration::net_configuration()
17{
18  strcpy(name,get_login());
19
20  strcpy(server_name,"My Netgame");
21
22
23  min_players=2;
24  max_players=8;
25  kills=25;
26  port=20202;
27  server_port=20202;
28  state=SINGLE_PLAYER;
29}
30
31
32extern char game_name[50];
33
34enum { NET_OK=1, NET_CANCEL, NET_SERVER_NAME, NET_NAME, NET_PORT, NET_SERVER_PORT, NET_MAX, NET_MIN, NET_KILLS, CFG_ERR_OK, NET_SERVER,
35       NET_CLIENT, NET_SINGLE, NET_GAME=400,  MIN_1,MIN_2,MIN_3,MIN_4,MIN_5,MIN_6,MIN_7,MIN_8,   MAX_2,MAX_3,MAX_4,MAX_5,MAX_6,MAX_7,MAX_8,
36       LVL_2,LVL_4,LVL_8,LEVEL_BOX } ;
37
38
39void net_configuration::cfg_error(char *msg)
40{
41  jwindow *j=eh->new_window(-1,0,-1,-1,new info_field(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP,0,msg,
42      new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+30,CFG_ERR_OK,symbol_str("ok_button"),NULL)),symbol_str("input_error"));
43  event ev;
44  do
45  {
46    eh->flush_screen();
47    do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
48  } while (ev.type!=EV_MESSAGE || ev.message.id!=CFG_ERR_OK || ev.type==EV_CLOSE_WINDOW || (ev.type==EV_KEY && ev.key==JK_ESC));
49  eh->close_window(j);
50  eh->flush_screen();
51}
52
53int net_configuration::restart_state()
54{
55  switch (state)
56  {
57    case RESTART_SERVER :
58    case RESTART_CLIENT :
59    case RESTART_SINGLE :
60      return 1;
61  }
62  return 0;
63}
64
65int net_configuration::notify_reset()
66{
67  switch (state)
68  {
69    case RESTART_SERVER : { state=SERVER; } break;
70    case RESTART_CLIENT : { state=CLIENT; } break;
71    case RESTART_SINGLE : { state=SINGLE_PLAYER; } break;
72  }
73
74
75  return 1;
76}
77
78
79int net_configuration::confirm_inputs(input_manager *i, int server)
80{
81  if (server)
82  {
83    int mx,mn,pt,kl;
84
85    if (sscanf(i->get(NET_KILLS)->read(),"%d",&kl)!=1 || kl<1 || kl>99)  {  error(symbol_str("kill_error")); return 0; }
86
87    char *nm=i->get(NET_NAME)->read();
88    if (strstr(nm,"\"")) {  error(symbol_str("name_error")); return 0; }   
89    strcpy(name,nm);
90
91    min_players=((ifield *)(i->get(NET_MIN)->read()))->id-MIN_1+1;
92    max_players=((ifield *)(i->get(NET_MAX)->read()))->id-MAX_2+2;
93    if (max_players<min_players)  {  error(symbol_str("max_error")); return 0; }   
94
95    char *s_nm=i->get(NET_SERVER_NAME)->read();
96    if (strstr(s_nm,"\"")) {  error(symbol_str("game_error")); return 0; }   
97
98    strcpy(game_name,s_nm);
99
100    bFILE *fp=open_file("addon/deathmat/gamename.lsp","wb");
101    if (!fp->open_failure())
102    {
103      char str[100];
104      sprintf(str,"(setq gamename \"%s\")\n",game_name);
105      fp->write(str,strlen(str)+1);
106    }
107    delete fp;
108    strcpy(lsf,"addon/deathmat/deathmat.lsp");
109
110
111    fp=open_file("addon/deathmat/levelset.lsp","wb");
112    if (!fp->open_failure())
113    {
114      char str[100];
115      if (((ifield *)(i->get(LEVEL_BOX)->read()))->id==LVL_2)
116        sprintf(str,"(load \"addon/deathmat/small.lsp\")\n");
117      else if (((ifield *)(i->get(LEVEL_BOX)->read()))->id==LVL_4)
118        sprintf(str,"(load \"addon/deathmat/medium.lsp\")\n");
119      else
120        sprintf(str,"(load \"addon/deathmat/large.lsp\")\n");
121      fp->write(str,strlen(str)+1);
122    }
123    delete fp;
124
125
126    kills=kl;
127
128  } else  {
129    int mx,mn,pt,s_pt,kl;
130    char *nm=i->get(NET_NAME)->read();
131    if (strstr(nm,"\"")) {  error(symbol_str("name_error")); return 0; }   
132    strcpy(name,nm);
133  }
134
135  bFILE *fp=open_file("addon/deathmat/username.lsp","wb");
136  if (!fp->open_failure())
137  {
138    char str[100];
139    sprintf(str,"(setq username \"%s\")\n",name);
140    fp->write(str,strlen(str)+1);
141  }
142  delete fp;
143
144
145
146  return 1;
147
148}
149
150extern int start_running,demo_start,start_edit;
151
152/*int net_configuration::input()   // pulls up dialog box and input fileds
153{
154  ifield *ilist=NULL;
155  int x=WINDOW_FRAME_LEFT,y=WINDOW_FRAME_TOP;
156 
157  jwindow *sv=eh->new_window(50,80,-1,-1,new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP,NET_SERVER,symbol_str("server"),
158                                     new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+eh->font()->height()*2,NET_CLIENT,symbol_str("client"),
159                                     new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+eh->font()->height()*4,NET_SINGLE,symbol_str("single_play"),
160                                     new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+eh->font()->height()*6,NET_CANCEL,symbol_str("cancel_net"),
161                                                NULL)))),symbol_str("Networking"));
162
163  event ev;
164  int done=0;
165  do
166  {
167    eh->flush_screen();
168    do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
169    if (ev.type==EV_MESSAGE)
170    {
171      if (ev.message.id==NET_SERVER) { done=1; state=RESTART_SERVER;  start_edit=0; demo_start=0; start_running=1; }
172      else if (ev.message.id==NET_CLIENT) { done=1; state=RESTART_CLIENT;  start_edit=0; demo_start=0; start_running=1; }
173      else if (ev.message.id==NET_SINGLE) { done=1; state=RESTART_SINGLE;  start_edit=0; demo_start=0; start_running=0; }
174      else if (ev.message.id==NET_CANCEL) { done=1; }
175    } else if (ev.type==EV_CLOSE_WINDOW || (ev.type==EV_KEY & ev.key==JK_ESC)) done=1;
176 
177  } while (!done);
178
179  eh->close_window(sv);
180  eh->flush_screen();
181           
182  if (state==RESTART_SINGLE)
183  {
184    strcpy(lsf,"abuse.lsp");
185    return 1;
186  }
187  if (ev.message.id==NET_CANCEL || state==RESTART_SINGLE) return 0;
188
189  if (state==RESTART_SERVER)
190  {
191    ilist=new button(x,y,NET_CANCEL,symbol_str("cancel_button"),ilist);
192    ilist=new button(x,y,NET_OK,       symbol_str("server"),ilist);
193    ilist=new text_field(x,y,NET_KILLS,symbol_str("kills_to_win"),"******",kills,ilist);
194    ilist=new text_field(x,y,NET_MAX,symbol_str("max_play"),"******",max_players,ilist);
195    ilist=new text_field(x,y,NET_MIN,symbol_str("min_play"),"******",min_players,ilist);
196    ilist=new text_field(x,y,NET_PORT,symbol_str("use_port"),"******",port,ilist);
197    ilist=new text_field(x,y,NET_NAME,symbol_str("your_name"),"****************",name,ilist);
198
199  } else
200  {
201    ilist=new button(x,y,NET_CANCEL,symbol_str("cancel_button"),ilist);
202    ilist=new button(x,y,NET_OK,symbol_str("client"),ilist);
203//    ilist=new text_field(x,y,NET_PORT,symbol_str("use_port"),"******",port,ilist);
204    ilist=new text_field(x,y,NET_SERVER_PORT,symbol_str("server_port"),"******",server_port,ilist);
205    ilist=new text_field(x,y,NET_SERVER_NAME,symbol_str("server_name"),"*********************************",game_name,ilist);
206    ilist=new text_field(x,y,NET_NAME,symbol_str("your_name"),"****************",name,ilist);
207  }
208
209  ifield *i=ilist;
210  for (;i;i=i->next)
211  {
212    i->y=y;
213    int x1,y1,x2,y2;
214    i->area(x1,y1,x2,y2,eh);
215    y=y2+2;
216  }
217
218
219  jwindow *nw=eh->new_window(0,0,-1,-1,ilist,symbol_str("Networking"));
220
221  done=0;
222  do
223  {
224    eh->flush_screen();
225    do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
226    if (ev.type==EV_MESSAGE && ev.message.id==NET_OK && confirm_inputs(nw,state==RESTART_SERVER))
227      done=1;
228    if (ev.type==EV_MESSAGE && (ev.message.id==NET_CANCEL || ev.message.id==NET_SINGLE))
229       done=1;
230    if (ev.type==EV_CLOSE_WINDOW || (ev.type==EV_KEY && ev.key==JK_ESC))
231      done=1;
232
233  } while (!done);
234  eh->close_window(nw);
235  eh->flush_screen();
236
237  return ev.message.id==NET_OK;
238}
239
240*/
241
242
243void net_configuration::error(char *message)
244{
245  image *screen_backup=screen->copy();
246
247  image *ns=cash.img(cash.reg("art/frame.spe","net_screen",SPEC_IMAGE,1));
248  int ns_w=ns->width(),ns_h=ns->height();
249  int x=(xres+1)/2-ns_w/2,y=(yres+1)/2-ns_h/2;
250  ns->put_image(screen,x,y);
251  JCFont *fnt=eh->font();
252
253  uchar *remap=white_light+30*256;
254
255  uchar *sl=screen->scan_line(0);
256  int xx=screen->width()*screen->height();
257  for (;xx;xx--,sl++) *sl=remap[*sl];
258
259  int fx=x+ns_w/2-strlen(message)*fnt->width()/2,
260    fy=y+ns_h/2-fnt->height();
261
262  fnt->put_string(screen,fx+1,fy+1,message,eh->black());
263  fnt->put_string(screen,fx,fy,message,eh->bright_color());
264
265 
266  {   
267    char *ok=symbol_str("ok_button");
268
269    int bx=x+ns_w/2-strlen(ok)*fnt->width()/2-3,
270      by=y+ns_h/2+fnt->height()*3;
271   
272    button *sb=new button(bx,by,NET_SERVER,ok,NULL);
273
274    input_manager inm(screen,eh,sb);
275    inm.allow_no_selections();
276    inm.clear_current();
277
278    int done=0;
279    event ev;
280    do
281    {
282      eh->flush_screen();
283      do  { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
284      inm.handle_event(ev,NULL,eh);
285      if ((ev.type==EV_KEY && (ev.key==JK_ESC || ev.key==JK_ENTER)) ||
286          ev.type==EV_MESSAGE) done=1;
287    } while (!done);
288  }
289
290  screen_backup->put_image(screen,0,0);
291  eh->flush_screen();
292  delete screen_backup;
293}
294
295
296ifield *net_configuration::center_ifield(ifield *i,int x1, int x2, ifield *place_below)
297{
298  int X1,Y1,X2,Y2;
299  i->area(X1,Y1,X2,Y2,eh);
300  i->x=(x1+x2)/2-(X2-X1)/2;
301
302  if (place_below)
303  {
304    place_below->area(X1,Y1,X2,Y2,eh);
305    i->y=Y2+2;
306  }
307  return i;
308}
309
310int net_configuration::get_options(int server)
311{
312  image *ns=cash.img(cash.reg("art/frame.spe","net_screen",SPEC_IMAGE,1));
313  int ns_w=ns->width(),ns_h=ns->height();
314  int x=(xres+1)/2-ns_w/2,y=(yres+1)/2-ns_h/2;
315  ns->put_image(screen,x,y);
316  JCFont *fnt=eh->font();
317  image *ok_image=cash.img(cash.reg("art/frame.spe","dev_ok",SPEC_IMAGE,1))->copy(),
318    *cancel_image=cash.img(cash.reg("art/frame.spe","cancel",SPEC_IMAGE,1))->copy();
319
320  ifield *list=NULL;
321
322  if (server)
323  {       
324    list=center_ifield(new text_field(x,y+30,NET_NAME,symbol_str("your_name"),"************************",name,list),x,x+ns_w,NULL);
325    list=center_ifield(new text_field(0,0,NET_SERVER_NAME,symbol_str("server_name"),"************************",game_name,list),x,x+ns_w,list);
326    list=center_ifield(new info_field(0,0,0,symbol_str("min_play"),list),x,x+ns_w,list);
327
328
329    button_box *b=new button_box(0,0,NET_MIN,1,NULL,list);
330    b->add_button(new button(0,0,MIN_8,"8",NULL));
331    b->add_button(new button(0,0,MIN_7,"7",NULL));
332    b->add_button(new button(0,0,MIN_6,"6",NULL));
333    b->add_button(new button(0,0,MIN_5,"5",NULL));
334    b->add_button(new button(0,0,MIN_4,"4",NULL));
335    b->add_button(new button(0,0,MIN_3,"3",NULL));
336    button *r=new button(0,0,MIN_2,"2",NULL); r->push();
337    b->add_button(r);
338    b->add_button(new button(0,0,MIN_1,"1",NULL));
339
340
341
342    b->arrange_left_right(eh);
343    center_ifield(b,x,x+ns_w,list);
344    b->arrange_left_right(eh);
345    list=b;
346
347    list=center_ifield(new info_field(0,0,0,symbol_str("max_play"),list),x,x+ns_w,list);
348
349    b=new button_box(0,0,NET_MAX,1,NULL,list);
350    button *q=new button(0,0,MAX_8,"8",NULL); q->push();
351    b->add_button(q);
352    b->add_button(new button(0,0,MAX_7,"7",NULL));
353    b->add_button(new button(0,0,MAX_6,"6",NULL));
354    b->add_button(new button(0,0,MAX_5,"5",NULL));
355    b->add_button(new button(0,0,MAX_4,"4",NULL));
356    b->add_button(new button(0,0,MAX_3,"3",NULL));
357    b->add_button(new button(0,0,MAX_2,"2",NULL));
358    b->arrange_left_right(eh);
359    center_ifield(b,x,x+ns_w,list);
360    b->arrange_left_right(eh);   
361    list=b;
362
363
364    list=center_ifield(new info_field(0,0,0,symbol_str("level_size"),list),x,x+ns_w,list);
365
366    b=new button_box(0,0,LEVEL_BOX,1,NULL,list);
367    b->add_button(new button(0,0,LVL_8,symbol_str("lvl_8"),NULL));
368    b->add_button(new button(0,0,LVL_4,symbol_str("lvl_4"),NULL));
369    q=new button(0,0,LVL_2,symbol_str("lvl_2"),NULL); q->push();
370    b->add_button(q);
371
372    b->arrange_left_right(eh);
373    center_ifield(b,x,x+ns_w,list);
374    b->arrange_left_right(eh);   
375    list=b;
376
377    list=center_ifield(new text_field(0,0,NET_KILLS,symbol_str("kills_to_win"),"***","25",list),x,x+ns_w,list);
378
379
380
381                                   
382  } else
383  {
384    list=center_ifield(new text_field(x,y+80,NET_NAME,symbol_str("your_name"),"************************",name,list),x,x+ns_w,NULL);
385  }
386
387
388  list=new button(x+80-17,y+ns_h-20-fnt->height(),NET_OK,ok_image,list);
389  list=new button(x+80+17,y+ns_h-20-fnt->height(),NET_CANCEL,cancel_image,list);
390
391  int ret=0;
392
393  {
394    input_manager inm(screen,eh,list);
395    inm.allow_no_selections();
396    inm.clear_current();
397   
398    int done=0;
399    event ev;
400    do
401    {
402      eh->flush_screen();
403      do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
404      inm.handle_event(ev,NULL,eh);
405      if (ev.type==EV_MESSAGE)
406      {
407        switch (ev.message.id)
408        {
409          case NET_OK : { if (confirm_inputs(&inm,server))
410          { ret=1; done=1; }                     
411          else { ((button *)inm.get(NET_OK))->push(); inm.redraw(); }
412          } break;
413          case NET_CANCEL : done=1;         
414        }
415      } if (ev.type==EV_KEY && ev.key==JK_ESC) done=1;
416
417    } while (!done);
418
419  }
420  delete ok_image;
421  delete cancel_image;
422
423  return ret;
424}
425
426int net_configuration::input()   // pulls up dialog box and input fileds
427{
428  int ret=0;
429  screen->clear();
430 
431  image *ns=cash.img(cash.reg("art/frame.spe","net_screen",SPEC_IMAGE,1));
432  int ns_w=ns->width(),ns_h=ns->height();
433  int x=(xres+1)/2-ns_w/2,y=(yres+1)/2-ns_h/2;
434  ns->put_image(screen,x,y);
435  char *nw_s=symbol_str("Networking");
436  JCFont *fnt=eh->font();
437
438
439  eh->font()->put_string(screen,x+ns_w/2-strlen(nw_s)*fnt->width()/2,y+21/2-fnt->height()/2,
440      nw_s,eh->medium_color());
441  {
442
443    char *server_str=symbol_str("server");
444    button *sb=new button(x+40,y+ns_h-23-fnt->height(),NET_SERVER,server_str,NULL);
445
446    if (main_net_cfg && (main_net_cfg->state==CLIENT || main_net_cfg->state==SERVER))
447      sb=new button(x+40,y+ns_h-9-fnt->height(),NET_SINGLE,symbol_str("single_play"),sb);
448
449    input_manager inm(screen,eh,sb);
450
451    inm.allow_no_selections();
452    inm.clear_current();
453
454
455    event ev;
456    int done=0;
457    int button_y=25,total_games=0;
458    enum { MAX_GAMES=9 };
459    net_address *game_addr[MAX_GAMES+1];
460    int join_game=-1;
461    time_marker start,now;
462
463    do
464    {
465      if (eh->event_waiting())
466      {
467        do  { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
468        inm.handle_event(ev,NULL,eh);
469        if (ev.type==EV_MESSAGE)
470        {
471          switch (ev.message.id)
472          {
473            case NET_CANCEL : done=1; break;
474            case NET_SERVER : done=1; break;
475            case NET_SINGLE : done=1; break;
476            default :
477              if (ev.message.id>=NET_GAME && ev.message.id<NET_GAME+MAX_GAMES)
478              {
479                join_game=ev.message.id-NET_GAME;
480                done=1;
481              }       
482          }
483                                 
484        } else if (ev.type==EV_KEY && ev.key==JK_ESC) done=1;
485      }
486
487      eh->flush_screen();
488      char name[256];
489
490      now.get_time();
491      if (total_games<MAX_GAMES && now.diff_time(&start)>0.5)
492      {
493        start.get_time();
494        net_address *find=prot->find_address(0x9090,name);    // was server_port
495        if (find)
496        {
497          int bw=strlen(name)*fnt->width();
498          inm.add(new button(x+ns_w/2-bw/2,y+button_y,NET_GAME+total_games,name,NULL));
499          find->set_port(server_port);
500          game_addr[total_games]=find;
501                       
502          total_games++;
503          button_y+=fnt->height()+10;
504          inm.redraw();
505        }
506      }
507
508    } while (!done);
509
510    prot->reset_find_list();
511
512    if (join_game>=0)
513    {
514      if (get_options(0))
515      {
516        int still_there=1;  // change this back to 0, to check if games are stil alive
517        time_marker start,now;
518        do
519        {
520          now.get_time();
521          char name[256];
522          net_address *find=prot->find_address(0x9090,name);  // was server_port
523          if (find)
524          {       
525            if (find->equal(game_addr[join_game]))
526              still_there=1;
527            delete find;
528          }
529                       
530        } while (now.diff_time(&start)<3 && !still_there);
531                       
532        if (still_there)
533        {
534          game_addr[join_game]->store_string(server_name,sizeof(server_name));
535          state=RESTART_CLIENT;
536          ret=1;
537                       
538        } else error(symbol_str("not_there"));
539                       
540                       
541        prot->reset_find_list();
542        int i;
543        for (i=0;i<total_games;i++)        // delete all the addresses we found and stored
544          delete game_addr[join_game]; 
545      }
546    } else if (ev.type==EV_MESSAGE && ev.message.id==NET_SERVER)
547    {
548      if (get_options(1))
549      {
550        state=RESTART_SERVER;
551        return 1;
552      }
553      else return 0;
554    } else if (ev.type==EV_MESSAGE && ev.message.id==NET_SINGLE)
555    {
556      state=RESTART_SINGLE;
557      start_running=0;
558
559      strcpy(lsf,"abuse.lsp");
560      return 1;     
561    }     
562  }
563
564  return ret;
565}
566
567
Note: See TracBrowser for help on using the repository browser.