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