source: abuse/trunk/src/loader2.cpp @ 481

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

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

File size: 11.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <math.h>
13
14#include "timing.h"
15#include "loader2.h"
16#include "chars.h"
17#include "specs.h"
18#include "lisp.h"
19#include "jrand.h"
20#include "menu.h"
21#include "dev.h"
22#include "director.h"
23
24#include "dev.h"
25#include "light.h"
26#include "dprint.h"
27#include "particle.h"
28#include "clisp.h"
29#include "compiled.h"
30#include "sbar.h"
31#include "help.h"
32#include "loadgame.h"
33#include "nfserver.h"
34#include "specache.h"
35
36extern int past_startup;
37
38property_manager *prop;
39int *backtiles;
40int *foretiles;
41JCFont *big_font,*console_font;
42int nforetiles,nbacktiles,f_wid,f_hi,b_wid,b_hi,total_songs=0,sfx_volume,music_volume,sound_avail=0;
43song *current_song=NULL;
44
45uint16_t current_start_type,start_position_type,last_start_number;
46int light_buttons[13];
47int joy_picts[2*9];
48palette *pal;
49
50int big_font_pict=-1,small_font_pict=-1,console_font_pict=-1,cdc_logo;
51
52int title_screen;
53
54color_filter *color_table;
55
56
57int border_tile,window_texture,
58    raise_volume,lower_volume,record_button,play_button,music_button,sfx_button,
59    window_colors,pause_image,damage_pict,block_pict,vmm_image,earth,earth_mask,clouds,
60    numbers[10],ok_button,cancel_button;
61
62int start_running=0;
63
64int c_mouse1,c_mouse2,c_normal,c_target;
65
66long bg_xmul,bg_xdiv,bg_ymul,bg_ydiv;    // brackground scroll rates
67char mouse_scrolling=0,palettes_locked=1,view_shift_disabled=0;
68
69int light_connection_color;
70
71
72image *load_image(spec_entry *e, bFILE *fp)
73{
74  image *im=new image(e,fp);
75  if (scale_mult!=1 || scale_div!=1)
76    im->resize(im->width()*scale_mult/scale_div,im->height()*scale_mult/scale_div);
77  return im;
78}
79
80image *load_image(bFILE *fp)
81{
82  image *im=new image(fp);
83  if (scale_mult!=1 || scale_div!=1)
84    im->resize(im->width()*scale_mult/scale_div,im->height()*scale_mult/scale_div);
85
86  return im;
87}
88
89void use_file(char *filename, bFILE *&fp, spec_directory *&sd)
90{
91  char fn[100];
92  fp=open_file(filename,"rb");
93  if (fp->open_failure())
94  {
95    delete fp;
96    sprintf(fn,"art/%s",filename);
97    fp=open_file(fn,"rb");
98    if (fp->open_failure())
99    {
100      printf("Unable to open file %s\n",filename);
101      delete fp;
102      exit(1);
103    }
104  }
105  sd=new spec_directory(fp);
106}
107
108void done_file(bFILE *&fp, spec_directory *&sd)
109{
110  delete fp;
111  delete sd;
112}
113
114void insert_tiles(char *filename)
115{
116  bFILE *fp=open_file(filename,"rb");
117  if (!fp->open_failure())
118  {
119    int ft=0,bt=0;
120    spec_directory sd(fp);
121    delete fp;
122    int i=0;
123    for (;i<sd.total;i++)
124    {
125      spec_entry *se=sd.entries[i];
126      if (se->type==SPEC_FORETILE)
127        ft++;
128      else if (se->type==SPEC_BACKTILE)
129        bt++;
130    }
131    if (bt)
132      printf("%s : adding %d background tiles (range %d-%d)\n",
133         filename,bt,nbacktiles,nbacktiles+bt-1);
134    if (ft)
135      printf("%s : adding %d foreground tiles (range %d-%d)\n",
136         filename,ft,nforetiles,nforetiles+bt-1);
137    if (!ft && !bt)
138      printf("Warning : file %s has no background or foreground tiles\n",filename);
139    else
140    {
141      int fon=nforetiles,bon=nbacktiles;
142      if (ft)
143        foretiles=(int *)realloc(foretiles,sizeof(int)*(nforetiles+ft));
144      if (bt)
145        backtiles=(int *)realloc(backtiles,sizeof(int)*(nbacktiles+bt));
146
147      for (i=0;i<sd.total;i++)
148      {
149    if (sd.entries[i]->type==SPEC_FORETILE)
150    {
151      foretiles[fon]=cache.reg(filename,sd.entries[i]->name);
152      fon++;
153      nforetiles++;
154    }
155    if (sd.entries[i]->type==SPEC_BACKTILE)
156    {
157      backtiles[bon]=cache.reg(filename,sd.entries[i]->name);
158      bon++;
159      nbacktiles++;
160    }
161      }
162    }
163  } else
164    printf("Warning : insert_tiles -> file %s could not be read from\n",filename);
165}
166
167void load_tiles(Cell *file_list)
168{
169  bFILE *fp;
170  spec_directory *sd;
171  spec_entry *spe;
172
173
174  int num;
175
176
177
178  Cell *fl;
179  int old_fsize=nforetiles,
180      old_bsize=nbacktiles;
181
182  for (fl=file_list;!NILP(fl);fl=lcdr(fl))
183  {
184    fp=open_file(lstring_value(lcar(fl)),"rb");
185    if (fp->open_failure())
186    {
187      printf("Warning : open %s for reading\n",lstring_value(lcar(fl)));
188      delete fp;
189    }
190    else
191    {
192      sd=new spec_directory(fp);
193      delete fp;
194      int i;
195      for (i=0;i<sd->total;i++)
196      {
197    spe=sd->entries[i];
198        switch (spe->type)
199        {
200          case SPEC_BACKTILE :
201            if (!sscanf(spe->name,"%d",&num))
202              printf("Warning : background tile %s has no number\n",spe->name);
203            else if (nbacktiles<=num) nbacktiles=num+1;
204          break;
205
206          case SPEC_FORETILE :
207            if (!sscanf(spe->name,"%d",&num))
208              printf("Warning : foreground tile %s has no number\n",spe->name);
209            else if (nforetiles<=num) nforetiles=num+1;
210          break;
211        }
212      }
213      delete sd;
214    }
215  }
216
217  // enlarge the arrays if needed.
218  if (nbacktiles>old_bsize)
219  {
220    backtiles=(int *)realloc(backtiles,sizeof(int)*nbacktiles);
221    memset(backtiles+old_bsize,-1,(nbacktiles-old_bsize)*sizeof(int));
222  }
223
224  if (nforetiles>old_fsize)
225  {
226    foretiles=(int *)realloc(foretiles,sizeof(int)*nforetiles);
227    memset(foretiles+old_fsize,-1,(nforetiles-old_fsize)*sizeof(int));
228  }
229
230
231// now load them up
232  for (fl=file_list;!NILP(fl);fl=lcdr(fl))
233  {
234    char const *fn=lstring_value(lcar(fl));
235    fp=open_file(fn,"rb");
236    if (!fp->open_failure())
237    {
238      sd=new spec_directory(fp);
239      delete fp;
240
241      int i;
242      for (i=0;i<sd->total;i++)
243      {
244    spe=sd->entries[i];
245        switch (spe->type)
246        {
247          case SPEC_BACKTILE :
248   
249            if (sscanf(spe->name,"%d",&num))
250        {
251          if (backtiles[num]>=0)
252          {
253        dprintf("Warning : background tile %d redefined\n",num);
254        cache.unreg(backtiles[num]);
255          }
256          backtiles[num]=cache.reg(fn,spe->name,SPEC_BACKTILE);   
257        }
258            break;
259          case SPEC_FORETILE :
260            if (sscanf(spe->name,"%d",&num))
261        {
262          if (foretiles[num]>=0)
263          {
264        dprintf("Warning : foreground tile %d redefined\n",num);
265        cache.unreg(foretiles[num]);
266          }
267          foretiles[num]=cache.reg(fn,spe->name,SPEC_FORETILE);
268        }
269            break;
270        }
271      }
272      delete sd;
273    } else delete fp;
274  }
275
276}
277
278
279extern unsigned char fnt6x13[192*104];
280char lsf[256]="abuse.lsp";
281
282void load_data(int argc, char **argv)
283{
284    total_objects=0;
285    total_weapons=0;
286    weapon_types=NULL;
287    figures=NULL;
288    nforetiles=nbacktiles=0;
289    foretiles=NULL;
290    backtiles=NULL;
291    pal=NULL;
292    color_table=NULL;
293
294# if 0
295    int should_save_sd_cache = 0;
296
297    char *cachepath;
298    cachepath = (char *)malloc( strlen( get_save_filename_prefix() ) + 12 + 1 );
299    sprintf( cachepath, "%ssd_cache.tmp", get_save_filename_prefix() );
300
301    bFILE *load = open_file( cachepath, "rb" );
302    if( !load->open_failure() )
303    {
304        sd_cache.load( load );
305    }
306    else
307    {
308        should_save_sd_cache = 1;
309    }
310    delete load;
311#endif
312
313  if (!net_start())              // don't let them specify a startup file we are connect elsewhere
314  {
315    for (int i=1;i<argc;i++)
316    {
317      if (!strcmp(argv[i],"-lsf"))
318      {
319    i++;
320    strcpy(lsf,argv[i]);
321      }
322      if (!strcmp(argv[i],"-a"))
323      {
324    i++;
325    sprintf(lsf,"addon/%s/%s.lsp",argv[i],argv[i]);
326      }
327    }
328  } else if (!get_remote_lsf(net_server,lsf))
329  {
330    dprintf("Unable to get remote lsf from %s\n",net_server);
331    exit(0);
332  }
333  char prog[100];
334  char const *cs;
335
336  c_mouse1=cache.reg("art/dev.spe","c_mouse1",SPEC_IMAGE,0);
337  c_mouse2=cache.reg("art/dev.spe","c_mouse2",SPEC_IMAGE,0);
338  c_normal=cache.reg("art/dev.spe","c_normal",SPEC_IMAGE,0);
339  c_target=cache.reg("art/dev.spe","c_target",SPEC_IMAGE,0);
340
341
342  sprintf(prog,"(load \"%s\")\n",lsf);
343
344  cs=prog;
345  if (!eval(compile(cs)))
346  {
347    printf("unable to open file '%s'\n",lsf);
348    exit(0);
349  }
350  compiled_init();
351  clear_tmp();
352  resize_tmp(0x4000);
353
354  dprintf("Engine : Registering base graphics\n");
355  for (int z=0;z<=11;z++)
356  {
357    char nm[10];
358    sprintf(nm,"l%d",z);
359    light_buttons[z]=cache.reg("art/dev.spe",nm,SPEC_IMAGE,0);
360  }
361
362
363  image *tmp_image=new image(192,104,fnt6x13);
364  big_font=new JCFont(tmp_image);
365  delete tmp_image;
366
367
368  char const *ff;
369  if (DEFINEDP(symbol_value(make_find_symbol("frame_file"))))
370    ff=lstring_value(symbol_value(make_find_symbol("frame_file")));
371  else
372    ff="art/frame.spe";
373
374  ok_button   =      cache.reg(ff,"dev_ok",SPEC_IMAGE);
375  cancel_button  =   cache.reg(ff,"cancel",SPEC_IMAGE);
376
377//  clouds      =      cache.reg(ff,"clouds",SPEC_IMAGE);
378
379  lower_volume=      cache.reg(ff,"lower_volume",SPEC_IMAGE);
380  raise_volume=      cache.reg(ff,"raise_volume",SPEC_IMAGE);
381  music_button=      cache.reg(ff,"music",SPEC_IMAGE);
382  sfx_button=        cache.reg(ff,"sound_fx",SPEC_IMAGE);
383  record_button=     cache.reg(ff,"record",SPEC_IMAGE);
384  play_button=       cache.reg(ff,"play",SPEC_IMAGE);
385  window_colors=     cache.reg(ff,"window_colors",SPEC_IMAGE);
386  pause_image=       cache.reg(ff,"pause_image",SPEC_IMAGE);
387  vmm_image=         cache.reg(ff,"vmm",SPEC_IMAGE);
388  border_tile=       cache.reg(ff,"border_tile",SPEC_IMAGE);
389  window_texture=    cache.reg(ff,"window_texture",SPEC_IMAGE);
390
391
392  help_screens=NULL;
393  total_help_screens=0;
394
395  if (DEFINEDP(symbol_value(l_help_screens)))
396  {
397    void *v=symbol_value(l_help_screens);
398    char *ff=lstring_value(CAR(v));  v=CDR(v);
399    total_help_screens=0;
400    while (v) { total_help_screens++; v=CDR(v); }
401    if (total_help_screens)
402    {
403      help_screens=(int *)malloc(sizeof(int)*total_help_screens);
404      v=CDR(symbol_value(l_help_screens));
405      int i=0;
406      for (;v;v=CDR(v),i++)
407        help_screens[i]=cache.reg(ff,lstring_value(CAR(v)),SPEC_IMAGE);
408    }
409    else
410      dprintf("Warning no help images following filename\n");
411  }
412
413  int i;
414  for (i=1;i<argc;i++)
415  {
416    if (!strcmp(argv[i],"-ec"))
417      set_symbol_value(l_empty_cache,true_symbol);
418    if (!strcmp(argv[i],"-t"))
419    {
420      i++;
421      insert_tiles(argv[i]);
422    }
423  }
424
425  if (DEFINEDP(symbol_value(l_title_screen)))
426    title_screen=cache.reg_object(NULL,symbol_value(l_title_screen),SPEC_IMAGE,1);
427  else title_screen=-1;
428
429  if (DEFINEDP(symbol_value(l_cdc_logo)))
430    cdc_logo=cache.reg_object(NULL,symbol_value(l_cdc_logo),SPEC_IMAGE,1);
431  else cdc_logo=-1;
432
433  start_position_type=0xffff;
434  for(i=0;i<total_objects;i++)
435    if (!strcmp(object_names[i],"START"))
436      start_position_type=i;
437  if (start_position_type==0xffff)
438  {
439    printf("No object named START, cannot start game.\n");
440    exit(0);
441  }
442
443  sbar.load();
444
445  load_number_icons();
446
447
448  ERROR(nbacktiles,"No background tiles defined!");
449  ERROR(nforetiles,"No foreground tiles defined!");
450  ERROR(foretiles[0]>=0,"No black (0) foreground tile defined!");
451  ERROR(backtiles[0]>=0,"No black (0) background tile defined!");
452  ERROR(big_font_pict!=-1 || small_font_pict!=-1,
453    "No font loaded (use load_big_font or load_small_font)!");
454  f_wid=cache.foret(foretiles[0])->im->width();
455  f_hi=cache.foret(foretiles[0])->im->height();
456  b_wid=cache.backt(backtiles[0])->im->width();
457  b_hi=cache.backt(backtiles[0])->im->height();
458
459#if 0
460    if( should_save_sd_cache )
461    {
462        bFILE *save = open_file( cachepath, "wb" );
463        if( !save->open_failure() )
464        {
465            sd_cache.save( save );
466        }
467        delete save;
468    }
469#endif
470
471    sd_cache.clear();
472    past_startup = 1;
473#if 0
474    free( cachepath );
475#endif
476}
477
478
479
480
481
482char *load_script(char *name)
483{
484  char fn[100];
485  char *s;
486
487  sprintf(fn,"%s",name);
488  bFILE *fp=open_file(fn,"rb");
489  if (fp->open_failure())
490  {
491    delete fp;
492    return NULL;
493  }
494
495  long l=fp->file_size();
496  s=(char *)malloc(l+1);
497  ERROR(s,"Malloc error in load_script");
498
499  fp->read(s,l);
500  s[l]=0;
501  delete fp;
502  return s;
503}
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
Note: See TracBrowser for help on using the repository browser.