source: abuse/trunk/src/view.cpp @ 124

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