1 /* $Id: stage-clear.c,v 1.9 2005/07/10 04:32:37 oohara Exp $ */
2 
3 #include <stdio.h>
4 /* malloc */
5 #include <stdlib.h>
6 /* strlen, strcmp */
7 #include <string.h>
8 
9 #include "const.h"
10 #include "tenm_object.h"
11 #include "tenm_graphic.h"
12 #include "util.h"
13 #include "tenm_table.h"
14 #include "stage.h"
15 #include "score.h"
16 #include "scheduler.h"
17 #include "ship.h"
18 
19 #include "stage-clear.h"
20 
21 static int stage_clear_act(tenm_object *my, const tenm_object *player);
22 static int stage_clear_draw(tenm_object *my, int priority);
23 
24 tenm_object *
stage_clear_new(int t)25 stage_clear_new(int t)
26 {
27   tenm_object *new = NULL;
28   int *count = NULL;
29 
30   /* sanity check */
31   if (t < 0)
32   {
33     fprintf(stderr, "stage_clear_new: t is negative (%d)\n", t);
34     return NULL;
35   }
36 
37   count = (int *) malloc(sizeof(int) * 2);
38   if (count == NULL)
39   {
40     fprintf(stderr, "stage_clear_new: malloc(count) failed\n");
41     return NULL;
42   }
43 
44   /* list of count
45    * [0] timer
46    * [1] message delay
47    */
48   count[0] = -1;
49   count[1] = t;
50 
51   new = tenm_object_new("stage clear", 0, 0,
52                         1, 0.0, 0.0,
53                         2, count, 0, NULL, 0, NULL,
54                         (int (*)(tenm_object *, double))
55                         NULL,
56                         (int (*)(tenm_object *, tenm_object *))
57                         NULL,
58                         (int (*)(tenm_object *, const tenm_object *))
59                         (&stage_clear_act),
60                         (int (*)(tenm_object *, int))
61                         (&stage_clear_draw));
62 
63   if (new == NULL)
64   {
65     fprintf(stderr, "stage_clear_new: tenm_object_new failed\n");
66     if (count != NULL)
67       free(count);
68     return NULL;
69   }
70 
71   return new;
72 }
73 
74 static int
stage_clear_act(tenm_object * my,const tenm_object * player)75 stage_clear_act(tenm_object *my, const tenm_object *player)
76 {
77   int stage;
78 
79   /* sanity check */
80   if (my == NULL)
81   {
82     fprintf(stderr, "stage_clear_act: my is NULL\n");
83     return 0;
84   }
85   if (player == NULL)
86     return 0;
87 
88   if (get_ship() < 0)
89     return 1;
90 
91   (my->count[0])++;
92   if (my->count[0] == 0)
93   {
94     stage = get_stage_number();
95     if ((stage >= 1) && (stage <= 5))
96       set_stage_cleared(stage, 1);
97   }
98   if (my->count[0] >= my->count[1] + 100)
99   {
100     set_this_stage_cleared(1);
101     return 1;
102   }
103 
104   return 0;
105 }
106 
107 static int
stage_clear_draw(tenm_object * my,int priority)108 stage_clear_draw(tenm_object *my, int priority)
109 {
110   int status = 0;
111   char temp[32];
112 
113   /* sanity check */
114   if ((get_stage_number() < 1) || (get_stage_number() > 5))
115     return 0;
116 
117   if (priority != 1)
118     return 0;
119   if (my->count[0] < my->count[1])
120     return 0;
121   if (get_ship() < 0)
122     return 0;
123 
124   if (get_stage_number() <= 4)
125     sprintf(temp, "stage %d cleared", get_stage_number());
126   else
127     sprintf(temp, "final stage cleared");
128   if (draw_string(WINDOW_WIDTH / 2 - ((int) strlen(temp)) * 9 / 2, 190,
129                   temp, (int) strlen(temp)) != 0)
130   {
131     fprintf(stderr, "stage_clear_draw: draw_string (stage number) failed\n");
132     status = 1;
133   }
134 
135   sprintf(temp, "stage score: %8d", get_stage_score(get_stage_number()));
136   if (draw_string(WINDOW_WIDTH / 2 - ((int) strlen(temp)) * 9 / 2, 220,
137                   temp, (int) strlen(temp)) != 0)
138   {
139     fprintf(stderr, "stage_title_draw: draw_string (stage score) failed\n");
140     status = 1;
141   }
142 
143   return status;
144 }
145