Source

doom / src / t_array.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id: t_array.c 805 2011-03-09 00:36:20Z wesleyjohnson $
//
// Copyright(C) 2000 James Haley
// Copyright (C) 2001-2011 by DooM Legacy Team.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//
// $Log: t_array.c,v $
// Revision 1.1  2004/07/27 08:22:01  exl
// Add fs arrys files
//
//
//
//--------------------------------------------------------------------------
//
// DESCRIPTION:
//
//   Array support for FraggleScript
//
//   By James Haley, with special thanks to SoM
//
//--------------------------------------------------------------------------

#include "z_zone.h"
#include "t_array.h"
#include "t_vari.h"
//#include "p_enemy.h"

// Save list for arrays -- put ALL arrays spawned into this list,
// and they'll live until the end of the level. This is very lazy
// garbage collection, but its the only real solution given the
// state of the FS source (the word "haphazard" comes to mind...)
#ifdef FS_ARRAYLIST_STRUCTHEAD
array_t fs_arraylist =
{
   NULL,
   0,
   0,
   NULL,
};
#else
fs_array_t * fs_arraylist = NULL;
#endif

// add an array into the array save list
void T_Add_FSArray(fs_array_t *array)
{
   fs_array_t *temp;

   // always insert at head of list
#ifdef FS_ARRAYLIST_STRUCTHEAD
   temp = fs_arraylist.next;
   fs_arraylist.next = array;
#else
   temp = fs_arraylist;
   fs_arraylist = array;
#endif
   array->next = temp;
}

static void * initsave_levelclear = NULL;  // indicates when PU_LEVEL cleared
  
// call from P_SetupLevel and P_SaveGame
// Clears added array values but not base of fs_arraylist
void T_Init_FSArrayList(void)
{
   // Z_Malloc of arrays is PU_LEVEL, but does not pass a user ptr, so level
   // clear will have released all this memory without informing the owners.
   if( initsave_levelclear )	// level not cleared, as in load saved game
   {
       fs_array_t * sfap, * sfap_nxt;
       // enable to test if this is happening
//       fprintf(stderr, "T_Init_FSArrayList: clearing array list\n" );
#ifdef FS_ARRAYLIST_STRUCTHEAD
       sfap = fs_arraylist.next;
#else      
       sfap = fs_arraylist;
#endif
       for( ; sfap; sfap=sfap_nxt )
       {
	  if( sfap->values )   Z_Free( sfap->values );
	  sfap_nxt = sfap->next;  // get next before deallocate
	  Z_Free( sfap );
       }
   }
   else
   {
       // will trip when level is cleared
       Z_Malloc( 1, PU_LEVEL, &initsave_levelclear );
   }
#ifdef FS_ARRAYLIST_STRUCTHEAD
   fs_arraylist.next = NULL;
#else      
   fs_arraylist = NULL;
#endif
}

#if 0
// Clears all array values including base of fs_arraylist
void T_Init_FSArrays(void)
{
   // Z_Malloc of arrays does not pass a user ptr, so level clear
   // will not have destroyed these arrays.
   T_Init_FSArrayList();	// clear fs_arraylist.next
   if( fs_arraylist.values )  Z_Free( fs_arraylist.values );
   fs_arraylist.values = NULL;
   fs_arraylist.saveindex = 0;
   fs_arraylist.length = 0;
}
#endif

// SF Handler functions for calling from scripts

//
// SF_NewArray
// 
//  Create a new fs_array_t and initialize it with values
//
//  Implements: array newArray(...)
//
// array functions in t_array.c
void SF_NewArray(void)
{
   int i;
   fs_array_t *newArray;

   if(!t_argc) // empty, do nothing
      return;

   // allocate a fs_array_t
   newArray = Z_Malloc(sizeof(fs_array_t), PU_LEVEL, NULL);

   // init all fields to zero
   memset(newArray, 0, sizeof(fs_array_t));
   
   // allocate t_argc number of values, set length
   newArray->values = Z_Malloc(t_argc*sizeof(fs_value_t), PU_LEVEL, NULL);
   memset(newArray->values, 0, t_argc*sizeof(fs_value_t));
   
   newArray->length = t_argc;

   for(i=0; i<t_argc; i++)
   {
      // strings, arrays are ignored
      if(t_argv[i].type == FSVT_string || t_argv[i].type == FSVT_array)
	 continue;

      // copy all the argument values into the local array
      memcpy(&(newArray->values[i]), &t_argv[i], sizeof(fs_value_t));
   }

   T_Add_FSArray(newArray); // add the new array to the save list
   
   t_return.type = FSVT_array;
   // t_return is an internal value which may not be captured in
   // an svariable_t, so we don't count it as a reference --
   // in the cases of immediate value usage, the garbage collector
   // won't have a chance to free it until it has been used
   t_return.value.a = newArray;
}



