source: abuse/tags/pd/abuse/src/view.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: 25.2 KB
Line 
1#include "system.h"
2#include "view.hpp"
3#include "lisp.hpp"
4#include "jwindow.hpp"
5#include "config.hpp"
6#include "scroller.hpp"
7#include "id.hpp"
8#include "dev.hpp"
9#include "jrand.hpp"
10#include "dprint.hpp"
11#include "transp.hpp"
12#include "clisp.hpp"
13#include "demo.hpp"
14#include "sbar.hpp"
15#include "nfserver.hpp"
16#include "chat.hpp"
17
18#define SHIFT_DOWN_DEFAULT 15
19#define SHIFT_RIGHT_DEFAULT 0
20 
21extern window_manager *eh;
22view *player_list=NULL;
23int morph_sel_frame_color;
24
25view::~view()
26{
27  if (local_player())
28    sbar.associate(NULL);
29
30  if (total_weapons)
31  {
32    jfree(weapons);
33    jfree(last_weapons);
34  }
35}
36
37
38extern uchar bright_tint[256];
39
40void view::add_ammo(int weapon_type, int total)
41
42  if (weapon_type>=total_weapons || weapon_type<0)
43  {
44    printf("weapon out of range\n");
45    return ;
46  }
47  if (weapons[weapon_type]==-1) return ;   // don't have weapon yet, can't give ammo
48  int rf=0;
49
50  weapons[weapon_type]+=total;
51  if (weapons[weapon_type]<0) 
52    weapons[weapon_type]=0; 
53
54  if (weapons[weapon_type]>999)
55    weapons[weapon_type]=999;
56
57  if (weapon_total(current_weapon)==0 && current_weapon)
58  {
59    suggest.send_weapon_change=1;
60    if (DEFINEDP(symbol_value(l_switch_to_powerful)) && symbol_value(l_switch_to_powerful))
61    {
62      int x=total_weapons-1;
63      while (x>0 && (x==3 || weapons[x]<=0)) x--;         
64      suggest.new_weapon=x;
65    } else
66      suggest.new_weapon=0;
67  }
68
69}
70
71void view::give_weapon(int type)
72{
73  if (type>=total_weapons || type<0)
74  {
75    printf("weapon out of range\n");
76    return ;
77  }
78  if (weapons[type]==-1)
79  {
80    weapons[type]=0;
81    sbar.need_refresh();
82  }
83}
84
85int view::weapon_total(int type)
86{
87  if (type>=total_weapons || type<0)
88  {
89    printf("weapon out of range\n");
90    return 0;
91  }
92  if (god) return 100;
93  else if (weapons[type]==-1) return 0;
94  else return weapons[type];
95}
96
97
98long view::xoff()
99{
100  if (focus)
101  {
102    int x=last_x-(cx2-cx1+1)/2+shift_right+pan_x;
103    if (x<0) return 0;
104    else return x;
105  } else return pan_x;
106}
107
108long view::interpolated_xoff()
109{
110  if (focus)
111  {
112    int x=(last_last_x+last_x)/2-(cx2-cx1+1)/2+shift_right+pan_x;
113    if (x<0) return 0;
114    else return x;
115  } else return pan_x;
116}
117
118
119long view::yoff()
120{
121  if (focus)
122  {
123    int y=last_y-(cy2-cy1+1)/2-shift_down+pan_y;
124    if (y<0) return 0;
125    else return y;
126  } else return pan_y;
127}
128
129
130long view::interpolated_yoff()
131{
132  if (focus)
133  {
134    int y=(last_y+last_last_y)/2-(cy2-cy1+1)/2-shift_down+pan_y;
135    if (y<0) return 0;
136    else return y;
137  } else return pan_y;
138}
139
140
141void view::update_scroll()
142{
143  if (focus)
144  {
145    last_last_x=last_x;
146    last_last_y=last_y;
147    if (focus->x>last_x)
148    {
149      if (focus->x-last_x>=no_xright)
150        last_x=focus->x-no_xright;
151    } else if (focus->x<last_x)
152    {
153      if (last_x-focus->x>=no_xleft)
154        last_x=focus->x+no_xleft;
155    }
156    if (focus->y>last_y)
157    {
158      if (focus->y-last_y>=no_ybottom)
159        last_y=focus->y-no_ybottom;
160    } else if (focus->y<last_y)
161    {
162      if (last_y-focus->y>=no_ytop)
163        last_y=focus->y+no_ytop;
164    }
165  }
166}
167
168char cur_user_name[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
169
170#ifdef __WATCOMC__
171char *get_login()
172{  if (cur_user_name[0]) return cur_user_name; else return "DOS user"; }
173
174#include <dos.h>
175#elif defined( __POWERPC__ )
176
177char *get_login()
178{  if (cur_user_name[0]) return cur_user_name; else return "Mac user"; }
179
180#else
181char *get_login()
182{  if (cur_user_name[0]) return cur_user_name; else return (getlogin() ? getlogin() : "unknown"); }
183
184#endif
185
186void set_login(char *name)
187{ strncpy(cur_user_name,name,20); }
188
189view::view(game_object *Focus, view *Next, int number)
190{
191  chat_buf[0]=0;
192
193  draw_solid=-1;
194  no_xleft=0;
195  no_xright=0;
196  no_ytop=0;
197  no_ybottom=0;
198  if (Focus)
199  {
200    last_x=Focus->x;
201    last_y=Focus->y;
202  } else
203  {
204    last_x=last_y=0;
205  }
206
207  last_last_x=last_x;
208  last_last_y=last_y;
209  last_hp=last_ammo=-1;
210  last_type=-1;
211  tsecrets=secrets=0;
212  tkills=kills=0;
213
214  reset_keymap();
215
216  ambient=32;
217  current_weapon=0;
218
219  strcpy(name,get_login());
220  suggest.send_view=0;
221  suggest.send_weapon_change=0;
222
223
224  god=0;
225
226  player_number=number;
227  cx1=0;
228  cy1=0;
229  cx2=100;
230  cy2=100;
231  focus=Focus;
232  next=Next;
233  shift_down=SHIFT_DOWN_DEFAULT;
234  shift_right=SHIFT_RIGHT_DEFAULT;
235  x_suggestion=0;
236  y_suggestion=0;
237  b1_suggestion=0;
238  b2_suggestion=0;
239  b3_suggestion=0;
240  b4_suggestion=0;
241  pointer_x=0;
242  pointer_y=0;
243
244  pan_x=0;
245  pan_y=0;
246  last_type=0;
247  freeze_time=0;
248
249  if (total_weapons)
250  {
251    weapons=(long *)jmalloc(total_weapons*sizeof(long),"weapon array");
252    last_weapons=(long *)jmalloc(total_weapons*sizeof(long),"last weapon array");
253    memset(weapons,0xff,total_weapons*sizeof(long));   // set all to -1
254    memset(last_weapons,0xff,total_weapons*sizeof(long));   // set all to -1 
255  }
256
257  if (total_weapons)
258    weapons[0]=0;
259  if (local_player())
260    sbar.associate(this);
261  sbar.need_refresh();
262}
263
264long view::x_center()
265{
266  if (!focus)
267    return (cx1+cx2)/2;
268  else
269    return focus->x;
270}   
271
272long view::y_center()
273{
274  if (!focus)
275    return (cy1+cy2)/2;
276  else
277    return focus->y;
278}
279
280void view::draw_character_damage()
281{   
282  if (focus && drawable())
283  {
284    if (last_hp!=focus->hp()) draw_hp();
285    int i;
286    for (i=0;i<total_weapons;i++)
287      if (weapons[i]!=last_weapons[i])
288      {
289        last_weapons[i]=weapons[i];
290        sbar.draw_ammo(screen,i,weapons[i],current_weapon==i);
291      }
292  }
293}
294
295
296
297ushort make_sync()
298{
299  ushort x=0;
300  if (!current_level) return 0;
301  if (current_level)
302  {
303    view *f=player_list;
304    for (;f;f=f->next)
305    {
306      if (f->focus)
307      {
308        x^=(f->focus->x&0xffff);
309        x^=(f->focus->y&0xffff);
310      }
311    }
312  }
313  x^=rand_on;
314
315  return x;
316}
317
318
319
320void view::get_input()
321{
322  int sug_x,sug_y,sug_b1,sug_b2,sug_b3,sug_b4;
323  long sug_px,sug_py;
324
325  if (DEFINEDP(symbol_function(l_get_local_input)))
326  {
327    void *ret=eval_function((lisp_symbol *)l_get_local_input,NULL);
328    sug_x=lnumber_value(CAR(ret));  ret=CDR(ret);
329    sug_y=lnumber_value(CAR(ret));  ret=CDR(ret);
330    if (CAR(ret)) sug_b1=1; else sug_b1=0; ret=CDR(ret);
331    if (CAR(ret)) sug_b2=1; else sug_b2=0;  ret=CDR(ret);
332    int x=lnumber_value(CAR(ret)); ret=CDR(ret);
333    if (x<0) sug_b3=1; else sug_b3=0;
334    if (x>0) sug_b4=1; else sug_b4=0;   
335   
336    long bx=lnumber_value(CAR(ret));  ret=CDR(ret);
337    long by=lnumber_value(CAR(ret));  ret=CDR(ret);
338    the_game->mouse_to_game(bx,by,sug_px,sug_py,this);
339
340  } else
341  {
342    get_movement(0,sug_x,sug_y,sug_b1,sug_b2,sug_b3,sug_b4);
343    if (focus)
344    {
345      the_game->mouse_to_game(last_demo_mx,last_demo_my,sug_px,sug_py,this);
346      if (last_demo_mbut&1) sug_b2=1;
347      if (last_demo_mbut&2) sug_b1=1;
348    } else sug_px=sug_py=0;
349  }
350
351
352  if (view_changed())
353  {
354    base->packet.write_byte(SCMD_VIEW_RESIZE);
355    base->packet.write_byte(player_number);
356    base->packet.write_long(suggest.cx1);
357    base->packet.write_long(suggest.cy1);
358    base->packet.write_long(suggest.cx2);
359    base->packet.write_long(suggest.cy2);
360
361    base->packet.write_long(suggest.pan_x);
362    base->packet.write_long(suggest.pan_y);
363    base->packet.write_long(suggest.shift_down);
364    base->packet.write_long(suggest.shift_right);
365  }
366
367  if (weapon_changed())
368  {
369    base->packet.write_byte(SCMD_WEAPON_CHANGE);
370    base->packet.write_byte(player_number);
371    base->packet.write_long(suggest.new_weapon);
372  }
373
374  base->packet.write_byte(SCMD_SET_INPUT);
375  base->packet.write_byte(player_number);
376
377
378  uchar mflags=0;
379  if (sug_x>0)
380    mflags|=1;
381  else if (sug_x<0)
382    mflags|=2;
383
384  if (sug_y>0)
385    mflags|=4;
386  else if (sug_y<0)
387    mflags|=8;
388
389  if (sug_b1) mflags|=16;
390  if (sug_b2) mflags|=32;
391  if (sug_b3) mflags|=64;
392  if (sug_b4) mflags|=128;
393
394  base->packet.write_byte(mflags);
395
396  base->packet.write_short((ushort)((short)sug_px));
397  base->packet.write_short((ushort)((short)sug_py));
398
399}
400
401
402void view::add_chat_key(int key)  // return string if buf is complete
403{
404  int len=strlen(chat_buf);
405  if (key==JK_BACKSPACE)
406  {
407    if (len)
408    {
409      chat_buf[len-1]=0;
410      if (local_player() && chat)
411        chat->draw_user(chat_buf);
412    }
413  } else if (key!=JK_ENTER)
414  {
415    chat_buf[len]=key;
416    chat_buf[len+1]=0;
417    if (local_player() && chat)
418      chat->draw_user(chat_buf);
419  }
420
421  if (len>38 || key==JK_ENTER)
422  {
423    if (DEFINEDP(symbol_function(l_chat_input)))
424    {
425      game_object *o=current_object;
426      current_object=focus;
427
428      void *m=mark_heap(TMP_SPACE);
429      void *list=NULL;
430      push_onto_list(new_lisp_string(chat_buf),list);       
431      eval_function((lisp_symbol *)l_chat_input,list);
432      restore_heap(m,TMP_SPACE);
433
434      current_object=o;
435
436    } else
437    {           
438      if (chat)
439        chat->put_all(chat_buf);
440    }
441    chat_buf[0]=0;
442    if (local_player() && chat)
443      chat->draw_user(chat_buf);
444  }
445}
446
447int view::process_input(char cmd, uchar *&pk)   // return 0 if something went wrong
448{
449  switch (cmd)
450  {
451    case SCMD_CHAT_KEYPRESS :
452    {
453      add_chat_key(*(pk++));
454    } break;
455    case SCMD_VIEW_RESIZE :
456    {
457      long x[8];
458      memcpy(x,pk,8*4);  pk+=8*4;
459      cx1=lltl(x[0]);
460      cy1=lltl(x[1]);
461      cx2=lltl(x[2]);
462      cy2=lltl(x[3]);
463       
464      pan_x=lltl(x[4]);
465      pan_y=lltl(x[5]);
466      shift_down=lltl(x[6]);
467      shift_right=lltl(x[7]);
468      if (small_render)
469      {
470        small_render->resize(cx2-cx1+1,cy2-cy1+1);
471      }
472
473      suggest.send_view=0;
474      if (local_player())
475        the_game->draw();
476      return 1;
477    }
478    case SCMD_WEAPON_CHANGE :
479    {
480      long x;
481      memcpy(&x,pk,4);  pk+=4;
482      current_weapon=lltl(x);
483
484      if (local_player())
485        sbar.need_refresh();
486      suggest.send_weapon_change=0;
487      return 1;
488    } break;
489
490    case SCMD_SET_INPUT :
491    {
492      uchar x=*(pk++);
493
494      if (x&1) x_suggestion=1;
495      else if (x&2) x_suggestion=-1;
496      else x_suggestion=0;
497
498      if (x&4) y_suggestion=1;
499      else if (x&8) y_suggestion=-1;
500      else y_suggestion=0;
501
502      if (x&16) b1_suggestion=1; else b1_suggestion=0;
503      if (x&32) b2_suggestion=1; else b2_suggestion=0;
504      if (x&64) b3_suggestion=1; else b3_suggestion=0;
505      if (x&128) b4_suggestion=1; else b4_suggestion=0;
506
507      ushort p[2];
508      memcpy(p,pk,2*2);  pk+=2*2;
509     
510      pointer_x=(short)(lstl(p[0]));
511      pointer_y=(short)(lstl(p[1]));
512       
513      return 1;
514    } break;
515    case SCMD_KEYPRESS : set_key_down(*(pk++),1); break;
516    case SCMD_EXT_KEYPRESS : set_key_down(*(pk++)+256,1); break;
517    case SCMD_KEYRELEASE : set_key_down(*(pk++),0); break;
518    case SCMD_EXT_KEYRELEASE : set_key_down(*(pk++)+256,0); break;
519  }
520  return 1;
521}
522
523int view::local_player()
524{
525  return player_number==client_number();
526}
527
528void view::next_weapon()
529{
530  int c=current_weapon;
531
532  while (c<total_weapons-1)
533  {
534    c++;
535    if (weapon_total(c)>0)
536    {
537      suggest.send_weapon_change=1;
538      suggest.new_weapon=c;
539      return ;
540    }
541  }
542 
543  c=0;
544  while (c!=current_weapon)
545  {
546    if (weapon_total(c)>0)
547    {
548      suggest.send_weapon_change=1;
549      suggest.new_weapon=c;
550      return ;
551    }
552    c++;
553  }
554}
555
556void view::last_weapon()
557{
558 
559  int c=current_weapon;
560
561  while (c>=1)
562  {
563    c--;
564    if (weapon_total(c)>0 || c==0)
565    {
566      suggest.send_weapon_change=1;
567      suggest.new_weapon=c;
568      return ;
569    }
570  }
571
572  c=total_weapons-1;
573  while (c!=current_weapon)
574  {
575    if (weapon_total(c)>0 || c==0)
576    {
577      suggest.send_weapon_change=1;
578      suggest.new_weapon=c;
579      return ;
580    }
581    c--;
582  }
583
584}
585
586int view::handle_event(event &ev)
587{
588  if (ev.type==EV_KEY)
589  {
590   switch (ev.key)
591   {
592     case '1' :
593     case '2' :
594     case '3' :
595     case '4' :
596     case '5' :
597     case '6' :
598     case '7' :
599     { if (((dev&EDIT_MODE)==0) && (weapon_total(ev.key-'1')>0))
600       {
601         suggest.send_weapon_change=1;
602         suggest.new_weapon=ev.key-'1';
603       }
604     } break;
605
606     case JK_HOME :
607     case JK_CTRL_L :
608     case JK_CTRL_R :
609     { if (total_weapons) last_weapon(); return 1; } break;
610     case JK_PAGEUP :
611     case JK_INSERT :
612     { if (total_weapons) next_weapon(); return 1;} break;
613   }
614 }
615  return 0;
616}
617
618
619void view::draw_hp()
620{
621  if (focus)
622  {
623    int h=focus->hp();   
624    last_hp=h;
625    sbar.draw_health(screen,focus->hp());
626  } else
627    sbar.draw_health(screen,0);
628}
629
630int view::drawable()
631
632  return local_player();
633}
634
635
636void recalc_local_view_space()   // calculates view areas for local players, should be called
637                                 // when adding or deleting local players
638{
639  if (screen)
640  {
641    int t=total_local_players();
642    if (!t) return ;
643
644    int Xres=small_render ? xres/2 : xres;
645    int Yres=small_render ? yres/2 : yres;
646
647    int h=Yres/t;
648    int w=h*320/200,y=5;
649    if (w<300) w=300;
650
651    for (view *f=player_list;f;f=f->next)
652    {
653      if (f->local_player())
654      {
655        f->suggest.cx1=Xres/2-w/2;
656        f->suggest.cx2=Xres/2+w/2;
657        if (f->suggest.cx1<2) f->suggest.cx1=2;
658        if (f->suggest.cx2>Xres-2) f->suggest.cx2=Xres-2;   
659
660        f->suggest.cy1=y;
661        f->suggest.cy2=h-(total_weapons ? 33 : 0);
662
663        f->suggest.shift_down=f->shift_down;
664        f->suggest.shift_right=f->shift_right;
665        f->suggest.pan_x=f->pan_x;
666        f->suggest.pan_y=f->pan_y;
667        f->suggest.send_view=1;
668
669        if (!player_list->next)
670        {
671          f->cx1=f->suggest.cx1;
672          f->cy1=f->suggest.cy1;
673          f->cx2=f->suggest.cx2;
674          f->cy2=f->suggest.cy2;
675          f->suggest.send_view=0;
676        }
677        y+=h;
678      }
679    }
680  }
681
682}
683
684
685void set_local_players(int total)
686{
687  int rdw=0;
688  if (total<1) return ;
689
690  view *last=NULL;
691  for (view *f=player_list;f;f=f->next)
692  {
693    if (total && f->local_player())
694      total--;
695    else if (!total && f->local_player())  // too many local players, delete this one
696    {
697      view *n=last->next;
698      while (n && !n->local_player()) n=n->next;  // find next local player
699
700      if (last)
701        last->next=n;
702      else       
703      {
704        if (n)    // make sure we have at least one local player
705          player_list=n;
706      }           
707      last=f;
708      rdw=1;
709    }
710  }
711 
712  while (total)   // see if we need to add new players
713  {
714    game_object *o=create(current_start_type,50,50);   
715    view *v;
716    if (!player_list)
717    {
718      player_list=new view(o,NULL,0);
719      v=player_list;
720    }
721    else
722    {
723      view *f=player_list;
724      for (;f && f->next;f=f->next);     
725      f->next=new view(o,NULL,f->player_number+1);
726      v=f->next;
727    }
728    v->cx1=320/2-155; v->cy1=200/2-95; v->cx2=320/2+155; v->cy2=200/2+(total_weapons ? 60 : 95);
729    v->focus->set_controller(v);
730    total--;
731    rdw=1;
732  } 
733  if (rdw)
734    recalc_local_view_space();
735}
736
737
738int total_local_players()
739{
740  int t=0;
741  for (view *f=player_list;f;f=f->next)
742    if (f->local_player()) t++;
743  return t;
744}
745
746
747void view::resize_view(long Cx1, long Cy1, long Cx2, long Cy2)
748{
749  if (cx1!=Cx1 || cx2!=Cx2 || cy1!=Cy1 || cy2!=Cy2)
750  {
751    cx1=Cx1; cy1=Cy1;
752    cx2=Cx2; cy2=Cy2;
753    if (playing_state(the_game->state) && local_player())
754      the_game->draw(0);
755  }
756}
757
758
759void view::set_input(int cx, int cy, int b1, int b2, int b3, int b4, int px, int py)
760{
761  x_suggestion=cx;
762  y_suggestion=cy;
763  b1_suggestion=b1;
764  b2_suggestion=b2;
765  b3_suggestion=b3;
766  b4_suggestion=b4;
767  pointer_x=px;
768  pointer_y=py;
769}
770
771
772
773void view::reset_player()
774{
775  if (focus)
776  {
777   
778    game_object *start=current_level ? current_level->get_random_start(320,focus->controller()) : 0;
779    focus->defaults();
780    if (start)
781    {
782      focus->x=start->x;
783      focus->y=start->y;
784      dprintf("reset position to %d %d\n",start->x,start->y);
785    }
786    focus->set_state(stopped);
787    memset(weapons,0xff,total_weapons*sizeof(long));   
788    memset(last_weapons,0xff,total_weapons*sizeof(long));   
789
790    shift_down=SHIFT_DOWN_DEFAULT;
791    shift_right=SHIFT_RIGHT_DEFAULT;
792
793    if (total_weapons)
794      weapons[0]=0;  // give him the first weapon
795    current_weapon=0;
796
797    memset(focus->lvars,0,figures[focus->otype]->tv*4);
798    focus->set_aistate(0);
799    if (figures[focus->otype]->get_fun(OFUN_CONSTRUCTOR))
800    {
801      game_object *o=current_object;
802      current_object=focus;
803      eval_user_fun((lisp_symbol *)figures[focus->otype]->get_fun(OFUN_CONSTRUCTOR),NULL);
804      current_object=o;
805    }
806    sbar.redraw(screen);
807
808    int i;
809    for (i=0;i<focus->total_objects();i++)   // reset the vars for the attached objects
810    {     
811      game_object *o=focus->get_object(i);
812      memset(o->lvars,0,figures[o->otype]->tv*4);
813    }
814
815  }
816}
817
818
819
820
821
822object_node *make_player_onodes(int player_num)
823{
824  object_node *first=NULL,*last;
825  for (view *o=player_list;o;o=o->next)
826  {
827    if (o->focus && (player_num==-1 || o->player_number==player_num))
828    {
829      if (!object_to_number_in_list(o->focus,first))
830      {
831        object_node *q=new object_node(o->focus,NULL);
832        if (first)
833          last->next=q;
834        else first=q;
835        last=q;
836      }
837      for (int i=0;i<o->focus->total_objects();i++)
838      {
839        game_object *p=o->focus->get_object(i);
840
841        if (!object_to_number_in_list(p,first))
842        {
843          object_node *q=new object_node(p,NULL);
844          if (first)
845            last->next=q;
846          else first=q;
847          last=q;
848        }
849      }     
850    }
851  }
852  return first;
853}
854
855
856
857
858enum { V_CX1, V_CY1, V_CX2, V_CY2,
859       V_SHIFT_DOWN, V_SHIFT_RIGHT,
860       V_GOD,
861       V_PLAYER_NUMBER,
862       V_DRAW_SOLID,
863       V_LIVES,
864       V_CURRENT_WEAPON,
865       V_X_SUGGESTION, V_Y_SUGGESTION, V_B1_SUGGESTION, V_B2_SUGGESTION, V_B3_SUGGESTION, V_B4_SUGGESTION,
866       V_PAN_X, V_PAN_Y,
867       V_NO_XLEFT, V_NO_XRIGHT, V_NO_YTOP, V_NO_YBOTTOM,
868       V_LAST_X, V_LAST_Y, V_LAST_LEFT, V_LAST_RIGHT, V_LAST_UP, V_LAST_DOWN,
869       V_LAST_B1, V_LAST_B2, V_LAST_B3, V_LAST_B4,
870       V_LAST_HP,
871       V_SECRETS, V_KILLS, V_TSECRETS, V_TKILLS,
872       V_AMBIENT,
873       V_POINTER_X, V_POINTER_Y,
874       V_LAST_LAST_X, V_LAST_LAST_Y,
875       V_FREEZE_TIME };
876
877#define TVV (V_FREEZE_TIME+1)
878
879static char *vv_names[TVV]={
880  "view.cx1",  "view.cy1",  "view.cx2",  "view.cy2",
881  "view.shift_down",  "view.shift_right",
882  "view.god",
883  "view.player_number",
884  "view.draw_solid",
885  "view.lives",
886  "view.current_weapon",
887  "view.x_suggestion",  "view.y_suggestion", 
888   "view.b1_suggestion",  "view.b2_suggestion",  "view.b3_suggestion",  "view.b4_suggestion",
889  "view.pan_x",  "view.pan_y",
890  "view.no_xleft",  "view.no_xright",  "view.no_ytop",  "view.no_ybottom",
891  "view.last_x",  "view.last_y",  "view.last_left",  "view.last_right",  "view.last_up",  "view.last_down",
892  "view.last_b1",  "view.last_b2",  "view.last_b3",  "view.last_b4",
893  "view.last_hp",
894  "view.secrets",  "view.kills",  "view.tsecrets",  "view.tkills",
895  "view.ambient",
896  "view.pointer_x",  "view.pointer_y",
897  "view.last_last_x",  "view.last_last_y",
898  "view.freeze_time"
899};
900
901
902int total_view_vars()
903{ return TVV;
904}
905
906char *get_view_var_name(int num)
907{ return vv_names[num]; }
908
909long view::get_view_var_value(int num)
910{
911  switch (num)
912  {
913    case V_CX1 : return cx1; break;
914    case V_CY1 : return cy1; break;
915    case V_CX2 : return cx2; break;
916    case V_CY2 : return cy2; break;
917    case V_SHIFT_DOWN : return shift_down; break;
918    case V_SHIFT_RIGHT : return shift_right; break;
919    case V_GOD : return god; break;
920    case V_PLAYER_NUMBER : return player_number; break;
921
922    case V_DRAW_SOLID : return draw_solid; break;
923    case V_CURRENT_WEAPON : return current_weapon; break;
924    case V_X_SUGGESTION : return x_suggestion; break;
925    case V_Y_SUGGESTION : return y_suggestion; break;
926    case V_B1_SUGGESTION : return b1_suggestion; break;
927    case V_B2_SUGGESTION : return b2_suggestion; break;
928    case V_B3_SUGGESTION : return b3_suggestion; break;
929    case V_B4_SUGGESTION : return b4_suggestion; break;
930
931    case V_PAN_X : return pan_x; break;
932    case V_PAN_Y : return pan_y; break;
933    case V_NO_XLEFT : return no_xleft; break;
934    case V_NO_XRIGHT : return no_xright; break;
935    case V_NO_YTOP : return no_ytop; break;
936    case V_NO_YBOTTOM : return no_ybottom; break;
937    case V_LAST_X : return last_x; break;
938    case V_LAST_Y : return last_y; break;
939    case V_LAST_LEFT : return last_left; break;
940    case V_LAST_RIGHT : return last_right; break;
941    case V_LAST_UP : return last_up; break;
942    case V_LAST_DOWN : return last_down; break;
943    case V_LAST_B1 : return last_b1; break;
944    case V_LAST_B2 : return last_b2; break;
945    case V_LAST_B3 : return last_b3; break;
946    case V_LAST_B4 : return last_b4; break;
947    case V_LAST_HP : return last_hp; break;
948    case V_SECRETS : return secrets; break;
949    case V_KILLS : return kills; break;
950    case V_TSECRETS : return tsecrets; break;
951    case V_TKILLS : return tkills; break;
952    case V_AMBIENT : return ambient; break;
953    case V_POINTER_X : return pointer_x; break;
954    case V_POINTER_Y : return pointer_y; break;
955    case V_LAST_LAST_X : return last_last_x; break;
956    case V_LAST_LAST_Y : return last_last_y; break;
957    case V_FREEZE_TIME : return freeze_time; break;
958  }
959  return 0;
960}
961
962
963
964long view::set_view_var_value(int num, long x)
965{
966  switch (num)
967  {
968    case V_CX1 : cx1=x; break;
969    case V_CY1 : cy1=x; break;
970    case V_CX2 : cx2=x; break;
971    case V_CY2 : cy2=x; break;
972    case V_SHIFT_DOWN : shift_down=x; break;
973    case V_SHIFT_RIGHT : shift_right=x; break;
974    case V_GOD : god=x; break;
975    case V_PLAYER_NUMBER : { player_number=x; if (local_player()) sbar.associate(this); }  break;
976
977    case V_DRAW_SOLID : draw_solid=x; break;
978    case V_CURRENT_WEAPON : { current_weapon=x; sbar.need_refresh(); } break;
979    case V_X_SUGGESTION : x_suggestion=x; break;
980    case V_Y_SUGGESTION : y_suggestion=x; break;
981    case V_B1_SUGGESTION : b1_suggestion=x; break;
982    case V_B2_SUGGESTION : b2_suggestion=x; break;
983    case V_B3_SUGGESTION : b3_suggestion=x; break;
984    case V_B4_SUGGESTION : b4_suggestion=x; break;
985
986    case V_PAN_X : pan_x=x; break;
987    case V_PAN_Y : pan_y=x; break;
988    case V_NO_XLEFT : no_xleft=x; break;
989    case V_NO_XRIGHT : no_xright=x; break;
990    case V_NO_YTOP : no_ytop=x; break;
991    case V_NO_YBOTTOM : no_ybottom=x; break;
992    case V_LAST_X : last_x=x; break;
993    case V_LAST_Y : last_y=x; break;
994    case V_LAST_LEFT : last_left=x; break;
995    case V_LAST_RIGHT : last_right=x; break;
996    case V_LAST_UP : last_up=x; break;
997    case V_LAST_DOWN : last_down=x; break;
998    case V_LAST_B1 : last_b1=x; break;
999    case V_LAST_B2 : last_b2=x; break;
1000    case V_LAST_B3 : last_b3=x; break;
1001    case V_LAST_B4 : last_b4=x; break;
1002
1003    case V_LAST_HP : last_hp=x; break;
1004    case V_SECRETS : secrets=x; break;
1005    case V_KILLS : kills=x; break;
1006    case V_TSECRETS : tsecrets=x; break;
1007    case V_TKILLS : tkills=x; break;
1008    case V_AMBIENT : ambient=x; break;
1009    case V_POINTER_X : pointer_x=x; break;
1010    case V_POINTER_Y : pointer_y=x; break;
1011    case V_LAST_LAST_X : last_last_x=x; break;
1012    case V_LAST_LAST_Y : last_last_y=x; break;
1013    case V_FREEZE_TIME : freeze_time=x; break;
1014  }
1015  return 1;
1016}
1017
1018
1019void view::configure_for_area(area_controller *a)
1020{
1021  if (a->ambient>=0 && a->ambient!=ambient)
1022  {
1023    if (ambient>a->ambient)
1024    {
1025      ambient-=a->ambient_speed;
1026      if (ambient<a->ambient)
1027        ambient=a->ambient;
1028    }
1029    else
1030    {
1031      ambient+=a->ambient_speed;
1032      if (ambient>a->ambient)
1033        ambient=a->ambient;
1034    }   
1035  }
1036
1037  if (!view_shift_disabled)
1038  {
1039    if (a->view_xoff!=pan_x)
1040    {
1041      if (pan_x>a->view_xoff)
1042      {
1043        pan_x-=a->view_xoff_speed;
1044        if (pan_x<a->view_xoff)
1045        pan_x=a->view_xoff;
1046      }
1047      else
1048      {
1049        pan_x+=a->view_xoff_speed;
1050        if (pan_x>a->view_xoff)
1051        pan_x=a->view_xoff;
1052      }
1053    }
1054
1055    if (a->view_yoff!=pan_y)
1056    {
1057      if (pan_y>a->view_yoff)
1058      {
1059        pan_y-=a->view_yoff_speed;
1060        if (pan_y<a->view_yoff)
1061        pan_y=a->view_yoff;
1062      }
1063      else
1064      {
1065        pan_y+=a->view_yoff_speed;
1066        if (pan_y>a->view_yoff)
1067        pan_y=a->view_yoff;
1068      }
1069    } 
1070  }
1071}
1072
1073
1074void process_packet_commands(uchar *pk, int size)
1075{
1076  long sync_short=-1;
1077
1078  if (!size) return ;
1079  pk[size]=SCMD_END_OF_PACKET;
1080
1081  uchar cmd;
1082  int already_reloaded=0;
1083
1084
1085  do
1086  {
1087    cmd=*(pk++);
1088    switch (cmd)
1089    {
1090      case SCMD_WEAPON_CHANGE :
1091      case SCMD_SET_INPUT :
1092      case SCMD_VIEW_RESIZE :
1093      case SCMD_KEYPRESS :
1094      case SCMD_KEYRELEASE :
1095      case SCMD_EXT_KEYPRESS :
1096      case SCMD_EXT_KEYRELEASE :
1097      case SCMD_CHAT_KEYPRESS :
1098      {
1099        uchar player_num=*(pk++);
1100
1101        view *v=player_list;
1102        for (;v && v->player_number!=player_num;v=v->next);
1103        if (v)
1104        {
1105          if (v->player_number==player_num)
1106          v->process_input(cmd,pk);
1107        }     
1108        else
1109        {
1110          dprintf("Evil error : bad player number in packet\n");
1111          return ;
1112        }
1113      } break;
1114      case SCMD_RELOAD :
1115      {
1116        if (!already_reloaded)
1117        {
1118          net_reload();
1119          already_reloaded=1;
1120        }
1121      } break;
1122
1123      case SCMD_SYNC :
1124      {
1125        ushort x;
1126        memcpy(&x,pk,2);  pk+=2;
1127        x=lstl(x);
1128        if (demo_man.current_state()==demo_manager::PLAYING)
1129        sync_short=make_sync();
1130
1131        if (sync_short==-1)
1132        sync_short=x;
1133        else if (x!=sync_short && !already_reloaded)
1134        {
1135          dprintf("out of sync %d (packet=%d, calced=%d)\n",current_level->tick_counter(),x,sync_short);
1136          if (demo_man.current_state()==demo_manager::NORMAL)
1137            net_reload();
1138          already_reloaded=1;
1139        }
1140      } break;
1141      case SCMD_DELETE_CLIENT :
1142      {
1143        uchar player_num=*(pk++);
1144        view *v=player_list,*last=NULL;
1145        for (;v && v->player_number!=player_num;v=v->next)
1146        last=v;
1147        if (!v)
1148        dprintf("evil : delete client %d, but no such client\n");
1149        else
1150        {
1151
1152          // make a list of all objects associated with this player
1153          object_node *on=make_player_onodes(player_num);   
1154          while (on)
1155          {
1156            current_level->delete_object(on->me);
1157            object_node *last=on;
1158            on=on->next;
1159            delete last;
1160          }
1161
1162          v->focus=NULL;
1163          if (last)
1164          last->next=v->next;
1165          else player_list=player_list->next;
1166
1167          delete v;
1168        }
1169      } break;
1170      defualt :
1171      dprintf("Unknown net command %d\n",cmd);
1172
1173    }
1174  } while (cmd!=SCMD_END_OF_PACKET);
1175}
Note: See TracBrowser for help on using the repository browser.