source: abuse/trunk/src/netcfg.cpp @ 2

Last change on this file since 2 was 2, checked in by Sam Hocevar, 14 years ago
  • imported original 0.7.0 tarball
File size: 16.2 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 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    char *nm=i->get(NET_NAME)->read();
130    if (strstr(nm,"\"")) {  error(symbol_str("name_error")); return 0; }   
131    strcpy(name,nm);
132  }
133
134  bFILE *fp=open_file("addon/deathmat/username.lsp","wb");
135  if (!fp->open_failure())
136  {
137    char str[100];
138    sprintf(str,"(setq username \"%s\")\n",name);
139    fp->write(str,strlen(str)+1);
140  }
141  delete fp;
142
143
144
145  return 1;
146
147}
148
149extern int start_running,demo_start,start_edit;
150
151/*int net_configuration::input()   // pulls up dialog box and input fileds
152{
153  ifield *ilist=NULL;
154  int x=WINDOW_FRAME_LEFT,y=WINDOW_FRAME_TOP;
155 
156  jwindow *sv=eh->new_window(50,80,-1,-1,new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP,NET_SERVER,symbol_str("server"),
157                                     new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+eh->font()->height()*2,NET_CLIENT,symbol_str("client"),
158                                     new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+eh->font()->height()*4,NET_SINGLE,symbol_str("single_play"),
159                                     new button(WINDOW_FRAME_LEFT,WINDOW_FRAME_TOP+eh->font()->height()*6,NET_CANCEL,symbol_str("cancel_net"),
160                                                NULL)))),symbol_str("Networking"));
161
162  event ev;
163  int done=0;
164  do
165  {
166    eh->flush_screen();
167    do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
168    if (ev.type==EV_MESSAGE)
169    {
170      if (ev.message.id==NET_SERVER) { done=1; state=RESTART_SERVER;  start_edit=0; demo_start=0; start_running=1; }
171      else if (ev.message.id==NET_CLIENT) { done=1; state=RESTART_CLIENT;  start_edit=0; demo_start=0; start_running=1; }
172      else if (ev.message.id==NET_SINGLE) { done=1; state=RESTART_SINGLE;  start_edit=0; demo_start=0; start_running=0; }
173      else if (ev.message.id==NET_CANCEL) { done=1; }
174    } else if (ev.type==EV_CLOSE_WINDOW || (ev.type==EV_KEY & ev.key==JK_ESC)) done=1;
175 
176  } while (!done);
177
178  eh->close_window(sv);
179  eh->flush_screen();
180           
181  if (state==RESTART_SINGLE)
182  {
183    strcpy(lsf,"abuse.lsp");
184    return 1;
185  }
186  if (ev.message.id==NET_CANCEL || state==RESTART_SINGLE) return 0;
187
188  if (state==RESTART_SERVER)
189  {
190    ilist=new button(x,y,NET_CANCEL,symbol_str("cancel_button"),ilist);
191    ilist=new button(x,y,NET_OK,       symbol_str("server"),ilist);
192    ilist=new text_field(x,y,NET_KILLS,symbol_str("kills_to_win"),"******",kills,ilist);
193    ilist=new text_field(x,y,NET_MAX,symbol_str("max_play"),"******",max_players,ilist);
194    ilist=new text_field(x,y,NET_MIN,symbol_str("min_play"),"******",min_players,ilist);
195    ilist=new text_field(x,y,NET_PORT,symbol_str("use_port"),"******",port,ilist);
196    ilist=new text_field(x,y,NET_NAME,symbol_str("your_name"),"****************",name,ilist);
197
198  } else
199  {
200    ilist=new button(x,y,NET_CANCEL,symbol_str("cancel_button"),ilist);
201    ilist=new button(x,y,NET_OK,symbol_str("client"),ilist);
202//    ilist=new text_field(x,y,NET_PORT,symbol_str("use_port"),"******",port,ilist);
203    ilist=new text_field(x,y,NET_SERVER_PORT,symbol_str("server_port"),"******",server_port,ilist);
204    ilist=new text_field(x,y,NET_SERVER_NAME,symbol_str("server_name"),"*********************************",game_name,ilist);
205    ilist=new text_field(x,y,NET_NAME,symbol_str("your_name"),"****************",name,ilist);
206  }
207
208  ifield *i=ilist;
209  for (;i;i=i->next)
210  {
211    i->y=y;
212    int x1,y1,x2,y2;
213    i->area(x1,y1,x2,y2,eh);
214    y=y2+2;
215  }
216
217
218  jwindow *nw=eh->new_window(0,0,-1,-1,ilist,symbol_str("Networking"));
219
220  done=0;
221  do
222  {
223    eh->flush_screen();
224    do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
225    if (ev.type==EV_MESSAGE && ev.message.id==NET_OK && confirm_inputs(nw,state==RESTART_SERVER))
226      done=1;
227    if (ev.type==EV_MESSAGE && (ev.message.id==NET_CANCEL || ev.message.id==NET_SINGLE))
228       done=1;
229    if (ev.type==EV_CLOSE_WINDOW || (ev.type==EV_KEY && ev.key==JK_ESC))
230      done=1;
231
232  } while (!done);
233  eh->close_window(nw);
234  eh->flush_screen();
235
236  return ev.message.id==NET_OK;
237}
238
239*/
240
241
242void net_configuration::error(char *message)
243{
244  image *screen_backup=screen->copy();
245
246  image *ns=cash.img(cash.reg("art/frame.spe","net_screen",SPEC_IMAGE,1));
247  int ns_w=ns->width(),ns_h=ns->height();
248  int x=(xres+1)/2-ns_w/2,y=(yres+1)/2-ns_h/2;
249  ns->put_image(screen,x,y);
250  JCFont *fnt=eh->font();
251
252  uchar *remap=white_light+30*256;
253
254  uchar *sl=screen->scan_line(0);
255  int xx=screen->width()*screen->height();
256  for (;xx;xx--,sl++) *sl=remap[*sl];
257
258  int fx=x+ns_w/2-strlen(message)*fnt->width()/2,
259    fy=y+ns_h/2-fnt->height();
260
261  fnt->put_string(screen,fx+1,fy+1,message,eh->black());
262  fnt->put_string(screen,fx,fy,message,eh->bright_color());
263
264 
265  {   
266    char *ok=symbol_str("ok_button");
267
268    int bx=x+ns_w/2-strlen(ok)*fnt->width()/2-3,
269      by=y+ns_h/2+fnt->height()*3;
270   
271    button *sb=new button(bx,by,NET_SERVER,ok,NULL);
272
273    input_manager inm(screen,eh,sb);
274    inm.allow_no_selections();
275    inm.clear_current();
276
277    int done=0;
278    event ev;
279    do
280    {
281      eh->flush_screen();
282      do  { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
283      inm.handle_event(ev,NULL,eh);
284      if ((ev.type==EV_KEY && (ev.key==JK_ESC || ev.key==JK_ENTER)) ||
285          ev.type==EV_MESSAGE) done=1;
286    } while (!done);
287  }
288
289  screen_backup->put_image(screen,0,0);
290  eh->flush_screen();
291  delete screen_backup;
292}
293
294
295ifield *net_configuration::center_ifield(ifield *i,int x1, int x2, ifield *place_below)
296{
297  int X1,Y1,X2,Y2;
298  i->area(X1,Y1,X2,Y2,eh);
299  i->x=(x1+x2)/2-(X2-X1)/2;
300
301  if (place_below)
302  {
303    place_below->area(X1,Y1,X2,Y2,eh);
304    i->y=Y2+2;
305  }
306  return i;
307}
308
309int net_configuration::get_options(int server)
310{
311  image *ns=cash.img(cash.reg("art/frame.spe","net_screen",SPEC_IMAGE,1));
312  int ns_w=ns->width(),ns_h=ns->height();
313  int x=(xres+1)/2-ns_w/2,y=(yres+1)/2-ns_h/2;
314  ns->put_image(screen,x,y);
315  JCFont *fnt=eh->font();
316  image *ok_image=cash.img(cash.reg("art/frame.spe","dev_ok",SPEC_IMAGE,1))->copy(),
317    *cancel_image=cash.img(cash.reg("art/frame.spe","cancel",SPEC_IMAGE,1))->copy();
318
319  ifield *list=NULL;
320
321  if (server)
322  {       
323    list=center_ifield(new text_field(x,y+30,NET_NAME,symbol_str("your_name"),"************************",name,list),x,x+ns_w,NULL);
324    list=center_ifield(new text_field(0,0,NET_SERVER_NAME,symbol_str("server_name"),"************************",game_name,list),x,x+ns_w,list);
325    list=center_ifield(new info_field(0,0,0,symbol_str("min_play"),list),x,x+ns_w,list);
326
327
328    button_box *b=new button_box(0,0,NET_MIN,1,NULL,list);
329    b->add_button(new button(0,0,MIN_8,"8",NULL));
330    b->add_button(new button(0,0,MIN_7,"7",NULL));
331    b->add_button(new button(0,0,MIN_6,"6",NULL));
332    b->add_button(new button(0,0,MIN_5,"5",NULL));
333    b->add_button(new button(0,0,MIN_4,"4",NULL));
334    b->add_button(new button(0,0,MIN_3,"3",NULL));
335    button *r=new button(0,0,MIN_2,"2",NULL); r->push();
336    b->add_button(r);
337    b->add_button(new button(0,0,MIN_1,"1",NULL));
338
339
340
341    b->arrange_left_right(eh);
342    center_ifield(b,x,x+ns_w,list);
343    b->arrange_left_right(eh);
344    list=b;
345
346    list=center_ifield(new info_field(0,0,0,symbol_str("max_play"),list),x,x+ns_w,list);
347
348    b=new button_box(0,0,NET_MAX,1,NULL,list);
349    button *q=new button(0,0,MAX_8,"8",NULL); q->push();
350    b->add_button(q);
351    b->add_button(new button(0,0,MAX_7,"7",NULL));
352    b->add_button(new button(0,0,MAX_6,"6",NULL));
353    b->add_button(new button(0,0,MAX_5,"5",NULL));
354    b->add_button(new button(0,0,MAX_4,"4",NULL));
355    b->add_button(new button(0,0,MAX_3,"3",NULL));
356    b->add_button(new button(0,0,MAX_2,"2",NULL));
357    b->arrange_left_right(eh);
358    center_ifield(b,x,x+ns_w,list);
359    b->arrange_left_right(eh);   
360    list=b;
361
362
363    list=center_ifield(new info_field(0,0,0,symbol_str("level_size"),list),x,x+ns_w,list);
364
365    b=new button_box(0,0,LEVEL_BOX,1,NULL,list);
366    b->add_button(new button(0,0,LVL_8,symbol_str("lvl_8"),NULL));
367    b->add_button(new button(0,0,LVL_4,symbol_str("lvl_4"),NULL));
368    q=new button(0,0,LVL_2,symbol_str("lvl_2"),NULL); q->push();
369    b->add_button(q);
370
371    b->arrange_left_right(eh);
372    center_ifield(b,x,x+ns_w,list);
373    b->arrange_left_right(eh);   
374    list=b;
375
376    list=center_ifield(new text_field(0,0,NET_KILLS,symbol_str("kills_to_win"),"***","25",list),x,x+ns_w,list);
377
378
379
380                                   
381  } else
382  {
383    list=center_ifield(new text_field(x,y+80,NET_NAME,symbol_str("your_name"),"************************",name,list),x,x+ns_w,NULL);
384  }
385
386
387  list=new button(x+80-17,y+ns_h-20-fnt->height(),NET_OK,ok_image,list);
388  list=new button(x+80+17,y+ns_h-20-fnt->height(),NET_CANCEL,cancel_image,list);
389
390  int ret=0;
391
392  {
393    input_manager inm(screen,eh,list);
394    inm.allow_no_selections();
395    inm.clear_current();
396   
397    int done=0;
398    event ev;
399    do
400    {
401      eh->flush_screen();
402      do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
403      inm.handle_event(ev,NULL,eh);
404      if (ev.type==EV_MESSAGE)
405      {
406        switch (ev.message.id)
407        {
408          case NET_OK : { if (confirm_inputs(&inm,server))
409          { ret=1; done=1; }                     
410          else { ((button *)inm.get(NET_OK))->push(); inm.redraw(); }
411          } break;
412          case NET_CANCEL : done=1;         
413        }
414      } if (ev.type==EV_KEY && ev.key==JK_ESC) done=1;
415
416    } while (!done);
417
418  }
419  delete ok_image;
420  delete cancel_image;
421
422  return ret;
423}
424
425int net_configuration::input()   // pulls up dialog box and input fileds
426{
427  int ret=0;
428  screen->clear();
429 
430  image *ns=cash.img(cash.reg("art/frame.spe","net_screen",SPEC_IMAGE,1));
431  int ns_w=ns->width(),ns_h=ns->height();
432  int x=(xres+1)/2-ns_w/2,y=(yres+1)/2-ns_h/2;
433  ns->put_image(screen,x,y);
434  char *nw_s=symbol_str("Networking");
435  JCFont *fnt=eh->font();
436
437
438  eh->font()->put_string(screen,x+ns_w/2-strlen(nw_s)*fnt->width()/2,y+21/2-fnt->height()/2,
439      nw_s,eh->medium_color());
440  {
441
442    char *server_str=symbol_str("server");
443    button *sb=new button(x+40,y+ns_h-23-fnt->height(),NET_SERVER,server_str,NULL);
444
445    if (main_net_cfg && (main_net_cfg->state==CLIENT || main_net_cfg->state==SERVER))
446      sb=new button(x+40,y+ns_h-9-fnt->height(),NET_SINGLE,symbol_str("single_play"),sb);
447
448    input_manager inm(screen,eh,sb);
449
450    inm.allow_no_selections();
451    inm.clear_current();
452
453
454    event ev;
455    int done=0;
456    int button_y=25,total_games=0;
457    enum { MAX_GAMES=9 };
458    net_address *game_addr[MAX_GAMES+1];
459    int join_game=-1;
460    time_marker start,now;
461
462    do
463    {
464      if (eh->event_waiting())
465      {
466        do  { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
467        inm.handle_event(ev,NULL,eh);
468        if (ev.type==EV_MESSAGE)
469        {
470          switch (ev.message.id)
471          {
472            case NET_CANCEL : done=1; break;
473            case NET_SERVER : done=1; break;
474            case NET_SINGLE : done=1; break;
475            default :
476              if (ev.message.id>=NET_GAME && ev.message.id<NET_GAME+MAX_GAMES)
477              {
478                join_game=ev.message.id-NET_GAME;
479                done=1;
480              }
481          }
482        }
483                else if (ev.type==EV_KEY && ev.key==JK_ESC )
484                {
485                        done=1;
486                }
487                else
488                {
489                        // No event waiting...  We can't wait for long, because we are pretending to broadcast.
490                        milli_wait(5); // ECS - Added so waiting in dialog doesn't use 100% of CPU
491                }
492        }
493
494      eh->flush_screen();
495      char name[256];
496
497      now.get_time();
498      if (total_games<MAX_GAMES && now.diff_time(&start)>0.5)
499      {
500        start.get_time();
501        net_address *find=prot->find_address(0x9090,name);    // was server_port
502        if (find)
503        {
504          int bw=strlen(name)*fnt->width();
505          inm.add(new button(x+ns_w/2-bw/2,y+button_y,NET_GAME+total_games,name,NULL));
506          find->set_port(server_port);
507          game_addr[total_games]=find;
508                       
509          total_games++;
510          button_y+=fnt->height()+10;
511          inm.redraw();
512        }
513      }
514
515    } while (!done);
516
517    prot->reset_find_list();
518
519    if (join_game>=0)
520    {
521      if (get_options(0))
522      {
523        int still_there=1;  // change this back to 0, to check if games are stil alive
524        time_marker start,now;
525        do
526        {
527          now.get_time();
528          char name[256];
529          net_address *find=prot->find_address(0x9090,name);  // was server_port
530          if (find)
531          {       
532            if (find->equal(game_addr[join_game]))
533              still_there=1;
534            delete find;
535          }
536                       
537        } while (now.diff_time(&start)<3 && !still_there);
538                       
539        if (still_there)
540        {
541          game_addr[join_game]->store_string(server_name,sizeof(server_name));
542          state=RESTART_CLIENT;
543          ret=1;
544                       
545        } else error(symbol_str("not_there"));
546                       
547                       
548        prot->reset_find_list();
549        int i;
550        for (i=0;i<total_games;i++)        // delete all the addresses we found and stored
551          delete game_addr[join_game]; 
552      }
553    } else if (ev.type==EV_MESSAGE && ev.message.id==NET_SERVER)
554    {
555      if (get_options(1))
556      {
557        state=RESTART_SERVER;
558        return 1;
559      }
560      else return 0;
561    } else if (ev.type==EV_MESSAGE && ev.message.id==NET_SINGLE)
562    {
563      state=RESTART_SINGLE;
564      start_running=0;
565
566      strcpy(lsf,"abuse.lsp");
567      return 1;     
568    }     
569  }
570
571  return ret;
572}
573
574
Note: See TracBrowser for help on using the repository browser.