//
// SF_NewEmptyArray
// 
//  Create a new fs_array_t and initialize it with a standard value
//
void SF_NewEmptyArray(void)
{
   int i;
   fs_array_t *newArray;
   fs_value_t	newval;

   if(t_argc < 2) // empty, do nothing
      return;

   // bad types
   if(t_argv[0].type != FSVT_int || t_argv[1].type != FSVT_int)
   {
	   script_error("newemptyarray: expected integer\n");
	   return;
   }

   // Type out of bounds
   if(t_argv[1].value.i < 0 || t_argv[1].value.i > 2)
   {
	   script_error("newemptyarray: invalid type\n");
	   return;
   }



   // allocate a fs_array_t
   newArray = Z_Malloc(sizeof(fs_array_t), PU_LEVEL, NULL);

   // init all fields to zero
   memset(newArray, 0, sizeof(fs_array_t));
   
   // allocate t_argc number of values, set length
   newArray->values = Z_Malloc(t_argv[0].value.i*sizeof(fs_value_t), PU_LEVEL, NULL);
   memset(newArray->values, 0, t_argv[0].value.i*sizeof(fs_value_t));
   
   
   newArray->length = t_argv[0].value.i;

   // initialize each value
   switch(t_argv[1].value.i)
   {
		case 0:
			newval.type = FSVT_int;
			newval.value.i = 0;
			break;
		case 1:
			newval.type = FSVT_fixed;
			newval.value.f = 0;
			break;
		case 2:
			newval.type = FSVT_mobj;
			newval.value.mobj = NULL;
			break;
	}


   for(i=0; i<t_argv[0].value.i; i++)
   {
      
      // Copy the new element into the array
	  memcpy(&(newArray->values[i]), &newval, sizeof(fs_value_t));

   }

   T_Add_FSArray(newArray); // add the new array to the save list
   
   t_return.type = FSVT_array;
   // t_return is an internal value which may not be captured in
   // an svariable_t, so we don't count it as a reference --
   // in the cases of immediate value usage, the garbage collector
   // won't have a chance to free it until it has been used
   t_return.value.a = newArray;
}


//
// SF_ArrayCopyInto
//
// Copies the values from one array into the values of another.
// Arrays must be non-empty and must be of equal length.
//
// Implements: void copyInto(array source, array target)
//
void SF_ArrayCopyInto(void)
{
   unsigned int i;
   fs_array_t *source, *target;
   
   if(t_argc != 2)
   {
      script_error("insufficient arguments to function\n");
      return;
   }

   if(t_argv[0].type != FSVT_array || t_argv[1].type != FSVT_array)
   {
      script_error("copyinto must be called on arrays\n");
      return;
   }

   source = t_argv[0].value.a;
   target = t_argv[1].value.a;

   if(!source || !target)
   {
      script_error("copyinto cannot function on empty arrays\n");
      return;
   }

   if(source->length != target->length)
   {
      script_error("copyinto must be passed arrays of equal length\n");
      return;
   }

   for(i=0; i<source->length; i++)
   {
      memcpy(&(target->values[i]), &(source->values[i]), 
	     sizeof(fs_value_t));
   }
}

//
// SF_ArrayElementAt
//
// Retrieves a value at a specific index
//
// Implements: 'a elementAt(array x, int i)
//
// This function is somewhat unique at it has a polymorphic
// return type :)
//
void SF_ArrayElementAt(void)
{
   unsigned int index;
   
   if(t_argc != 2)
   {
      script_error("incorrect arguments to function");
      return;
   }

   if(t_argv[0].type != FSVT_array || !t_argv[0].value.a)
   {
      script_error("elementat must be called on a non-empty array\n");
      return;
   }

   // get index from second arg
   index = intvalue(t_argv[1]);

   if(index < 0 || index >= t_argv[0].value.a->length)
   {
      script_error("array index out of bounds\n");
      return;
   }

   // copy full fs_value_t to t_return
   memcpy(&t_return, &(t_argv[0].value.a->values[index]), 
          sizeof(fs_value_t));
}

//
// SF_ArraySetElementAt
//
// Sets a specific value in an array
//
// Implements: void setElementAt(array x, 'a val, int i)
//
void SF_ArraySetElementAt(void)
{
   unsigned int index;
   
   if(t_argc != 3)
   {
      script_error("incorrect arguments to function");
      return;
   }

   if(t_argv[0].type != FSVT_array || !t_argv[0].value.a)
   {
      script_error("setelementat must be called on a non-empty array\n");
      return;
   }

   // get index from third arg this time...
   index = intvalue(t_argv[2]);

   if(index < 0 || index >= t_argv[0].value.a->length)
   {
      script_error("array index out of bounds\n");
      return;
   }

   // type checking on second arg: restricted types
   if(t_argv[1].type == FSVT_array || t_argv[1].type == FSVT_string)
   {
      script_error("%s cannot be an array element\n",
	 t_argv[1].type == FSVT_array ? "an array" : "a string");
      return;
   }

   // copy full fs_value_t into array at given index
   memcpy(&(t_argv[0].value.a->values[index]), &t_argv[1],
          sizeof(fs_value_t));
}

//
// SF_ArrayLength
//
// Retrieves the length of an array
//
// Implements: int length(array x)
//
void SF_ArrayLength(void)
{
   if(!t_argc)
   {
      script_error("insufficient arguments to function\n");
      return;
   }

   if(t_argv[0].type != FSVT_array)
   {
      script_error("length must be called on an array\n");
      return;
   }

   t_return.type = FSVT_int;

   if(!t_argv[0].value.a)
      t_return.value.i = 0;
   else
      t_return.value.i = t_argv[0].value.a->length;
}