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

Last change on this file since 651 was 651, checked in by Sam Hocevar, 9 years ago

build: add a --disable-network compilation flag and get rid of most of
the CELLOS_LV2 ifdefs.

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