Very minimalistic simplification of some code in ball/util.c.
[neverball] / ball / levels.c
index b098d78..1c9ccb1 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
+#include <assert.h>
 
 #include "level.h"
 #include "levels.h"
-#include "image.h"
 #include "game.h"
-#include "geom.h"
 #include "demo.h"
 #include "audio.h"
 #include "config.h"
@@ -35,7 +34,7 @@ static struct level_game current_level_game;
 
 int level_replay(const char *filename)
 {
-    return demo_replay_init(filename, &current_level_game); 
+    return demo_replay_init(filename, &current_level_game);
 }
 
 static struct level single_level; /* a level without set */
@@ -44,60 +43,52 @@ int level_play_go(void)
 /* Start to play the current level */
 {
     struct level_game *lg = &current_level_game;
-    int mode  = lg->mode;
-    const struct level *l;
-
-    if (curr_set())
-       l = get_level(lg->level);
-    else
-        l = &single_level;         
-    
+    const struct level *l = lg->level;
+    int mode = lg->mode;
+
+    assert(l != NULL);
+
     lg->goal = (mode == MODE_PRACTICE) ? 0 : l->goal;
     lg->time = (mode == MODE_PRACTICE) ? 0 : l->time;
-    
+
     /* clear other fields */
     lg->state = GAME_NONE;
     lg->coins = 0;
     lg->timer = lg->time;
-    lg->coin_rank = lg->goal_rank = lg->time_rank = 
-           lg->score_rank = lg-> times_rank = 3;
-    lg->next_level = 0;
-    
+    lg->coin_rank = lg->goal_rank = lg->time_rank =
+        lg->score_rank = lg->times_rank = 3;
+    lg->win = lg->dead = lg->unlock = 0;
+    lg->next_level = NULL;
+
     return demo_play_init(USER_REPLAY_FILE, l, lg);
 }
 
+/* Prepare to play a single level */
 
 void level_play_single(const char *filename)
-/* Prepare to play a single level */
 {
     struct level *l = &single_level;
 
-    current_level_game.mode  = MODE_SINGLE;
-    current_level_game.level = 0;
-    
-    strncpy(l->file, filename, MAXSTR);
-    l->back[0] = '\0';
-    l->grad[0] = '\0';
-    l->song[0] = '\0';
-    l->shot[0] = '\0';
-    l->goal    = 0;
-    l->time    = 0;
+    level_load(filename, l);
+    level_play(l, MODE_SINGLE);
 }
 
-void level_play(int i, int m)
 /* Prepare to play a level sequence from the `i'th level */
+
+void level_play(const struct level *l, int m)
 {
-    current_level_game.mode = m;
-    current_level_game.level = i;
+    struct level_game *lg = &current_level_game;
 
-    current_level_game.score = 0;
-    current_level_game.balls = 3;
-    current_level_game.times = 0;
+    memset(lg, 0, sizeof (struct level_game));
+
+    lg->mode  = m;
+    lg->level = l;
+    lg->balls = 3;
 }
 
 /*---------------------------------------------------------------------------*/
 
-const struct level_game * curr_lg(void)
+const struct level_game *curr_lg(void)
 {
     return &current_level_game;
 }
@@ -109,55 +100,52 @@ int count_extra_balls(int old_score, int coins)
     return sum / 100;
 }
 
-void level_stop(int state, int clock, int coins)
 /* Stop the current playing level */
+
+void level_stop(int state, int state_value, int clock, int coins)
 {
-    struct level_game * lg = &current_level_game;
+    struct level_game *lg = &current_level_game;
     int mode = lg->mode;
-    int timer = (mode == MODE_PRACTICE || mode == MODE_SINGLE) ? clock : lg->time - clock;
+    int timer = (mode == MODE_PRACTICE
+                 || mode == MODE_SINGLE) ? clock : lg->time - clock;
 
     lg->state = state;
     lg->coins = coins;
     lg->timer = timer;
-   
-    /* Performs challenge mode opperations */ 
+    lg->state_value = state_value;
+
+    /* Performs challenge mode opperations */
     if (mode == MODE_CHALLENGE)
     {
-       /* sum time */
-       lg->times += timer; 
-           
-       /* sum coins an earn extra balls */
-       if (state == GAME_GOAL)
-       {
-           lg->balls += count_extra_balls(lg->score, coins);
-           lg->score += coins;
-       }
-
-       /* lose ball */
-        if (state == GAME_TIME || state == GAME_FALL)
-           lg->balls--;
+        /* sum time */
+        lg->times += timer;
+
+        /* sum coins an earn extra balls */
+        if (state == GAME_GOAL || state == GAME_SPEC || lg->level->is_bonus)
+        {
+            lg->balls += count_extra_balls(lg->score, coins);
+            lg->score += coins;
+        }
+
+        /* lose ball and game */
+        else                    /* if ((state == GAME_TIME || state == GAME_FALL) && !lg->level->is_bonus) */
+        {
+            lg->dead = (lg->balls <= 0);
+            lg->balls--;
+        }
     }
-    
+
     /* Update high-scores and next level */
-    if (state == GAME_GOAL && curr_set())
-       set_finish_level(lg, config_simple_get_s(CONFIG_PLAYER));
-    else
-       lg->next_level = -1;
+    set_finish_level(lg, config_simple_get_s(CONFIG_PLAYER));
 
-    /* stop demo recording */  
+    /* stop demo recording */
     demo_play_stop(lg);
 }
 
-int level_dead(void)
-{
-    int mode = current_level_game.mode;
-    int balls = current_level_game.balls;
-    return (mode == MODE_CHALLENGE) && (balls <= 0);
-}
-
 void level_next(void)
 {
-    current_level_game.level = current_level_game.next_level;
+    struct level_game *lg = &current_level_game;
+    lg->level = lg->next_level;
 }
 
 void level_update_player_name(void)