~ubuntu-branches/ubuntu/precise/frogatto/precise

« back to all changes in this revision

Viewing changes to src/level_logic.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2010-07-21 16:21:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100721162145-zid0u93fm3xz73gh
Tags: upstream-1.0+dfsg1
ImportĀ upstreamĀ versionĀ 1.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cstdlib>
 
2
#include <limits.h>
 
3
 
 
4
#include "level.hpp"
 
5
#include "level_logic.hpp"
 
6
 
 
7
bool cliff_edge_within(const level& lvl, int xpos, int ypos, int deltax)
 
8
{
 
9
        const int FeetWidth = 5;
 
10
        return !lvl.standable(xpos + deltax, ypos) &&
 
11
               !lvl.standable(xpos + deltax, ypos + std::abs(deltax) + FeetWidth);
 
12
}
 
13
 
 
14
int distance_to_cliff(const level& lvl, int xpos, int ypos, int facing)
 
15
{
 
16
        const int max_search = 1000;
 
17
        const int cliff_face = 5;
 
18
        const int cliff_drop = 2;
 
19
 
 
20
        bool found = false;
 
21
        
 
22
        //search for up to three pixels below us to try to get a starting
 
23
        //position which is standable.
 
24
        for(int n = 0; n != 3; ++n) {
 
25
                if(lvl.standable_tile(xpos, ypos)) {
 
26
                        found = true;
 
27
                        break;
 
28
                }
 
29
 
 
30
                ++ypos;
 
31
        }
 
32
 
 
33
        if(!found) {
 
34
                return max_search;
 
35
        }
 
36
 
 
37
        //make sure we are at the surface.
 
38
        while(lvl.standable_tile(xpos, ypos-1)) {
 
39
                --ypos;
 
40
        }
 
41
 
 
42
        int result = 0;
 
43
        for(; result < max_search; xpos += facing, ++result) {
 
44
                if(lvl.standable_tile(xpos, ypos) || lvl.standable_tile(xpos, ypos-1)) {
 
45
                        int ydiff = 0;
 
46
                        while(lvl.standable_tile(xpos, ypos-1) && ydiff < cliff_face) {
 
47
                                --ypos;
 
48
                                ++ydiff;
 
49
                        }
 
50
 
 
51
                        if(ydiff == cliff_face) {
 
52
                                return max_search;
 
53
                        }
 
54
                } else {
 
55
                        int ydiff = 0;
 
56
                        while(!lvl.standable_tile(xpos, ypos) && ydiff < cliff_drop) {
 
57
                                ++ypos;
 
58
                                ++ydiff;
 
59
                        }
 
60
 
 
61
                        if(ydiff == cliff_drop) {
 
62
                                return result;
 
63
                        }
 
64
                }
 
65
        }
 
66
 
 
67
        return result;
 
68
}
 
69
 
 
70
int find_ground_level(const level& lvl, int xpos, int ypos, int max_search)
 
71
{
 
72
        if(lvl.standable(xpos, ypos)) {
 
73
                --ypos;
 
74
                while(lvl.standable(xpos, ypos) && --max_search > 0) {
 
75
                        --ypos;
 
76
                }
 
77
 
 
78
                if(!max_search) {
 
79
                        return INT_MIN;
 
80
                }
 
81
 
 
82
                return ypos + 1;
 
83
        } else {
 
84
                //search both up and down, since in the case of a platform the
 
85
                //ground may be above us.
 
86
                for(int n = 1; n < max_search; ++n) {
 
87
                        if(lvl.standable(xpos, ypos + n)) {
 
88
                                return ypos + n - 1;
 
89
                        }
 
90
 
 
91
                        if(lvl.standable(xpos, ypos - n) && !lvl.standable(xpos, ypos - n - 1)) {
 
92
                                return ypos - n;
 
93
                        }
 
94
                }
 
95
 
 
96
                return INT_MIN;
 
97
        }
 
98
}