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

Last change on this file since 2 was 2, checked in by Sam Hocevar, 14 years ago
  • imported original 0.7.0 tarball
File size: 11.4 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",filename);
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;
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        total_objects=0;
275        total_weapons=0;
276        weapon_types=NULL;
277        figures=NULL;
278        nforetiles=nbacktiles=0;
279        foretiles=NULL;
280        backtiles=NULL;
281        pal=NULL;
282        color_table=NULL;
283
284        int should_save_sd_cache = 0;
285
286        char *cachepath;
287        cachepath = (char *)jmalloc( strlen( get_save_filename_prefix() ) + 12, "cachepath" );
288        sprintf( cachepath, "%ssd_cache.tmp\0", get_save_filename_prefix() );
289
290        bFILE *load = open_file( cachepath, "rb" );
291        if( !load->open_failure() )
292        {
293                sd_cache.load( load );
294        }
295        else
296        {
297                should_save_sd_cache = 1;
298        }
299        delete load;
300
301  if (!net_start())              // don't let them specify a startup file we are connect elsewhere
302  {
303    for (int i=1;i<argc;i++)
304    {
305      if (!strcmp(argv[i],"-lsf"))
306      {
307        i++;
308        strcpy(lsf,argv[i]);
309      }
310      if (!strcmp(argv[i],"-a"))
311      {
312        i++;
313        sprintf(lsf,"addon/%s/%s.lsp",argv[i],argv[i]);     
314      }
315    }
316  } else if (!get_remote_lsf(net_server,lsf))
317  {
318    dprintf("Unable to get remote lsf from %s\n",net_server);
319    exit(0);
320  }
321  char prog[100],*cs;
322
323  c_mouse1=cash.reg("art/dev.spe","c_mouse1",SPEC_IMAGE,0);
324  c_mouse2=cash.reg("art/dev.spe","c_mouse2",SPEC_IMAGE,0);
325  c_normal=cash.reg("art/dev.spe","c_normal",SPEC_IMAGE,0);
326  c_target=cash.reg("art/dev.spe","c_target",SPEC_IMAGE,0);
327
328
329  sprintf(prog,"(load \"%s\")\n",lsf);
330
331  cs=prog;
332  if (!eval(compile(cs)))
333  {
334    printf("unable to open file '%s'\n",lsf);
335    exit(0);
336  }
337  compiled_init();
338  clear_tmp();
339  resize_tmp(0x4000);
340
341  dprintf("Engine : Registering base graphics\n");
342  for (int z=0;z<=11;z++)
343  {
344    char nm[10];
345    sprintf(nm,"l%d",z);
346    light_buttons[z]=cash.reg("art/dev.spe",nm,SPEC_IMAGE,0);
347  }
348
349
350  image *tmp_image=new image(192,104,fnt6x13);
351  big_font=new JCFont(tmp_image);
352  delete tmp_image;
353
354
355  char *ff;
356  if (DEFINEDP(symbol_value(make_find_symbol("frame_file"))))
357    ff=lstring_value(symbol_value(make_find_symbol("frame_file")));
358  else
359    ff="art/frame.spe";
360
361  ok_button   =      cash.reg(ff,"dev_ok",SPEC_IMAGE);
362  cancel_button  =   cash.reg(ff,"cancel",SPEC_IMAGE);
363
364//  clouds      =      cash.reg(ff,"clouds",SPEC_IMAGE);
365
366  lower_volume=      cash.reg(ff,"lower_volume",SPEC_IMAGE);
367  raise_volume=      cash.reg(ff,"raise_volume",SPEC_IMAGE);
368  music_button=      cash.reg(ff,"music",SPEC_IMAGE);
369  sfx_button=        cash.reg(ff,"sound_fx",SPEC_IMAGE);
370  record_button=     cash.reg(ff,"record",SPEC_IMAGE); 
371  play_button=       cash.reg(ff,"play",SPEC_IMAGE);
372  window_colors=     cash.reg(ff,"window_colors",SPEC_IMAGE);
373  pause_image=       cash.reg(ff,"pause_image",SPEC_IMAGE);
374  vmm_image=         cash.reg(ff,"vmm",SPEC_IMAGE);
375  border_tile=       cash.reg(ff,"border_tile",SPEC_IMAGE);
376  window_texture=    cash.reg(ff,"window_texture",SPEC_IMAGE);
377 
378
379  help_screens=NULL;
380  total_help_screens=0; 
381
382  if (DEFINEDP(symbol_value(l_help_screens)))
383  {
384    void *v=symbol_value(l_help_screens);
385    char *ff=lstring_value(CAR(v));  v=CDR(v);
386    total_help_screens=0;
387    while (v) { total_help_screens++; v=CDR(v); }
388    if (total_help_screens)
389    {
390      help_screens=(int *)jmalloc(sizeof(int)*total_help_screens,"help image ids");     
391      v=CDR(symbol_value(l_help_screens));
392      int i=0;
393      for (;v;v=CDR(v),i++)
394        help_screens[i]=cash.reg(ff,lstring_value(CAR(v)),SPEC_IMAGE);     
395    }
396    else
397      dprintf("Warning no help images following filename\n");
398  } 
399     
400  int i;
401  for (i=1;i<argc;i++)
402  {
403    if (!strcmp(argv[i],"-ec"))
404      set_symbol_value(l_empty_cache,true_symbol);
405    if (!strcmp(argv[i],"-t"))
406    {
407      i++;
408      insert_tiles(argv[i]);
409    }
410  }
411
412  if (DEFINEDP(symbol_value(l_title_screen)))
413    title_screen=cash.reg_object(NULL,symbol_value(l_title_screen),SPEC_IMAGE,1);
414  else title_screen=-1;
415
416  if (DEFINEDP(symbol_value(l_cdc_logo)))
417    cdc_logo=cash.reg_object(NULL,symbol_value(l_cdc_logo),SPEC_IMAGE,1);
418  else cdc_logo=-1;
419 
420  start_position_type=0xffff;
421  for(i=0;i<total_objects;i++)
422    if (!strcmp(object_names[i],"START"))
423      start_position_type=i;
424  if (start_position_type==0xffff)
425  {
426    printf("No object named START, cannot start game.\n");
427    exit(0);
428  }
429
430  sbar.load();
431 
432  load_number_icons();
433
434
435  ERROR(nbacktiles,"No background tiles defined!");
436  ERROR(nforetiles,"No foreground tiles defined!");
437  ERROR(foretiles[0]>=0,"No black (0) foreground tile defined!");
438  ERROR(backtiles[0]>=0,"No black (0) background tile defined!");
439  ERROR(big_font_pict!=-1 || small_font_pict!=-1,
440        "No font loaded (use load_big_font or load_small_font)!");
441  f_wid=cash.foret(foretiles[0])->im->width();
442  f_hi=cash.foret(foretiles[0])->im->height();
443  b_wid=cash.backt(backtiles[0])->im->width();
444  b_hi=cash.backt(backtiles[0])->im->height();
445
446        if( should_save_sd_cache )
447        {
448                bFILE *save = open_file( cachepath, "wb" );
449                if( !save->open_failure() )
450                {
451                        sd_cache.save( save );
452                }
453                delete save;
454        }
455
456        sd_cache.clear();
457        past_startup = 1;
458        jfree( cachepath );
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.