1 /* $Id: score.c,v 1.35 2005/07/10 04:40:58 oohara Exp $ */
2 
3 #include <stdio.h>
4 /* strlen */
5 #include <string.h>
6 
7 #include "util.h"
8 #include "tenm_object.h"
9 #include "const.h"
10 #include "stage.h"
11 #include "ship.h"
12 
13 #include "score.h"
14 
15 #define EXTEND_FIRST 200000
16 #define EXTEND_LATER_EVERY 200000
17 
18 static int score = 0;
19 static int stage_score[6];
20 static int stage_cleared[6];
21 static int extend_next = EXTEND_FIRST;
22 
23 void
clear_score(void)24 clear_score(void)
25 {
26   int i;
27 
28   score = 0;
29   for (i = 0; i < 6; i++)
30   {
31     stage_score[i] = 0;
32     stage_cleared[i] = 0;
33   }
34   extend_next = EXTEND_FIRST;
35 }
36 
37 int
get_score(void)38 get_score(void)
39 {
40   return score;
41 }
42 
43 int
get_stage_score(int stage)44 get_stage_score(int stage)
45 {
46   /* sanity check */
47   if ((stage <= 0) || (stage > 6))
48   {
49     fprintf(stderr, "get_stage_score: strange stage (%d)\n", stage);
50     return 0;
51   }
52 
53   return stage_score[stage - 1];
54 }
55 
56 int
get_stage_cleared(int stage)57 get_stage_cleared(int stage)
58 {
59   /* sanity check */
60   if ((stage <= 0) || (stage > 6))
61   {
62     fprintf(stderr, "get_stage_cleared: strange stage (%d)\n", stage);
63     return 0;
64   }
65 
66   return stage_cleared[stage - 1];
67 }
68 
69 void
set_stage_cleared(int stage,int n)70 set_stage_cleared(int stage, int n)
71 {
72   /* sanity check */
73   if ((stage <= 0) || (stage > 6))
74   {
75     fprintf(stderr, "set_stage_cleared: strange stage (%d)\n", stage);
76     return;
77   }
78 
79   stage_cleared[stage - 1] = n;
80 }
81 
82 /* return the actual score added to the total */
83 int
add_score(int delta)84 add_score(int delta)
85 {
86   if (score + delta < 0)
87     delta = -score;
88 
89   score += delta;
90   if ((get_stage_number() >= 1) && (get_stage_number() <= 6))
91     stage_score[get_stage_number() - 1] += delta;
92 
93   while (score >= extend_next)
94   {
95     extend_next += EXTEND_LATER_EVERY;
96     /* you don't get an extra ship if you have already
97      * lost the game, if you have cleared the game or
98      * if you are watching the tutorial demo
99      */
100     if ((get_ship() >= 0)
101         && (get_stage_number() >= 1)
102         && (get_stage_number() <= 5)
103         && (get_stage_id(get_stage_number()) > 0))
104       add_ship(1);
105   }
106 
107   return delta;
108 }
109 
110 /* return the damege that should be subtracted from
111  * the hit point of the enemy */
112 int
add_damage_score(int hit_point,int damage)113 add_damage_score(int hit_point, int damage)
114 {
115   /* sanity check */
116   if (hit_point <= 0)
117     return 0;
118   if (damage <= 0)
119     return 0;
120 
121   if (hit_point > damage)
122   {
123     add_score(damage);
124   }
125   else
126   {
127     damage = hit_point;
128     add_score(damage - 1);
129   }
130 
131   return damage;
132 }
133