File: sarcamp.c

package info (click to toggle)
searchandrescue 1.5.0-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,768 kB
  • sloc: ansic: 89,609; cpp: 7,699; makefile: 194; sh: 123
file content (495 lines) | stat: -rw-r--r-- 13,272 bytes parent folder | download | duplicates (5)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
These functions create a campaign mission file.
See sarcamp.h for details on how the functions work.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sarcamp.h"




FILE *Create_Campaign_File()
{
    char *filename;
    FILE *camp_file;

   filename = Get_Campaign_Filename();
   if (! filename)
      return NULL;

   camp_file = fopen(filename, "w");
   if (! camp_file)
      printf("Unable to open file %s\n", filename);

   free(filename);
   return camp_file;
}



char *Create_Campaign_Weather()
{
   char *weather_string;
   int random_num;

   weather_string = (char *) calloc(64, sizeof(char));
   if (! weather_string)
      return NULL;

   random_num = rand() % 8;
   switch (random_num)
   {
      case 0: strcpy(weather_string, "weather Clear"); break;
      case 1: strcpy(weather_string, "weather Hazy"); break;
      case 2: strcpy(weather_string, "weather Scattered"); break;
      case 3: strcpy(weather_string, "weather Cloudy"); break;
      case 4: strcpy(weather_string, "weather Overcast"); break;
      case 5: strcpy(weather_string, "weather Foggy"); break;
      case 6: strcpy(weather_string, "weather Stormy Sparse"); break;
      case 7: strcpy(weather_string, "weather Stormy Dense"); break;
   }
   
   return weather_string;
}


char *Create_Campaign_Scenery()
{
    char *scene_file;

    scene_file = (char *) calloc(128, sizeof(char));
    if (! scene_file)
       return NULL;

    strcpy(scene_file, "mission_scene_file scenery/los_angeles.scn");
    return scene_file;
}


int Create_Campaign_Start_Point()
{
     int start_number;

    start_number = (rand() % 4) + 1;
    return start_number;
}


char *Create_Campaign_Description(int first_objective)
{
    char *buffer;
    char *place_name;

    buffer = (char *) calloc(1024, sizeof(char));
    if (! buffer)
       return NULL;

    strcpy(buffer, "name Mission: Patrol\n");
    strcat(buffer, "description ");

    if (first_objective == LOCATION_SEARCH)
         strcat(buffer, "Search the local area for an accident victim.\n");
    else
    {
       place_name = Get_Location_Name(first_objective);
       if (place_name)
       {
           strcat(buffer, "Fly to ");
           strcat(buffer, place_name);
           strcat(buffer, ".\n");
           free(place_name);
       }
    }

    return buffer;
}



int Get_Coordinates(int location, int *x, int *y)
{
    int status = True;

    switch (location)
    {
        case LOCATION_LAX: *x = -5430 ; *y = 35750; break;
        case LOCATION_SMO: *x = -7300; *y = 42580; break;
        case LOCATION_UCLA: *x = -4920; *y = 44800; break;
        case LOCATION_CAT: *x = -4931; *y = -25183; break;
        case LOCATION_USCG: *x = -23758; *y = 34647; break;
        default: status = False; break;
    }

    return status;
}



char *Place_Campaign_Ship()
{
    char *buffer;

    buffer = (char *) calloc(4096, sizeof(char));
    if (! buffer)
      return NULL;

    strcpy(buffer, "create_object 3\n");
    strcat(buffer, "model_file vessels/uscg378.3d\n");
    strcat(buffer, "translation -23758.0 34647.0 0.0\n");
    strcat(buffer, "rotate 300 0 0\n");
    strcat(buffer, "object_name uscg378_01\n");
    strcat(buffer, "create_helipad vehicle 29.0 18.0 3.2 USCG y y y y y uscg378_01 0.0 -58.5 36.0  0.0 0.0 0.0\n");
    strcat(buffer, "object_name helipad_uscg\n");
    strcat(buffer, "object_map_description Coast Guard helipad\n");
    strcat(buffer, "range 2200\n");
   
    return buffer;
}



