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

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