source: abuse/tags/pd/macabuse/src/view.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 26.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    dprintf("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    dprintf("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    dprintf("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( __MAC__ )
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  set_size();
228
229  focus=Focus;
230  next=Next;
231  shift_down=SHIFT_DOWN_DEFAULT;
232  shift_right=SHIFT_RIGHT_DEFAULT;
233  x_suggestion=0;
234  y_suggestion=0;
235  b1_suggestion=0;
236  b2_suggestion=0;
237  b3_suggestion=0;
238  b4_suggestion=0;
239  pointer_x=0;
240  pointer_y=0;
241
242  pan_x=0;
243  pan_y=0;
244  last_type=0;
245  freeze_time=0;
246
247  if (total_weapons)
248  {
249    weapons=(long *)jmalloc(total_weapons*sizeof(long),"weapon array");
250    last_weapons=(long *)jmalloc(total_weapons*sizeof(long),"last weapon array");
251    memset(weapons,0xff,total_weapons*sizeof(long));   // set all to -1
252    memset(last_weapons,0xff,total_weapons*sizeof(long));   // set all to -1 
253  }
254
255  if (total_weapons)
256    weapons[0]=0;
257  if (local_player())
258    sbar.associate(this);
259  sbar.need_refresh();
260}
261
262long view::x_center()
263{
264  if (!focus)
265    return (cx1+cx2)/2;
266  else
267    return focus->x;
268}   
269
270long view::y_center()
271{
272  if (!focus)
273    return (cy1+cy2)/2;
274  else
275    return focus->y;
276}
277
278void view::draw_character_damage()
279{   
280  if (focus && drawable())
281  {
282    if (last_hp!=focus->hp()) draw_hp();
283    int i;
284    for (i=0;i<total_weapons;i++)
285      if (weapons[i]!=last_weapons[i])
286      {
287        last_weapons[i]=weapons[i];
288        sbar.draw_ammo(screen,i,weapons[i],current_weapon==i);
289      }
290  }
291}
292
293
294
295ushort make_sync()
296{
297  ushort x=0;
298  if (!current_level) return 0;
299  if (current_level)
300  {
301    view *f=player_list;
302    for (;f;f=f->next)
303    {
304      if (f->focus)
305      {
306        x^=(f->focus->x&0xffff);
307        x^=(f->focus->y&0xffff);
308      }
309    }
310  }
311  x^=rand_on;
312
313  return x;
314}
315
316
317
318void view::get_input()
319{
320  int sug_x,sug_y,sug_b1,sug_b2,sug_b3,sug_b4;
321  long sug_px,sug_py;
322
323
324  if (the_game->key_down(lnumber_value(symbol_value(l_left_key))))
325    sug_x=-1;
326  else if (the_game->key_down(lnumber_value(symbol_value(l_right_key))))
327    sug_x=1;
328  else sug_x=0;
329
330  if (the_game->key_down(lnumber_value(symbol_value(l_up_key))))
331    sug_y=-1;
332  else if (the_game->key_down(lnumber_value(symbol_value(l_down_key))))
333    sug_y=1;
334  else sug_y=0;
335
336  if ((last_demo_mbut&2) || the_game->key_down(lnumber_value(symbol_value(l_special_key))))
337    sug_b1=1;
338  else sug_b1=0;
339
340  if (last_demo_mbut&1)
341    sug_b2=1;
342  else sug_b2=0;
343
344  sug_b3=sug_b4=0;
345  the_game->mouse_to_game(last_demo_mx,last_demo_my,sug_px,sug_py,this);
346
347
348  /*
349
350  if (DEFINEDP(symbol_function(l_get_local_input)))
351  {
352    void *ret=eval_function((lisp_symbol *)l_get_local_input,NULL);
353    sug_x=lnumber_value(CAR(ret));  ret=CDR(ret);
354    sug_y=lnumber_value(CAR(ret));  ret=CDR(ret);
355    if (CAR(ret)) sug_b1=1; else sug_b1=0; ret=CDR(ret);
356    if (CAR(ret)) sug_b2=1; else sug_b2=0;  ret=CDR(ret);
357    int x=lnumber_value(CAR(ret)); ret=CDR(ret);
358    if (x<0) sug_b3=1; else sug_b3=0;
359    if (x>0) sug_b4=1; else sug_b4=0;   
360   
361    long bx=lnumber_value(CAR(ret));  ret=CDR(ret);
362    long by=lnumber_value(CAR(ret));  ret=CDR(ret);
363    the_game->mouse_to_game(bx,by,sug_px,sug_py,this);
364
365  } else
366  {
367    get_movement(0,sug_x,sug_y,sug_b1,sug_b2,sug_b3,sug_b4);
368    if (focus)
369    {
370      the_game->mouse_to_game(last_demo_mx,last_demo_my,sug_px,sug_py,this);
371      if (last_demo_mbut&1) sug_b2=1;
372      if (last_demo_mbut&2) sug_b1=1;
373    } else sug_px=sug_py=0;
374  }
375  */
376
377  if (view_changed())
378  {
379    base->packet.write_byte(SCMD_VIEW_RESIZE);
380    base->packet.write_byte(player_number);
381    base->packet.write_long(suggest.cx1);
382    base->packet.write_long(suggest.cy1);
383    base->packet.write_long(suggest.cx2);
384    base->packet.write_long(suggest.cy2);
385
386    base->packet.write_long(suggest.pan_x);
387    base->packet.write_long(suggest.pan_y);
388    base->packet.write_long(suggest.shift_down);
389    base->packet.write_long(suggest.shift_right);
390  }
391
392  if (weapon_changed())
393  {
394    base->packet.write_byte(SCMD_WEAPON_CHANGE);
395    base->packet.write_byte(player_number);
396    base->packet.write_long(suggest.new_weapon);
397  }
398
399  base->packet.write_byte(SCMD_SET_INPUT);
400  base->packet.write_byte(player_number);
401
402
403  uchar mflags=0;
404  if (sug_x>0)
405    mflags|=1;
406  else if (sug_x<0)
407    mflags|=2;
408
409  if (sug_y>0)
410    mflags|=4;
411  else if (sug_y<0)
412    mflags|=8;
413
414  if (sug_b1) mflags|=16;
415  if (sug_b2) mflags|=32;
416  if (sug_b3) mflags|=64;
417  if (sug_b4) mflags|=128;
418
419  base->packet.write_byte(mflags);
420
421  base->packet.write_short((ushort)((short)sug_px));
422  base->packet.write_short((ushort)((short)sug_py));
423
424}
425
426
427void view::add_chat_key(int key)  // return string if buf is complete
428{
429  int len=strlen(chat_buf);
430  if (key==JK_BACKSPACE)
431  {
432    if (len)
433    {
434      chat_buf[len-1]=0;
435      if (local_player() && chat)
436        chat->draw_user(chat_buf);
437    }
438  } else if (key!=JK_ENTER)
439  {
440    chat_buf[len]=key;
441    chat_buf[len+1]=0;
442    if (local_player() && chat)
443      chat->draw_user(chat_buf);
444  }
445
446  if (len>38 || key==JK_ENTER)
447  {
448    if (DEFINEDP(symbol_function(l_chat_input)))
449    {
450      game_object *o=current_object;
451      current_object=focus;
452
453      void *m=mark_heap(TMP_SPACE);
454      void *list=NULL;
455      push_onto_list(new_lisp_string(chat_buf),list);       
456      eval_function((lisp_symbol *)l_chat_input,list);
457      restore_heap(m,TMP_SPACE);
458
459      current_object=o;
460
461    } else
462    {           
463      if (chat)
464        chat->put_all(chat_buf);
465    }
466    chat_buf[0]=0;
467    if (local_player() && chat)
468      chat->draw_user(chat_buf);
469  }
470}
471
472int view::process_input(char cmd, uchar *&pk)   // return 0 if something went wrong
473{
474  switch (cmd)
475  {
476    case SCMD_CHAT_KEYPRESS :
477    {
478      add_chat_key(*(pk++));
479    } break;
480    case SCMD_VIEW_RESIZE :
481    {
482      long x[8];
483      memcpy(x,pk,8*4);  pk+=8*4;
484      /*      cx1=lltl(x[0]);
485      cy1=lltl(x[1]);
486      cx2=lltl(x[2]);
487      cy2=lltl(x[3]); */
488       
489      pan_x=lltl(x[4]);
490      pan_y=lltl(x[5]);
491      shift_down=lltl(x[6]);
492      shift_right=lltl(x[7]);
493      if (small_render)
494      {
495        small_render->resize(cx2-cx1+1,cy2-cy1+1);
496      }
497
498      suggest.send_view=0;
499      if (local_player())
500        the_game->draw();
501      return 1;
502    }
503    case SCMD_WEAPON_CHANGE :
504    {
505      long x;
506      memcpy(&x,pk,4);  pk+=4;
507      current_weapon=lltl(x);
508
509      if (local_player())
510        sbar.need_refresh();
511      suggest.send_weapon_change=0;
512      return 1;
513    } break;
514
515    case SCMD_SET_INPUT :
516    {
517      uchar x=*(pk++);
518
519      if (x&1) x_suggestion=1;
520      else if (x&2) x_suggestion=-1;
521      else x_suggestion=0;
522
523      if (x&4) y_suggestion=1;
524      else if (x&8) y_suggestion=-1;
525      else y_suggestion=0;
526
527      if (x&16) b1_suggestion=1; else b1_suggestion=0;
528      if (x&32) b2_suggestion=1; else b2_suggestion=0;
529      if (x&64) b3_suggestion=1; else b3_suggestion=0;
530      if (x&128) b4_suggestion=1; else b4_suggestion=0;
531
532      ushort p[2];
533      memcpy(p,pk,2*2);  pk+=2*2;
534     
535      pointer_x=(short)(lstl(p[0]));
536      pointer_y=(short)(lstl(p[1]));
537       
538      return 1;
539    } break;
540    case SCMD_KEYPRESS : set_key_down(*(pk++),1); break;
541    case SCMD_EXT_KEYPRESS : set_key_down(*(pk++)+256,1); break;
542    case SCMD_KEYRELEASE : set_key_down(*(pk++),0); break;
543    case SCMD_EXT_KEYRELEASE : set_key_down(*(pk++)+256,0); break;
544  }
545  return 1;
546}
547
548int view::local_player()
549{
550  return player_number==client_number();
551}
552
553void view::next_weapon()
554{
555  int c=current_weapon;
556
557  while (c<total_weapons-1)
558  {
559    c++;
560    if (weapon_total(c)>0)
561    {
562      suggest.send_weapon_change=1;
563      suggest.new_weapon=c;
564      return ;
565    }
566  }
567 
568  c=0;
569  while (c!=current_weapon)
570  {
571    if (weapon_total(c)>0)
572    {
573      suggest.send_weapon_change=1;
574      suggest.new_weapon=c;
575      return ;
576    }
577    c++;
578  }
579}
580
581void view::last_weapon()
582{
583 
584  int c=current_weapon;
585
586  while (c>=1)
587  {
588    c--;
589    if (weapon_total(c)>0 || c==0)
590    {
591      suggest.send_weapon_change=1;
592      suggest.new_weapon=c;
593      return ;
594    }
595  }
596
597  c=total_weapons-1;
598  while (c!=current_weapon)
599  {
600    if (weapon_total(c)>0 || c==0)
601    {
602      suggest.send_weapon_change=1;
603      suggest.new_weapon=c;
604      return ;
605    }
606    c--;
607  }
608
609}
610
611int view::handle_event(event &ev)
612{
613  if (ev.type==EV_KEY)
614  {
615                if (ev.key == lnumber_value(symbol_value(l_weapon_left_key)))
616    {
617        if (total_weapons)
618                last_weapon();
619        return 1;
620    }
621                else if (ev.key == lnumber_value(symbol_value(l_weapon_right_key)))
622    {
623        if (total_weapons)
624                next_weapon();
625        return 1;
626    }
627#ifndef __MAC__
628                else if (ev.key >= '1' && ev.key <= '7')
629          {
630                if (((dev&EDIT_MODE)==0) && (weapon_total(ev.key-'1')>0))
631            {
632                                suggest.send_weapon_change=1;
633                                suggest.new_weapon=ev.key-'1';
634            }
635          }
636#endif
637 }
638  return 0;
639}
640
641
642void view::draw_hp()
643{
644  if (focus)
645  {
646    int h=focus->hp();   
647    last_hp=h;
648    sbar.draw_health(screen,focus->hp());
649  } else
650    sbar.draw_health(screen,0);
651}
652
653int view::drawable()
654
655  return local_player();
656}
657
658
659void recalc_local_view_space()   // calculates view areas for local players, should be called
660                                 // when adding or deleting local players
661{
662  if (screen)
663  {
664
665    int Xres=small_render ? xres/2 : xres;
666    int Yres=small_render ? yres/2 : yres;
667
668    int h=Yres;
669    int w=h*320/200,y=5;
670    if (w<300) w=300;
671
672    for (view *f=player_list;f;f=f->next)
673    {
674      f->set_size();
675      /*
676      if (f->local_player())
677      {
678                                f->suggest.cx1=0;
679                                f->suggest.cx2=Xres;
680//                              f->suggest.cx1=Xres/2-w/2;
681//                              f->suggest.cx2=Xres/2+w/2;
682//                              if (f->suggest.cx1<2) f->suggest.cx1=2;
683//                              if (f->suggest.cx2>Xres-2) f->suggest.cx2=Xres-2;   
684                       
685                                f->suggest.cy1=y;
686                                f->suggest.cy2=h-(total_weapons ? 33 : 0);
687                       
688                                f->suggest.shift_down=f->shift_down;
689                                f->suggest.shift_right=f->shift_right;
690                                f->suggest.pan_x=f->pan_x;
691                                f->suggest.pan_y=f->pan_y;
692                                f->suggest.send_view=1;
693                       
694                                if (!player_list->next)
695                                {
696                                  f->cx1=f->suggest.cx1;
697                                  f->cy1=f->suggest.cy1;
698                                  f->cx2=f->suggest.cx2;
699                                  f->cy2=f->suggest.cy2;
700                                  f->suggest.send_view=0;
701                                }
702                                y+=h;
703      } */
704
705
706    }
707  }
708
709}
710
711
712void set_local_players(int total)
713{
714  int rdw=0;
715  if (total<1) return ;
716
717  view *last=NULL;
718  for (view *f=player_list;f;f=f->next)
719  {
720    if (total && f->local_player())
721      total--;
722    else if (!total && f->local_player())  // too many local players, delete this one
723    {
724      view *n=last->next;
725      while (n && !n->local_player()) n=n->next;  // find next local player
726
727      if (last)
728        last->next=n;
729      else       
730      {
731        if (n)    // make sure we have at least one local player
732          player_list=n;
733      }           
734      last=f;
735      rdw=1;
736    }
737  }
738 
739  while (total)   // see if we need to add new players
740  {
741    game_object *o=create(current_start_type,50,50);   
742    view *v;
743    if (!player_list)
744    {
745      player_list=new view(o,NULL,0);
746      v=player_list;
747    }
748    else
749    {
750      view *f=player_list;
751      for (;f && f->next;f=f->next);     
752      f->next=new view(o,NULL,f->player_number+1);
753      v=f->next;
754    }
755
756    v->set_size();
757
758
759    //  v->cx1=320/2-155; v->cy1=200/2-95; v->cx2=320/2+155; v->cy2=200/2+(total_weapons ? 60 : 95);
760    v->focus->set_controller(v);
761    total--;
762    rdw=1;
763  } 
764  if (rdw)
765    recalc_local_view_space();
766}
767
768
769int total_local_players()
770{
771  int t=0;
772  for (view *f=player_list;f;f=f->next)
773    if (f->local_player()) t++;
774  return t;
775}
776
777
778void view::resize_view(long Cx1, long Cy1, long Cx2, long Cy2)
779{
780  if (cx1!=Cx1 || cx2!=Cx2 || cy1!=Cy1 || cy2!=Cy2)
781  {
782    cx1=Cx1; cy1=Cy1;
783    cx2=Cx2; cy2=Cy2;
784    if (playing_state(the_game->state) && local_player())
785      the_game->draw(0);
786  }
787}
788
789
790void view::set_input(int cx, int cy, int b1, int b2, int b3, int b4, int px, int py)
791{
792  x_suggestion=cx;
793  y_suggestion=cy;
794  b1_suggestion=b1;
795  b2_suggestion=b2;
796  b3_suggestion=b3;
797  b4_suggestion=b4;
798  pointer_x=px;
799  pointer_y=py;
800}
801
802
803
804void view::reset_player()
805{
806  if (focus)
807  {
808    if (!player_list->next)
809      tkills=0;
810
811    kills=0;
812
813    game_object *start=current_level ? current_level->get_random_start(320,focus->controller()) : 0;
814    focus->defaults();
815    if (start)
816    {
817      focus->x=start->x;
818      focus->y=start->y;
819      dprintf("reset position to %d %d\n",start->x,start->y);
820    }
821    focus->set_state(stopped);
822    memset(weapons,0xff,total_weapons*sizeof(long));   
823    memset(last_weapons,0xff,total_weapons*sizeof(long));   
824
825    shift_down=SHIFT_DOWN_DEFAULT;
826    shift_right=SHIFT_RIGHT_DEFAULT;
827
828    if (total_weapons)
829      weapons[0]=0;  // give him the first weapon
830    current_weapon=0;
831
832    memset(focus->lvars,0,figures[focus->otype]->tv*4);
833    focus->set_aistate(0);
834    if (figures[focus->otype]->get_fun(OFUN_CONSTRUCTOR))
835    {
836      game_object *o=current_object;
837      current_object=focus;
838      eval_user_fun((lisp_symbol *)figures[focus->otype]->get_fun(OFUN_CONSTRUCTOR),NULL);
839      current_object=o;
840    }
841    sbar.redraw(screen);
842
843    int i;
844    for (i=0;i<focus->total_objects();i++)   // reset the vars for the attached objects
845    {     
846      game_object *o=focus->get_object(i);
847      memset(o->lvars,0,figures[o->otype]->tv*4);
848    }
849
850  }
851}
852
853
854
855
856
857object_node *make_player_onodes(int player_num)
858{
859  object_node *first=NULL,*last;
860  for (view *o=player_list;o;o=o->next)
861  {
862    if (o->focus && (player_num==-1 || o->player_number==player_num))
863    {
864      if (!object_to_number_in_list(o->focus,first))
865      {
866        object_node *q=new object_node(o->focus,NULL);
867        if (first)
868          last->next=q;
869        else first=q;
870        last=q;
871      }
872      for (int i=0;i<o->focus->total_objects();i++)
873      {
874        game_object *p=o->focus->get_object(i);
875
876        if (!object_to_number_in_list(p,first))
877        {
878          object_node *q=new object_node(p,NULL);
879          if (first)
880            last->next=q;
881          else first=q;
882          last=q;
883        }
884      }     
885    }
886  }
887  return first;
888}
889
890
891
892
893enum { V_CX1, V_CY1, V_CX2, V_CY2,
894       V_SHIFT_DOWN, V_SHIFT_RIGHT,
895       V_GOD,
896       V_PLAYER_NUMBER,
897       V_DRAW_SOLID,
898       V_LIVES,
899       V_CURRENT_WEAPON,
900       V_X_SUGGESTION, V_Y_SUGGESTION, V_B1_SUGGESTION, V_B2_SUGGESTION, V_B3_SUGGESTION, V_B4_SUGGESTION,
901       V_PAN_X, V_PAN_Y,
902       V_NO_XLEFT, V_NO_XRIGHT, V_NO_YTOP, V_NO_YBOTTOM,
903       V_LAST_X, V_LAST_Y, V_LAST_LEFT, V_LAST_RIGHT, V_LAST_UP, V_LAST_DOWN,
904       V_LAST_B1, V_LAST_B2, V_LAST_B3, V_LAST_B4,
905       V_LAST_HP,
906       V_SECRETS, V_KILLS, V_TSECRETS, V_TKILLS,
907       V_AMBIENT,
908       V_POINTER_X, V_POINTER_Y,
909       V_LAST_LAST_X, V_LAST_LAST_Y,
910       V_FREEZE_TIME };
911
912#define TVV (V_FREEZE_TIME+1)
913
914static char *vv_names[TVV]={
915  "view.cx1",  "view.cy1",  "view.cx2",  "view.cy2",
916  "view.shift_down",  "view.shift_right",
917  "view.god",
918  "view.player_number",
919  "view.draw_solid",
920  "view.lives",
921  "view.current_weapon",
922  "view.x_suggestion",  "view.y_suggestion", 
923   "view.b1_suggestion",  "view.b2_suggestion",  "view.b3_suggestion",  "view.b4_suggestion",
924  "view.pan_x",  "view.pan_y",
925  "view.no_xleft",  "view.no_xright",  "view.no_ytop",  "view.no_ybottom",
926  "view.last_x",  "view.last_y",  "view.last_left",  "view.last_right",  "view.last_up",  "view.last_down",
927  "view.last_b1",  "view.last_b2",  "view.last_b3",  "view.last_b4",
928  "view.last_hp",
929  "view.secrets",  "view.kills",  "view.tsecrets",  "view.tkills",
930  "view.ambient",
931  "view.pointer_x",  "view.pointer_y",
932  "view.last_last_x",  "view.last_last_y",
933  "view.freeze_time"
934};
935
936
937int total_view_vars()
938{ return TVV;
939}
940
941char *get_view_var_name(int num)
942{ return vv_names[num]; }
943
944long view::get_view_var_value(int num)
945{
946  switch (num)
947  {
948    case V_CX1 : return cx1; break;
949    case V_CY1 : return cy1; break;
950    case V_CX2 : return cx2; break;
951    case V_CY2 : return cy2; break;
952    case V_SHIFT_DOWN : return shift_down; break;
953    case V_SHIFT_RIGHT : return shift_right; break;
954    case V_GOD : return god; break;
955    case V_PLAYER_NUMBER : return player_number; break;
956
957    case V_DRAW_SOLID : return draw_solid; break;
958    case V_CURRENT_WEAPON : return current_weapon; break;
959    case V_X_SUGGESTION : return x_suggestion; break;
960    case V_Y_SUGGESTION : return y_suggestion; break;
961    case V_B1_SUGGESTION : return b1_suggestion; break;
962    case V_B2_SUGGESTION : return b2_suggestion; break;
963    case V_B3_SUGGESTION : return b3_suggestion; break;
964    case V_B4_SUGGESTION : return b4_suggestion; break;
965
966    case V_PAN_X : return pan_x; break;
967    case V_PAN_Y : return pan_y; break;
968    case V_NO_XLEFT : return no_xleft; break;
969    case V_NO_XRIGHT : return no_xright; break;
970    case V_NO_YTOP : return no_ytop; break;
971    case V_NO_YBOTTOM : return no_ybottom; break;
972    case V_LAST_X : return last_x; break;
973    case V_LAST_Y : return last_y; break;
974    case V_LAST_LEFT : return last_left; break;
975    case V_LAST_RIGHT : return last_right; break;
976    case V_LAST_UP : return last_up; break;
977    case V_LAST_DOWN : return last_down; break;
978    case V_LAST_B1 : return last_b1; break;
979    case V_LAST_B2 : return last_b2; break;
980    case V_LAST_B3 : return last_b3; break;
981    case V_LAST_B4 : return last_b4; break;
982    case V_LAST_HP : return last_hp; break;
983    case V_SECRETS : return secrets; break;
984    case V_KILLS : return kills; break;
985    case V_TSECRETS : return tsecrets; break;
986    case V_TKILLS : return tkills; break;
987    case V_AMBIENT : return ambient; break;
988    case V_POINTER_X : return pointer_x; break;
989    case V_POINTER_Y : return pointer_y; break;
990    case V_LAST_LAST_X : return last_last_x; break;
991    case V_LAST_LAST_Y : return last_last_y; break;
992    case V_FREEZE_TIME : return freeze_time; break;
993  }
994  return 0;
995}
996
997
998
999long view::set_view_var_value(int num, long x)
1000{
1001  switch (num)
1002  {
1003    /*    case V_CX1 : cx1=x; break;
1004    case V_CY1 : cy1=x; break;
1005    case V_CX2 : cx2=x; break;
1006    case V_CY2 : cy2=x; break; */
1007    case V_SHIFT_DOWN : shift_down=x; break;
1008    case V_SHIFT_RIGHT : shift_right=x; break;
1009    case V_GOD : god=x; break;
1010    case V_PLAYER_NUMBER : { player_number=x; if (local_player()) sbar.associate(this); }  break;
1011
1012    case V_DRAW_SOLID : draw_solid=x; break;
1013    case V_CURRENT_WEAPON : { current_weapon=x; sbar.need_refresh(); } break;
1014    case V_X_SUGGESTION : x_suggestion=x; break;
1015    case V_Y_SUGGESTION : y_suggestion=x; break;
1016    case V_B1_SUGGESTION : b1_suggestion=x; break;
1017    case V_B2_SUGGESTION : b2_suggestion=x; break;
1018    case V_B3_SUGGESTION : b3_suggestion=x; break;
1019    case V_B4_SUGGESTION : b4_suggestion=x; break;
1020
1021    case V_PAN_X : pan_x=x; break;
1022    case V_PAN_Y : pan_y=x; break;
1023    case V_NO_XLEFT : no_xleft=x; break;
1024    case V_NO_XRIGHT : no_xright=x; break;
1025    case V_NO_YTOP : no_ytop=x; break;
1026    case V_NO_YBOTTOM : no_ybottom=x; break;
1027    case V_LAST_X : last_x=x; break;
1028    case V_LAST_Y : last_y=x; break;
1029    case V_LAST_LEFT : last_left=x; break;
1030    case V_LAST_RIGHT : last_right=x; break;
1031    case V_LAST_UP : last_up=x; break;
1032    case V_LAST_DOWN : last_down=x; break;
1033    case V_LAST_B1 : last_b1=x; break;
1034    case V_LAST_B2 : last_b2=x; break;
1035    case V_LAST_B3 : last_b3=x; break;
1036    case V_LAST_B4 : last_b4=x; break;
1037
1038    case V_LAST_HP : last_hp=x; break;
1039    case V_SECRETS : secrets=x; break;
1040    case V_KILLS : kills=x; break;
1041    case V_TSECRETS : tsecrets=x; break;
1042    case V_TKILLS : tkills=x; break;
1043    case V_AMBIENT : ambient=x; break;
1044    case V_POINTER_X : pointer_x=x; break;
1045    case V_POINTER_Y : pointer_y=x; break;
1046    case V_LAST_LAST_X : last_last_x=x; break;
1047    case V_LAST_LAST_Y : last_last_y=x; break;
1048    case V_FREEZE_TIME : freeze_time=x; break;
1049  }
1050  return 1;
1051}
1052
1053
1054void view::configure_for_area(area_controller *a)
1055{
1056  if (a->ambient>=0 && a->ambient!=ambient)
1057  {
1058    if (ambient>a->ambient)
1059    {
1060      ambient-=a->ambient_speed;
1061      if (ambient<a->ambient)
1062        ambient=a->ambient;
1063    }
1064    else
1065    {
1066      ambient+=a->ambient_speed;
1067      if (ambient>a->ambient)
1068        ambient=a->ambient;
1069    }   
1070  }
1071
1072  if (!view_shift_disabled)
1073  {
1074    if (a->view_xoff!=pan_x)
1075    {
1076      if (pan_x>a->view_xoff)
1077      {
1078        pan_x-=a->view_xoff_speed;
1079        if (pan_x<a->view_xoff)
1080        pan_x=a->view_xoff;
1081      }
1082      else
1083      {
1084        pan_x+=a->view_xoff_speed;
1085        if (pan_x>a->view_xoff)
1086        pan_x=a->view_xoff;
1087      }
1088    }
1089
1090    if (a->view_yoff!=pan_y)
1091    {
1092      if (pan_y>a->view_yoff)
1093      {
1094        pan_y-=a->view_yoff_speed;
1095        if (pan_y<a->view_yoff)
1096        pan_y=a->view_yoff;
1097      }
1098      else
1099      {
1100        pan_y+=a->view_yoff_speed;
1101        if (pan_y>a->view_yoff)
1102        pan_y=a->view_yoff;
1103      }
1104    } 
1105  }
1106}
1107
1108
1109void process_packet_commands(uchar *pk, int size)
1110{
1111  long sync_short=-1;
1112
1113  if (!size) return ;
1114  pk[size]=SCMD_END_OF_PACKET;
1115
1116  uchar cmd;
1117  int already_reloaded=0;
1118
1119
1120  do
1121  {
1122    cmd=*(pk++);
1123    switch (cmd)
1124    {
1125      case SCMD_WEAPON_CHANGE :
1126      case SCMD_SET_INPUT :
1127      case SCMD_VIEW_RESIZE :
1128      case SCMD_KEYPRESS :
1129      case SCMD_KEYRELEASE :
1130      case SCMD_EXT_KEYPRESS :
1131      case SCMD_EXT_KEYRELEASE :
1132      case SCMD_CHAT_KEYPRESS :
1133      {
1134        uchar player_num=*(pk++);
1135
1136        view *v=player_list;
1137        for (;v && v->player_number!=player_num;v=v->next);
1138        if (v)
1139        {
1140          if (v->player_number==player_num)
1141          v->process_input(cmd,pk);
1142        }     
1143        else
1144        {
1145          dprintf("Evil error : bad player number in packet\n");
1146          return ;
1147        }
1148      } break;
1149      case SCMD_RELOAD :
1150      {
1151        if (!already_reloaded)
1152        {
1153          net_reload();
1154          already_reloaded=1;
1155        }
1156      } break;
1157
1158      case SCMD_SYNC :
1159      {
1160        ushort x;
1161        memcpy(&x,pk,2);  pk+=2;
1162        x=lstl(x);
1163        if (demo_man.current_state()==demo_manager::PLAYING)
1164        sync_short=make_sync();
1165
1166        if (sync_short==-1)
1167        sync_short=x;
1168        else if (x!=sync_short && !already_reloaded)
1169        {
1170          dprintf("out of sync %d (packet=%d, calced=%d)\n",current_level->tick_counter(),x,sync_short);
1171          dprintf("rand_on=%d\n",rand_on);
1172          int i=0;
1173          view *f=player_list;
1174          for (;f;f=f->next,i++)
1175          {
1176            if (f->focus)
1177              dprintf("player %d (x=%d,y=%d)\n",i,f->focus->x,f->focus->y);
1178          }
1179
1180
1181          if (demo_man.current_state()==demo_manager::NORMAL)
1182            net_reload();
1183          already_reloaded=1;
1184        }
1185      } break;
1186      case SCMD_DELETE_CLIENT :
1187      {
1188        uchar player_num=*(pk++);
1189        view *v=player_list,*last=NULL;
1190        for (;v && v->player_number!=player_num;v=v->next)
1191        last=v;
1192        if (!v)
1193        dprintf("evil : delete client %d, but no such client\n");
1194        else
1195        {
1196
1197          // make a list of all objects associated with this player
1198          object_node *on=make_player_onodes(player_num);   
1199          while (on)
1200          {
1201            current_level->delete_object(on->me);
1202            object_node *last=on;
1203            on=on->next;
1204            delete last;
1205          }
1206
1207          v->focus=NULL;
1208          if (last)
1209          last->next=v->next;
1210          else player_list=player_list->next;
1211
1212          delete v;
1213        }
1214      } break;
1215      defualt :
1216      dprintf("Unknown net command %d\n",cmd);
1217
1218    }
1219  } while (cmd!=SCMD_END_OF_PACKET);
1220}
Note: See TracBrowser for help on using the repository browser.