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

Last change on this file since 657 was 657, checked in by Sam Hocevar, 11 years ago

game: refactor the mouse/game coordinate conversion logic.

File size: 28.0 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    vec2i sug_p(0, 0);
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            sug_p = the_game->MouseToGame(vec2i(last_demo_mx, last_demo_my));
387            if(last_demo_mbut & 1)
388                sug_b2 = 1;
389            if(last_demo_mbut & 2)
390                sug_b1 = 1;
391        }
392    }
393
394    if( view_changed() )
395    {
396        base->packet.write_uint8( SCMD_VIEW_RESIZE );
397        base->packet.write_uint8( player_number );
398        base->packet.write_uint32( suggest.cx1 );
399        base->packet.write_uint32( suggest.cy1 );
400        base->packet.write_uint32( suggest.cx2 );
401        base->packet.write_uint32( suggest.cy2 );
402
403        base->packet.write_uint32( suggest.pan_x );
404        base->packet.write_uint32( suggest.pan_y );
405        base->packet.write_uint32( suggest.shift_down );
406        base->packet.write_uint32( suggest.shift_right );
407    }
408
409    if( weapon_changed() )
410    {
411        base->packet.write_uint8( SCMD_WEAPON_CHANGE );
412        base->packet.write_uint8( player_number );
413        base->packet.write_uint32( suggest.new_weapon );
414    }
415
416    base->packet.write_uint8( SCMD_SET_INPUT );
417    base->packet.write_uint8( player_number );
418
419    uint8_t mflags = 0;
420    if( sug_x > 0 )
421        mflags |= 1;
422    else if ( sug_x < 0 )
423        mflags |= 2;
424
425    if( sug_y > 0 )
426        mflags |= 4;
427    else if( sug_y < 0 )
428        mflags |= 8;
429
430    if( sug_b1 )
431        mflags |= 16;
432    if( sug_b2 )
433        mflags |= 32;
434    if( sug_b3 )
435        mflags |= 64;
436    if( sug_b4 )
437        mflags |= 128;
438
439    base->packet.write_uint8(mflags);
440    base->packet.write_uint16((uint16_t)sug_p.x);
441    base->packet.write_uint16((uint16_t)sug_p.y);
442}
443
444
445void view::add_chat_key(int key)  // return string if buf is complete
446{
447  int len=strlen(chat_buf);
448  if (key==JK_BACKSPACE)
449  {
450    if (len)
451    {
452      chat_buf[len-1]=0;
453      if (local_player() && chat)
454        chat->draw_user(chat_buf);
455    }
456  } else if (key!=JK_ENTER)
457  {
458    chat_buf[len]=key;
459    chat_buf[len+1]=0;
460    if (local_player() && chat)
461      chat->draw_user(chat_buf);
462  }
463
464  if (len>38 || key==JK_ENTER)
465  {
466    if (DEFINEDP(l_chat_input->GetFunction()))
467    {
468      game_object *o=current_object;
469      current_object=focus;
470
471      void *m = LSpace::Tmp.Mark();
472      void *list=NULL;
473      push_onto_list(LString::Create(chat_buf),list);
474      ((LSymbol *)l_chat_input)->EvalFunction(list);
475      LSpace::Tmp.Restore(m);
476
477      current_object=o;
478
479    } else
480    {
481      if (chat)
482        chat->put_all(chat_buf);
483    }
484    chat_buf[0]=0;
485    if (local_player() && chat)
486      chat->draw_user(chat_buf);
487  }
488}
489
490int view::process_input(char cmd, uint8_t *&pk)   // return 0 if something went wrong
491{
492  switch (cmd)
493  {
494    case SCMD_CHAT_KEYPRESS :
495    {
496      add_chat_key(*(pk++));
497    } break;
498    case SCMD_VIEW_RESIZE :
499    {
500      int32_t x[8];
501      memcpy(x,pk,8*4);  pk+=8*4;
502      cx1=lltl(x[0]);
503      cy1=lltl(x[1]);
504      cx2=lltl(x[2]);
505      cy2=lltl(x[3]);
506
507      pan_x=lltl(x[4]);
508      pan_y=lltl(x[5]);
509      shift_down=lltl(x[6]);
510      shift_right=lltl(x[7]);
511      if (small_render)
512      {
513        small_render->Scale(vec2i(cx2 - cx1 + 1, cy2 - cy1 + 1));
514      }
515
516      suggest.send_view=0;
517      if (local_player())
518        the_game->draw();
519      return 1;
520    }
521    case SCMD_WEAPON_CHANGE :
522    {
523      int32_t x;
524      memcpy(&x,pk,4);  pk+=4;
525      current_weapon=lltl(x);
526
527      if (local_player())
528        sbar.need_refresh();
529      suggest.send_weapon_change=0;
530      return 1;
531    } break;
532
533    case SCMD_SET_INPUT :
534    {
535      uint8_t x=*(pk++);
536
537      if (x&1) x_suggestion=1;
538      else if (x&2) x_suggestion=-1;
539      else x_suggestion=0;
540
541      if (x&4) y_suggestion=1;
542      else if (x&8) y_suggestion=-1;
543      else y_suggestion=0;
544
545      if (x&16) b1_suggestion=1; else b1_suggestion=0;
546      if (x&32) b2_suggestion=1; else b2_suggestion=0;
547      if (x&64) b3_suggestion=1; else b3_suggestion=0;
548      if (x&128) b4_suggestion=1; else b4_suggestion=0;
549
550      uint16_t p[2];
551      memcpy(p,pk,2*2);  pk+=2*2;
552
553      pointer_x=(int16_t)(lstl(p[0]));
554      pointer_y=(int16_t)(lstl(p[1]));
555
556      return 1;
557    } break;
558    case SCMD_KEYPRESS : set_key_down(*(pk++),1); break;
559    case SCMD_EXT_KEYPRESS : set_key_down(*(pk++)+256,1); break;
560    case SCMD_KEYRELEASE : set_key_down(*(pk++),0); break;
561    case SCMD_EXT_KEYRELEASE : set_key_down(*(pk++)+256,0); break;
562  }
563  return 1;
564}
565
566int view::local_player()
567{
568  return player_number==client_number();
569}
570
571void view::next_weapon()
572{
573  int c=current_weapon;
574
575  while (c<total_weapons-1)
576  {
577    c++;
578    if (weapon_total(c)>0)
579    {
580      suggest.send_weapon_change=1;
581      suggest.new_weapon=c;
582      return ;
583    }
584  }
585
586  c=0;
587  while (c!=current_weapon)
588  {
589    if (weapon_total(c)>0)
590    {
591      suggest.send_weapon_change=1;
592      suggest.new_weapon=c;
593      return ;
594    }
595    c++;
596  }
597}
598
599void view::last_weapon()
600{
601
602  int c=current_weapon;
603
604  while (c>=1)
605  {
606    c--;
607    if (weapon_total(c)>0 || c==0)
608    {
609      suggest.send_weapon_change=1;
610      suggest.new_weapon=c;
611      return ;
612    }
613  }
614
615  c=total_weapons-1;
616  while (c!=current_weapon)
617  {
618    if (weapon_total(c)>0 || c==0)
619    {
620      suggest.send_weapon_change=1;
621      suggest.new_weapon=c;
622      return ;
623    }
624    c--;
625  }
626
627}
628
629int view::handle_event(Event &ev)
630{
631    if( ev.type == EV_KEY )
632    {
633        if( ev.key == (int)',' )
634        {
635            if( total_weapons )
636            {
637                last_weapon();
638            }
639            return 1;
640        }
641        else if( ev.key == (int)'.' )
642        {
643            if( total_weapons )
644            {
645                next_weapon();
646            }
647            return 1;
648        }
649        else if( ev.key == get_key_binding( "b3", 0 ) )
650        {
651            if( total_weapons )
652            {
653                last_weapon();
654            }
655            return 1;
656        }
657        else if( ev.key == get_key_binding( "b4", 0 ) )
658        {
659            if( total_weapons )
660            {
661                next_weapon();
662            }
663            return 1;
664        }
665
666        switch( ev.key )
667        {
668            case '1':
669            case '2':
670            case '3':
671            case '4':
672            case '5':
673            case '6':
674            case '7':
675            {
676                if((( dev & EDIT_MODE ) == 0 ) && ( weapon_total( ev.key - '1' ) > 0 ))
677                {
678                    suggest.send_weapon_change = 1;
679                    suggest.new_weapon=ev.key - '1';
680                }
681            } break;
682
683            case JK_HOME:
684            case JK_CTRL_L:
685            case JK_CTRL_R:
686            {
687                if( total_weapons )
688                {
689                    last_weapon();
690                }
691                return 1;
692            } break;
693            case JK_PAGEUP:
694            case JK_INSERT:
695            {
696                if( total_weapons )
697                {
698                    next_weapon();
699                }
700                return 1;
701            } break;
702        }
703    }
704    return 0;
705}
706
707void view::draw_hp()
708{
709    if (focus)
710    {
711        int h = focus->hp();
712        last_hp=h;
713        sbar.draw_health( main_screen, focus->hp() );
714    }
715    else
716    {
717        sbar.draw_health( main_screen, 0 );
718    }
719}
720
721int view::drawable()
722{
723    return local_player();
724}
725
726
727void recalc_local_view_space()   // calculates view areas for local players, should be called
728                                 // when adding or deleting local players
729{
730  if (main_screen)
731  {
732    int t=total_local_players();
733    if (!t) return ;
734
735    int Xres=small_render ? xres/2 : xres;
736    int Yres=small_render ? yres/2 : yres;
737
738    int h=Yres/t;
739    int w=h*320/200,y=5;
740    if (w<300) w=300;
741
742    for (view *f=player_list; f; f=f->next)
743    {
744      if (f->local_player())
745      {
746    f->suggest.cx1=Xres/2-w/2;
747    f->suggest.cx2=Xres/2+w/2;
748    if (f->suggest.cx1<2) f->suggest.cx1=2;
749    if (f->suggest.cx2>Xres-2) f->suggest.cx2=Xres-2;
750
751    f->suggest.cy1=y;
752    f->suggest.cy2=h-(total_weapons ? 33 : 0);
753
754    f->suggest.shift_down=f->shift_down;
755    f->suggest.shift_right=f->shift_right;
756    f->suggest.pan_x=f->pan_x;
757    f->suggest.pan_y=f->pan_y;
758    f->suggest.send_view=1;
759
760    if (!player_list->next)
761    {
762      f->cx1=f->suggest.cx1;
763      f->cy1=f->suggest.cy1;
764      f->cx2=f->suggest.cx2;
765      f->cy2=f->suggest.cy2;
766      f->suggest.send_view=0;
767    }
768    y+=h;
769      }
770    }
771  }
772
773}
774
775
776void set_local_players(int total)
777{
778  int rdw=0;
779  if (total<1) return ;
780
781  view *last=NULL;
782  for (view *f=player_list; f; f=f->next)
783  {
784    if (total && f->local_player())
785      total--;
786    else if (!total && f->local_player())  // too many local players, delete this one
787    {
788      view *n=last->next;
789      while (n && !n->local_player()) n=n->next;  // find next local player
790
791      if (last)
792        last->next=n;
793      else
794      {
795    if (n)    // make sure we have at least one local player
796          player_list=n;
797      }
798      last=f;
799      rdw=1;
800    }
801  }
802
803  while (total)   // see if we need to add new players
804  {
805    game_object *o=create(current_start_type,50,50);
806    view *v;
807    if (!player_list)
808    {
809      player_list=new view(o,NULL,0);
810      v=player_list;
811    }
812    else
813    {
814      view *f=player_list;
815      for (; f && f->next; f=f->next);
816      f->next=new view(o,NULL,f->player_number+1);
817      v=f->next;
818    }
819    v->cx1=320/2-155; v->cy1=200/2-95; v->cx2=320/2+155; v->cy2=200/2+(total_weapons ? 60 : 95);
820    v->focus->set_controller(v);
821    total--;
822    rdw=1;
823  }
824  if (rdw)
825    recalc_local_view_space();
826}
827
828
829int total_local_players()
830{
831  int t=0;
832  for (view *f=player_list; f; f=f->next)
833    if (f->local_player()) t++;
834  return t;
835}
836
837
838void view::resize_view(int32_t Cx1, int32_t Cy1, int32_t Cx2, int32_t Cy2)
839{
840  if (cx1!=Cx1 || cx2!=Cx2 || cy1!=Cy1 || cy2!=Cy2)
841  {
842    cx1=Cx1; cy1=Cy1;
843    cx2=Cx2; cy2=Cy2;
844    if (playing_state(the_game->state) && local_player())
845      the_game->draw(0);
846  }
847}
848
849
850void view::set_input(int cx, int cy, int b1, int b2, int b3, int b4, int px, int py)
851{
852    x_suggestion=cx;
853    y_suggestion=cy;
854    b1_suggestion=b1;
855    b2_suggestion=b2;
856    b3_suggestion=b3;
857    b4_suggestion=b4;
858    pointer_x=px;
859    pointer_y=py;
860}
861
862
863
864void view::reset_player()
865{
866  if (focus)
867  {
868
869    game_object *start=current_level ? current_level->get_random_start(320,focus->controller()) : 0;
870    focus->defaults();
871    if (start)
872    {
873      focus->x=start->x;
874      focus->y=start->y;
875      dprintf("reset player position to %d %d\n",start->x,start->y);
876    }
877    focus->set_state(stopped);
878    focus->set_tint(_tint);
879    focus->set_team(_team);
880    memset(weapons,0xff,total_weapons*sizeof(int32_t));
881    memset(last_weapons,0xff,total_weapons*sizeof(int32_t));
882
883    shift_down=SHIFT_DOWN_DEFAULT;
884    shift_right=SHIFT_RIGHT_DEFAULT;
885
886    if (total_weapons)
887      weapons[0]=0;  // give him the first weapon
888    current_weapon=0;
889
890    memset(focus->lvars,0,figures[focus->otype]->tv*4);
891    focus->set_aistate(0);
892    if (figures[focus->otype]->get_fun(OFUN_CONSTRUCTOR))
893    {
894      game_object *o=current_object;
895      current_object=focus;
896      ((LSymbol *)figures[focus->otype]->get_fun(OFUN_CONSTRUCTOR))->EvalUserFunction(NULL);
897      current_object=o;
898    }
899    sbar.redraw(main_screen);
900
901    int i;
902    for (i=0; i<focus->total_objects(); i++)   // reset the vars for the attached objects
903    {
904      game_object *o=focus->get_object(i);
905      memset(o->lvars,0,figures[o->otype]->tv*4);
906    }
907
908  }
909}
910
911
912
913
914
915object_node *make_player_onodes(int player_num)
916{
917  object_node *first=NULL,*last=NULL;
918  for (view *o=player_list; o; o=o->next)
919  {
920    if (o->focus && (player_num==-1 || o->player_number==player_num))
921    {
922      if (!object_to_number_in_list(o->focus,first))
923      {
924    object_node *q=new object_node(o->focus,NULL);
925    if (first)
926      last->next=q;
927    else first=q;
928    last=q;
929      }
930      for (int i=0; i<o->focus->total_objects(); i++)
931      {
932    game_object *p=o->focus->get_object(i);
933
934    if (!object_to_number_in_list(p,first))
935    {
936      object_node *q=new object_node(p,NULL);
937      if (first)
938        last->next=q;
939      else first=q;
940      last=q;
941    }
942      }
943    }
944  }
945  return first;
946}
947
948
949
950
951enum { V_CX1, V_CY1, V_CX2, V_CY2,
952       V_SHIFT_DOWN, V_SHIFT_RIGHT,
953       V_GOD,
954       V_PLAYER_NUMBER,
955       V_DRAW_SOLID,
956       V_LIVES,
957       V_CURRENT_WEAPON,
958       V_X_SUGGESTION, V_Y_SUGGESTION, V_B1_SUGGESTION, V_B2_SUGGESTION, V_B3_SUGGESTION, V_B4_SUGGESTION,
959       V_PAN_X, V_PAN_Y,
960       V_NO_XLEFT, V_NO_XRIGHT, V_NO_YTOP, V_NO_YBOTTOM,
961       V_LAST_X, V_LAST_Y, V_LAST_LEFT, V_LAST_RIGHT, V_LAST_UP, V_LAST_DOWN,
962       V_LAST_B1, V_LAST_B2, V_LAST_B3, V_LAST_B4,
963       V_LAST_HP,
964       V_SECRETS, V_KILLS, V_TSECRETS, V_TKILLS,
965       V_AMBIENT,
966       V_POINTER_X, V_POINTER_Y,
967       V_LAST_LAST_X, V_LAST_LAST_Y,
968       V_FREEZE_TIME };
969
970#define TVV (V_FREEZE_TIME+1)
971
972static char const *vv_names[TVV] =
973{
974    "view.cx1",  "view.cy1",  "view.cx2",  "view.cy2",
975    "view.shift_down",  "view.shift_right",
976    "view.god",
977    "view.player_number",
978    "view.draw_solid",
979    "view.lives",
980    "view.current_weapon",
981    "view.x_suggestion",  "view.y_suggestion",
982    "view.b1_suggestion",  "view.b2_suggestion",  "view.b3_suggestion",  "view.b4_suggestion",
983    "view.pan_x",  "view.pan_y",
984    "view.no_xleft",  "view.no_xright",  "view.no_ytop",  "view.no_ybottom",
985    "view.last_x",  "view.last_y",  "view.last_left",  "view.last_right",  "view.last_up",  "view.last_down",
986    "view.last_b1",  "view.last_b2",  "view.last_b3",  "view.last_b4",
987    "view.last_hp",
988    "view.secrets",  "view.kills",  "view.tsecrets",  "view.tkills",
989    "view.ambient",
990    "view.pointer_x",  "view.pointer_y",
991    "view.last_last_x",  "view.last_last_y",
992    "view.freeze_time"
993};
994
995
996int total_view_vars()
997{ return TVV;
998}
999
1000char const *get_view_var_name(int num)
1001{ return vv_names[num]; }
1002
1003int32_t view::get_view_var_value(int num)
1004{
1005  switch (num)
1006  {
1007    case V_CX1 : return cx1; break;
1008    case V_CY1 : return cy1; break;
1009    case V_CX2 : return cx2; break;
1010    case V_CY2 : return cy2; break;
1011    case V_SHIFT_DOWN : return shift_down; break;
1012    case V_SHIFT_RIGHT : return shift_right; break;
1013    case V_GOD : return god; break;
1014    case V_PLAYER_NUMBER : return player_number; break;
1015
1016    case V_DRAW_SOLID : return draw_solid; break;
1017    case V_CURRENT_WEAPON : return current_weapon; break;
1018    case V_X_SUGGESTION : return x_suggestion; break;
1019    case V_Y_SUGGESTION : return y_suggestion; break;
1020    case V_B1_SUGGESTION : return b1_suggestion; break;
1021    case V_B2_SUGGESTION : return b2_suggestion; break;
1022    case V_B3_SUGGESTION : return b3_suggestion; break;
1023    case V_B4_SUGGESTION : return b4_suggestion; break;
1024
1025    case V_PAN_X : return pan_x; break;
1026    case V_PAN_Y : return pan_y; break;
1027    case V_NO_XLEFT : return no_xleft; break;
1028    case V_NO_XRIGHT : return no_xright; break;
1029    case V_NO_YTOP : return no_ytop; break;
1030    case V_NO_YBOTTOM : return no_ybottom; break;
1031    case V_LAST_X : return last_x; break;
1032    case V_LAST_Y : return last_y; break;
1033    case V_LAST_LEFT : return last_left; break;
1034    case V_LAST_RIGHT : return last_right; break;
1035    case V_LAST_UP : return last_up; break;
1036    case V_LAST_DOWN : return last_down; break;
1037    case V_LAST_B1 : return last_b1; break;
1038    case V_LAST_B2 : return last_b2; break;
1039    case V_LAST_B3 : return last_b3; break;
1040    case V_LAST_B4 : return last_b4; break;
1041    case V_LAST_HP : return last_hp; break;
1042    case V_SECRETS : return secrets; break;
1043    case V_KILLS : return kills; break;
1044    case V_TSECRETS : return tsecrets; break;
1045    case V_TKILLS : return tkills; break;
1046    case V_AMBIENT : return ambient; break;
1047    case V_POINTER_X : return pointer_x; break;
1048    case V_POINTER_Y : return pointer_y; break;
1049    case V_LAST_LAST_X : return last_last_x; break;
1050    case V_LAST_LAST_Y : return last_last_y; break;
1051    case V_FREEZE_TIME : return freeze_time; break;
1052  }
1053  return 0;
1054}
1055
1056
1057
1058int32_t view::set_view_var_value(int num, int32_t x)
1059{
1060  switch (num)
1061  {
1062    case V_CX1 : cx1=x; break;
1063    case V_CY1 : cy1=x; break;
1064    case V_CX2 : cx2=x; break;
1065    case V_CY2 : cy2=x; break;
1066    case V_SHIFT_DOWN : shift_down=x; break;
1067    case V_SHIFT_RIGHT : shift_right=x; break;
1068    case V_GOD : god=x; break;
1069    case V_PLAYER_NUMBER : { player_number=x; if (local_player()) sbar.associate(this); }  break;
1070
1071    case V_DRAW_SOLID : draw_solid=x; break;
1072    case V_CURRENT_WEAPON : { current_weapon=x; sbar.need_refresh(); } break;
1073    case V_X_SUGGESTION : x_suggestion=x; break;
1074    case V_Y_SUGGESTION : y_suggestion=x; break;
1075    case V_B1_SUGGESTION : b1_suggestion=x; break;
1076    case V_B2_SUGGESTION : b2_suggestion=x; break;
1077    case V_B3_SUGGESTION : b3_suggestion=x; break;
1078    case V_B4_SUGGESTION : b4_suggestion=x; break;
1079
1080    case V_PAN_X : pan_x=x; break;
1081    case V_PAN_Y : pan_y=x; break;
1082    case V_NO_XLEFT : no_xleft=x; break;
1083    case V_NO_XRIGHT : no_xright=x; break;
1084    case V_NO_YTOP : no_ytop=x; break;
1085    case V_NO_YBOTTOM : no_ybottom=x; break;
1086    case V_LAST_X : last_x=x; break;
1087    case V_LAST_Y : last_y=x; break;
1088    case V_LAST_LEFT : last_left=x; break;
1089    case V_LAST_RIGHT : last_right=x; break;
1090    case V_LAST_UP : last_up=x; break;
1091    case V_LAST_DOWN : last_down=x; break;
1092    case V_LAST_B1 : last_b1=x; break;
1093    case V_LAST_B2 : last_b2=x; break;
1094    case V_LAST_B3 : last_b3=x; break;
1095    case V_LAST_B4 : last_b4=x; break;
1096
1097    case V_LAST_HP : last_hp=x; break;
1098    case V_SECRETS : secrets=x; break;
1099    case V_KILLS : kills=x; break;
1100    case V_TSECRETS : tsecrets=x; break;
1101    case V_TKILLS : tkills=x; break;
1102    case V_AMBIENT : ambient=x; break;
1103    case V_POINTER_X : pointer_x=x; break;
1104    case V_POINTER_Y : pointer_y=x; break;
1105    case V_LAST_LAST_X : last_last_x=x; break;
1106    case V_LAST_LAST_Y : last_last_y=x; break;
1107    case V_FREEZE_TIME : freeze_time=x; break;
1108  }
1109  return 1;
1110}
1111
1112
1113void view::configure_for_area(area_controller *a)
1114{
1115  if (a->ambient>=0 && a->ambient!=ambient)
1116  {
1117    if (ambient>a->ambient)
1118    {
1119      ambient-=a->ambient_speed;
1120      if (ambient<a->ambient)
1121        ambient=a->ambient;
1122    }
1123    else
1124    {
1125      ambient+=a->ambient_speed;
1126      if (ambient>a->ambient)
1127        ambient=a->ambient;
1128    }
1129  }
1130
1131  if (!view_shift_disabled)
1132  {
1133    if (a->view_xoff!=pan_x)
1134    {
1135      if (pan_x>a->view_xoff)
1136      {
1137    pan_x-=a->view_xoff_speed;
1138    if (pan_x<a->view_xoff)
1139        pan_x=a->view_xoff;
1140      }
1141      else
1142      {
1143    pan_x+=a->view_xoff_speed;
1144    if (pan_x>a->view_xoff)
1145        pan_x=a->view_xoff;
1146      }
1147    }
1148
1149    if (a->view_yoff!=pan_y)
1150    {
1151      if (pan_y>a->view_yoff)
1152      {
1153    pan_y-=a->view_yoff_speed;
1154    if (pan_y<a->view_yoff)
1155        pan_y=a->view_yoff;
1156      }
1157      else
1158      {
1159    pan_y+=a->view_yoff_speed;
1160    if (pan_y>a->view_yoff)
1161        pan_y=a->view_yoff;
1162      }
1163    }
1164  }
1165}
1166
1167
1168void process_packet_commands(uint8_t *pk, int size)
1169{
1170  int32_t sync_uint16=-1;
1171
1172  if (!size) return ;
1173  pk[size]=SCMD_END_OF_PACKET;
1174
1175  uint8_t cmd;
1176  int already_reloaded=0;
1177
1178
1179  do
1180  {
1181    cmd=*(pk++);
1182    switch (cmd)
1183    {
1184      case SCMD_WEAPON_CHANGE :
1185      case SCMD_SET_INPUT :
1186      case SCMD_VIEW_RESIZE :
1187      case SCMD_KEYPRESS :
1188      case SCMD_KEYRELEASE :
1189      case SCMD_EXT_KEYPRESS :
1190      case SCMD_EXT_KEYRELEASE :
1191      case SCMD_CHAT_KEYPRESS :
1192      {
1193    uint8_t player_num=*(pk++);
1194
1195    view *v=player_list;
1196    for (; v && v->player_number!=player_num; v=v->next);
1197    if (v)
1198    {
1199      if (v->player_number==player_num)
1200      v->process_input(cmd,pk);
1201    }
1202    else
1203    {
1204      dprintf("Evil error : bad player number in packet\n");
1205      return ;
1206    }
1207      } break;
1208      case SCMD_RELOAD :
1209      {
1210    if (!already_reloaded)
1211    {
1212      net_reload();
1213      already_reloaded=1;
1214    }
1215      } break;
1216
1217      case SCMD_SYNC :
1218      {
1219    uint16_t x;
1220    memcpy(&x,pk,2);  pk+=2;
1221    x=lstl(x);
1222    if (demo_man.current_state()==demo_manager::PLAYING)
1223    sync_uint16=make_sync();
1224
1225    if (sync_uint16==-1)
1226    sync_uint16=x;
1227    else if (x!=sync_uint16 && !already_reloaded)
1228    {
1229      dprintf("out of sync %d (packet=%d, calced=%d)\n",current_level->tick_counter(),x,sync_uint16);
1230      if (demo_man.current_state()==demo_manager::NORMAL)
1231        net_reload();
1232      already_reloaded=1;
1233    }
1234      } break;
1235      case SCMD_DELETE_CLIENT :
1236      {
1237    uint8_t player_num=*(pk++);
1238    view *v=player_list,*last=NULL;
1239    for (; v && v->player_number!=player_num; v=v->next)
1240    last=v;
1241    if (!v)
1242    dprintf("evil : delete client %d, but no such client\n");
1243    else
1244    {
1245
1246      // make a list of all objects associated with this player
1247      object_node *on=make_player_onodes(player_num);
1248      while (on)
1249      {
1250        current_level->delete_object(on->me);
1251        object_node *last=on;
1252        on=on->next;
1253        delete last;
1254      }
1255
1256      v->focus=NULL;
1257      if (last)
1258      last->next=v->next;
1259      else player_list=player_list->next;
1260
1261      delete v;
1262    }
1263      } break;
1264      default :
1265      dprintf("Unknown net command %d\n",cmd);
1266
1267    }
1268  } while (cmd!=SCMD_END_OF_PACKET);
1269}
1270
1271void view::set_tint(int tint)
1272{
1273    if(tint < 0)
1274        tint = 0;
1275    _tint = tint;
1276    focus->set_tint(tint);
1277}
1278
1279int view::get_tint()
1280{
1281    return _tint;
1282}
1283
1284void view::set_team(int team)
1285{
1286    if(team < 0)
1287        team = 0;
1288    _team = team;
1289    focus->set_team(team);
1290}
1291
1292int view::get_team()
1293{
1294    return _team;
1295}
1296
Note: See TracBrowser for help on using the repository browser.