source: abuse/trunk/src/level.cpp @ 129

Last change on this file since 129 was 129, checked in by Sam Hocevar, 14 years ago
  • Get rid of jmalloc and replace it with standard malloc. Modern operating systems certainly perform a lot better than this custom implementation, and we have superior tools (eg. valgrind) to debug and profile memory usage without interfering with the code itself.
File size: 75.1 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#if (defined(__MACH__) || !defined(__APPLE__))
13#include <sys/stat.h>
14#endif
15
16#include <limits.h>
17#include <time.h>
18
19#include "light.hpp"
20#include "level.hpp"
21#include "game.hpp"
22#include "intsect.hpp"
23#include "lisp.hpp"
24#include "dprint.hpp"
25#include "particle.hpp"
26#include "objects.hpp"
27#include "jrand.hpp"
28#include "macs.hpp"
29#include "clisp.hpp"
30#include "status.hpp"
31#include "dev.hpp"
32#include "demo.hpp"
33#include "pcxread.hpp"
34#include "profile.hpp"
35#include "sbar.hpp"
36#include "cop.hpp"
37#include "nfserver.hpp"
38#include "lisp_gc.hpp"
39
40level *current_level;
41
42game_object *level::attacker(game_object *who)
43{
44  int32_t d=0x7fffffff;
45  game_object *c=NULL;
46  view *f=the_game->first_view;
47  for (;f;f=f->next)
48  {
49    if (f->focus)
50    {
51      int32_t tmp_d=abs(f->focus->x-who->x)+abs(f->focus->y-who->y);
52      if (tmp_d<d)
53      {
54    d=tmp_d;
55    c=f->focus;
56      }
57    }
58  }
59  CONDITION(c,"no attacker found");
60  return c;
61}
62
63
64
65int level::is_attacker(game_object *who)
66{
67  return who->controller()!=NULL;
68}
69
70
71game_object *level::main_character()
72{
73  return the_game->first_view->focus;
74}
75
76void level::load_fail()
77{
78  if (map_fg)    free(map_fg);   map_fg=NULL;
79  if (map_bg)    free(map_bg);   map_bg=NULL;
80  if (Name)      free(Name);     Name=NULL;
81
82  first_active=NULL;
83  view *f=player_list;
84  for (;f;f=f->next)
85    if (f->focus)
86      current_level->remove_object(f->focus);
87
88  while (first)
89  {
90    first_active=first;
91    first=first->next;
92    if (dev_cont)
93      dev_cont->notify_deleted_object(first_active);
94    delete first_active;
95  }
96
97  while (area_list)
98  {
99    area_controller *l=area_list;
100    area_list=area_list->next;
101    delete l;
102  }
103
104  last=NULL;
105  delete_panims();
106  delete_all_lights();
107
108}
109
110level::~level()
111{
112  load_fail();
113  if (attack_list) free(attack_list);
114  if (target_list) free(target_list);
115  if (block_list) free(block_list);
116  if (all_block_list) free(all_block_list);
117  if (first_name) free(first_name);
118}
119
120void level::restart()
121{
122  view *f;
123  game_object *found=NULL,*o;
124  f=the_game->first_view;
125  for (o=first;f && o;o=o->next)
126  {
127    while (f && !f->focus) f=f->next;
128    if (f)
129    {
130      if (!strcmp(object_names[o->otype],"START"))
131      {
132    if (!found) found=o;
133    f->focus->x=o->x;
134    f->focus->y=o->y;
135    f->focus->set_hp(get_ability(f->focus->otype,start_hp));
136    f->focus->set_state(stopped);
137    f=f->next;
138      }
139    }
140  }
141  while (f)
142  {
143    if (f->focus)
144    {
145      f->focus->x=found->x;
146      f->focus->y=found->y;
147      f->focus->set_hp(get_ability(f->focus->otype,start_hp));
148      f->focus->set_state(stopped);
149    }
150    f=f->next;
151  }
152}
153
154
155void level::next_focus()
156{
157/*  int i;
158  for (i=0;i<total_objs;i++)
159    if (obj[i]==the_game->first_view->focus)
160    {
161      int tries=total_objs;
162      do
163      {
164    i++;
165    if (i==total_objs)
166      i=0;
167    the_game->first_view->focus=obj[i];
168      }  while ((!the_game->first_view->focus->is_playable() ||
169         the_game->first_view->focus->hp<=0) && tries--);
170      return ;
171    }            */
172}
173
174void level::unactivate_all()
175{
176  first_active=NULL;
177  game_object *o=first;
178  attack_total=0;  // reset the attack list
179  target_total=0;
180  block_total=0;
181  all_block_total=0;
182
183  for (;o;o=o->next)
184    o->active=0;
185}
186
187
188void level::pull_actives(game_object *o, game_object *&last_active, int &t)
189{
190  int i=o->total_objects();
191  for (;i;i--)        // pull any linked object into active list
192  {
193    game_object *other=o->get_object(i-1);
194    if (!other->active)
195    {
196      other->active=1;
197      if (other->can_block())              // if object can block other player, keep a list for fast testing
198      {
199    add_block(other);
200    add_all_block(other);
201      } else if (other->hurtable())
202        add_all_block(other);
203
204      t++;
205      last_active->next_active=other;
206      last_active=other;   
207      pull_actives(o,last_active,t);
208    }
209  }
210}
211
212int level::add_actives(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
213{
214  int t=0;
215  game_object *last_active=NULL;
216  if (first_active)
217    for (last_active=first_active;last_active->next_active;last_active=last_active->next_active);
218
219  game_object *o=first;
220  for (;o;o=o->next)
221  {
222    if (!o->active)
223    {
224      int32_t xr=figures[o->otype]->rangex,
225           yr=figures[o->otype]->rangey;
226
227      if (o->x+xr>=x1 && o->x-xr<=x2 && o->y+yr>=y1 && o->y-yr<=y2)
228      {
229   
230    if (o->can_block())              // if object can block other player, keep a list for fast testing
231    {
232      add_block(o);
233      add_all_block(o);
234    } else if (o->hurtable())
235          add_all_block(o);
236
237
238    o->active=1;
239    t++;
240    if (!first_active)
241      first_active=o;   
242    else
243          last_active->next_active=o;
244    last_active=o;
245
246    pull_actives(o,last_active,t);   
247      }
248    }
249  }
250  if (last_active)
251    last_active->next_active=NULL;
252  return t;
253}
254
255
256int level::add_drawables(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
257{
258  int t=0,ft=0;
259  game_object *last_active=NULL;
260  if (first_active)
261  {
262    for (last_active=first_active;last_active->next_active;last_active=last_active->next_active);
263  } else ft=1;
264
265  game_object *o=first;
266  for (;o;o=o->next)
267  {
268    if (ft || !o->active)
269    {
270      int32_t xr=figures[o->otype]->draw_rangex,
271      yr=figures[o->otype]->draw_rangey;
272
273      if (o->x+xr>=x1 && o->x-xr<=x2 && o->y+yr>=y1 && o->y-yr<=y2)
274      {
275    t++;
276    if (!first_active)
277    first_active=o;   
278    else
279    last_active->next_active=o;
280    last_active=o;
281    o->active=1;
282      } else if (ft) o->active=0;  // if this is the first pass, then mark objects not in this ranges as not active
283    }
284  }
285  if (last_active)
286    last_active->next_active=NULL;
287  return t;
288}
289
290
291view *level::make_view_list(int nplayers)
292{
293  int startable;
294  CONDITION(nplayers>0,"make_view_list with <=0 players!\n");
295  view *f=NULL;
296  int j,use_type=current_start_type;
297  figures[use_type]->cache_in();
298  game_object *o,*last_start=NULL;
299  int num=0;
300
301  for (j=0,o=first;o && j<nplayers;o=o->next)
302  {
303    if (!strcmp(object_names[o->otype],"START"))
304    {
305      f=new view(create(use_type,o->x,o->y),f,num); num++;
306      f->focus->set_controller(f);
307      add_object_after(f->focus,o);
308      j++;
309      last_start=o;
310    }
311  }
312
313  // if we couldn't find enough starts then create the rest of the players at the original start
314  startable=j;  // if we haven't created anyone yet, it's because we can't
315
316  for (;j<nplayers;j++)
317  {
318    if (startable)
319    {
320      game_object *o=create(use_type,f->focus->x,f->focus->y);
321      f=new view(o,f,num); num++;
322      f->focus->set_controller(f);
323      add_object_after(o,last_start);
324    }
325    else
326    {
327      f=new view(NULL,f,num);
328      num++;
329    }
330  }
331  return f;
332}
333
334void level::wall_push()
335{
336  int32_t sx1,sy1,sx2,sy2,xv,yv;
337  game_object *o=first_active;
338  for (;o;o=o->next_active)
339  {
340    if (o->pushable())
341    {
342      o->picture_space(sx1,sy1,sx2,sy2);
343      xv=sx1-o->x;
344      yv=0;
345      o->try_move(o->x,o->y-1,xv,yv,1);         // check for wall pushes on the left using feet
346      if (xv!=sx1-o->x)                         // is the character in the wall?
347      {
348    xv=-xv;
349    o->try_move(o->x,o->y-1,xv,yv,1);       // see how far to the right we can push the character
350    o->x+=xv;
351      } else
352      {
353    xv=sx2-o->x;
354    o->try_move(o->x,o->y-1,xv,yv,1);      // now check the right of the character for a wall
355    if (xv!=sx2-o->x)
356    {
357      xv=-xv;
358      o->try_move(o->x,o->y-1,xv,yv,1);
359      o->x+=xv;
360    }
361      }
362    }
363  }
364}
365
366
367void level::try_pushback(game_object *subject,game_object *target)
368{
369  if (subject->pushable() && target->pushable() &&
370      subject->state!=dead && target->state!=dead &&
371      subject->state!=dieing && target->state!=dieing)
372  {
373    int b1=subject->push_range(),b2=target->push_range();
374    if (abs(subject->x-target->x)<b1+b2)
375    {
376      int32_t tmove=b1+b2-abs(subject->x-target->x),xv,yv=0,xv2;
377      if (subject->x>target->x)
378        xv=tmove/2;
379      else xv=-tmove/2;
380      xv2=-xv;
381
382      subject->try_move(subject->x,subject->y,xv,yv,3);
383      subject->x+=xv;
384
385      yv=0;
386      target->try_move(target->x,target->y,xv2,yv,3);
387      target->x+=xv2;
388    }
389  }
390}
391
392/*
393void level::check_collisions()
394{
395  game_object *target,*reciever=NULL;
396  int32_t sx1,sy1,sx2,sy2,tx1,ty1,tx2,ty2,hitx,hity,
397      s_centerx,t_centerx;
398
399  for (game_object *subject=first_active;subject;subject=subject->next_active)
400  {
401    subject->picture_space(sx1,sy1,sx2,sy2);
402    s_centerx=subject->x_center();
403
404    int hit=0;
405    reciever=NULL;
406    for (target=first_active;target;target=target->next_active)
407    {
408      if (target!=subject)
409      {
410    target->picture_space(tx1,ty1,tx2,ty2);
411
412        if (!(sx2<tx1 || sx1>tx2 || sy1>ty2 || sy2<ty1))  // are they semi/overlapping?
413        {
414      try_pushback(subject,target);
415      if (subject->can_hurt(target))    // see if we can hurt him before calculating
416      {   
417        t_centerx=target->x_center();
418        point_list *s_hit,*t_damage;
419                 
420        s_hit=subject->current_figure()->hit;   
421        t_damage=target->current_figure()->damage;
422
423        unsigned char *s_dat=s_hit->data,
424        *t_dat;
425        int i,j;
426        for (i=(int)s_hit->tot-1;i>0 && !hit;i--)
427        {
428          for (t_dat=t_damage->data,j=(int)t_damage->tot-1;j>0 && !hit;j--)
429          {
430        int32_t x1,y1,x2,y2,          // define the two line segments to check
431        xp1,yp1,xp2,yp2;
432
433        xp1=target->x+target->tx(*t_dat);  t_dat++;   
434        yp1=target->y+target->ty(*t_dat);  t_dat++;
435        xp2=target->x+target->tx(*t_dat);
436        yp2=target->y+target->ty(t_dat[1]);
437
438        x1=subject->x+subject->tx(s_dat[0]);
439        y1=subject->y+subject->ty(s_dat[1]);
440        x2=subject->x+subject->tx(s_dat[2]);
441        y2=subject->y+subject->ty(s_dat[3]);
442   
443
444        // ok, now we know which line segemnts to check for intersection
445        // now check to see if (x1,y1-x2,y2) intercest with (xp1,yp1-xp2,yp2)
446        int _x2=x2,_y2=y2;   
447        setback_intersect(x1, y1, x2, y2, xp1, yp1, xp2, yp2,0);
448
449
450        if (x2!=_x2 || _y2!=y2)
451        {
452          reciever=target;
453          hitx=((x1+x2)/2+(xp1+xp2)/2)/2;
454          hity=((y1+y1)/2+(yp1+yp2)/2)/2;
455        }
456          }
457          s_dat+=2;
458        }   
459      }
460    }
461      }
462    }
463    if (reciever)
464    {
465      reciever->do_damage((int)subject->current_figure()->hit_damage,subject,hitx,hity,0,0);
466      subject->note_attack(reciever);
467      hit=1;
468    }
469  }
470}
471*/
472
473game_object *level::boundary_setback(game_object *subject, int32_t x1, int32_t y1, int32_t &x2, int32_t &y2)
474{
475  game_object *l=NULL;
476  int32_t tx1,ty1,tx2,ty2,t_centerx;
477  game_object *target=first_active;
478  game_object **blist=block_list;
479  int t=block_total;
480  for (;t;t--,blist++)
481  {
482    target=*blist;
483    if (target!=subject && (target->total_objects()==0 || target->get_object(0)!=subject))
484    {
485      target->picture_space(tx1,ty1,tx2,ty2);
486      if (!((x2<tx1 && x1<tx1) || (x1>tx2 && x2>tx2) ||
487        (y1>ty2 && y2>ty2) || (y1<ty1 && y2<ty1)))  // are they semi/overlapping?
488      {
489    t_centerx=target->x_center();
490    boundary *t_damage;
491    if (target->direction>0)
492    t_damage=target->current_figure()->f_damage;
493    else
494    t_damage=target->current_figure()->b_damage;
495    unsigned char *t_dat=t_damage->data,*ins=t_damage->inside;
496    int iter=t_damage->tot-1;
497    while(iter-->0)
498    {
499      int32_t xp1=target->x+target->tx(*t_dat);  t_dat++;   
500      int32_t yp1=target->y+target->ty(*t_dat);  t_dat++;
501      int32_t xp2=target->x+target->tx(*t_dat);
502      int32_t yp2=target->y+target->ty(t_dat[1]);
503
504      // now check to see if (x1,y1-x2,y2) intercest with (xp1,yp1-xp2,yp2)
505      if (*ins)
506      {             
507        if (setback_intersect(x1,y1,x2,y2,xp1,yp1,xp2,yp2,1))
508        l=target;
509      }   
510      else
511      {
512        if (setback_intersect(x1,y1,x2,y2,xp1,yp1,xp2,yp2,-1))
513        l=target;
514      }   
515      ins++;
516   
517    }   
518      }
519    }
520  }
521  return l;       // return the last person we intersected
522}
523
524
525game_object *level::all_boundary_setback(game_object *subject, int32_t x1, int32_t y1, int32_t &x2, int32_t &y2)
526{
527  game_object *l=NULL;
528  int32_t tx1,ty1,tx2,ty2,t_centerx;
529  game_object *target=first_active;
530  game_object **blist=all_block_list;
531  int t=all_block_total;
532  for (;t;t--,blist++)
533  {
534    target=*blist;
535    if (target!=subject && (target->total_objects()==0 || target->get_object(0)!=subject))
536    {
537      target->picture_space(tx1,ty1,tx2,ty2);
538      if (!((x2<tx1 && x1<tx1) || (x1>tx2 && x2>tx2) ||
539        (y1>ty2 && y2>ty2) || (y1<ty1 && y2<ty1)))  // are they semi/overlapping?
540      {
541    t_centerx=target->x_center();
542    boundary *t_damage;
543    if (target->direction>0)
544    t_damage=target->current_figure()->f_damage;
545    else
546    t_damage=target->current_figure()->b_damage;
547    unsigned char *t_dat=t_damage->data,*ins=t_damage->inside;
548    int iter=t_damage->tot-1;
549    while(iter-->0)
550    {
551      int32_t xp1=target->x+target->tx(*t_dat);  t_dat++;   
552      int32_t yp1=target->y+target->ty(*t_dat);  t_dat++;
553      int32_t xp2=target->x+target->tx(*t_dat);
554      int32_t yp2=target->y+target->ty(t_dat[1]);
555
556      // now check to see if (x1,y1-x2,y2) intercest with (xp1,yp1-xp2,yp2)
557      if (*ins)
558      {             
559        if (setback_intersect(x1,y1,x2,y2,xp1,yp1,xp2,yp2,1))
560        l=target;
561      }   
562      else
563      {
564        if (setback_intersect(x1,y1,x2,y2,xp1,yp1,xp2,yp2,-1))
565        l=target;
566      }   
567      ins++;   
568    }   
569      }
570    }
571  }
572  return l;       // return the last person we intersected
573}
574
575
576//bFILE *rcheck=NULL,*rcheck_lp=NULL;
577
578void level::interpolate_draw_objects(view *v)
579{
580  int32_t old_x,old_y;
581  current_view=v;
582
583  game_object *o=first_active;
584  for (;o;o=o->next_active)
585  {
586    old_x=o->x;
587    old_y=o->y;
588    o->x=(o->last_x+o->x)/2;
589    o->y=(o->last_y+o->y)/2;
590    o->last_x=old_x;
591    o->last_y=old_y;
592  }
593
594  for (o=first_active;o;o=o->next_active)
595    o->draw();
596
597  for (o=first_active;o;o=o->next_active)
598  {
599    o->x=o->last_x;
600    o->y=o->last_y;
601  }
602}
603
604bFILE *rcheck=NULL,*rcheck_lp=NULL;
605
606extern int sshot_fcount,screen_shot_on;
607
608int level::tick()
609{
610  game_object *o,*l=NULL,  // l is last, used for delete
611              *cur;        // cur is current object, NULL if object deletes it's self
612  int ret=1;
613
614  if (profiling())
615    profile_reset();
616
617/*  // test to see if demo is in sync
618  if (current_demo_mode()==DEMO_PLAY)
619  {
620    if (!rcheck) rcheck=open_file("rcheck","rb");
621    int32_t x=rcheck->read_uint32();
622    if (x!=rand_on)
623      dprintf("off!\n");
624  } else if (current_demo_mode()==DEMO_RECORD)
625  {
626    if (!rcheck)
627    {
628      rcheck=open_file("rcheck","wb");
629      rcheck_lp=open_file("rcheck.lp","wb");
630    }
631    rcheck->write_uint32(rand_on);
632  } else
633  {
634    if (rcheck)
635    {
636      delete rcheck;
637      rcheck=NULL;
638    }
639    if (rcheck_lp)
640    {
641      delete rcheck_lp;
642      rcheck_lp=NULL;
643    }
644  }*/
645
646  for (o=first_active;o;)
647  {
648    o->last_x=o->x;
649    o->last_y=o->y;
650    cur=o;
651    view *c=o->controller();
652    if (!(dev&SUSPEND_MODE) || c)
653    {
654      o->set_flags(o->flags()&(0xff-FLAG_JUST_HIT-FLAG_JUST_BLOCKED));
655
656      if (c)
657      {
658    area_controller *a,*smallest=NULL;
659    int32_t smallest_size=0xffffffff;
660    for (a=area_list;a;a=a->next)
661      if (o->x>=a->x && o->y>=a->y && o->x<=a->x+a->w && o->y<=a->y+a->h)
662      {
663        int32_t size=a->w*a->h;
664        if (size<smallest_size)
665        {
666          smallest=a;
667          smallest_size=size;
668        }
669      }
670
671    if (c->local_player())
672    {
673      if (!shutdown_lighting)       // should we initiate a lighting shutdown?
674      {
675        if (massive_frame_panic>30)
676        {
677          shutdown_lighting=100;
678          shutdown_lighting_value=c->ambient;
679        }
680      } else if (massive_frame_panic)  // do we need brighten towards 63?
681      {
682        if (shutdown_lighting_value<63)
683          shutdown_lighting_value++;
684      } else if (shutdown_lighting>1)        // delay for some time before turning back on
685        shutdown_lighting--;
686      else if (shutdown_lighting_value!=c->ambient) // do we need to lower light toward real ambient?
687      {
688        if (abs(shutdown_lighting_value-c->ambient)<4)
689          shutdown_lighting_value=c->ambient;
690        else
691          if (shutdown_lighting_value<c->ambient)
692              shutdown_lighting_value+=4;
693        else if (shutdown_lighting_value>c->ambient)
694          shutdown_lighting_value-=4;
695      } else shutdown_lighting=0;                    // back to normal
696    }
697
698    if (smallest)
699      c->configure_for_area(smallest);
700
701
702    o->move(c->x_suggestion,c->y_suggestion,c->b1_suggestion|(c->b2_suggestion<<1)|
703        (c->b3_suggestion<<2));
704
705    if (o->otype!=current_start_type)
706    {
707      int32_t fmp=o->fmp();
708      int reduce=figures[o->otype]->morph_power;
709      if (reduce)
710      {
711        fmp-=reduce;
712        o->add_power(fmp>>16);
713        o->set_fmp(fmp&0xffff);
714        if (o->mp()<=0)
715        o->morph_into(current_start_type,NULL,-1,9);
716      }
717    }
718
719    l=o;
720    o=o->next_active;
721      }
722      else if (!o->decide())      // if object returns 0, delete it... I don't like 0's :)
723      {
724    game_object *p=o;
725    o=o->next_active;
726    delete_object(p);
727    cur=NULL;
728      } else
729      {
730    o=o->next_active;
731    l=o;
732      }
733    } else
734    {
735      o=o->next_active;
736      l=o;
737    }
738
739    clear_tmp();
740
741    if (cur)
742    {
743      point_list *p=cur->current_figure()->hit;  // see if this character is on an attack frame
744      if (p && p->tot)
745        add_attacker(cur);               // if so add him to attack list for later collision detect
746
747      if (cur->hurtable())                    // add to target list if is hurtable
748        add_target(cur);
749
750    }
751
752  }
753  tick_panims();
754
755  check_collisions();
756//  wall_push();
757
758  set_tick_counter(tick_counter()+1);
759
760  if (sshot_fcount!=-1)
761  {
762    sshot_fcount++;
763    if ((sshot_fcount%70)==0)
764    {
765      char name[100];
766      sprintf(name,"shot%04d.pcx",screen_shot_on++);
767      write_PCX(screen,pal,name);
768    }
769  }
770
771  return ret;
772}
773
774void level::set_tick_counter(uint32_t x)
775{
776  ctick=x;
777}
778
779void level::draw_areas(view *v)
780{
781  int32_t sx1,sy1,sx2,sy2;
782  area_controller *a=area_list;
783  for (;a;a=a->next)
784  {
785    int c1,c2;
786    if (a->active)
787    {
788      c1=morph_sel_frame_color;
789      c2=wm->bright_color();
790    } else
791    {
792      c2=morph_sel_frame_color;
793      c1=wm->bright_color();
794    }
795
796    the_game->game_to_mouse(a->x,a->y,v,sx1,sy1);
797    the_game->game_to_mouse(a->x+a->w,a->y+a->h,v,sx2,sy2);
798    screen->rectangle(sx1,sy1,sx2,sy2,c1);
799    screen->bar(sx1-1,sy1-1,sx1+1,sy1+1,c2);
800    screen->bar(sx2-1,sy2-1,sx2+1,sy2+1,c2);
801  }
802}
803
804void level::draw_objects(view *v)
805{
806  current_view=v;
807  game_object *o=first_active;
808  if (dev&MAP_MODE)
809  {
810    for (;o;o=o->next_active)
811      o->map_draw();
812  } else
813  {
814    for (;o;o=o->next_active)
815      o->draw();
816  }
817
818  clear_tmp();
819}
820
821void calc_bgsize(uint16_t fgw, uint16_t  fgh, uint16_t  &bgw, uint16_t  &bgh)
822{
823  bgw=fgw/ASPECT+8;
824  bgh=fgh/ASPECT+8;
825}
826
827
828void level::set_size(int w, int h)
829{
830  if (w*h>200000)
831  {
832    the_game->show_help(symbol_str("too_big"));
833    return ;
834  }
835
836  uint16_t *new_fg,*new_bg;
837  new_fg=(uint16_t *)malloc(w*h*sizeof(int16_t));
838  memset(new_fg,0,w*h*sizeof(int16_t));
839
840  int x,y,miny=(h<fg_height)? h : fg_height,minx=(w<fg_width)? w : fg_width;
841
842  uint16_t nbw,nbh;
843  calc_bgsize(w,h,nbw,nbh);
844
845  new_bg=(uint16_t *)malloc((int)nbw*(int)nbh*sizeof(int16_t));
846  memset(new_bg,0,(int)nbw*(int)nbh*sizeof(int16_t));
847
848  for (y=0;y<miny;y++)
849    for (x=0;x<minx;x++)
850      new_fg[x+y*w]=get_fg(x,y);
851
852  miny=(nbh<bg_height) ? nbh : bg_height;
853  minx=(nbw<bg_width) ? nbw : bg_width;
854
855  for (y=0;y<miny;y++)
856    for (x=0;x<minx;x++)
857      new_bg[x+y*nbw]=get_bg(x,y);
858
859  free(map_fg);
860  free(map_bg);
861  map_fg=new_fg;
862  map_bg=new_bg;
863  fg_width=w;
864  fg_height=h;
865  bg_height=nbh;
866  bg_width=nbw;
867
868  char msg[80];
869  sprintf(msg,"Level %s size now %d %d\n",name(),foreground_width(),foreground_height());
870  the_game->show_help(msg);
871}
872
873
874int locate_var(bFILE *fp, spec_directory *sd, char *str, int size)
875{
876  spec_entry *se=sd->find(str);
877  if (se)
878  {
879    fp->seek(se->offset,0);
880    if (RC_type_size(fp->read_uint8())!=size)
881      return 0;
882    else return 1;
883  }
884  return 0;
885}
886
887
888// load objects assumes current objects have already been disposed of
889void level::old_load_objects(spec_directory *sd, bFILE *fp)
890{
891  spec_entry *se=sd->find("objects");
892  total_objs=0;
893  first=last=first_active=NULL;
894  int i,j;
895  if (se)
896  {
897    fp->seek(se->offset,0);
898    /******************************* Read debug info ******************************/
899    int16_t old_tot=fp->read_uint16();
900    uint16_t *o_remap=(uint16_t *)malloc(old_tot * 2);
901    char old_name[150];
902    for (i=0;i<old_tot;i++)
903    {
904      fp->read(old_name,fp->read_uint8());    // read the name
905      for (o_remap[i]=0xffff,j=0;j<total_objects;j++)  // check for matching current name
906      {
907    if (!strcmp(old_name,object_names[j]))
908          o_remap[i]=j;
909      }
910    }
911
912
913    /***************************** Read state names *********************************/
914    int old_stot=fp->read_uint16();
915    unsigned char *s_remap=(unsigned char *)malloc(old_stot);
916    for (i=0;i<old_stot;i++)
917    {
918      fp->read(old_name,fp->read_uint8());
919      s_remap[i]=stopped;           // non exsitant states get mapped into stopped state
920      for (j=0;j<MAX_STATE;j++)                  // see if old state exist now
921    if (!strcmp(state_names[j],old_name))
922         s_remap[i]=j;
923    }
924    total_objs=fp->read_uint32();
925
926    se=sd->find("type");
927    if (se)
928    {
929      fp->seek(se->offset,0);
930      last=NULL;
931      if (fp->read_uint8()==RC_16)    //  read type array, this should be type RC_16
932      {
933    for (i=0;i<total_objs;i++)
934    {
935      uint16_t t=fp->read_uint16();
936      game_object *p=new game_object(o_remap[t],1);
937      clear_tmp();
938      if (!first) first=p; else last->next=p;
939      last=p; p->next=NULL;       
940    }
941
942
943    se=sd->find("state");
944    if (se)
945    {
946      fp->seek(se->offset,0);
947      if (fp->read_uint8()==RC_16)    //  read state array, this should be type RC_16
948      {
949        game_object *l=first;
950        for (i=0;i<total_objs;i++,l=l->next)
951        {
952          character_state s=(character_state)s_remap[fp->read_uint16()];
953          if (l->otype!=0xffff)
954          {
955        if (l->has_sequence((character_state)s))
956          l->state=s;
957        else l->state=stopped;
958        l->current_frame=0;
959          }
960        }
961      }
962    }
963
964    int frame_var=0;
965    int i=0;
966    for (;i<TOTAL_OBJECT_VARS;i++)
967      if (!strcmp(object_descriptions[i].name,"cur_frame"))
968        frame_var=i;
969   
970    int j=0;
971    for (;j<default_simple.total_vars();j++)
972    {
973      spec_entry *se=sd->find(object_descriptions[j].name);
974      if (se)
975      {
976        fp->seek(se->offset,0);
977        int t=object_descriptions[j].type;
978        if (fp->read_uint8()!=t)
979          dprintf("Warning : load level -> var '%s' size changed\n");
980        else
981        {
982          game_object *f=first;
983          for (;f;f=f->next)
984          {   
985        switch (t)
986        {
987          case RC_8 : f->set_var(j,fp->read_uint8()); break;
988          case RC_16 : f->set_var(j,fp->read_uint16()); break;
989          case RC_32 : f->set_var(j,fp->read_uint32()); break;
990        }
991
992        // check to make sure the frame number is not out of bounds from the time
993        // it was last saved
994        if (j==frame_var)
995        {
996          if (f->otype!=0xffff && f->current_frame>=
997              figures[f->otype]->get_sequence(f->state)->length())
998            f->current_frame=0;
999        }
1000          }
1001        }   
1002      } else dprintf("Warning : load level -> no previous var %s\n",default_simple.var_name(j));
1003    }
1004      }
1005    }
1006
1007    free(o_remap);
1008    free(s_remap);
1009  }
1010
1011}
1012
1013
1014// load objects assumes current objects have already been disposed of
1015void level::load_objects(spec_directory *sd, bFILE *fp)
1016{
1017  spec_entry *se=sd->find("object_descripitions");
1018  total_objs=0;
1019  first=last=first_active=NULL;
1020  int i,j;
1021  if (!se)
1022  {
1023    old_load_objects(sd,fp);
1024    return ;
1025  }
1026  else if (se)
1027  {
1028    fp->seek(se->offset,0);
1029    int16_t old_tot=fp->read_uint16();
1030    se=sd->find("describe_names");
1031    if (!se || !old_tot)
1032      return ;
1033
1034    uint16_t *o_remap=(uint16_t *)malloc(old_tot * 2);
1035    uint16_t *o_backmap=(uint16_t *)malloc(total_objects * 2);
1036    memset(o_backmap,0xff,total_objects*2);
1037    char old_name[150];
1038    for (i=0;i<old_tot;i++)
1039    {
1040      fp->read(old_name,fp->read_uint8());    // read the name
1041      for (o_remap[i]=0xffff,j=0;j<total_objects;j++)  // check for matching current name
1042      {
1043    if (!strcmp(old_name,object_names[j]))
1044    {
1045          o_remap[i]=j;
1046      o_backmap[j]=i;
1047    }
1048      }
1049    }
1050
1051    se=sd->find("describe_states");
1052    if (!se) { free(o_remap); free(o_backmap); return ; }
1053    int16_t **s_remap=(int16_t **)malloc(old_tot*sizeof(int16_t *));
1054    int16_t *s_remap_totals=(int16_t *)malloc(old_tot*sizeof(int16_t));
1055    fp->seek(se->offset,0);
1056    int i=0;
1057    for (;i<old_tot;i++)
1058    {
1059      int16_t t=fp->read_uint16();
1060      s_remap_totals[i]=t;
1061      if (t)
1062      {
1063        s_remap[i]=(int16_t *)malloc(t*sizeof(int16_t));
1064    int j=0;
1065    for (;j<t;j++)
1066      *(s_remap[i]+j)=stopped;    // if no remap found, then go to stopped state
1067      }
1068      else s_remap[i]=0;
1069
1070      int j=0;
1071      for (;j<t;j++)
1072      {
1073    fp->read(old_name,fp->read_uint8());
1074    int new_type=o_remap[i];   
1075    if (new_type<total_objects)     // make sure old object still exsist
1076    {
1077      int k=0;
1078      for (;k<figures[new_type]->ts;k++)
1079      {
1080        if (figures[new_type]->seq[k] &&
1081           !strcmp(lstring_value(symbol_name(figures[new_type]->seq_syms[k])),old_name))
1082        *(s_remap[i]+j)=k;
1083      }
1084    }
1085      }
1086    }
1087
1088    int16_t **v_remap=NULL;
1089    int16_t *v_remap_totals=NULL;
1090    int load_vars=1;
1091    se=sd->find("describe_lvars");
1092    if (se)
1093    {
1094      v_remap=(int16_t **)malloc(old_tot*sizeof(int16_t *));
1095      v_remap_totals=(int16_t *)malloc(old_tot*sizeof(int16_t));
1096
1097      fp->seek(se->offset,0);
1098      int i=0;
1099      for (;i<old_tot;i++)
1100      {
1101    int16_t t=fp->read_uint16();
1102    v_remap_totals[i]=t;
1103    if (t)
1104    {
1105      v_remap[i]=(int16_t *)malloc(t*sizeof(int16_t));
1106      memset(v_remap[i],0xff,t*sizeof(int16_t));
1107    } else { v_remap[i]=NULL; }
1108    int j=0;
1109    for (;j<t;j++)
1110    {
1111      fp->read(old_name,fp->read_uint8());
1112      int new_type=o_remap[i];
1113      if (new_type!=0xffff)        // make sure old object still exsist
1114      {
1115        int k=0;
1116        for (;k<figures[new_type]->tiv;k++)
1117        {
1118          if (figures[new_type]->vars[k])
1119          {
1120        if (!strcmp(lstring_value(symbol_name(figures[new_type]->vars[k])),old_name))
1121          *(v_remap[i]+j)=figures[new_type]->var_index[k];
1122          }
1123        }
1124      }
1125    }
1126      }
1127      load_vars=1;
1128    }
1129
1130    se=sd->find("object_list");
1131    if (se)
1132    {
1133      total_objs=fp->read_uint32();
1134
1135      se=sd->find("type");
1136      if (se)
1137      {
1138    fp->seek(se->offset,0);
1139    last=NULL;
1140    if (fp->read_uint8()==RC_16)    //  read type array, this should be type RC_16
1141    {
1142      int i=0;
1143      for (;i<total_objs;i++)
1144      {
1145        uint16_t t=fp->read_uint16();
1146        game_object *p=new game_object(o_remap[t],1);
1147        clear_tmp();
1148        if (!first) first=p; else last->next=p;
1149        last=p; p->next=NULL;       
1150      }
1151
1152      se=sd->find("state");
1153      if (se)
1154      {
1155        fp->seek(se->offset,0);
1156        if (fp->read_uint8()==RC_16)    //  read state array, this should be type RC_16
1157        {
1158          game_object *l=first;
1159          for (i=0;i<total_objs;i++,l=l->next)
1160          {
1161        int st=fp->read_uint16();
1162        if (l->otype==0xffff)
1163          l->state=stopped;
1164        else
1165        {
1166          character_state s=(character_state)(*(s_remap[o_backmap[l->otype]]+st));
1167          if (l->has_sequence((character_state)s))
1168                l->state=s;
1169          else l->state=stopped;
1170          l->current_frame=0;
1171        }
1172          }
1173        }
1174      }
1175   
1176      se=sd->find("lvars");
1177      if (se && load_vars)
1178      {
1179        fp->seek(se->offset,0);
1180        int abort=0;
1181        game_object *o=first;
1182        for (;o && !abort;o=o->next)
1183        {
1184          int16_t ot=fp->read_uint16();
1185          int k=0;
1186          for (;k<ot;k++)
1187          {
1188        if (fp->read_uint8()!=RC_32) abort=1;
1189        else
1190        {
1191          int32_t v=fp->read_uint32();
1192          if (o->otype!=0xffff)     // non-exstant object
1193          {
1194            int remap=*(v_remap[o_backmap[o->otype]]+k);
1195            if (remap!=-1 && figures[o->otype]->tiv>=k)
1196            {
1197              o->lvars[remap]=v;
1198            }
1199          }
1200        }
1201          }
1202        }
1203      }
1204
1205      int frame_var=0;
1206      for (i=0;i<TOTAL_OBJECT_VARS;i++)
1207        if (!strcmp(object_descriptions[i].name,"cur_frame"))
1208          frame_var=i;
1209   
1210
1211      int j=0;
1212      for (;j<default_simple.total_vars();j++)
1213      {
1214        spec_entry *se=sd->find(object_descriptions[j].name);
1215        if (se)
1216        {
1217          fp->seek(se->offset,0);
1218          int t=object_descriptions[j].type;
1219          if (fp->read_uint8()!=t)
1220            dprintf("Warning : load level -> var '%s' size changed\n");
1221          else
1222          {
1223        game_object *f=first;
1224        for (;f;f=f->next)
1225        {   
1226          switch (t)
1227          {
1228            case RC_8 :
1229            { f->set_var(j,fp->read_uint8()); } break;
1230            case RC_16 :
1231            { f->set_var(j,fp->read_uint16()); } break;
1232            case RC_32 :
1233            { f->set_var(j,fp->read_uint32()); } break;
1234          }
1235       
1236          // check to make sure the frame number is not out of bounds from the time
1237          // it was last saved
1238          if (j==frame_var)
1239          {
1240            if (f->otype!=0xffff && f->current_frame>=
1241            figures[f->otype]->get_sequence(f->state)->length())
1242            f->current_frame=0;
1243          }
1244        }
1245          }   
1246        } else dprintf("Warning : load level -> no previous var %s\n",default_simple.var_name(j));
1247      }
1248    }
1249      }
1250    }
1251
1252    int k=0;
1253    for (;k<old_tot;k++)
1254    {
1255      if (s_remap_totals[k])
1256        free(s_remap[k]);
1257    }
1258
1259    int l=0;
1260    for (;l<old_tot;l++)
1261    {
1262      if (v_remap_totals[l])
1263        free(v_remap[l]);
1264    }
1265    free(v_remap_totals);
1266    free(s_remap_totals);
1267    free(o_remap);
1268    free(o_backmap);
1269    free(s_remap);
1270    free(v_remap);
1271  }
1272
1273}
1274
1275level::level(spec_directory *sd, bFILE *fp, char const *lev_name)
1276{
1277  spec_entry *e;
1278  area_list=NULL;
1279
1280  attack_list=NULL;
1281  attack_list_size=attack_total=0;
1282
1283  target_list=NULL;
1284  target_list_size=target_total=0;
1285
1286  block_list=NULL;
1287  block_list_size=block_total=0;
1288
1289  all_block_list=NULL;
1290  all_block_list_size=all_block_total=0;
1291  first_name=NULL;
1292
1293  the_game->need_refresh();
1294
1295  char cmd[100];
1296  sprintf(cmd,symbol_str("loading"),lev_name);
1297  stack_stat stat(cmd);
1298  Name=strcpy((char *)malloc(strlen(lev_name)+1),lev_name);
1299
1300  e=sd->find("first name");
1301  if (e)
1302  {
1303    fp->seek(e->offset,0);
1304    int len=fp->read_uint8();   // read the length of the string
1305    first_name=(char *)malloc(len);
1306    fp->read(first_name,len);    // read the string
1307  } else
1308  {
1309    first_name=(char *)malloc(strlen(Name)+1);
1310    strcpy(first_name,Name);
1311  }
1312
1313  e=sd->find("fgmap");
1314  int no_fg=0,no_bg=0;
1315
1316  if (e)
1317  {
1318    fp->seek(e->offset,0);
1319    fg_width=fp->read_uint32();
1320    fg_height=fp->read_uint32();
1321    map_fg=(uint16_t *)malloc(2*fg_width*fg_height);
1322    fp->read((char *)map_fg,2*fg_width*fg_height);
1323    int t=fg_width*fg_height;
1324    uint16_t *map=map_fg;
1325    while (t) { *map=lstl(*map); map++; t--; }
1326  } else
1327  {
1328    the_game->show_help("Warning foreground map missing");
1329    no_fg=1;
1330  }
1331  stat_man->update(5);
1332
1333  e=sd->find("bgmap");
1334  if (e)
1335  {
1336    fp->seek(e->offset,0);
1337    bg_width=fp->read_uint32();
1338    bg_height=fp->read_uint32();
1339    map_bg=(uint16_t *)malloc(2*bg_width*bg_height);
1340    fp->read((char *)map_bg,2*bg_width*bg_height);
1341    int t=bg_width*bg_height;
1342    uint16_t *map=map_bg;
1343    while (t) { *map=lstl(*map); map++; t--; }
1344  } else
1345  {
1346    the_game->show_help("Warning background map missing");
1347    no_bg=1;
1348  }
1349
1350  if (no_fg && !no_bg)
1351  {
1352    fg_width=bg_width;
1353    fg_height=bg_height;
1354    map_fg=(uint16_t *)malloc(2*fg_width*fg_height);
1355    memset(map_fg,0,2*fg_width*fg_height);
1356  }
1357
1358  if (no_bg)
1359  {
1360    bg_width=fg_width/8+8;
1361    bg_height=fg_height/8+8;
1362    map_bg=(uint16_t *)malloc(2*bg_width*bg_height);
1363    memset(map_bg,0,2*bg_width*bg_height);
1364  }
1365  stat_man->update(10);
1366
1367  /***************** Check map for non exsistant tiles **************************/
1368  int32_t i,w;
1369  uint16_t *m;
1370  spec_entry *load_all=sd->find("player_info");
1371  for (i=0,w=fg_width*fg_height,m=map_fg;i<w;i++,m++)
1372  {
1373    if (!load_all)
1374      (*m)=(*m)&(~0x8000);    // clear the has-seen bit on the tile
1375
1376    if (fgvalue(*m)>=nforetiles || foretiles[fgvalue(*m)]<0)
1377      *m=0;
1378  }
1379
1380  for (i=0,w=bg_width*bg_height,m=map_bg;i<w;i++,m++)
1381  {
1382    if ( (bgvalue(*m)>=nbacktiles) || backtiles[bgvalue(*m)]<0)
1383       *m=0;
1384  }
1385
1386  load_options(sd,fp);
1387  stat_man->update(15);
1388
1389//  first=first_active=last=NULL;
1390  load_objects(sd,fp);
1391  stat_man->update(25);
1392
1393  object_node *players,*objs;
1394  players=make_player_onodes();
1395  objs=make_not_list(players);
1396
1397
1398
1399  read_lights(sd,fp,lev_name);
1400  load_links(fp,sd,objs,players);
1401  int players_got_loaded=load_player_info(fp,sd,objs);
1402
1403
1404  game_object *l=first;
1405  for (;l;)
1406  {
1407    game_object *p=l;
1408    l=l->next;
1409    if (p->otype==0xffff || p->x<0 || p->y<0)
1410      delete_object(p);
1411  }
1412
1413  load_cache_info(sd,fp);
1414
1415  if (!players_got_loaded)
1416  {
1417    level *old=current_level;
1418    current_level=this;
1419
1420    object_node *list=NULL;
1421    list=make_not_list(list);     // create a list of the object list in case objects change positions
1422
1423    object_node *ln=list;
1424    for (;ln;ln=ln->next)
1425      ln->me->reload_notify();
1426    delete_object_list(list);
1427
1428    current_level=old;
1429
1430    insert_players();
1431  }
1432
1433  delete_object_list(players);
1434  delete_object_list(objs);
1435
1436}
1437
1438
1439/*
1440   [object_descriptions] 2 total_type
1441   for(1..total_types)
1442   {
1443     ["object_names"]  1,(name)
1444
1445     ["object_states"]  2(total),<2=number,1,name>
1446
1447     ["object_lvars"]   2(total),<1(type),1,name>
1448   }
1449
1450  [object_list]
1451   4 total_objects
1452   for(1..total_objects)
1453   {
1454     ["type"]
1455     ["state"]
1456     ["lvars"]
1457     ...
1458   }
1459
1460*/
1461
1462
1463void get_prof_assoc_filename(char *filename, char *prof_filename)
1464{
1465  char *s1,*s2,*dot=NULL;
1466  for (s1=filename,s2=prof_filename,dot=NULL;*s1;s1++,s2++)
1467  {
1468    *s2=*s1;
1469    if (*s1=='.') dot=s2;
1470  }
1471  if (dot) s2=dot+1;
1472
1473  *(s2++)='c';
1474  *(s2++)='p';
1475  *(s2++)='f';
1476  *s2=0;
1477}
1478
1479void level::level_loaded_notify()
1480{
1481  char *n;
1482  if (first_name)
1483    n=first_name;
1484  else
1485    n=name();
1486  if (strstr(n,"levels/level"))
1487  {
1488    char nm[100];
1489    sprintf(nm,"music/abuse%c%c.hmi",n[12],n[13]);
1490    bFILE *fp=open_file(nm,"rb");
1491    if (fp->open_failure())
1492    {
1493      delete fp;
1494    }
1495    else
1496    {
1497      if (current_song) { current_song->stop(); delete current_song; }
1498
1499      delete fp;
1500      current_song=new song(nm);
1501      current_song->play(music_volume);
1502    }
1503  }
1504
1505/*  if (DEFINEDP(symbol_function(l_level_loaded)))
1506  {
1507    int sp=current_space;
1508    current_space=PERM_SPACE;
1509
1510    void *arg_list=NULL;
1511    p_ref r1(arg_list);
1512    push_onto_list(new_lisp_string(n),arg_list);
1513    eval_function((lisp_symbol *)l_level_loaded,arg_list);
1514
1515    current_space=sp;
1516  } */
1517}
1518
1519
1520bFILE *level::create_dir(char *filename, int save_all,
1521             object_node *save_list, object_node *exclude_list)
1522{
1523  spec_directory sd;
1524  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"Copyright 1995 Crack dot Com, All Rights reserved",NULL,0,0));
1525  if (first_name)
1526    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"first name",NULL,strlen(first_name)+2,0));
1527
1528
1529
1530  sd.add_by_hand(new spec_entry(SPEC_GRUE_FGMAP,"fgmap",NULL,4+4+fg_width*fg_height*2,0));
1531  sd.add_by_hand(new spec_entry(SPEC_GRUE_BGMAP,"bgmap",NULL,4+4+bg_width*bg_height*2,0));
1532  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"bg_scroll_rate",NULL,1+4*4,0));
1533
1534  int ta=0;
1535  area_controller *a=area_list;
1536  for (;a;a=a->next) ta++;
1537
1538  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"area_list.v1",NULL,1+ta*(4*11)+4,0));
1539
1540  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"tick_counter",NULL,1+4,0));
1541
1542
1543
1544  // how many object types are we goint to save, use a short to specify how many
1545  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"object_descripitions",NULL,2,0));
1546
1547
1548  int size=0;
1549  int i=0;
1550  for (;i<total_objects;i++)       // now save the names of the objects so if ordering
1551    size+=1+strlen(object_names[i])+1;    // changes in future versions we can adjust in load
1552  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"describe_names",NULL,size,0));
1553
1554
1555  size=0;
1556  for (i=0;i<total_objects;i++)
1557  {
1558    size+=2;  // total number of states
1559    int j=0;
1560    for (;j<figures[i]->ts;j++)
1561      if (figures[i]->seq[j])
1562        size+=1+strlen(lstring_value(symbol_name(figures[i]->seq_syms[j])))+1;
1563  }
1564  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"describe_states",NULL,size,0));
1565
1566
1567
1568  size=0;
1569  for (i=0;i<total_objects;i++)
1570  {
1571    size+=2;  // total number of variables
1572    int j=0;
1573    for (;j<figures[i]->tiv;j++)
1574      if (figures[i]->vars[j])
1575        size+=1+strlen(lstring_value(symbol_name(figures[i]->vars[j])))+1;
1576  }
1577  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"describe_lvars",NULL,size,0));
1578
1579
1580
1581  // how many objects are we goint to save, use a int32_t to specify how many
1582  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"object_list",NULL,4,0));
1583
1584  int32_t t=0;
1585  object_node *o=save_list;
1586  for (;o;o=o->next)
1587    t++;
1588
1589  // type and state aren't normal records because they will be remapped on loading
1590  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"type",NULL,1+2*t,0));
1591  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"state",NULL,1+2*t,0));
1592
1593
1594  // now save all the lvars for each object
1595  for (size=0,o=save_list;o;o=o->next)
1596    size+=figures[o->me->otype]->tv*5+2;
1597  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"lvars",NULL,size,0));
1598
1599
1600  for (i=0;i<TOTAL_OBJECT_VARS;i++)
1601    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,object_descriptions[i].name,NULL,1+
1602              RC_type_size(object_descriptions[i].type)*t,0));
1603
1604  add_light_spec(&sd,Name);
1605
1606
1607  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"object_links",NULL,1+4+total_object_links(save_list)*8,0));
1608  sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"light_links",NULL,1+4+total_light_links(save_list)*8,0));
1609
1610  if (save_all)
1611  {
1612    t=0;
1613    view *v=player_list;
1614    for (;v;v=v->next) t++;
1615    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"player_info",NULL,t*4+4,0));
1616
1617    int tv=total_view_vars();
1618    int i=0;
1619    for (;i<tv;i++)
1620      sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,get_view_var_name(i),NULL,1+4*t,0));
1621    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"random_start",NULL,5,0));
1622
1623    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"weapon_array",NULL,1+4+total_weapons*4*t,0));
1624
1625    int name_len=0;
1626    for (v=player_list;v;v=v->next)
1627      name_len+=strlen(v->name)+2;
1628    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"player_names",NULL,name_len,0));
1629
1630    sd.add_by_hand(new spec_entry(SPEC_IMAGE,"thumb nail",NULL,4+160*(100+wm->font()->height()*2),0));
1631  }
1632
1633  sd.calc_offsets();
1634
1635  return sd.write(filename);
1636}
1637
1638void scale_put(image *im, image *screen, int x, int y, short new_width, short new_height);
1639
1640void level::write_thumb_nail(bFILE *fp, image *im)
1641{
1642  image *i=new image(160,100+wm->font()->height()*2);
1643  i->clear();
1644  scale_put(im,i,0,0,160,100);
1645  if (first_name)
1646    wm->font()->put_string(i,80-strlen(first_name)*wm->font()->width()/2,100,first_name);
1647
1648  time_t t;
1649  t=time(NULL);
1650  char buf[80];
1651
1652  strftime(buf,80,"%T %A %B %d",localtime(&t));
1653  wm->font()->put_string(i,80-strlen(buf)*wm->font()->width()/2,100+wm->font()->height(),buf);
1654
1655  fp->write_uint16(i->width());
1656  fp->write_uint16(i->height());
1657
1658  i->lock();
1659  for(int y = 0; y < i->height(); y++)
1660    fp->write(i->scan_line(y),i->width());
1661  i->unlock();
1662
1663  delete i;
1664}
1665
1666void level::write_player_info(bFILE *fp, object_node *save_list)
1667{
1668  int32_t t=0;
1669  view *v=player_list;
1670  for (;v;v=v->next) t++;
1671  fp->write_uint32(t);
1672
1673  for (v=player_list;v;v=v->next)
1674    fp->write_uint32(object_to_number_in_list(v->focus,save_list));
1675
1676  int tv=total_view_vars();
1677  int i=0;
1678  for (;i<tv;i++)
1679  {
1680    fp->write_uint8(RC_32);
1681    for (v=player_list;v;v=v->next)
1682      fp->write_uint32(v->get_view_var_value(i));
1683  }
1684
1685  fp->write_uint8(RC_32);
1686  fp->write_uint32(rand_on);
1687
1688  fp->write_uint8(RC_32);
1689  fp->write_uint32(total_weapons);
1690  for (v=player_list;v;v=v->next)
1691    for (i=0;i<total_weapons;i++)
1692      fp->write_uint32(v->weapons[i]);
1693
1694  for (v=player_list;v;v=v->next)
1695  {
1696    int len=strlen(v->name)+1;
1697    fp->write_uint8(len);
1698    fp->write(v->name,len);
1699  }
1700}
1701
1702
1703int level::load_player_info(bFILE *fp, spec_directory *sd, object_node *save_list)
1704{
1705  int ret;
1706  spec_entry *se=sd->find("player_info");
1707  if (se)
1708  {
1709    fp->seek(se->offset,0);
1710
1711    int set_first_view=0;
1712    if (the_game->first_view==player_list) set_first_view=1;
1713    int my_player_number=-1;
1714
1715    view *v=player_list;
1716    for (;v;v=v->next)
1717    { v->suggest.send_view=0;
1718      v->suggest.send_weapon_change=0;
1719    }
1720
1721    for (v=player_list;v;v=v->next)
1722      if (v->local_player())
1723         my_player_number=v->player_number;
1724
1725    while (player_list)    // delete all of the views (they will get recreated)
1726    {
1727      v=player_list;
1728      if (v->focus)
1729      {
1730        if (v->focus->controller())
1731         v->focus->set_controller(NULL);
1732    delete v->focus;
1733      }
1734
1735      player_list=player_list->next;
1736      delete v;
1737    }
1738
1739    int32_t total_players=fp->read_uint32();
1740    view *last=NULL;
1741    int i=0;
1742    for (;i<total_players;i++)
1743    {
1744      game_object *o=number_to_object_in_list(fp->read_uint32(),save_list);
1745      v=new view(o,NULL,0);
1746      if (o) o->set_controller(v);
1747      if (player_list)
1748        last->next=v;
1749      else player_list=v;
1750      last=v;
1751    }
1752    if (set_first_view)
1753      the_game->first_view=player_list;
1754
1755    for (i=0;i<total_view_vars();i++)
1756    {
1757      char const *find_name = get_view_var_name(i);
1758      se=sd->find(find_name);
1759
1760      if (se)
1761      {
1762    fp->seek(se->offset,0);
1763    if (fp->read_uint8()==RC_32)
1764    {
1765      for (v=player_list;v;v=v->next)
1766            v->set_view_var_value(i,fp->read_uint32());
1767    }
1768      } else
1769      {
1770    for (v=player_list;v;v=v->next)
1771        v->set_view_var_value(i,0);
1772      }
1773    }
1774
1775    se=sd->find("random_start");      // start of index into random table
1776    if (se)
1777    {
1778      fp->seek(se->offset,0);
1779      if (fp->read_uint8()==RC_32)
1780        rand_on=fp->read_uint32();
1781    } else rand_on=0;
1782
1783    se=sd->find("weapon_array");
1784    if (se)
1785    {
1786      fp->seek(se->offset,0);
1787      if (fp->read_uint8()==RC_32)
1788      {
1789    int32_t m=fp->read_uint32();  // read how many weapons exsisted when last saved
1790    int i;
1791    for (v=player_list;v;v=v->next)   
1792    {
1793      for (i=0;i<m;i++)
1794      {
1795        int32_t x=fp->read_uint32();
1796        if (i<total_weapons)
1797        {
1798          v->weapons[i]=x;
1799          v->last_weapons[i]=x;
1800        }
1801      }
1802    }
1803      }
1804    }  else
1805    {
1806      for (v=player_list;v;v=v->next)   
1807      {
1808    memset(v->last_weapons,0xff,total_weapons*sizeof(int32_t));
1809    memset(v->weapons,0xff,total_weapons*sizeof(int32_t));
1810      }
1811    }
1812
1813    se=sd->find("player_names");
1814    if (se)
1815    {
1816      fp->seek(se->offset,0);
1817      for (v=player_list;v;v=v->next)   
1818      {
1819    uint8_t len=fp->read_uint8();
1820    fp->read(v->name,len);
1821      }
1822    }
1823
1824    ret=1;
1825    recalc_local_view_space();
1826
1827  } else
1828  {
1829    void *fun=make_find_symbol("set_player_defaults");
1830    if (DEFINEDP(symbol_function(fun)))
1831    {
1832      view *f;
1833      game_object *o=current_object;
1834      for (f=player_list;f;f=f->next)
1835      {
1836    if (f->focus)
1837    {
1838      current_object=f->focus;
1839      void *m=mark_heap(TMP_SPACE);
1840      eval_function((lisp_symbol *)fun,NULL);
1841      restore_heap(m,TMP_SPACE);
1842    }
1843      }   
1844      current_object=o;
1845    }
1846    ret=0;
1847  }
1848
1849  view *vw;
1850  for (vw=player_list;vw;vw=vw->next)
1851  {
1852    if (total_weapons && !vw->has_weapon(vw->current_weapon))
1853    {
1854      vw->suggest.send_weapon_change=1;
1855      vw->suggest.new_weapon=0;
1856    }
1857  }
1858
1859  return ret;
1860}
1861
1862
1863void level::write_objects(bFILE *fp, object_node *save_list)
1864{
1865  // record information in the file about what the data structures look like
1866  // right now, so if they change later, they don't get get screwed up
1867  fp->write_uint16(total_objects);   // mark how many objects we know about right now
1868
1869  int i=0;
1870  for (;i<total_objects;i++)   // loop through all the object types we know of
1871  {
1872    fp->write_uint8(strlen(object_names[i])+1);                    // sizeof name
1873    fp->write(object_names[i],strlen(object_names[i])+1);      // write object name
1874  }
1875
1876
1877  // write state numbers and names for each object
1878  for (i=0;i<total_objects;i++)
1879  {
1880    int total=0;
1881    int j=0;
1882    for (;j<figures[i]->ts;j++)
1883      if (figures[i]->seq[j]) total++;
1884    fp->write_uint16(total);
1885
1886    for (j=0;j<figures[i]->ts;j++)
1887      if (figures[i]->seq[j])
1888      {
1889    char *state_name=lstring_value(symbol_name(figures[i]->seq_syms[j]));
1890    fp->write_uint8(strlen(state_name)+1);
1891    fp->write(state_name,strlen(state_name)+1);
1892      }
1893  }
1894
1895
1896  // write object lvar names
1897  for (i=0;i<total_objects;i++)
1898  {
1899    fp->write_uint16(figures[i]->tv);
1900    int j,x;
1901
1902    for (x=0;x<figures[i]->tv;x++)
1903    {
1904      for (j=0;j<figures[i]->tiv;j++)
1905      {
1906        if (figures[i]->vars[j] && figures[i]->var_index[j]==x)
1907    {
1908      char *var_name=lstring_value(symbol_name(figures[i]->vars[j]));
1909      fp->write_uint8(strlen(var_name)+1);
1910      fp->write(var_name,strlen(var_name)+1);
1911    }
1912      }
1913    }
1914  }
1915
1916  int32_t t=0;
1917  object_node *o=save_list;
1918  for (;o;o=o->next) t++;
1919  fp->write_uint32(t);
1920
1921
1922  fp->write_uint8(RC_16);                                    // save type info for each record
1923  for (o=save_list;o;o=o->next) fp->write_uint16(o->me->type());
1924
1925  fp->write_uint8(RC_16);                                    // save state info for each record
1926  for (o=save_list;o;o=o->next) fp->write_uint16(o->me->reduced_state());
1927
1928  for (o=save_list;o;o=o->next)                            // save lvars
1929  {
1930    fp->write_uint16(figures[o->me->otype]->tv);
1931    for (i=0;i<figures[o->me->otype]->tv;i++)
1932    {
1933      fp->write_uint8(RC_32);                           // for now the only type allowed is int32_t
1934      fp->write_uint32(o->me->lvars[i]);
1935    }
1936  }
1937
1938  for (i=0;i<default_simple.total_vars();i++)
1939  {
1940    int t=object_descriptions[i].type;
1941    fp->write_uint8(t);
1942    for (o=save_list;o;o=o->next)
1943    {
1944      switch (t)
1945      {     
1946    case RC_8 :
1947    { fp->write_uint8(o->me->get_var(i)); } break;
1948    case RC_16 :
1949    { fp->write_uint16(o->me->get_var(i)); } break;
1950    case RC_32 :
1951    { fp->write_uint32(o->me->get_var(i)); } break;
1952      }
1953    }
1954  }
1955}
1956
1957
1958int32_t level::total_object_links(object_node *list)
1959{
1960  int32_t tl=0;
1961  for (object_node *o=list;o;o=o->next)
1962    tl+=o->me->total_objects();
1963  return tl;
1964}
1965
1966int32_t level::total_light_links(object_node *list)
1967{
1968  int32_t tl=0;
1969  for (object_node *o=list;o;o=o->next)
1970    tl+=o->me->total_lights();
1971  return tl;
1972}
1973
1974void level::write_links(bFILE *fp, object_node *save_list, object_node *exclude_list)
1975{
1976  fp->write_uint8(RC_32);
1977  fp->write_uint32(total_object_links(save_list));
1978
1979  int x=1;
1980  object_node *o=save_list;
1981
1982  for (;o;o=o->next,x++)
1983  {
1984    int i=0;
1985    for (;i<o->me->total_objects();i++)
1986    {
1987      fp->write_uint32(x);
1988      int32_t x=object_to_number_in_list(o->me->get_object(i),save_list);
1989      if (x)
1990        fp->write_uint32(x);
1991      else                            // save links to excluded items as negative
1992        fp->write_uint32((int32_t)(-(object_to_number_in_list(o->me,exclude_list))));
1993    }
1994  }
1995
1996  fp->write_uint8(RC_32);
1997  fp->write_uint32(total_light_links(save_list));
1998
1999  x=1;
2000  for (o=save_list;o;o=o->next,x++)
2001  {
2002    int i=0;
2003    for (;i<o->me->total_lights();i++)
2004    {
2005      fp->write_uint32(x);
2006      fp->write_uint32(light_to_number(o->me->get_light(i)));
2007    }
2008  }
2009
2010}
2011
2012
2013void level::load_links(bFILE *fp, spec_directory *sd,
2014               object_node *save_list, object_node *exclude_list)
2015{
2016  spec_entry *se=sd->find("object_links");
2017  if (se)
2018  {
2019    fp->seek(se->offset,0);
2020    if (fp->read_uint8()==RC_32)
2021    {
2022      int32_t t=fp->read_uint32();
2023      while (t)
2024      {
2025    int32_t x1=fp->read_uint32();
2026    CONDITION(x1>=0,"expected x1 for object link to be > 0\n");
2027    int32_t x2=fp->read_uint32();
2028    game_object *p,*q=number_to_object_in_list(x1,save_list);
2029    if (x2>0)
2030      p=number_to_object_in_list(x2,save_list);
2031    else p=number_to_object_in_list(-x2,exclude_list);
2032    if (q)
2033      q->add_object(p);
2034    else dprintf("bad object link\n");
2035
2036    t--;
2037      }
2038    }
2039  }
2040
2041  se=sd->find("light_links");
2042  if (se)
2043  {
2044    fp->seek(se->offset,0);
2045    if (fp->read_uint8()==RC_32)
2046    {
2047      int32_t t=fp->read_uint32();
2048      while (t)
2049      {
2050    int32_t x1=fp->read_uint32();
2051    int32_t x2=fp->read_uint32();
2052    game_object *p=number_to_object_in_list(x1,save_list);
2053    if (p)
2054      p->add_light(number_to_light(x2));
2055    else dprintf("bad object/light link\n");
2056    t--;
2057      }
2058    }
2059  }
2060
2061}
2062
2063
2064void level::write_options(bFILE *fp)
2065{
2066  // save background scroll rate
2067  fp->write_uint8(RC_32);
2068  fp->write_uint32(bg_xmul);
2069  fp->write_uint32(bg_xdiv);
2070  fp->write_uint32(bg_ymul);
2071  fp->write_uint32(bg_ydiv);
2072
2073  fp->write_uint8(RC_32);
2074  int ta=0;
2075  area_controller *a=area_list;
2076  for (;a;a=a->next) ta++;
2077  fp->write_uint32(ta);
2078  for (a=area_list;a;a=a->next)
2079  {
2080    fp->write_uint32(a->x);
2081    fp->write_uint32(a->y);
2082    fp->write_uint32(a->w);
2083    fp->write_uint32(a->h);
2084    fp->write_uint32(a->active);
2085
2086    fp->write_uint32(a->ambient);
2087    fp->write_uint32(a->view_xoff);
2088    fp->write_uint32(a->view_yoff);
2089    fp->write_uint32(a->ambient_speed);
2090    fp->write_uint32(a->view_xoff_speed);
2091    fp->write_uint32(a->view_yoff_speed);
2092  }
2093  fp->write_uint8(RC_32);
2094  fp->write_uint32(tick_counter());
2095}
2096
2097void level::load_options(spec_directory *sd, bFILE *fp)
2098{
2099  spec_entry *se=sd->find("bg_scroll_rate");
2100  if (se)
2101  {
2102    fp->seek(se->offset,0);
2103    if (fp->read_uint8()!=RC_32)
2104    { bg_xmul=bg_ymul=1; bg_xdiv=bg_ydiv=8; }
2105    else
2106    {
2107      bg_xmul=fp->read_uint32();
2108      bg_xdiv=fp->read_uint32();
2109      bg_ymul=fp->read_uint32();
2110      bg_ydiv=fp->read_uint32();
2111    }
2112  } else { bg_xmul=bg_ymul=1; bg_xdiv=bg_ydiv=8; }
2113
2114  se=sd->find("area_list.v1");
2115  if (se)
2116  {
2117    fp->seek(se->offset,0);
2118    if (fp->read_uint8()==RC_32)
2119    {
2120      area_controller *l=NULL,*p;
2121      int32_t ta=fp->read_uint32();
2122      int i=0;
2123      for (;i<ta;i++)
2124      {
2125    int32_t x,y,w,h;
2126    x=fp->read_uint32();
2127    y=fp->read_uint32();
2128    w=fp->read_uint32();
2129    h=fp->read_uint32();   
2130    p=new area_controller(x,y,w,h,NULL);
2131    if (l) l->next=p;
2132    else area_list=p;
2133    l=p;
2134    p->active=fp->read_uint32();
2135    p->ambient=fp->read_uint32();
2136    p->view_xoff=fp->read_uint32();
2137    p->view_yoff=fp->read_uint32();
2138    p->ambient_speed=fp->read_uint32();
2139    p->view_xoff_speed=fp->read_uint32();
2140    p->view_yoff_speed=fp->read_uint32();
2141      }
2142    }
2143  }
2144
2145  se=sd->find("tick_counter");
2146  if (se)
2147  {
2148    fp->seek(se->offset,0);
2149    if (fp->read_uint8()==RC_32)
2150      set_tick_counter(fp->read_uint32());
2151    else set_tick_counter(0);
2152  } else set_tick_counter(0);
2153}
2154
2155
2156void level::write_cache_prof_info()
2157{
2158  if (cache.prof_is_on())
2159  {
2160    char pf_name[100];
2161    if (first_name)
2162      get_prof_assoc_filename(first_name,pf_name);
2163    else
2164      get_prof_assoc_filename(Name,pf_name);
2165
2166
2167    spec_directory sd;
2168    sd.add_by_hand(new spec_entry(SPEC_DATA_ARRAY,"cache profile info",NULL,cache.prof_size(),0));
2169    sd.calc_offsets();
2170    jFILE *fp2=sd.write(pf_name);
2171    if (!fp2)
2172      the_game->show_help("Unable to open cache profile output file");
2173    else
2174    {
2175      cache.prof_write(fp2);
2176      delete fp2;
2177    }
2178    sd.delete_entries();
2179  }
2180
2181}
2182
2183void level::load_cache_info(spec_directory *sd, bFILE *fp)
2184{
2185  if (!DEFINEDP(symbol_value(l_empty_cache)) || !symbol_value(l_empty_cache))
2186  {
2187    char pf_name[100];
2188    if (first_name)
2189      get_prof_assoc_filename(first_name,pf_name);  // get cache info from orignal filename if this is a savegame
2190    else
2191      get_prof_assoc_filename(Name,pf_name);
2192
2193
2194    cache.load_cache_prof_info(pf_name,this);
2195  }
2196}
2197
2198
2199int level::save(char const *filename, int save_all)
2200{
2201    char name[255], bkname[255];
2202
2203    sprintf( name, "%s%s", get_save_filename_prefix(), filename );
2204    sprintf( bkname, "%slevsave.bak", get_save_filename_prefix() );
2205    if( !save_all && DEFINEDP( symbol_value( l_keep_backup ) ) &&
2206        symbol_value( l_keep_backup ) )   // make a backup
2207    {
2208        bFILE *fp = open_file( name, "rb" );    // does file already exist?
2209        if( !fp->open_failure() )
2210        {
2211            unlink( bkname );
2212            bFILE *bk = open_file( bkname, "wb" );
2213            if( bk->open_failure() )
2214                dprintf("unable to open backup file %s\n", bkname );
2215            else
2216            {
2217                uint8_t buf[0x1000];
2218                int32_t size = fp->file_size();
2219                int tr = 1;
2220                while( size && tr )
2221                {
2222                    int tr = fp->read(buf,0x1000);
2223                    if( tr )
2224                    tr = bk->write(buf,tr);
2225                    size -= tr;
2226                }
2227            }
2228            delete bk;
2229#if (defined(__MACH__) || !defined(__APPLE__))
2230            chmod( bkname, S_IRWXU | S_IRWXG | S_IRWXO );
2231#endif
2232        }
2233        delete fp;
2234    }
2235
2236    // if we are not doing a savegame then change the first_name to this name
2237    if( !save_all )
2238    {
2239        if( first_name )
2240            free(first_name);
2241        first_name = (char *)malloc( strlen( name ) + 1 );
2242        strcpy( first_name, name );
2243    }
2244
2245    object_node *players, *objs;
2246    if( save_all )
2247        players = NULL;
2248    else
2249        players = make_player_onodes();
2250
2251    objs = make_not_list(players);     // negate the above list
2252
2253    bFILE *fp = create_dir( name, save_all, objs, players);
2254    if( fp != NULL )
2255    {
2256        if( !fp->open_failure() )
2257        {
2258            if( first_name )
2259            {
2260                fp->write_uint8( strlen( first_name ) + 1 );
2261                fp->write( first_name, strlen( first_name ) + 1 );
2262            }
2263            else
2264            {
2265                fp->write_uint8( 1 );
2266                fp->write_uint8( 0 );
2267            }
2268
2269            fp->write_uint32( fg_width );
2270            fp->write_uint32( fg_height );
2271
2272            int t  = fg_width * fg_height;
2273            uint16_t *rm = map_fg;
2274            for (;t;t--,rm++)
2275            {
2276                uint16_t x = *rm;
2277                x = lstl(x);            // convert to intel endianess
2278                *rm = x;
2279            }
2280
2281            fp->write( (char *)map_fg, 2 * fg_width * fg_height );
2282            t = fg_width * fg_height;
2283            rm = map_fg;
2284            for (;t;t--,rm++)
2285            {
2286                uint16_t x = *rm;
2287                x = lstl( x );            // convert to intel endianess
2288                *rm = x;
2289            }
2290
2291            fp->write_uint32( bg_width );
2292            fp->write_uint32( bg_height );
2293            t = bg_width * bg_height;
2294            rm = map_bg;
2295
2296            for (;t;t--,rm++)
2297            {
2298                uint16_t x=*rm;
2299                x = lstl( x );        // convert to intel endianess
2300                *rm = x;
2301            }
2302
2303            fp->write( (char *)map_bg, 2 * bg_width * bg_height );
2304            rm = map_bg;
2305            t = bg_width*bg_height;
2306
2307            for (;t;t--,rm++)
2308            {
2309                uint16_t x = *rm;
2310                x = lstl( x );        // convert to intel endianess
2311                *rm = x;
2312            }
2313
2314            write_options( fp );
2315            write_objects( fp, objs );
2316            write_lights( fp );
2317            write_links( fp, objs, players );
2318            if( save_all )
2319            {
2320                write_player_info( fp, objs );
2321                write_thumb_nail( fp,screen );
2322            }
2323
2324            delete fp;
2325#if (defined(__MACH__) || !defined(__APPLE__))
2326            chmod( name, S_IRWXU | S_IRWXG | S_IRWXO );
2327#endif
2328            write_cache_prof_info();
2329        }
2330        else
2331        {
2332            the_game->show_help( "Unable to open file for saving\n" );
2333            delete fp;
2334            return 0;
2335        }
2336    }
2337    else
2338    {
2339        the_game->show_help( "Unable to open file for saving.\n" );
2340        printf( "\nFailed to save game.\n" );
2341        printf( "I was trying to save to: '%s'\n\tPath: '%s'\n\tFile: '%s'\n", name, get_save_filename_prefix(), filename );
2342        printf( "\nPlease send an email to:\n\ttrandor@labyrinth.net.au\nwith these details.\nThanks.\n" );
2343        return 0;
2344    }
2345
2346    delete_object_list(players);
2347    delete_object_list(objs);
2348
2349    return 1;
2350}
2351
2352level::level(int width, int height, char const *name)
2353{
2354  the_game->need_refresh();
2355  area_list=NULL;
2356  set_tick_counter(0);
2357
2358  attack_list=NULL;
2359  attack_list_size=attack_total=0;
2360
2361  target_list=NULL;
2362  target_list_size=target_total=0;
2363
2364  block_list=NULL;
2365  block_list_size=block_total=0;
2366
2367  all_block_list=NULL;
2368  all_block_list_size=all_block_total=0;
2369
2370  Name=NULL;
2371  first_name=NULL;
2372
2373  set_name(name);
2374  first=first_active=NULL;
2375
2376  fg_width=width;
2377  fg_height=height;
2378  calc_bgsize(fg_width,fg_height,bg_width,bg_height);
2379
2380  map_bg=(uint16_t *)malloc(sizeof(int16_t)*bg_width*bg_height);
2381  map_fg=(uint16_t *)malloc(sizeof(int16_t)*fg_width*fg_height);
2382
2383
2384
2385  memset(map_bg,0,sizeof(int16_t)*bg_width*bg_height);
2386  memset(map_fg,0,sizeof(int16_t)*fg_width*fg_height);
2387
2388  int i;
2389  for (i=0;i<fg_width;i++)
2390  {
2391    map_fg[i]=1;
2392    map_fg[fg_width*(fg_height-1)+i]=1;
2393  }
2394  for (i=0;i<fg_height;i++)
2395  {
2396    map_fg[fg_width*i]=1;
2397    map_fg[fg_width*i+fg_width-1]=1;
2398  }
2399
2400  total_objs=0;
2401  insert_players();
2402}
2403
2404
2405void level::add_object(game_object *new_guy)
2406{
2407  total_objs++;
2408  new_guy->next=NULL;
2409  if (figures[new_guy->otype]->get_cflag(CFLAG_ADD_FRONT))
2410  {
2411    if (!first)
2412      first=new_guy;
2413    else
2414      last->next=new_guy;
2415    last=new_guy;
2416  } else
2417  {
2418    if (!first)
2419      last=first=new_guy;
2420    else
2421    {
2422      new_guy->next=first;
2423      first=new_guy;
2424    }
2425  }
2426}
2427
2428void level::add_object_after(game_object *new_guy,game_object *who)
2429{
2430  if (!who) add_object(new_guy);
2431  else
2432  {
2433    total_objs++;
2434    if (who==last) last=new_guy;
2435    new_guy->next=who->next;
2436    who->next=new_guy;
2437  }
2438}
2439
2440void level::delete_object(game_object *who)
2441{
2442  remove_object(who);
2443  delete who;
2444}
2445
2446void level::remove_block(game_object *who)
2447{
2448  int i=0,j;
2449  game_object **o=block_list;
2450  for (;i<block_total;i++)
2451  {
2452    if (*o==who)        // is this object in the block list?
2453    {
2454      block_total--;    // squish the block list in
2455      o++;
2456      for (j=i;j<block_total;j++)
2457        block_list[j]=block_list[j+1];
2458    } else o++;
2459  }
2460}
2461
2462
2463// searches through the all_block list for who and if it finds it deletes it
2464void level::remove_all_block(game_object *who)
2465{
2466  int i=0,j;
2467  game_object **o=all_block_list;
2468  for (;i<all_block_total;i++)
2469  {
2470    if (*o==who)        // is this object in the block list?
2471    {
2472      all_block_total--;    // squish the block list in
2473      o++;
2474      for (j=i;j<all_block_total;j++)
2475        all_block_list[j]=all_block_list[j+1];
2476    } else o++;
2477  }
2478}
2479
2480void level::remove_object(game_object *who)
2481{
2482  if (dev_cont)
2483    dev_cont->notify_deleted_object(who);
2484
2485  if (who==first)
2486  {
2487    if (who==last) last=NULL;
2488    first=first->next;
2489  }
2490  else
2491  {
2492    game_object *o=first;
2493    for (;o && o->next!=who;o=o->next);
2494    if (o)
2495    {
2496      o->next=who->next;
2497      if (!o->next) last=o;
2498    }
2499    else return ;     // if object is not in level, don't try to do anything else
2500  }
2501  total_objs--;
2502
2503
2504  if (first_active==who)
2505    first_active=who->next_active;
2506  else
2507  {
2508    game_object *o=first_active;
2509    for (;o && o->next_active!=who;o=o->next_active);
2510    if (o)
2511      o->next_active=who->next_active;
2512  }
2513
2514  if (who->flags()&KNOWN_FLAG)
2515  {
2516    game_object *o=first;
2517    for (;o;o=o->next)
2518    {
2519      int t=o->total_objects();
2520      int i=0;
2521      for (;i<t;i++)
2522        if (o->get_object(i)==who)
2523    {
2524      o->remove_object(who);
2525      t=o->total_objects();
2526    }
2527    }
2528  }
2529
2530  if (who->otype<0xffff)
2531  {
2532    if (who->can_block())  // remove object from block list and all_block if nessasary
2533    {
2534      remove_block(who);
2535      remove_all_block(who);
2536    } else if (who->hurtable())
2537      remove_all_block(who);
2538  }
2539
2540
2541  int t=who->total_objects();
2542  while (t) { who->remove_object(who->get_object(0)); t--; }
2543
2544  t=who->total_lights();
2545  while (t) { who->remove_light(who->get_light(0)); t--; }
2546}
2547
2548void level::to_front(game_object *o)  // move to end of list, so we are drawn last, therefore top
2549{
2550  if (o==last) return ;
2551  first_active=NULL;     // make sure nothing goes screwy with the active list
2552
2553  if (o==first)
2554    first=first->next;
2555  else
2556  {
2557    game_object *w=first;
2558    for (;w && w->next!=o;w=w->next);
2559    if (!w) return ;
2560    w->next=o->next;
2561  }
2562
2563  last->next=o;
2564  o->next=NULL;
2565  last=o;
2566}
2567
2568void level::to_back(game_object *o)   // to make the character drawn in back, put at front of list
2569{
2570  if (o==first) return;
2571  first_active=NULL;     // make sure nothing goes screwy with the active list
2572
2573  game_object *w=first;
2574  for (;w && w->next!=o;w=w->next);
2575  if (!w) return;
2576  if (last==o)
2577    last=w;
2578  w->next=o->next;
2579  o->next=first;
2580  first=o;
2581}
2582
2583
2584game_object *level::find_self(game_object *me)
2585{
2586  return me;
2587}
2588
2589game_object *level::find_object(int32_t x, int32_t y)
2590{
2591  int32_t x1,y1,x2,y2;
2592  game_object *o=first;
2593  for (;o;o=o->next)
2594  {
2595    o->picture_space(x1,y1,x2,y2);
2596    if (x<x2 && x>=x1 && y<y2 && y>=y1)
2597      return o;
2598  }
2599  return NULL;
2600}
2601
2602int32_t last_tile_hit_x,last_tile_hit_y;
2603
2604#define remapx(x) (x==0 ? -1 : x==tl-1 ? tl+1 : x)
2605#define remapy(y) (y==0 ? -1 : y==th-1 ? th+1 : y)
2606
2607void level::foreground_intersect(int32_t x1, int32_t y1, int32_t &x2, int32_t &y2)
2608{
2609/*  if (x1==x2)
2610  { vforeground_intersect(x1,y1,y2);
2611    return ;
2612  }  */
2613
2614  int32_t tl=the_game->ftile_width(),th=the_game->ftile_height(),
2615    j,
2616    xp1,yp1,xp2,yp2,    // starting and ending points of block line segment
2617    swap;               // temp var
2618  int32_t blockx1,blocky1,blockx2,blocky2,block,bx,by;
2619  point_list *block_list;
2620  unsigned char *bdat;
2621
2622  blockx1=x1;
2623  blocky1=y1;
2624  blockx2=x2;
2625  blocky2=y2;
2626  if (blockx1>blockx2) { swap=blockx1; blockx1=blockx2; blockx2=swap; }
2627  if (blocky1>blocky2) { swap=blocky1; blocky1=blocky2; blocky2=swap; }
2628  blockx1=(blockx1-2)/tl-1;
2629  blockx2=(blockx2+tl+2)/tl+1;
2630  blocky1=(blocky1-2)/th-1;
2631  blocky2=(blocky2+th+2)/th+1;
2632
2633
2634  if (blockx2>=foreground_width()) { x2=tl*foreground_width()-1; }
2635  if (blocky2>=foreground_height()) { y2=th*foreground_height()-1; }
2636  blockx1=max(blockx1,0);
2637  blocky1=max(blocky1,0);
2638
2639  if ((blockx1>blockx2) || (blocky1>blocky2)) return ;
2640
2641  // now check all the map positions this line could intersect
2642  for (bx=blockx1;bx<=blockx2;bx++)
2643  {
2644    for (by=blocky1;by<=blocky2;by++)
2645    {
2646      block=the_game->get_map_fg(bx,by);
2647      if (block>BLACK)        // don't check BLACK, should be no points in it
2648      {
2649        // now check the all the line segments in the block
2650        foretile *f=the_game->get_fg(block);
2651        block_list=f->points;
2652        unsigned char total=block_list->tot;
2653        bdat=block_list->data;
2654        unsigned char *ins=f->points->inside;
2655    int32_t xo=bx*tl,yo=by*th;
2656        for (j=0;j<total-1;j++,ins++)
2657        {
2658          // find the starting and ending points for this segment
2659      xp1=xo+remapx(*bdat);
2660      bdat++;
2661
2662      yp1=yo+remapy(*bdat);
2663      bdat++;
2664
2665      xp2=xo+remapx(*bdat);
2666      yp2=yo+remapy(bdat[1]);
2667
2668
2669      int32_t ox2=x2,oy2=y2;
2670          if (*ins)   
2671            setback_intersect(x1,y1,x2,y2,xp1,yp1,xp2,yp2,1);
2672          else
2673            setback_intersect(x1,y1,x2,y2,xp1,yp1,xp2,yp2,-1);
2674      if (ox2!=x2 || oy2!=y2)
2675      {
2676        last_tile_hit_x=bx;
2677        last_tile_hit_y=by;
2678      }
2679
2680        }
2681      }
2682    }
2683  }
2684}
2685
2686
2687void level::vforeground_intersect(int32_t x1, int32_t y1, int32_t &y2)
2688{
2689  int32_t tl=f_wid,th=f_hi,
2690    j,
2691    xp1,yp1,xp2,yp2;    // starting and ending points of block line segment temp var
2692  int32_t blocky1,blocky2,block,bx,by,checkx;
2693  point_list *block_list;
2694  unsigned char *bdat;
2695
2696  int y_addback;
2697  if (y1>y2)
2698  {
2699    blocky1=y2/th;
2700    blocky2=y1/th;
2701    y_addback=blocky2*f_hi;
2702  } else
2703  {
2704    blocky1=y1/th;
2705    blocky2=y2/th;
2706    y_addback=blocky1*f_hi;
2707  }
2708
2709  y1-=y_addback;
2710  y2-=y_addback;
2711
2712  bx=x1/f_wid;
2713  checkx=x1-bx*f_wid;
2714
2715
2716  // now check all the map positions this line could intersect
2717
2718  for (by=blocky1;by<=blocky2;by++,y1-=f_hi,y2-=f_hi,y_addback+=f_hi)
2719  {
2720    block=the_game->get_map_fg(bx,by);
2721
2722    // now check the all the line segments in the block
2723    foretile *f=the_game->get_fg(block);
2724    block_list=f->points;
2725
2726    unsigned char total=block_list->tot;
2727    bdat=block_list->data;
2728    unsigned char *ins=f->points->inside;
2729
2730//    int32_t xo=bx*tl,yo=by*th;
2731    for (j=0;j<total-1;j++,ins++)
2732    {
2733      // find the starting and ending points for this segment
2734      xp1=remapx(*bdat);
2735      bdat++;
2736
2737      yp1=remapy(*bdat);
2738      bdat++;
2739
2740      xp2=remapx(*bdat);
2741      yp2=remapy(bdat[1]);
2742
2743
2744      int32_t oy2=y2;
2745      if (*ins)   
2746        setback_intersect(checkx,y1,checkx,y2,xp1,yp1,xp2,yp2,1);
2747      else
2748        setback_intersect(checkx,y1,checkx,y2,xp1,yp1,xp2,yp2,-1);
2749      if (oy2!=y2)
2750      {
2751    last_tile_hit_x=bx;
2752    last_tile_hit_y=by;
2753      }
2754    }
2755  }
2756  y2+=y_addback;
2757}
2758
2759
2760
2761void level::send_signal(int32_t signal)
2762{
2763  if (signal)   // signal 0 is never sent!
2764  {
2765    game_object *o=first_active;
2766    for (;o;o=o->next_active)
2767      o->recieve_signal(signal);
2768  }
2769}
2770
2771
2772int level::crush(game_object *by_who, int xamount, int yamount)
2773{
2774  int32_t xv,yv,crushed=0;
2775  game_object *o=first_active;
2776  for (;o;o=o->next_active)
2777  {
2778    if (o->hurtable() && o!=by_who)
2779    {
2780      xv=-xamount;
2781      yv=-yamount;
2782      if (o->try_move(o->x,o->y,xv,yv,3)==by_who)
2783      {
2784    xv=xamount;
2785    yv=yamount;
2786    o->try_move(o->x,o->y,xv,yv,3);
2787    if (xv==0 && yv==0)
2788    {
2789      if (o->state!=dead && o->state!=dieing)
2790        o->do_damage(by_who->current_figure()->hit_damage,by_who,o->x,o->y,0,0);
2791
2792/*      {   
2793        if (o->has_sequence(dieing))
2794          o->set_state(dieing);
2795        else o->set_state(dead);
2796          o->hp=0;   
2797      }        */
2798      crushed=1;   
2799    }
2800      }
2801    }
2802  }
2803
2804  return crushed;
2805}
2806
2807
2808int level::platform_push(game_object *by_who, int xamount, int yamount)
2809{
2810  int failed=0;
2811  int32_t xv,yv;
2812  game_object *o=first_active;
2813  for (;o;o=o->next_active)
2814  {
2815    if (o->is_playable() && o->state!=dieing && o->state!=dead)
2816    {
2817      // check to see if the platform is going up and will run into us.
2818      int32_t tvx,tvy;
2819      if (yamount<0)
2820      {
2821    tvx=-xamount;
2822    tvy=-yamount;
2823    if (o->try_move(o->x,o->y,tvx,tvy,1)==by_who)
2824    {
2825      o->x+=tvx;
2826      o->y+=tvy;
2827    }
2828      }
2829
2830/*      xv=xamount;
2831      yv=yamount;
2832      tvx,tvy;
2833      if (xv>0) tvx=xv+1; else if (xv<0) tvx=xv-1; else tvx=0;
2834      if (yv>0) tvy=yv+1; else if (yv<0) tvx=yv-1; else tvy=0;
2835      if (o->try_move(o->x,o->y,tvx,tvy,1)==by_who)  // we the platform hit us?
2836      {
2837    o->x+=tvx;
2838    o->y+=tvy;
2839      }*/
2840
2841      xv=0;
2842      yv=2;
2843      if (o->try_move(o->x,o->y,xv,yv,1)==by_who)  // are we standing on the platform?
2844      {
2845    by_who->x=-by_who->x;
2846    xv=xamount;
2847    yv=yamount;
2848    o->try_move(o->x,o->y,xv,yv,3);
2849    if (xv!=xamount || yv!=yamount) failed=1;
2850    o->x+=xv;
2851    o->y+=yv;
2852    by_who->x=-by_who->x;
2853      }
2854    }
2855  }
2856  return !failed;
2857}
2858
2859int level::push_characters(game_object *by_who, int xamount, int yamount)
2860{
2861  int32_t xv,yv;
2862  int failed=0;
2863  game_object *o=first_active;
2864  for (;o;o=o->next_active)
2865  {
2866    if ((o->is_playable() || o->pushable()) && o->state!=dieing && o->state!=dead)
2867    {
2868      xv=-xamount;
2869      yv=-yamount;
2870      int32_t tvx,tvy;
2871      if (xv>0) tvx=xv+1; else if (xv<0) tvx=xv-1; else tvx=0;
2872      if (yv>0) tvy=yv+1; else if (yv<0) tvx=yv-1; else tvy=0;
2873      if (o->try_move(o->x+xamount,o->y+yamount,tvx,tvy,3)==by_who)
2874      {
2875    xv=(xamount-tvx);
2876    yv=(yamount-tvy);
2877    o->try_move(o->x,o->y,xv,yv,3);
2878    o->x+=xv;
2879    o->y+=yv;
2880    if (xv!=xamount-tvx || yv!=yamount-tvy)
2881      failed=1;
2882      }
2883    }
2884  }
2885  return !failed;
2886}
2887
2888game_object *level::find_xrange(int x, int y, int type, int xd)
2889{
2890  int32_t find_ydist=100000;
2891  game_object *find=NULL;
2892  game_object *o=first_active;
2893  for (;o;o=o->next_active)
2894  {
2895    if (o->otype==type)
2896    {
2897      int x_dist=abs(x-o->x);
2898      int y_dist=abs(y-o->y);
2899
2900      if (x_dist<xd  && y_dist<find_ydist)
2901      {
2902    find_ydist=y_dist;
2903    find=o;
2904      }
2905    }
2906  }
2907  return find;
2908}
2909
2910
2911game_object *level::find_xclosest(int x, int y, int type, game_object *who)
2912{
2913  int32_t find_ydist=100000,find_xdist=0xffffff;
2914  game_object *find=NULL;
2915  game_object *o=first_active;
2916  for (;o;o=o->next_active)
2917  {
2918    if (o->otype==type && o!=who)
2919    {
2920      int x_dist=abs(x-o->x);
2921      if (x_dist<find_xdist)
2922      {
2923    find_xdist=x_dist;
2924    find_ydist=abs(y-o->y);
2925    find=o;
2926      }
2927      else if (x_dist==find_xdist)
2928      {
2929    int y_dist=abs(y-o->y);
2930    if (y_dist<find_ydist)
2931    {
2932      find_ydist=y_dist;
2933      find=o;
2934    }
2935      }
2936    }
2937  }
2938  return find;
2939}
2940
2941game_object *level::find_closest(int x, int y, int type, game_object *who)
2942{
2943  int32_t find_dist=100000;
2944  game_object *find=NULL;
2945  game_object *o=first_active;
2946  for (;o;o=o->next_active)
2947  {
2948    if (o->otype==type && o!=who)
2949    {
2950      int d=(x-o->x)*(x-o->x)+(y-o->y)*(y-o->y);
2951      if (d<find_dist)
2952      {
2953    find=o;
2954    find_dist=d;
2955      }
2956    }
2957  }
2958  return find;
2959}
2960
2961
2962
2963void level::remove_light(light_source *which)
2964{
2965  if (which->known)
2966  {
2967    game_object *o=first;
2968    for (;o;o=o->next)
2969    {
2970      int t=o->total_lights();
2971      int i=0;
2972      for (;i<t;i++)
2973        if (o->get_light(i)==which)
2974      o->remove_light(o->get_light(i));
2975    }
2976  }
2977  delete_light(which);
2978}
2979
2980
2981game_object *level::find_type(int type, int skip)
2982{
2983  game_object *l=NULL;
2984  game_object *o=first;
2985  for (;o;o=o->next)
2986  {
2987    if (o->otype==type)
2988    {
2989      if (!skip)
2990        return o;
2991      skip--;
2992      l=o;
2993    }
2994  }
2995  return l;
2996}
2997
2998void level::hurt_radius(int32_t x, int32_t y,int32_t r, int32_t m, game_object *from, game_object *exclude,
2999            int max_push)
3000{
3001  if (r<1) return ;   // avoid dev vy zero
3002  game_object *o=first_active;
3003  for (;o;o=o->next_active)
3004  {
3005    if (o!=exclude && o->hurtable())
3006    {
3007      int32_t y1=o->y,y2=o->y-o->picture()->height();
3008      int32_t cx=abs(o->x-x),cy1=abs(y1-y),d1,d2,cy2=abs(y2-y);
3009      if (cx<cy1)
3010        d1=cx+cy1-(cx>>1);
3011      else d1=cx+cy1-(cy1>>1);
3012
3013      if (cx<cy2)
3014        d2=cx+cy2-(cx>>1);
3015      else d2=cx+cy2-(cy2>>1);
3016      if (d2<d1)
3017        d1=d2;
3018
3019
3020
3021      if (d1<r)
3022      {
3023
3024    int px=(r-cx)*max_push/r,py=(r-cy1)*max_push/r;
3025    if (o->x<x)
3026          px=-px;
3027    if (o->y<y)
3028          py=-py;
3029    o->do_damage((r-d1)*m/r,from,x,y1,px,py);
3030      }
3031
3032
3033    }
3034  }
3035
3036}
3037
3038
3039
3040game_object *level::get_random_start(int min_player_dist, view *exclude)
3041{
3042  int t=0;
3043  game_object *o=first;
3044  for (;o;o=o->next)
3045    if (o->otype==start_position_type) t++;    // count how many starts there are in the level
3046
3047  if (t==0) return NULL;                       // there aren't any starts in level!
3048
3049  int retries=t;
3050  do
3051  {
3052    int ctry=jrandom(t)+1;
3053    game_object *n=first;
3054    for (n=first;ctry && n;n=n->next)
3055    {
3056      if (n->otype==start_position_type)
3057      {
3058    o=n;
3059        ctry--;
3060      }
3061    }
3062
3063    int too_close=0;
3064    view *v=player_list;
3065    for (;v;v=v->next)
3066    {
3067      if (v!=exclude)
3068      {
3069    int32_t cx=abs(v->x_center()-o->x),cy=abs(v->y_center()-o->y),d;
3070    if (cx<cy)
3071          d=cx+cy-(cx>>1);
3072    else d=cx+cy-(cy>>1);
3073    if (d<min_player_dist) too_close=1;
3074      }
3075    }
3076    if (too_close) retries--;
3077    else retries=0;
3078  } while (retries);
3079
3080  return o;
3081}
3082
3083
3084
3085
3086
3087void level::insert_players()
3088{
3089
3090  int start=0;
3091  int i=0;
3092  for (;i<total_objects;i++)
3093    if (!strcmp(object_names[i],"START"))
3094      start=i;
3095
3096  view *f=player_list;
3097  for (;f;f=f->next)
3098  {
3099    game_object *st=find_type(start,f->player_number);   
3100    if (st)
3101    {
3102      f->focus->x=st->x;
3103      f->focus->y=st->y;
3104    }
3105    add_object_after(f->focus,st);
3106  }
3107
3108}
3109
3110
3111void level::add_attacker(game_object *who)
3112{
3113  if (attack_total>=attack_list_size)  // see if we need to grow the list size..
3114  {
3115    attack_list_size++;
3116    attack_list=(game_object **)realloc(attack_list,sizeof(game_object *)*attack_list_size);
3117  }
3118  attack_list[attack_total]=who;
3119  attack_total++;
3120}
3121
3122
3123
3124void level::add_target(game_object *who)
3125{
3126  if (target_total>=target_list_size)  // see if we need to grow the list size..
3127  {
3128    target_list_size++;
3129    target_list=(game_object **)realloc(target_list,sizeof(game_object *)*target_list_size);
3130  }
3131  target_list[target_total]=who;
3132  target_total++;
3133}
3134
3135
3136
3137void level::add_block(game_object *who)
3138{
3139  if (block_total>=block_list_size)  // see if we need to grow the list size..
3140  {
3141    block_list_size++;
3142    block_list=(game_object **)realloc(block_list,sizeof(game_object *)*block_list_size);
3143  }
3144  block_list[block_total]=who;
3145  block_total++;
3146}
3147
3148
3149void level::add_all_block(game_object *who)
3150{
3151  if (all_block_total>=all_block_list_size)  // see if we need to grow the list size..
3152  {
3153    all_block_list_size++;
3154    all_block_list=(game_object **)realloc(all_block_list,sizeof(game_object *)*all_block_list_size);
3155  }
3156  all_block_list[all_block_total]=who;
3157  all_block_total++;
3158}
3159
3160
3161game_object *level::find_object_in_area(int32_t x, int32_t y, int32_t x1, int32_t y1, int32_t x2, int32_t y2,
3162                     Cell *list, game_object *exclude)
3163{
3164  game_object *closest=NULL;
3165  int32_t closest_distance=0xfffffff,distance,xo,yo;
3166  game_object *o=first_active;
3167  for (;o;o=o->next_active)
3168  {
3169    int32_t xp1,yp1,xp2,yp2;
3170    o->picture_space(xp1,yp1,xp2,yp2);
3171
3172
3173    if (!(xp1>x2 || xp2<x1 || yp1>y2 || yp2<y1) && o!=exclude)
3174    {
3175      // check to see if the type is in the list
3176      Cell *v=list;
3177      for (;!NILP(v) && lnumber_value(CAR(v))!=o->otype;v=CDR(v));
3178      if (!NILP(v))
3179      {
3180    xo=abs(o->x-x);
3181    yo=abs(o->y-y);
3182    distance=xo*xo+yo*yo;
3183    if (distance<closest_distance)
3184    {
3185      closest_distance=distance;
3186      closest=o;
3187    }
3188      }
3189    }
3190  }
3191  return closest;
3192}
3193
3194
3195
3196
3197game_object *level::find_object_in_angle(int32_t x, int32_t y, int32_t start_angle, int32_t end_angle,
3198                    void *list, game_object *exclude)
3199{
3200  game_object *closest=NULL;
3201  int32_t closest_distance=0xfffffff,distance,xo,yo;
3202  game_object *o=first_active;
3203  for (;o;o=o->next_active)
3204  {
3205    int32_t angle=lisp_atan2(o->y-y,o->x-x);
3206    if (((start_angle<=end_angle && (angle>=start_angle && angle<=end_angle))
3207    || (start_angle>end_angle && (angle>=start_angle || angle<=end_angle)))
3208    && o!=exclude)
3209    {
3210      // check to see if the type is in the list
3211      Cell *v=(Cell *)list;
3212      for (;!NILP(v) && lnumber_value(CAR(v))!=o->otype;v=CDR(v));
3213      if (!NILP(v))
3214      {
3215    xo=abs(o->x-x);
3216    yo=abs(o->y-y);
3217    distance=xo*xo+yo*yo;
3218    if (distance<closest_distance)
3219    {
3220      closest_distance=distance;
3221      closest=o;
3222    }
3223      }
3224    }
3225  }
3226  return closest;
3227}
3228
3229
3230object_node *level::make_not_list(object_node *list)
3231{
3232  object_node *f=NULL,*l=NULL;
3233  game_object *o=first;
3234  for (;o;o=o->next)
3235  {
3236    if (!object_to_number_in_list(o,list))
3237    {
3238      object_node *q=new object_node(o,NULL);
3239      if (f)
3240        l->next=q;
3241      else f=q;
3242      l=q;
3243    }
3244  }
3245  return f;
3246}
3247
3248void level::write_object_info(char *filename)
3249{
3250  FILE *fp=open_FILE(filename,"wb");
3251  if (fp)
3252  {
3253    int i=0;
3254    game_object *o=first;
3255    for (;o;o=o->next)
3256    {
3257      fprintf(fp,"%3d %s %4ld %4ld %4ld %4ld %04d\n",i++,object_names[o->otype],(long)o->x,(long)o->y,
3258          (long)o->xvel(),(long)o->yvel(),o->current_frame);
3259    }
3260    fclose(fp);
3261  }
3262}
3263
3264
3265area_controller::area_controller(int32_t X, int32_t Y, int32_t W, int32_t H, area_controller *Next)
3266{
3267  x=X; y=Y; w=W; h=H;
3268  next=Next; active=0;
3269
3270  ambient=-1;
3271  view_xoff=-1;
3272  view_yoff=-1;
3273  ambient_speed=2;
3274  view_xoff_speed=4;
3275  view_yoff_speed=4;
3276}
Note: See TracBrowser for help on using the repository browser.