char *Place_Campaign_Humans(int start_place, int *destinations)
{
    char *buffer;
    char coordinates[256];
    int objective = 0;
    int last_location = start_place;
    int place_x = 0, place_y = 0;
    int human_x, human_y;

    buffer = (char *) calloc(4096, sizeof(char));
    if (! buffer)
       return NULL;

    strcpy(buffer, "# Place humans\n");
    while (objective < MAX_OBJECTIVES)
    {
        if (destinations[objective] == LOCATION_SEARCH)
        {
            if (last_location == LOCATION_USCG)
                strcat(buffer, "create_human default need_rescue alert aware in_water\n");
            else
               strcat(buffer, "create_human default need_rescue alert aware\n");
            strcat(buffer, "translation ");
            Get_Coordinates(last_location, &place_x, &place_y);
            human_x = (rand() % RESCUE_DISTANCE) - (RESCUE_DISTANCE / 2);
            human_x = place_x + human_x;
            human_y = (rand() & RESCUE_DISTANCE) - (RESCUE_DISTANCE / 2);
            human_y = place_y + human_y;
            sprintf(coordinates, "%d %d 0.0", human_x, human_y);
            strcat(buffer, coordinates);
            strcat(buffer, "\nset_human_mesg_enter Thank you for the lift.\n");

            if (last_location == LOCATION_CAT)
            {
                strcat(buffer, "create_fire 10.0 15.0\n");
                strcat(buffer, "translation ");
                sprintf(coordinates, "%d %d 0.0\n", human_x + 200, human_y - 100);
                strcat(buffer, coordinates);
            }
        }
        else if ( (destinations[objective] > LOCATION_NONE) &&
                  (destinations[objective] < LOCATION_SEARCH) )
            last_location = destinations[objective];

        objective++;
    }
 
    return buffer;
}




char *Create_Campaign_Aircraft(int all_arrivals)
{
     char *craft_name;
     int aircraft_number;

     craft_name = (char *) calloc(128, sizeof(char));
     if (! craft_name)
       return NULL;

     strcpy(craft_name, "player_model_file aircrafts/");
     /* we can use any helicoptor for all arrival missions */
     if (all_arrivals)
         aircraft_number = rand() % 8;
     else
         aircraft_number = rand() % 4;

     switch (aircraft_number)
     {
         case 0: strcat(craft_name, "hh60.3d"); break;
         case 1: strcat(craft_name, "hh65.3d"); break;
         case 2: strcat(craft_name, "ka27.3d"); break;
         case 3: strcat(craft_name, "v22.3d"); break;
         /* end of all all mission list. From here it's arrivals only */
         case 4: strcat(craft_name, "as350.3d"); break;
         case 5: strcat(craft_name, "b47.3d"); break;
         case 6: strcat(craft_name, "s64.3d"); break;
         case 7: strcat(craft_name, "uh1d.3d"); break;
     }

     return craft_name;     
}



char *Create_Campaign_Arrive_Objective(int current_location, int next_location)
{
     char *buffer;
     char *location_name;

     buffer = (char *) calloc( 512, sizeof(char));
     if (! buffer)
       return NULL;

     strcpy(buffer, "mission_objective_new arrive\n");
     strcat(buffer, "mission_objective_time_left 0.0\n");
     strcat(buffer, "mission_objective_arrive_at ");
     /* put in current location */
     location_name = Get_Location_Symbol(current_location);
     if (location_name)
     {
         strcat(buffer, location_name);
         free(location_name);
     }
     strcat(buffer, "\n");
     strcat(buffer, "mission_objective_message_success Welcome!");
     location_name = Get_Location_Name(next_location);
     /* put in next location */
     if (location_name)
     {
        strcat(buffer, " Next head for ");
        strcat(buffer, location_name);
        free(location_name);
     }
     strcat(buffer, "\nmission_objective_message_fail You did not arrive safely.\n");
     return buffer;
}



char *Create_Campaign_Pickup_Objective(int next_location)
{
    char *buffer;
    char *location_name;

    buffer = (char *) calloc(1024, sizeof(char));
    if (! buffer)
       return NULL;

    strcpy(buffer, "mission_objective_new pick_up\n");
    strcat(buffer, "mission_objective_time_left 0.0\n");
    strcat(buffer, "mission_objective_humans_tally 1 1\n");
    strcat(buffer, "mission_objective_message_success ");
    /* put next location in here */
    location_name = Get_Location_Name(next_location);
    if (location_name)
    {
       strcat(buffer, "Pick up complete. Head for ");
       strcat(buffer, location_name);
       free(location_name);
    }
    strcat(buffer, "\nmission_objective_message_fail You were unable to complete pick-up.\n");
    
   return buffer;
}




char *Get_Location_Symbol(int location)
{
    char *my_string;

     my_string = (char *) calloc(64, sizeof(char));
     if (! my_string)
         return NULL;

     switch (location)
     {
        case LOCATION_NONE: free(my_string); my_string = NULL; break;
        case LOCATION_LAX: strcpy(my_string, "helipad_lax"); break;
        case LOCATION_SMO: strcpy(my_string, "helipad_smo"); break;
        case LOCATION_UCLA: strcpy(my_string, "helipad_ucla"); break;
        case LOCATION_CAT: strcpy(my_string, "helipad_cat"); break;
        case LOCATION_USCG: strcpy(my_string, "helipad_uscg"); break;
        default: my_string[0] = '\0'; break;
 
     }
     return my_string;
}



