source: golgotha/src/golg/make_tlist.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 6.7 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include <stdio.h>
10#include "saver_id.hh"
11#include "status/status.hh"
12
13#include "lisp/lisp.hh"
14#include "file/file.hh"
15#include "memory/array.hh"
16#include "loaders/dir_load.hh"
17#include "error/alert.hh"
18
19
20static void g1_add_to_list(i4_array<i4_str *> &t_arr, i4_str *tname)
21{
22  if (tname->null())
23    i4_error("added null name");
24     
25  int found=0;
26  for (int j=0; !found && j<t_arr.size(); j++)
27    if ( (*t_arr[j])==(*tname))
28      found=1;
29
30  if (!found)
31    t_arr.add(tname);
32  else
33    delete tname; 
34}
35
36static void g1_add_to_list(i4_array<i4_str *> &t_arr,
37                           li_object *fmt, li_object *name)
38{
39  char buf[200];
40  sprintf(buf, li_string::get(fmt,0)->value(), li_string::get(name,0)->value());
41
42  g1_add_to_list(t_arr, new i4_str(buf));
43}
44
45
46
47void g1_make_texture_list_for_model(const i4_const_str model_name,
48                                    i4_array<i4_str *> &t_arr)
49{
50  i4_file_class *fp=i4_open(model_name);
51
52  if (fp)
53  {
54    i4_loader_class *gfp=i4_open_save_file(fp);
55    if (gfp)
56    {
57      if (gfp->goto_section(G1_SECTION_MODEL_TEXTURE_NAMES))
58      {
59        w16 quads=gfp->read_16();
60        for (int i=0; i<quads; i++)
61        {
62          i4_str *tname=gfp->read_counted_str();
63          if (tname->null())
64          {
65            i4_alert("Model %S has a face with no name!", 100, &model_name);
66            delete tname;
67          }
68          else
69            g1_add_to_list(t_arr, tname);
70        }
71      }
72      delete gfp;
73    }
74  }
75}
76
77static i4_array<i4_str *> *g1_current_tnames=0;
78static i4_array<i4_str *> *g1_current_model_names=0;
79static int g1_current_t_tiles=0;
80
81
82li_object *g1_get_models_texture(li_object *o, li_environment *env)
83{
84  li_object *fmt=li_eval(li_car(o,env),env);
85
86  for (o=li_cdr(o,env); o; o=li_cdr(o,env))  // skip path at start
87    g1_add_to_list(*g1_current_model_names, fmt, li_car(o,env));
88
89  return 0;
90}
91
92
93li_object *g1_get_tile_textures(li_object *o, li_environment *env)
94{
95  li_object *fmt=li_eval(li_car(o,env),env);
96
97  char buf[200];
98
99  for (o=li_cdr(o,env); o; o=li_cdr(o,env))    // skip path at start
100  {
101    char *name;
102    if (li_car(o,env)->type()==LI_STRING)
103      name=li_string::get(li_eval(li_car(o,env),env),env)->value();
104    else
105      name=li_string::get(li_eval(li_car(li_car(o,env),env),env),env)->value();
106
107    sprintf(buf, li_string::get(fmt,env)->value(), name);
108
109    g1_current_tnames->add(new i4_str(buf));
110    g1_current_t_tiles++;
111  }
112
113
114  return 0;
115}
116
117
118li_object *g1_sky_textures(li_object *o, li_environment *env)
119{
120  li_object *fmt=li_eval(li_car(o,env),env);
121
122  char buf[200], name1[200], name2[200];
123
124  for (o=li_cdr(o,env); o; o=li_cdr(o,env))    // skip path at start
125  {
126    char *name;
127    if (li_car(o,env)->type()==LI_STRING)
128      name=li_string::get(li_eval(li_car(o,env),env),env)->value();
129    else
130      name=li_string::get(li_eval(li_car(li_car(o,env),env),env),env)->value();
131
132    sprintf(buf, li_string::get(fmt,env)->value(), name);
133    sprintf(name1, "%s1", buf);
134    sprintf(name2, "%s2", buf);
135
136    g1_current_tnames->add(new i4_str(buf));
137    g1_current_tnames->add(new i4_str(name1));
138    g1_current_tnames->add(new i4_str(name2));
139    g1_current_t_tiles++;
140  }
141
142
143  return 0;
144}
145
146
147static li_symbol *s_model_name=0, *s_mini_object=0;
148
149li_object *g1_get_building_textures(li_object *o, li_environment *env)
150{
151  li_object *fmt=li_get_value("building_format", env);
152
153  for (; o; o=li_cdr(o,env))
154    g1_add_to_list(*g1_current_model_names, fmt, li_car(o,env));
155
156  return 0;
157}
158
159
160li_object *g1_get_object_textures(li_object *o, li_environment *env)
161{
162  li_object *fmt=li_get_value("object_format", env);
163
164  for (li_object *p=li_cdr(o,env); p; p=li_cdr(p,env))
165  {
166    li_symbol *sym=li_symbol::get(li_car(li_car(p,env),env),env);
167
168    if (sym==li_get_symbol("model_name",s_model_name))
169      g1_add_to_list(*g1_current_model_names, fmt, li_car(li_cdr(li_car(p,env),env),env));     
170    else if (sym==li_get_symbol("mini_object",s_mini_object))
171    {
172      li_object *s=li_cdr(li_car(p,env),env);  // s = ("gunport_barrel")
173      for (; s; s=li_cdr(s,env))
174        if (li_symbol::get(li_car(li_car(s,env),env),env)==
175            li_get_symbol("model_name", s_model_name))
176          g1_add_to_list(*g1_current_model_names, fmt, li_car(li_cdr(li_car(s,env),env),env));
177    }
178  }
179
180  return 0;
181}
182
183
184static li_object *g1_ignore(li_object *o, li_environment *env) {  return 0; }
185
186
187static int defaults_set=0;
188
189
190
191void g1_get_load_info(i4_file_class **fp, int t_files,
192                      i4_array<i4_str *> &texture_name_array,
193                      i4_array<i4_str *> &model_name_array,
194                      int &total_tiles,
195                      int include_model_textures)
196{
197  li_environment  *env=new li_environment(0,i4_T);
198
199
200  li_set_value("texture_format", new li_string("x:/crack/golgotha/textures/%s.tga"));
201  li_set_value("building_format", new li_string("x:/crack/golgotha/objects/%s.gmod"));
202  li_set_value("object_format", new li_string("x:/crack/golgotha/objects/%s.gmod"));
203
204 
205         
206
207  li_add_function("def_class", g1_ignore, env);
208  li_add_function("models", g1_get_models_texture, env);
209  li_add_function("textures", g1_get_tile_textures, env);
210  li_add_function("def_object", g1_get_object_textures, env);
211  li_add_function("def_buildings", g1_get_building_textures, env);
212  li_add_function("def_skys", g1_sky_textures, env);
213  li_add_function("find_models", g1_ignore, env);
214
215  g1_current_tnames = &texture_name_array;
216  g1_current_model_names = &model_name_array;
217  g1_current_t_tiles=0;
218
219  li_load("scheme/models.scm", env);
220  for (int j=0; j<t_files; j++)
221    li_load(fp[j], env);
222
223
224           
225  g1_current_model_names=0;
226  g1_current_tnames=0;
227
228  if (include_model_textures)
229  {
230    i4_status_class *stat=i4_create_status(i4gets("scanning_models"));
231
232 
233    for (int i=0; i<model_name_array.size(); i++)
234    {
235      if (stat)
236        stat->update((i+1)/(float)model_name_array.size());
237      g1_make_texture_list_for_model(*model_name_array[i], texture_name_array);
238    }
239    if (stat) delete stat;
240  }
241
242
243  total_tiles=g1_current_t_tiles;
244}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Note: See TracBrowser for help on using the repository browser.