source: abuse/trunk/src/loader2.cpp

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

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

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