char *Get_Location_Name(int location)
{
   char *my_string;
   my_string = (char *) calloc(64, sizeof(char));
   if (! my_string)
     return NULL;

     switch (location)
     {
        case LOCATION_NONE: free(my_string); my_string = NULL; break;
        case LOCATION_LAX: strcpy(my_string, "Los Angeles Airport"); break;
        case LOCATION_SMO: strcpy(my_string, "Santa Monica Airport"); break;
        case LOCATION_UCLA: strcpy(my_string, "UCLA Hospital"); break;
        case LOCATION_CAT: strcpy(my_string, "Catalina Airport"); break;
        case LOCATION_USCG: strcpy(my_string, "US Coast Guard ship"); break;
        case LOCATION_SEARCH: strcpy(my_string, "a nearby victim"); break;

     }
     return my_string;
}



char *Get_Campaign_Filename()
{
    char *full_filename;
    char *home_dir;

    home_dir = getenv("HOME");
    if (! home_dir)
       return NULL;

    full_filename = (char *) calloc( strlen(home_dir) + 64, sizeof(char) );
    if (! full_filename)
       return NULL;

    sprintf(full_filename, "%s/.SearchAndRescue/campaign.mis", home_dir);
    return full_filename;
}




int Create_Campaign()
{
   FILE *camp_file;
   char *objective_string, *temp_string;
   int start_point;
   int objective_type, all_arrivals = True;
   int number_of_objectives;
   int done_description = False;
   int destination[MAX_OBJECTIVES];
   int count;

   camp_file = Create_Campaign_File();
   if (!camp_file)
      return False;

   memset(destination, 0, MAX_OBJECTIVES * sizeof(int));
   start_point = Create_Campaign_Start_Point();
   number_of_objectives = (rand() % 3) + 3;

   /* figure out where we will go */
   count = 0;
   while (count < number_of_objectives)
   {
       if (count < (number_of_objectives - 1) )
           objective_type = rand() % 2;
       else         /* last one is always arrival */
           objective_type = OBJECTIVE_ARRIVE;

       if (objective_type == OBJECTIVE_ARRIVE)
       {
           int done;
           int temp_place;
           do
           {
               temp_place = (rand() % 5) + 1;
               if ( (count == 0) && (temp_place == start_point) )
                  done = False;
               else if ( (count > 0) && (temp_place == destination[count - 1]) )
                  done = False;
               else
                  done = True;
           } while (! done);
           destination[count] = temp_place;
       }
       else
           destination[count] = LOCATION_SEARCH;
       count++;
   }

   count = 0;
   while (count < number_of_objectives)
   {
       if (destination[count] == LOCATION_SEARCH)
       {
          objective_string = Create_Campaign_Pickup_Objective(destination[count + 1]);
          all_arrivals = False;
       }
       else
          objective_string = Create_Campaign_Arrive_Objective(destination[count], destination[count + 1]);

       if (! done_description)
       {
          temp_string = Create_Campaign_Description(destination[0]);
          if (temp_string)
          {
            fprintf(camp_file, "%s\n", temp_string);
            free(temp_string);
          }
          temp_string = Create_Campaign_Weather();
          if (temp_string)
          {
            fprintf(camp_file, "%s\n", temp_string);
            free(temp_string);
          }

          temp_string = Create_Campaign_Scenery();
          if (temp_string)
          {
             fprintf(camp_file, "%s\n", temp_string);
             free(temp_string);
          }

          temp_string = Place_Campaign_Ship();
          if (temp_string)
          {
               fprintf(camp_file, "%s\n", temp_string);
               free(temp_string);
          }

          temp_string = Get_Location_Symbol(start_point);
          if (temp_string)
          {
              fprintf(camp_file, "mission_begin_at %s\n", temp_string);
              free(temp_string);
          }
          done_description = True;
       }
       
       if (objective_string)
       {
           fprintf(camp_file, "%s\n", objective_string);
           free(objective_string);
       }
       count++;
   }

   temp_string = Place_Campaign_Humans(start_point, destination);
   if (temp_string)
   {
       fprintf(camp_file, "%s\n", temp_string);
       free(temp_string);
   }

   temp_string = Create_Campaign_Aircraft(all_arrivals);
   if (temp_string)
   {
        fprintf(camp_file, "%s\n", temp_string);
        free(temp_string);
   }
   fclose(camp_file);

   return True;
}