source: abuse/tags/pd/macabuse/src/loader2.c @ 49

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