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

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