An exciting survival game with random terrain by Jonne
Dependencies: PokittoLib
Fork of AnimationTest by
main.cpp@9:e92847eb42bf, 2018-05-02 (annotated)
- Committer:
- Pokitto
- Date:
- Wed May 02 06:48:29 2018 +0000
- Revision:
- 9:e92847eb42bf
- Parent:
- 8:809b1982b4ae
New pokittolib with improved volume controls & better button handling
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 8:809b1982b4ae | 1 | #include "Pokitto.h" |
Pokitto | 8:809b1982b4ae | 2 | #include "Pixonia.h" |
Pokitto | 8:809b1982b4ae | 3 | #include "Synth.h" |
Pokitto | 8:809b1982b4ae | 4 | |
Pokitto | 8:809b1982b4ae | 5 | Pokitto::Core gb; |
Pokitto | 8:809b1982b4ae | 6 | |
Pokitto | 8:809b1982b4ae | 7 | #define YOFF 4 |
Pokitto | 8:809b1982b4ae | 8 | #define XOFF 8 |
Pokitto | 8:809b1982b4ae | 9 | |
Pokitto | 8:809b1982b4ae | 10 | #define WORLDSIZE 50 |
Pokitto | 8:809b1982b4ae | 11 | #define TIMEINCREMENT 16 |
Pokitto | 8:809b1982b4ae | 12 | |
Pokitto | 8:809b1982b4ae | 13 | #define SUBDIVS 4 |
Pokitto | 8:809b1982b4ae | 14 | #define XSPEED 2<<4//2 |
Pokitto | 8:809b1982b4ae | 15 | #define YSPEED 1<<4//1 |
Pokitto | 8:809b1982b4ae | 16 | #define PILLARHEIGHT 2 |
Pokitto | 8:809b1982b4ae | 17 | #define GROUNDLIFT 1 |
Pokitto | 8:809b1982b4ae | 18 | |
Pokitto | 8:809b1982b4ae | 19 | #define MAXSTEP 2 |
Pokitto | 8:809b1982b4ae | 20 | #define MAXWOLFSTEP 3 |
Pokitto | 8:809b1982b4ae | 21 | #define WOLFENERGY 0x26F |
Pokitto | 8:809b1982b4ae | 22 | #define MONSTERMORNING 75 |
Pokitto | 8:809b1982b4ae | 23 | #define MONSTERNIGHT 130 |
Pokitto | 8:809b1982b4ae | 24 | //#define DEBUG |
Pokitto | 8:809b1982b4ae | 25 | |
Pokitto | 8:809b1982b4ae | 26 | #define MAXELEVATIONDIFF 1 |
Pokitto | 8:809b1982b4ae | 27 | #define DIGMAX 2 |
Pokitto | 8:809b1982b4ae | 28 | |
Pokitto | 8:809b1982b4ae | 29 | #define MAXHUMANS 10 |
Pokitto | 8:809b1982b4ae | 30 | #define MAXWOLVES 10 |
Pokitto | 8:809b1982b4ae | 31 | #define TREESNROCKS 1 |
Pokitto | 8:809b1982b4ae | 32 | #define HUTPROB 0x01 |
Pokitto | 8:809b1982b4ae | 33 | #define STONEPROB 0x24 |
Pokitto | 8:809b1982b4ae | 34 | #define TREEPROB 0x30 |
Pokitto | 8:809b1982b4ae | 35 | #define GOLDPROB 0x08 //was 7 |
Pokitto | 8:809b1982b4ae | 36 | #define MAXGOLD 50 |
Pokitto | 8:809b1982b4ae | 37 | #define GOLDMULT 3 |
Pokitto | 8:809b1982b4ae | 38 | #define WOLFMULT 1 |
Pokitto | 8:809b1982b4ae | 39 | #define MAPMULT 10 |
Pokitto | 8:809b1982b4ae | 40 | #define GLIMMERPROB 0x20 |
Pokitto | 8:809b1982b4ae | 41 | #define WOLFPROB 0x2000 |
Pokitto | 8:809b1982b4ae | 42 | |
Pokitto | 8:809b1982b4ae | 43 | |
Pokitto | 8:809b1982b4ae | 44 | |
Pokitto | 8:809b1982b4ae | 45 | #define DIR_NE 0 |
Pokitto | 8:809b1982b4ae | 46 | #define DIR_E 1 |
Pokitto | 8:809b1982b4ae | 47 | #define DIR_SE 2 |
Pokitto | 8:809b1982b4ae | 48 | #define DIR_S 3 |
Pokitto | 8:809b1982b4ae | 49 | #define DIR_SW 4 |
Pokitto | 8:809b1982b4ae | 50 | #define DIR_W 5 |
Pokitto | 8:809b1982b4ae | 51 | #define DIR_NW 6 |
Pokitto | 8:809b1982b4ae | 52 | #define DIR_N 7 |
Pokitto | 8:809b1982b4ae | 53 | |
Pokitto | 8:809b1982b4ae | 54 | |
Pokitto | 8:809b1982b4ae | 55 | #define TER_UNDEFINED 0 |
Pokitto | 8:809b1982b4ae | 56 | #define TER_WATER 31 |
Pokitto | 8:809b1982b4ae | 57 | #define TER_PLAIN 1 |
Pokitto | 8:809b1982b4ae | 58 | #define TER_HUT 2 |
Pokitto | 8:809b1982b4ae | 59 | #define TER_TREE 3 |
Pokitto | 8:809b1982b4ae | 60 | #define TER_STONE 4 |
Pokitto | 8:809b1982b4ae | 61 | #define TER_BUMP_NW 5 |
Pokitto | 8:809b1982b4ae | 62 | #define TER_BUMP_SE 6 |
Pokitto | 8:809b1982b4ae | 63 | #define TER_BUMP_NE 7 |
Pokitto | 8:809b1982b4ae | 64 | #define TER_BUMP_SW 8 |
Pokitto | 8:809b1982b4ae | 65 | #define TER_BUMP_N 9 |
Pokitto | 8:809b1982b4ae | 66 | #define TER_BUMP_S 10 |
Pokitto | 8:809b1982b4ae | 67 | #define TER_BUMP_E 11 |
Pokitto | 8:809b1982b4ae | 68 | #define TER_BUMP_W 12 |
Pokitto | 8:809b1982b4ae | 69 | #define TER_GOLD 13 |
Pokitto | 8:809b1982b4ae | 70 | #define TER_ROCK 14 |
Pokitto | 8:809b1982b4ae | 71 | #define TER_BEACHSW 29 |
Pokitto | 8:809b1982b4ae | 72 | #define TER_BEACHSE 30 |
Pokitto | 8:809b1982b4ae | 73 | |
Pokitto | 8:809b1982b4ae | 74 | enum walkDir { |
Pokitto | 8:809b1982b4ae | 75 | NE, |
Pokitto | 8:809b1982b4ae | 76 | SE, |
Pokitto | 8:809b1982b4ae | 77 | SW, |
Pokitto | 8:809b1982b4ae | 78 | NW |
Pokitto | 8:809b1982b4ae | 79 | }; |
Pokitto | 8:809b1982b4ae | 80 | |
Pokitto | 8:809b1982b4ae | 81 | enum humanTasks { |
Pokitto | 8:809b1982b4ae | 82 | IDLE, |
Pokitto | 8:809b1982b4ae | 83 | CHOPPING, |
Pokitto | 8:809b1982b4ae | 84 | RAISING, |
Pokitto | 8:809b1982b4ae | 85 | LOWERING, |
Pokitto | 8:809b1982b4ae | 86 | PLANTING, |
Pokitto | 8:809b1982b4ae | 87 | MOVING, |
Pokitto | 8:809b1982b4ae | 88 | HUTBUILDING |
Pokitto | 8:809b1982b4ae | 89 | }; |
Pokitto | 8:809b1982b4ae | 90 | |
Pokitto | 8:809b1982b4ae | 91 | enum AImodes { |
Pokitto | 8:809b1982b4ae | 92 | HOMING, |
Pokitto | 8:809b1982b4ae | 93 | INTERCEPT, |
Pokitto | 8:809b1982b4ae | 94 | CHASE, |
Pokitto | 8:809b1982b4ae | 95 | TURNCW, |
Pokitto | 8:809b1982b4ae | 96 | GOTO |
Pokitto | 8:809b1982b4ae | 97 | }; |
Pokitto | 8:809b1982b4ae | 98 | |
Pokitto | 8:809b1982b4ae | 99 | enum Gamestates { |
Pokitto | 8:809b1982b4ae | 100 | TITLE, |
Pokitto | 8:809b1982b4ae | 101 | NEXTLEVEL, |
Pokitto | 8:809b1982b4ae | 102 | INTRO, |
Pokitto | 8:809b1982b4ae | 103 | GAME, |
Pokitto | 8:809b1982b4ae | 104 | GAMEOVER, |
Pokitto | 8:809b1982b4ae | 105 | LEVELPASSED |
Pokitto | 8:809b1982b4ae | 106 | }; |
Pokitto | 8:809b1982b4ae | 107 | |
Pokitto | 8:809b1982b4ae | 108 | Gamestates gamestate = TITLE; |
Pokitto | 8:809b1982b4ae | 109 | int gamelevel=1; |
Pokitto | 8:809b1982b4ae | 110 | uint16_t worldsize=10; |
Pokitto | 8:809b1982b4ae | 111 | |
Pokitto | 8:809b1982b4ae | 112 | |
Pokitto | 8:809b1982b4ae | 113 | int32_t wx=-8,wy=0,cx=10,cy=10; |
Pokitto | 8:809b1982b4ae | 114 | uint16_t prevtile=0, playertile=0, focustile=0, centertile=0,oldcenter, tween_palette[16], golds=0, playergolds=0; |
Pokitto | 8:809b1982b4ae | 115 | uint16_t timeofday=0; //value>>8 0 is full daytime, 127 is full night |
Pokitto | 8:809b1982b4ae | 116 | uint8_t wave=0, oldtime=0,wolvestick=0,watertick=0; |
Pokitto | 8:809b1982b4ae | 117 | int8_t wading=1; |
Pokitto | 8:809b1982b4ae | 118 | bool focusing=false; |
Pokitto | 8:809b1982b4ae | 119 | bool inHut = false; |
Pokitto | 8:809b1982b4ae | 120 | int16_t tweenfactor=0,numwolves=0; |
Pokitto | 8:809b1982b4ae | 121 | |
Pokitto | 8:809b1982b4ae | 122 | /** function declarations **/ |
Pokitto | 8:809b1982b4ae | 123 | bool raiseTile(uint16_t); |
Pokitto | 8:809b1982b4ae | 124 | bool lowerTile(uint16_t); |
Pokitto | 8:809b1982b4ae | 125 | bool caught=false; |
Pokitto | 8:809b1982b4ae | 126 | int8_t getTileHeightToTile(uint16_t, uint16_t); |
Pokitto | 8:809b1982b4ae | 127 | uint8_t digCount=0, monsterdrain=1; |
Pokitto | 8:809b1982b4ae | 128 | uint16_t workingOnTile=0; |
Pokitto | 8:809b1982b4ae | 129 | |
Pokitto | 8:809b1982b4ae | 130 | |
Pokitto | 8:809b1982b4ae | 131 | |
Pokitto | 8:809b1982b4ae | 132 | struct coords { |
Pokitto | 8:809b1982b4ae | 133 | int16_t x; |
Pokitto | 8:809b1982b4ae | 134 | int16_t y; |
Pokitto | 8:809b1982b4ae | 135 | }; |
Pokitto | 8:809b1982b4ae | 136 | |
Pokitto | 8:809b1982b4ae | 137 | struct creature { |
Pokitto | 8:809b1982b4ae | 138 | int16_t energy; |
Pokitto | 8:809b1982b4ae | 139 | bool visible; |
Pokitto | 8:809b1982b4ae | 140 | walkDir facing; |
Pokitto | 8:809b1982b4ae | 141 | uint8_t animframe; |
Pokitto | 8:809b1982b4ae | 142 | uint16_t tile; |
Pokitto | 8:809b1982b4ae | 143 | coords position; |
Pokitto | 8:809b1982b4ae | 144 | coords goingto; |
Pokitto | 8:809b1982b4ae | 145 | AImodes ai; |
Pokitto | 8:809b1982b4ae | 146 | }; |
Pokitto | 8:809b1982b4ae | 147 | |
Pokitto | 8:809b1982b4ae | 148 | creature humans[MAXHUMANS]; |
Pokitto | 8:809b1982b4ae | 149 | creature werewolves[MAXWOLVES]; |
Pokitto | 8:809b1982b4ae | 150 | |
Pokitto | 8:809b1982b4ae | 151 | coords player,oldplayer,focus,goldnugget; |
Pokitto | 8:809b1982b4ae | 152 | uint8_t oldplayercount=0, goldnuggetcount; |
Pokitto | 8:809b1982b4ae | 153 | |
Pokitto | 8:809b1982b4ae | 154 | uint8_t world[WORLDSIZE*WORLDSIZE]; |
Pokitto | 8:809b1982b4ae | 155 | |
Pokitto | 8:809b1982b4ae | 156 | int getTileHeight(uint16_t i) { |
Pokitto | 8:809b1982b4ae | 157 | return (world[i]>>5); |
Pokitto | 8:809b1982b4ae | 158 | } |
Pokitto | 8:809b1982b4ae | 159 | |
Pokitto | 8:809b1982b4ae | 160 | int getTileType(uint16_t i) { |
Pokitto | 8:809b1982b4ae | 161 | return (world[i]&0x1F); |
Pokitto | 8:809b1982b4ae | 162 | } |
Pokitto | 8:809b1982b4ae | 163 | |
Pokitto | 8:809b1982b4ae | 164 | void setTileHeight(uint16_t i, int8_t h) { |
Pokitto | 8:809b1982b4ae | 165 | world[i] &= 0x1F; //save tile type, wipe height bits |
Pokitto | 8:809b1982b4ae | 166 | world[i] |= (h<<5); //store height in highest 3 bits |
Pokitto | 8:809b1982b4ae | 167 | } |
Pokitto | 8:809b1982b4ae | 168 | |
Pokitto | 8:809b1982b4ae | 169 | void setTileType(uint16_t i, uint8_t t) { |
Pokitto | 8:809b1982b4ae | 170 | world[i] &= 0xE0; // wipe bottom 5 bits (tile type) |
Pokitto | 8:809b1982b4ae | 171 | world[i] |= (t&0x1F); // set bottom 5 bits to type |
Pokitto | 8:809b1982b4ae | 172 | } |
Pokitto | 8:809b1982b4ae | 173 | |
Pokitto | 8:809b1982b4ae | 174 | uint16_t tileFromCoords (int16_t x,int16_t y) { |
Pokitto | 8:809b1982b4ae | 175 | if (x<0) x=0; |
Pokitto | 8:809b1982b4ae | 176 | if (y<0) y=0; |
Pokitto | 8:809b1982b4ae | 177 | uint16_t tile = (x/SUBDIVS)+(y/SUBDIVS)*gamelevel*MAPMULT; |
Pokitto | 8:809b1982b4ae | 178 | return tile; |
Pokitto | 8:809b1982b4ae | 179 | } |
Pokitto | 8:809b1982b4ae | 180 | |
Pokitto | 8:809b1982b4ae | 181 | coords tileToScreenXY (uint16_t tile) { |
Pokitto | 8:809b1982b4ae | 182 | coords s; |
Pokitto | 8:809b1982b4ae | 183 | uint16_t x = tile % worldsize; |
Pokitto | 8:809b1982b4ae | 184 | uint16_t y = tile / worldsize; |
Pokitto | 8:809b1982b4ae | 185 | s.y = y*YOFF+wy+(3+x)*YOFF+2+getTileHeight(tile); |
Pokitto | 8:809b1982b4ae | 186 | s.x = x*XOFF+wx+2*XOFF+2; |
Pokitto | 8:809b1982b4ae | 187 | return s; |
Pokitto | 8:809b1982b4ae | 188 | } |
Pokitto | 8:809b1982b4ae | 189 | |
Pokitto | 8:809b1982b4ae | 190 | uint16_t screenToTile (int16_t x, int16_t y) { |
Pokitto | 8:809b1982b4ae | 191 | /* tile 0 is at wx,wy in screen space */ |
Pokitto | 8:809b1982b4ae | 192 | int16_t distx = x-wx; |
Pokitto | 8:809b1982b4ae | 193 | int16_t disty = y-wy; |
Pokitto | 8:809b1982b4ae | 194 | uint16_t tilesx,tilesy,tx,ty; |
Pokitto | 8:809b1982b4ae | 195 | if (distx>0) { |
Pokitto | 8:809b1982b4ae | 196 | // point is to the right of 0 tile |
Pokitto | 8:809b1982b4ae | 197 | tilesx = distx/XOFF; //number of tiles moved right, XOFF = half width of tile |
Pokitto | 8:809b1982b4ae | 198 | tilesy = disty/YOFF; //num tiles downwards |
Pokitto | 8:809b1982b4ae | 199 | tx = tilesx + tilesy; // this actually gives on an isometric grid, the x row of tiles ! |
Pokitto | 8:809b1982b4ae | 200 | |
Pokitto | 8:809b1982b4ae | 201 | } else { |
Pokitto | 8:809b1982b4ae | 202 | // point is to the left of 0 tile |
Pokitto | 8:809b1982b4ae | 203 | distx = -distx; |
Pokitto | 8:809b1982b4ae | 204 | tilesx = distx/XOFF; //number of tiles moved right |
Pokitto | 8:809b1982b4ae | 205 | tilesy = disty/YOFF; //num tiles downwards |
Pokitto | 8:809b1982b4ae | 206 | } |
Pokitto | 8:809b1982b4ae | 207 | /* calculate how many tiles fit in between */ |
Pokitto | 8:809b1982b4ae | 208 | |
Pokitto | 8:809b1982b4ae | 209 | |
Pokitto | 8:809b1982b4ae | 210 | int32_t tilex = (x - wx - 2 - XOFF)/XOFF; |
Pokitto | 8:809b1982b4ae | 211 | int32_t tiley = (y-wy-2); |
Pokitto | 8:809b1982b4ae | 212 | tiley -=(3+tilex)*YOFF; |
Pokitto | 8:809b1982b4ae | 213 | tiley /= YOFF; |
Pokitto | 8:809b1982b4ae | 214 | return tilex+tiley*worldsize; |
Pokitto | 8:809b1982b4ae | 215 | } |
Pokitto | 8:809b1982b4ae | 216 | |
Pokitto | 8:809b1982b4ae | 217 | uint16_t worldToTile (uint16_t x, uint16_t y) { |
Pokitto | 8:809b1982b4ae | 218 | return (x/SUBDIVS)+(y/SUBDIVS)*worldsize; |
Pokitto | 8:809b1982b4ae | 219 | } |
Pokitto | 8:809b1982b4ae | 220 | |
Pokitto | 8:809b1982b4ae | 221 | coords worldToScreen(uint16_t x, uint16_t y) { |
Pokitto | 8:809b1982b4ae | 222 | uint16_t scry =(wy+y/SUBDIVS*4)+(x/SUBDIVS+1)*YOFF; |
Pokitto | 8:809b1982b4ae | 223 | uint16_t scrx = wx+(x/SUBDIVS+2)*XOFF-y/SUBDIVS*8; |
Pokitto | 8:809b1982b4ae | 224 | int16_t oy,ox; |
Pokitto | 8:809b1982b4ae | 225 | int16_t offsety,offsetx; |
Pokitto | 8:809b1982b4ae | 226 | offsety = y%SUBDIVS; |
Pokitto | 8:809b1982b4ae | 227 | offsetx = x%SUBDIVS; |
Pokitto | 8:809b1982b4ae | 228 | oy = -offsetx*YOFF/SUBDIVS - offsety*YOFF/SUBDIVS; |
Pokitto | 8:809b1982b4ae | 229 | ox = offsetx*XOFF/SUBDIVS - offsety*8/SUBDIVS; |
Pokitto | 8:809b1982b4ae | 230 | coords t; |
Pokitto | 8:809b1982b4ae | 231 | t.x = scrx+ox; t.y = scry-oy; |
Pokitto | 8:809b1982b4ae | 232 | return t; |
Pokitto | 8:809b1982b4ae | 233 | } |
Pokitto | 8:809b1982b4ae | 234 | |
Pokitto | 8:809b1982b4ae | 235 | bool onScreen(coords p) { |
Pokitto | 8:809b1982b4ae | 236 | coords t = worldToScreen(p.x,p.y); |
Pokitto | 8:809b1982b4ae | 237 | if (t.x>=0 && t.x<110 && t.y > 0 && t.y <88) return true; |
Pokitto | 8:809b1982b4ae | 238 | return false; |
Pokitto | 8:809b1982b4ae | 239 | } |
Pokitto | 8:809b1982b4ae | 240 | |
Pokitto | 8:809b1982b4ae | 241 | bool focusOnTile(uint16_t t) { |
Pokitto | 8:809b1982b4ae | 242 | if (!focusing || t!= focustile) { |
Pokitto | 8:809b1982b4ae | 243 | focusing = true; |
Pokitto | 8:809b1982b4ae | 244 | focustile = t; |
Pokitto | 8:809b1982b4ae | 245 | focus.x = t % worldsize; |
Pokitto | 8:809b1982b4ae | 246 | focus.y = t / worldsize; |
Pokitto | 8:809b1982b4ae | 247 | } |
Pokitto | 8:809b1982b4ae | 248 | uint16_t currentfocus = centertile;//screenToTile(55,44); |
Pokitto | 8:809b1982b4ae | 249 | if (currentfocus > focustile-3 && currentfocus <focustile+3) { |
Pokitto | 8:809b1982b4ae | 250 | focusing = false; |
Pokitto | 8:809b1982b4ae | 251 | return true; |
Pokitto | 8:809b1982b4ae | 252 | } |
Pokitto | 8:809b1982b4ae | 253 | int16_t x = currentfocus % worldsize; |
Pokitto | 8:809b1982b4ae | 254 | int16_t y = currentfocus / worldsize; |
Pokitto | 8:809b1982b4ae | 255 | int16_t dx = x - focus.x; |
Pokitto | 8:809b1982b4ae | 256 | int16_t dy = y - focus.y; |
Pokitto | 8:809b1982b4ae | 257 | if (dx<0) dx = -dx; |
Pokitto | 8:809b1982b4ae | 258 | if (dy<0) dy = -dy; |
Pokitto | 8:809b1982b4ae | 259 | if (dx+dy>20) dx = XSPEED*8; |
Pokitto | 8:809b1982b4ae | 260 | else if (dx+dy>5) dx = XSPEED*2; |
Pokitto | 8:809b1982b4ae | 261 | else dx=XSPEED; |
Pokitto | 8:809b1982b4ae | 262 | if (x<focus.x) wx-=dx; |
Pokitto | 8:809b1982b4ae | 263 | else if (x>focus.x) wx += dx; |
Pokitto | 8:809b1982b4ae | 264 | if (y<focus.y) wy-= dx; |
Pokitto | 8:809b1982b4ae | 265 | else if (y>focus.y) wy+= dx; |
Pokitto | 8:809b1982b4ae | 266 | return false; |
Pokitto | 8:809b1982b4ae | 267 | /*if (gb.buttons.upBtn()) { |
Pokitto | 8:809b1982b4ae | 268 | wy+=YSPEED;wx+=XSPEED;} |
Pokitto | 8:809b1982b4ae | 269 | if (gb.buttons.downBtn()) {wy-=YSPEED;wx-=XSPEED;} |
Pokitto | 8:809b1982b4ae | 270 | if (gb.buttons.leftBtn()) {wy-=YSPEED;wx+=XSPEED;} |
Pokitto | 8:809b1982b4ae | 271 | if (gb.buttons.rightBtn()) {wy+=YSPEED;wx-=XSPEED;}*/ |
Pokitto | 8:809b1982b4ae | 272 | } |
Pokitto | 8:809b1982b4ae | 273 | |
Pokitto | 8:809b1982b4ae | 274 | void drawToTile(uint16_t tile, const uint8_t* bitmap, int16_t offsetx, int16_t offsety, uint8_t flip) { |
Pokitto | 8:809b1982b4ae | 275 | uint16_t tilex,tiley; |
Pokitto | 8:809b1982b4ae | 276 | tiley = tile/worldsize; |
Pokitto | 8:809b1982b4ae | 277 | tilex = tile%worldsize; |
Pokitto | 8:809b1982b4ae | 278 | uint16_t scry=(wy+tiley*4)+(tilex+1)*YOFF; |
Pokitto | 8:809b1982b4ae | 279 | uint16_t scrx=wx+(tilex+2)*XOFF-tiley*8; |
Pokitto | 8:809b1982b4ae | 280 | int16_t oy,ox; |
Pokitto | 8:809b1982b4ae | 281 | oy = -offsetx*YOFF/SUBDIVS - offsety*YOFF/SUBDIVS+4; |
Pokitto | 8:809b1982b4ae | 282 | ox = offsetx*XOFF/SUBDIVS - offsety*8/SUBDIVS-3; |
Pokitto | 8:809b1982b4ae | 283 | int8_t t=0; |
Pokitto | 8:809b1982b4ae | 284 | if (!wading) t=-random(10); |
Pokitto | 8:809b1982b4ae | 285 | gb.display.drawBitmap(scrx+ox+t,scry-(getTileHeight(tile)<<1)-oy+t,bitmap,0,flip); |
Pokitto | 8:809b1982b4ae | 286 | } |
Pokitto | 8:809b1982b4ae | 287 | |
Pokitto | 8:809b1982b4ae | 288 | bool canMoveTo(uint16_t i) { |
Pokitto | 8:809b1982b4ae | 289 | if (getTileHeightToTile(i,prevtile)>MAXSTEP) return false; |
Pokitto | 8:809b1982b4ae | 290 | if (getTileType(i)==TER_HUT && humans[0].facing != NE ) return false; |
Pokitto | 8:809b1982b4ae | 291 | if (getTileType(prevtile)==TER_HUT && humans[0].facing != SW ) return false; |
Pokitto | 8:809b1982b4ae | 292 | if (getTileType(i)==TER_TREE) return false; |
Pokitto | 8:809b1982b4ae | 293 | //if (getTileType(i)==TER_WATER) return false; |
Pokitto | 8:809b1982b4ae | 294 | if (getTileType(i)==TER_STONE) return false; |
Pokitto | 8:809b1982b4ae | 295 | return true; |
Pokitto | 8:809b1982b4ae | 296 | } |
Pokitto | 8:809b1982b4ae | 297 | |
Pokitto | 8:809b1982b4ae | 298 | bool canWWMoveFromTo(uint16_t i,uint16_t j) { |
Pokitto | 8:809b1982b4ae | 299 | if (getTileHeightToTile(j,i)>MAXWOLFSTEP) return false; |
Pokitto | 8:809b1982b4ae | 300 | if (getTileType(j)==TER_HUT) return false; |
Pokitto | 8:809b1982b4ae | 301 | //if (getTileType(i)==TER_WATER) return false; |
Pokitto | 8:809b1982b4ae | 302 | //if (getTileType(i)==TER_STONE) return false; |
Pokitto | 8:809b1982b4ae | 303 | return true; |
Pokitto | 8:809b1982b4ae | 304 | } |
Pokitto | 8:809b1982b4ae | 305 | |
Pokitto | 8:809b1982b4ae | 306 | int8_t getTileDX(uint16_t i) { |
Pokitto | 8:809b1982b4ae | 307 | int8_t h_this = getTileHeight(i); |
Pokitto | 8:809b1982b4ae | 308 | uint16_t y = i / worldsize; |
Pokitto | 8:809b1982b4ae | 309 | uint16_t x = i % worldsize; |
Pokitto | 8:809b1982b4ae | 310 | int8_t h_prev = h_this, h_next=h_this ;// assume flat in case we are at the edge |
Pokitto | 8:809b1982b4ae | 311 | if (x>0) h_prev = getTileHeight(i-1); |
Pokitto | 8:809b1982b4ae | 312 | if (x<worldsize-1) h_next = getTileHeight(i+1); |
Pokitto | 8:809b1982b4ae | 313 | if (h_prev>h_next) return -1; //sloping down towards east |
Pokitto | 8:809b1982b4ae | 314 | else if (h_prev<h_next) return 1; // sloping down towards west |
Pokitto | 8:809b1982b4ae | 315 | return 0; // this axis is flat |
Pokitto | 8:809b1982b4ae | 316 | } |
Pokitto | 8:809b1982b4ae | 317 | |
Pokitto | 8:809b1982b4ae | 318 | int8_t getTileDY(uint16_t i) { |
Pokitto | 8:809b1982b4ae | 319 | int8_t h_this = getTileHeight(i); |
Pokitto | 8:809b1982b4ae | 320 | uint16_t y = i / worldsize; |
Pokitto | 8:809b1982b4ae | 321 | uint16_t x = i % worldsize; |
Pokitto | 8:809b1982b4ae | 322 | int8_t h_prev = h_this, h_next=h_this ;// assume flat in case we are at the edge |
Pokitto | 8:809b1982b4ae | 323 | if (y>0) h_next = getTileHeight(i-worldsize); |
Pokitto | 8:809b1982b4ae | 324 | if (y<worldsize-1) h_prev = getTileHeight(i+worldsize); |
Pokitto | 8:809b1982b4ae | 325 | if (h_prev<h_next) return 1; //rising up towards ne |
Pokitto | 8:809b1982b4ae | 326 | else if (h_prev>h_next) return -1; // sloping down towards ne (yes, this axis is mirrored) |
Pokitto | 8:809b1982b4ae | 327 | return 0; // this axis is flat |
Pokitto | 8:809b1982b4ae | 328 | } |
Pokitto | 8:809b1982b4ae | 329 | |
Pokitto | 8:809b1982b4ae | 330 | int8_t getTileHeightToTile(uint16_t a, uint16_t b) { |
Pokitto | 8:809b1982b4ae | 331 | int8_t c = getTileHeight(a); |
Pokitto | 8:809b1982b4ae | 332 | int8_t d = getTileHeight(b); |
Pokitto | 8:809b1982b4ae | 333 | return c-d; |
Pokitto | 8:809b1982b4ae | 334 | } |
Pokitto | 8:809b1982b4ae | 335 | |
Pokitto | 8:809b1982b4ae | 336 | uint8_t getTileSlope(uint16_t index) { |
Pokitto | 8:809b1982b4ae | 337 | int8_t dx=0,dy=0; |
Pokitto | 8:809b1982b4ae | 338 | dx=getTileDX(index); |
Pokitto | 8:809b1982b4ae | 339 | dy=getTileDY(index); |
Pokitto | 8:809b1982b4ae | 340 | switch (dy) { |
Pokitto | 8:809b1982b4ae | 341 | case 0: |
Pokitto | 8:809b1982b4ae | 342 | if (dx==0) return TER_PLAIN; |
Pokitto | 8:809b1982b4ae | 343 | else if (dx>0) return TER_BUMP_NW; |
Pokitto | 8:809b1982b4ae | 344 | return TER_BUMP_SE; |
Pokitto | 8:809b1982b4ae | 345 | break; |
Pokitto | 8:809b1982b4ae | 346 | case 1: |
Pokitto | 8:809b1982b4ae | 347 | if (dx==0) return TER_BUMP_SW; |
Pokitto | 8:809b1982b4ae | 348 | else if (dx>0) return TER_BUMP_W; |
Pokitto | 8:809b1982b4ae | 349 | return TER_BUMP_S; |
Pokitto | 8:809b1982b4ae | 350 | break; |
Pokitto | 8:809b1982b4ae | 351 | case -1: |
Pokitto | 8:809b1982b4ae | 352 | if (dx==0) return TER_BUMP_NE; |
Pokitto | 8:809b1982b4ae | 353 | else if (dx>0) return TER_BUMP_N; |
Pokitto | 8:809b1982b4ae | 354 | return TER_BUMP_E; |
Pokitto | 8:809b1982b4ae | 355 | break; |
Pokitto | 8:809b1982b4ae | 356 | } |
Pokitto | 8:809b1982b4ae | 357 | } |
Pokitto | 8:809b1982b4ae | 358 | |
Pokitto | 8:809b1982b4ae | 359 | void setSlopeAutomatic(uint16_t t) { |
Pokitto | 8:809b1982b4ae | 360 | uint8_t e = getTileType(t); // currently existing tile |
Pokitto | 8:809b1982b4ae | 361 | if (e<5 || e>12 && e != TER_PLAIN) return; // only change terrain formation tiles, not content |
Pokitto | 8:809b1982b4ae | 362 | uint8_t slope = getTileSlope(t); |
Pokitto | 8:809b1982b4ae | 363 | setTileType(t,slope); |
Pokitto | 8:809b1982b4ae | 364 | } |
Pokitto | 8:809b1982b4ae | 365 | |
Pokitto | 8:809b1982b4ae | 366 | bool recursiveGroundRaise(uint16_t tile) { |
Pokitto | 8:809b1982b4ae | 367 | uint16_t target=tile; |
Pokitto | 8:809b1982b4ae | 368 | if (tile>worldsize) { |
Pokitto | 8:809b1982b4ae | 369 | // Top row |
Pokitto | 8:809b1982b4ae | 370 | if (tile%worldsize) { |
Pokitto | 8:809b1982b4ae | 371 | target = target - worldsize - 1; //N tile |
Pokitto | 8:809b1982b4ae | 372 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); // recursive function, calls back to itself |
Pokitto | 8:809b1982b4ae | 373 | } |
Pokitto | 8:809b1982b4ae | 374 | target++; // NE tile |
Pokitto | 8:809b1982b4ae | 375 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 376 | if (tile%worldsize<worldsize-1) { |
Pokitto | 8:809b1982b4ae | 377 | target++; // E tile |
Pokitto | 8:809b1982b4ae | 378 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 379 | } |
Pokitto | 8:809b1982b4ae | 380 | } |
Pokitto | 8:809b1982b4ae | 381 | // Middle row |
Pokitto | 8:809b1982b4ae | 382 | if (tile%worldsize>0) { |
Pokitto | 8:809b1982b4ae | 383 | target = tile - 1; // NW tile |
Pokitto | 8:809b1982b4ae | 384 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 385 | } |
Pokitto | 8:809b1982b4ae | 386 | if (tile%worldsize<worldsize-1) { |
Pokitto | 8:809b1982b4ae | 387 | target+=2; //SE tile |
Pokitto | 8:809b1982b4ae | 388 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 389 | } |
Pokitto | 8:809b1982b4ae | 390 | // Bottom row |
Pokitto | 8:809b1982b4ae | 391 | if (tile<worldsize*worldsize-worldsize) { |
Pokitto | 8:809b1982b4ae | 392 | if (tile%worldsize>0) { |
Pokitto | 8:809b1982b4ae | 393 | target = tile + worldsize -1; // W tile |
Pokitto | 8:809b1982b4ae | 394 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 395 | } |
Pokitto | 8:809b1982b4ae | 396 | target++; // SW tile |
Pokitto | 8:809b1982b4ae | 397 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 398 | if (tile<worldsize*worldsize-1) { |
Pokitto | 8:809b1982b4ae | 399 | target++; //S tile |
Pokitto | 8:809b1982b4ae | 400 | if (getTileHeightToTile(target,tile)<-MAXELEVATIONDIFF) raiseTile(target); |
Pokitto | 8:809b1982b4ae | 401 | } |
Pokitto | 8:809b1982b4ae | 402 | } |
Pokitto | 8:809b1982b4ae | 403 | |
Pokitto | 8:809b1982b4ae | 404 | |
Pokitto | 8:809b1982b4ae | 405 | return false; |
Pokitto | 8:809b1982b4ae | 406 | } |
Pokitto | 8:809b1982b4ae | 407 | |
Pokitto | 8:809b1982b4ae | 408 | bool raiseTile(uint16_t t) { |
Pokitto | 8:809b1982b4ae | 409 | int8_t z = getTileHeight(t); |
Pokitto | 8:809b1982b4ae | 410 | if (z==0) setTileType(t,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 411 | z++; |
Pokitto | 8:809b1982b4ae | 412 | if (z>7) z=7; |
Pokitto | 8:809b1982b4ae | 413 | setTileHeight(t,z); |
Pokitto | 8:809b1982b4ae | 414 | recursiveGroundRaise(t); |
Pokitto | 8:809b1982b4ae | 415 | if (z==7) digCount = DIGMAX; |
Pokitto | 8:809b1982b4ae | 416 | return true; |
Pokitto | 8:809b1982b4ae | 417 | } |
Pokitto | 8:809b1982b4ae | 418 | |
Pokitto | 8:809b1982b4ae | 419 | uint16_t getFacingTile(uint16_t tile, walkDir facing) { |
Pokitto | 8:809b1982b4ae | 420 | switch (facing) { |
Pokitto | 8:809b1982b4ae | 421 | case NE: |
Pokitto | 8:809b1982b4ae | 422 | if (tile<worldsize) return -1; //out of bounds |
Pokitto | 8:809b1982b4ae | 423 | return tile-worldsize; |
Pokitto | 8:809b1982b4ae | 424 | break; |
Pokitto | 8:809b1982b4ae | 425 | case SE: |
Pokitto | 8:809b1982b4ae | 426 | if (tile%worldsize == worldsize-1) return -1; //out of bounds |
Pokitto | 8:809b1982b4ae | 427 | return tile+1; |
Pokitto | 8:809b1982b4ae | 428 | case SW: |
Pokitto | 8:809b1982b4ae | 429 | if (tile>worldsize*worldsize-worldsize) return -1; |
Pokitto | 8:809b1982b4ae | 430 | return tile+worldsize; |
Pokitto | 8:809b1982b4ae | 431 | case NW: |
Pokitto | 8:809b1982b4ae | 432 | if (tile%worldsize == 0) return -1; |
Pokitto | 8:809b1982b4ae | 433 | return tile-1; |
Pokitto | 8:809b1982b4ae | 434 | } |
Pokitto | 8:809b1982b4ae | 435 | return -1; |
Pokitto | 8:809b1982b4ae | 436 | } |
Pokitto | 8:809b1982b4ae | 437 | |
Pokitto | 8:809b1982b4ae | 438 | void spawnWolf() { |
Pokitto | 8:809b1982b4ae | 439 | for (uint8_t i = 0; i< (gamelevel-1) * WOLFMULT ; i++) { |
Pokitto | 8:809b1982b4ae | 440 | if (werewolves[i].energy <= 0) { |
Pokitto | 8:809b1982b4ae | 441 | uint16_t k; |
Pokitto | 8:809b1982b4ae | 442 | k=random(worldsize*worldsize-1); |
Pokitto | 8:809b1982b4ae | 443 | uint8_t a = (uint8_t)random(3)-1; |
Pokitto | 8:809b1982b4ae | 444 | switch (a) { |
Pokitto | 8:809b1982b4ae | 445 | case 0: |
Pokitto | 8:809b1982b4ae | 446 | werewolves[i].ai = HOMING; break; |
Pokitto | 8:809b1982b4ae | 447 | case 1: |
Pokitto | 8:809b1982b4ae | 448 | werewolves[i].ai = CHASE; break; |
Pokitto | 8:809b1982b4ae | 449 | case 2: |
Pokitto | 8:809b1982b4ae | 450 | werewolves[i].ai = INTERCEPT; break; |
Pokitto | 8:809b1982b4ae | 451 | } |
Pokitto | 8:809b1982b4ae | 452 | |
Pokitto | 8:809b1982b4ae | 453 | werewolves[i].energy = WOLFENERGY; |
Pokitto | 8:809b1982b4ae | 454 | werewolves[i].tile = k; |
Pokitto | 8:809b1982b4ae | 455 | k *= SUBDIVS; |
Pokitto | 8:809b1982b4ae | 456 | werewolves[i].position.x = k%worldsize; |
Pokitto | 8:809b1982b4ae | 457 | werewolves[i].position.y = k/worldsize; |
Pokitto | 8:809b1982b4ae | 458 | numwolves++; |
Pokitto | 8:809b1982b4ae | 459 | i=(gamelevel-1)*WOLFMULT; break; |
Pokitto | 8:809b1982b4ae | 460 | } |
Pokitto | 8:809b1982b4ae | 461 | } |
Pokitto | 8:809b1982b4ae | 462 | } |
Pokitto | 8:809b1982b4ae | 463 | |
Pokitto | 8:809b1982b4ae | 464 | void despawnWolf(uint8_t n) { |
Pokitto | 8:809b1982b4ae | 465 | werewolves[n].energy=0; |
Pokitto | 8:809b1982b4ae | 466 | werewolves[n].visible=false; |
Pokitto | 8:809b1982b4ae | 467 | numwolves--; |
Pokitto | 8:809b1982b4ae | 468 | } |
Pokitto | 8:809b1982b4ae | 469 | |
Pokitto | 8:809b1982b4ae | 470 | void despawnAllWolves() { |
Pokitto | 8:809b1982b4ae | 471 | for (uint8_t i = 0; i< (gamelevel-1)*WOLFMULT ; i++) { |
Pokitto | 8:809b1982b4ae | 472 | despawnWolf(i); |
Pokitto | 8:809b1982b4ae | 473 | } |
Pokitto | 8:809b1982b4ae | 474 | } |
Pokitto | 8:809b1982b4ae | 475 | |
Pokitto | 8:809b1982b4ae | 476 | void moveWolves() { |
Pokitto | 8:809b1982b4ae | 477 | for (uint8_t i = 0; i< (gamelevel-1)*WOLFMULT ; i++) { |
Pokitto | 8:809b1982b4ae | 478 | if (werewolves[i].energy>0) { |
Pokitto | 8:809b1982b4ae | 479 | if (wolvestick) { |
Pokitto | 8:809b1982b4ae | 480 | uint16_t fromtile = werewolves[i].tile; |
Pokitto | 8:809b1982b4ae | 481 | coords fromposition = werewolves[i].position; |
Pokitto | 8:809b1982b4ae | 482 | int16_t aix,aiy,bix,biy; |
Pokitto | 8:809b1982b4ae | 483 | switch (werewolves[i].ai) { |
Pokitto | 8:809b1982b4ae | 484 | case INTERCEPT: |
Pokitto | 8:809b1982b4ae | 485 | if (humans[i].facing==NE) {werewolves[i].position.y--; werewolves[i].animframe = 1 - werewolves[i].animframe; werewolves[i].facing = SE;} |
Pokitto | 8:809b1982b4ae | 486 | if (humans[i].facing==SE) {werewolves[i].position.x++; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NW;} |
Pokitto | 8:809b1982b4ae | 487 | if (humans[i].facing==SW) {werewolves[i].position.y++; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = SW;} |
Pokitto | 8:809b1982b4ae | 488 | if (humans[i].facing==NW) {werewolves[i].position.x--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NE;} |
Pokitto | 8:809b1982b4ae | 489 | if (xorshift8()>0xf0) { |
Pokitto | 8:809b1982b4ae | 490 | uint8_t a = (uint8_t)random(3); |
Pokitto | 8:809b1982b4ae | 491 | switch (a) { |
Pokitto | 8:809b1982b4ae | 492 | case 0: |
Pokitto | 8:809b1982b4ae | 493 | werewolves[i].ai = HOMING; break; |
Pokitto | 8:809b1982b4ae | 494 | case 1: |
Pokitto | 8:809b1982b4ae | 495 | werewolves[i].ai = CHASE; break; |
Pokitto | 8:809b1982b4ae | 496 | case 2: |
Pokitto | 8:809b1982b4ae | 497 | werewolves[i].ai = INTERCEPT; break; |
Pokitto | 8:809b1982b4ae | 498 | case 3: |
Pokitto | 8:809b1982b4ae | 499 | werewolves[i].ai = TURNCW; break; |
Pokitto | 8:809b1982b4ae | 500 | |
Pokitto | 8:809b1982b4ae | 501 | |
Pokitto | 8:809b1982b4ae | 502 | } |
Pokitto | 8:809b1982b4ae | 503 | } |
Pokitto | 8:809b1982b4ae | 504 | break; |
Pokitto | 8:809b1982b4ae | 505 | case CHASE: |
Pokitto | 8:809b1982b4ae | 506 | if (werewolves[i].position.x<oldplayer.x) {werewolves[i].position.x++; werewolves[i].animframe = 1 - werewolves[i].animframe; werewolves[i].facing = SE;} |
Pokitto | 8:809b1982b4ae | 507 | if (werewolves[i].position.x>oldplayer.x) {werewolves[i].position.x--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NW;} |
Pokitto | 8:809b1982b4ae | 508 | if (werewolves[i].position.y<oldplayer.y) {werewolves[i].position.y++; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = SW;} |
Pokitto | 8:809b1982b4ae | 509 | if (werewolves[i].position.y>oldplayer.y) {werewolves[i].position.y--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NE;} |
Pokitto | 8:809b1982b4ae | 510 | aiy = werewolves[i].position.y - player.y; |
Pokitto | 8:809b1982b4ae | 511 | aix = werewolves[i].position.x - player.x; |
Pokitto | 8:809b1982b4ae | 512 | if (aix<0) aix = -aix; |
Pokitto | 8:809b1982b4ae | 513 | if (aiy<0) aiy = -aiy; |
Pokitto | 8:809b1982b4ae | 514 | if (aix<30 || aiy <30) werewolves[i].ai = HOMING; |
Pokitto | 8:809b1982b4ae | 515 | break; |
Pokitto | 8:809b1982b4ae | 516 | case TURNCW: |
Pokitto | 8:809b1982b4ae | 517 | werewolves[i].goingto.x = werewolves[i].position.x+random(48)-24; |
Pokitto | 8:809b1982b4ae | 518 | werewolves[i].goingto.y = werewolves[i].position.y+random(48)-24; |
Pokitto | 8:809b1982b4ae | 519 | while (worldToTile(werewolves[i].goingto.x,werewolves[i].goingto.y)>worldsize*worldsize-1) { |
Pokitto | 8:809b1982b4ae | 520 | werewolves[i].goingto.x = werewolves[i].position.x+random(48)-24; |
Pokitto | 8:809b1982b4ae | 521 | werewolves[i].goingto.y = werewolves[i].position.y+random(48)-24; |
Pokitto | 8:809b1982b4ae | 522 | } |
Pokitto | 8:809b1982b4ae | 523 | werewolves[i].ai = GOTO; |
Pokitto | 8:809b1982b4ae | 524 | break; |
Pokitto | 8:809b1982b4ae | 525 | case GOTO: |
Pokitto | 8:809b1982b4ae | 526 | if (werewolves[i].position.x<werewolves[i].goingto.x) {werewolves[i].position.x++; werewolves[i].animframe = 1 - werewolves[i].animframe; werewolves[i].facing = SE;} |
Pokitto | 8:809b1982b4ae | 527 | if (werewolves[i].position.x>werewolves[i].goingto.x) {werewolves[i].position.x--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NW;} |
Pokitto | 8:809b1982b4ae | 528 | if (werewolves[i].position.y<werewolves[i].goingto.y) {werewolves[i].position.y++; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = SW;} |
Pokitto | 8:809b1982b4ae | 529 | if (werewolves[i].position.y>werewolves[i].goingto.y) {werewolves[i].position.y--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NE;} |
Pokitto | 8:809b1982b4ae | 530 | if (werewolves[i].position.x==werewolves[i].goingto.x && werewolves[i].position.y==werewolves[i].goingto.y) werewolves[i].ai = HOMING; |
Pokitto | 8:809b1982b4ae | 531 | break; |
Pokitto | 8:809b1982b4ae | 532 | case HOMING: |
Pokitto | 8:809b1982b4ae | 533 | if (werewolves[i].position.x<player.x) {werewolves[i].position.x++; werewolves[i].animframe = 1 - werewolves[i].animframe; werewolves[i].facing = SE;} |
Pokitto | 8:809b1982b4ae | 534 | if (werewolves[i].position.x>player.x) {werewolves[i].position.x--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NW;} |
Pokitto | 8:809b1982b4ae | 535 | if (werewolves[i].position.y<player.y) {werewolves[i].position.y++; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = SW;} |
Pokitto | 8:809b1982b4ae | 536 | if (werewolves[i].position.y>player.y) {werewolves[i].position.y--; werewolves[i].animframe = 1 - werewolves[i].animframe;werewolves[i].facing = NE;} |
Pokitto | 8:809b1982b4ae | 537 | break; |
Pokitto | 8:809b1982b4ae | 538 | } |
Pokitto | 8:809b1982b4ae | 539 | werewolves[i].tile = worldToTile(werewolves[i].position.x,werewolves[i].position.y); |
Pokitto | 8:809b1982b4ae | 540 | if (canWWMoveFromTo(fromtile,werewolves[i].tile)==false) { |
Pokitto | 8:809b1982b4ae | 541 | werewolves[i].position=fromposition; |
Pokitto | 8:809b1982b4ae | 542 | werewolves[i].tile= fromtile; |
Pokitto | 8:809b1982b4ae | 543 | uint8_t a = (uint8_t)random(3); |
Pokitto | 8:809b1982b4ae | 544 | switch (a) { |
Pokitto | 8:809b1982b4ae | 545 | case 0: |
Pokitto | 8:809b1982b4ae | 546 | werewolves[i].ai = HOMING; break; |
Pokitto | 8:809b1982b4ae | 547 | case 1: |
Pokitto | 8:809b1982b4ae | 548 | werewolves[i].ai = CHASE; break; |
Pokitto | 8:809b1982b4ae | 549 | case 2: |
Pokitto | 8:809b1982b4ae | 550 | werewolves[i].ai = INTERCEPT; break; |
Pokitto | 8:809b1982b4ae | 551 | case 3: |
Pokitto | 8:809b1982b4ae | 552 | werewolves[i].ai = TURNCW; break; |
Pokitto | 8:809b1982b4ae | 553 | } |
Pokitto | 8:809b1982b4ae | 554 | } |
Pokitto | 8:809b1982b4ae | 555 | } |
Pokitto | 8:809b1982b4ae | 556 | if (werewolves[i].position.x>(worldsize-1)*SUBDIVS) werewolves[i].position.x=(worldsize-1)*SUBDIVS; |
Pokitto | 8:809b1982b4ae | 557 | if (werewolves[i].position.y>(worldsize-1)*SUBDIVS) werewolves[i].position.y=(worldsize-1)*SUBDIVS; |
Pokitto | 8:809b1982b4ae | 558 | if (werewolves[i].position.x<0) werewolves[i].position.x=1; |
Pokitto | 8:809b1982b4ae | 559 | if (werewolves[i].position.y<0) werewolves[i].position.y=1; |
Pokitto | 8:809b1982b4ae | 560 | if (onScreen(werewolves[i].position)) werewolves[i].visible = true; |
Pokitto | 8:809b1982b4ae | 561 | else werewolves[i].visible = false; |
Pokitto | 8:809b1982b4ae | 562 | werewolves[i].energy-= monsterdrain; |
Pokitto | 8:809b1982b4ae | 563 | if (werewolves[i].energy<=0) despawnWolf(i); |
Pokitto | 8:809b1982b4ae | 564 | } else werewolves[i].visible=false; |
Pokitto | 8:809b1982b4ae | 565 | } |
Pokitto | 8:809b1982b4ae | 566 | } |
Pokitto | 8:809b1982b4ae | 567 | |
Pokitto | 8:809b1982b4ae | 568 | void initworld() { |
Pokitto | 8:809b1982b4ae | 569 | timeofday=0; //value>>8 0 is full daytime, 127 is full night |
Pokitto | 8:809b1982b4ae | 570 | wave=0; oldtime=0; wolvestick=0;watertick=0; |
Pokitto | 8:809b1982b4ae | 571 | playergolds=0; |
Pokitto | 8:809b1982b4ae | 572 | wading=1; |
Pokitto | 8:809b1982b4ae | 573 | focusing=false; |
Pokitto | 8:809b1982b4ae | 574 | inHut = false; |
Pokitto | 8:809b1982b4ae | 575 | worldsize = 10+gamelevel*MAPMULT; |
Pokitto | 8:809b1982b4ae | 576 | caught=false; humans[0].energy=0xFFF;golds=0;playergolds=0; |
Pokitto | 8:809b1982b4ae | 577 | gb.display.load565Palette(pokulous_pal); |
Pokitto | 8:809b1982b4ae | 578 | timeofday=random(0x3FFF); tweenfactor=0; golds=0; |
Pokitto | 8:809b1982b4ae | 579 | player.x=5*SUBDIVS; |
Pokitto | 8:809b1982b4ae | 580 | player.y=0; |
Pokitto | 8:809b1982b4ae | 581 | uint16_t i = 0; |
Pokitto | 8:809b1982b4ae | 582 | /** Empty the world **/ |
Pokitto | 8:809b1982b4ae | 583 | for (i=0;i<worldsize*worldsize;i++) { |
Pokitto | 8:809b1982b4ae | 584 | setTileHeight(i,0); |
Pokitto | 8:809b1982b4ae | 585 | setTileType(i,TER_UNDEFINED); |
Pokitto | 8:809b1982b4ae | 586 | } |
Pokitto | 8:809b1982b4ae | 587 | |
Pokitto | 8:809b1982b4ae | 588 | #define NUMPEAKS worldsize/10 |
Pokitto | 8:809b1982b4ae | 589 | uint16_t bigpeaks = NUMPEAKS*random(2)+NUMPEAKS*3; |
Pokitto | 8:809b1982b4ae | 590 | uint16_t smallpeaks = NUMPEAKS*random(2)+NUMPEAKS*30; |
Pokitto | 8:809b1982b4ae | 591 | uint16_t cliffs = NUMPEAKS*random(2)+NUMPEAKS*3; |
Pokitto | 8:809b1982b4ae | 592 | uint16_t shards = NUMPEAKS*random(4)+NUMPEAKS*30; |
Pokitto | 8:809b1982b4ae | 593 | |
Pokitto | 8:809b1982b4ae | 594 | switch (random(3)) { |
Pokitto | 8:809b1982b4ae | 595 | case 0: |
Pokitto | 8:809b1982b4ae | 596 | //normal ground |
Pokitto | 8:809b1982b4ae | 597 | break; |
Pokitto | 8:809b1982b4ae | 598 | case 1: |
Pokitto | 8:809b1982b4ae | 599 | // lots of land |
Pokitto | 8:809b1982b4ae | 600 | bigpeaks += NUMPEAKS*10; |
Pokitto | 8:809b1982b4ae | 601 | shards /= 10; |
Pokitto | 8:809b1982b4ae | 602 | smallpeaks /= 10; |
Pokitto | 8:809b1982b4ae | 603 | break; |
Pokitto | 8:809b1982b4ae | 604 | case 2: |
Pokitto | 8:809b1982b4ae | 605 | // tiny islands |
Pokitto | 8:809b1982b4ae | 606 | bigpeaks =0; |
Pokitto | 8:809b1982b4ae | 607 | cliffs /=10; |
Pokitto | 8:809b1982b4ae | 608 | shards =0; |
Pokitto | 8:809b1982b4ae | 609 | smallpeaks /= 10; |
Pokitto | 8:809b1982b4ae | 610 | break; |
Pokitto | 8:809b1982b4ae | 611 | case 3: |
Pokitto | 8:809b1982b4ae | 612 | // very rugged |
Pokitto | 8:809b1982b4ae | 613 | shards += NUMPEAKS*50; |
Pokitto | 8:809b1982b4ae | 614 | bigpeaks = 0; |
Pokitto | 8:809b1982b4ae | 615 | smallpeaks /=10; |
Pokitto | 8:809b1982b4ae | 616 | } |
Pokitto | 8:809b1982b4ae | 617 | |
Pokitto | 8:809b1982b4ae | 618 | /** Generate big peaks **/ |
Pokitto | 8:809b1982b4ae | 619 | for (uint8_t r=0; r<bigpeaks;r++) { |
Pokitto | 8:809b1982b4ae | 620 | uint16_t rx = random(worldsize-16)+8; |
Pokitto | 8:809b1982b4ae | 621 | uint16_t ry = random(worldsize-16)+8; |
Pokitto | 8:809b1982b4ae | 622 | uint8_t sx = random(NUMPEAKS)+1; |
Pokitto | 8:809b1982b4ae | 623 | uint8_t sy = random(NUMPEAKS)+1; |
Pokitto | 8:809b1982b4ae | 624 | uint8_t he = random(5)+2; |
Pokitto | 8:809b1982b4ae | 625 | for (uint8_t ty = 0; ty < sy; ty++) { |
Pokitto | 8:809b1982b4ae | 626 | for (uint8_t tx = 0; tx < sx; tx++) { |
Pokitto | 8:809b1982b4ae | 627 | uint32_t index = rx+tx+(ry+ty) * worldsize; |
Pokitto | 8:809b1982b4ae | 628 | if (index<worldsize*worldsize) { |
Pokitto | 8:809b1982b4ae | 629 | setTileHeight(index,he); |
Pokitto | 8:809b1982b4ae | 630 | setTileType(index,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 631 | } |
Pokitto | 8:809b1982b4ae | 632 | } |
Pokitto | 8:809b1982b4ae | 633 | } |
Pokitto | 8:809b1982b4ae | 634 | } |
Pokitto | 8:809b1982b4ae | 635 | /** Generate small peaks **/ |
Pokitto | 8:809b1982b4ae | 636 | for (uint16_t r=0; r<smallpeaks;r++) { |
Pokitto | 8:809b1982b4ae | 637 | uint16_t rx = random(worldsize-2)+1; |
Pokitto | 8:809b1982b4ae | 638 | uint16_t ry = random(worldsize-2)+1; |
Pokitto | 8:809b1982b4ae | 639 | uint8_t sx = random(3)+1; |
Pokitto | 8:809b1982b4ae | 640 | uint8_t sy = random(3)+1; |
Pokitto | 8:809b1982b4ae | 641 | uint8_t he = random(3)+1; |
Pokitto | 8:809b1982b4ae | 642 | for (uint8_t ty = 0; ty < sy; ty++) { |
Pokitto | 8:809b1982b4ae | 643 | for (uint8_t tx = 0; tx < sx; tx++) { |
Pokitto | 8:809b1982b4ae | 644 | uint32_t index = rx+tx+(ry+ty) * worldsize; |
Pokitto | 8:809b1982b4ae | 645 | if (index<worldsize*worldsize) { |
Pokitto | 8:809b1982b4ae | 646 | setTileHeight(index,he); |
Pokitto | 8:809b1982b4ae | 647 | setTileType(index,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 648 | } |
Pokitto | 8:809b1982b4ae | 649 | } |
Pokitto | 8:809b1982b4ae | 650 | } |
Pokitto | 8:809b1982b4ae | 651 | } |
Pokitto | 8:809b1982b4ae | 652 | /** generate slopes **/ |
Pokitto | 8:809b1982b4ae | 653 | int8_t level=7; |
Pokitto | 8:809b1982b4ae | 654 | while (level>-1) { |
Pokitto | 8:809b1982b4ae | 655 | i =0; |
Pokitto | 8:809b1982b4ae | 656 | for (int16_t y = 0; y<worldsize; y++) { |
Pokitto | 8:809b1982b4ae | 657 | for (int16_t x = 0; x<worldsize; x++, i++) { |
Pokitto | 8:809b1982b4ae | 658 | uint8_t a = getTileHeight(i); |
Pokitto | 8:809b1982b4ae | 659 | if (a==level) { |
Pokitto | 8:809b1982b4ae | 660 | if (a==0) setTileType(i,TER_WATER); // don't leave undefined tiles |
Pokitto | 8:809b1982b4ae | 661 | else { |
Pokitto | 8:809b1982b4ae | 662 | // set tile that is northwest(previous on x-axis) of this tile |
Pokitto | 8:809b1982b4ae | 663 | if ((x>0) && getTileType(i-1)==TER_UNDEFINED) {setTileType((i-1),TER_PLAIN); setTileHeight((i-1),level-1);} |
Pokitto | 8:809b1982b4ae | 664 | // set tile that is northeast (previous on logical y-axis) of this tile |
Pokitto | 8:809b1982b4ae | 665 | if ((y>0) && getTileType(i-worldsize)==TER_UNDEFINED) {setTileType((i-worldsize),TER_PLAIN); setTileHeight((i-worldsize),level-1);} |
Pokitto | 8:809b1982b4ae | 666 | // set tile that is southeast(next on x-axis) of this tile |
Pokitto | 8:809b1982b4ae | 667 | if ((x<worldsize-1) && getTileType(i+1)==TER_UNDEFINED) {setTileType((i+1),TER_PLAIN); setTileHeight((i+1),level-1);} |
Pokitto | 8:809b1982b4ae | 668 | // set tile that is southwest (next on logical y-axis) of this tile |
Pokitto | 8:809b1982b4ae | 669 | if ((y<worldsize-1) && getTileType(i+worldsize)==TER_UNDEFINED) {setTileType((i+worldsize),TER_PLAIN); setTileHeight((i+worldsize),level-1);} |
Pokitto | 8:809b1982b4ae | 670 | } |
Pokitto | 8:809b1982b4ae | 671 | } |
Pokitto | 8:809b1982b4ae | 672 | } |
Pokitto | 8:809b1982b4ae | 673 | } |
Pokitto | 8:809b1982b4ae | 674 | level--; |
Pokitto | 8:809b1982b4ae | 675 | } |
Pokitto | 8:809b1982b4ae | 676 | |
Pokitto | 8:809b1982b4ae | 677 | /** Generate cliffs **/ |
Pokitto | 8:809b1982b4ae | 678 | for (uint8_t r=0; r<cliffs;r++) { |
Pokitto | 8:809b1982b4ae | 679 | uint16_t rx = random(worldsize-16)+8; |
Pokitto | 8:809b1982b4ae | 680 | uint16_t ry = random(worldsize-16)+8; |
Pokitto | 8:809b1982b4ae | 681 | uint8_t sx = random(NUMPEAKS)+1; |
Pokitto | 8:809b1982b4ae | 682 | uint8_t sy = random(NUMPEAKS)+1; |
Pokitto | 8:809b1982b4ae | 683 | uint8_t he = random(5)+2; |
Pokitto | 8:809b1982b4ae | 684 | for (uint8_t ty = 0; ty < sy; ty++) { |
Pokitto | 8:809b1982b4ae | 685 | for (uint8_t tx = 0; tx < sx; tx++) { |
Pokitto | 8:809b1982b4ae | 686 | uint32_t index = rx+tx+(ry+ty) * worldsize; |
Pokitto | 8:809b1982b4ae | 687 | if (index<worldsize*worldsize) { |
Pokitto | 8:809b1982b4ae | 688 | setTileHeight(index,he); |
Pokitto | 8:809b1982b4ae | 689 | setTileType(index,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 690 | } |
Pokitto | 8:809b1982b4ae | 691 | } |
Pokitto | 8:809b1982b4ae | 692 | } |
Pokitto | 8:809b1982b4ae | 693 | } |
Pokitto | 8:809b1982b4ae | 694 | |
Pokitto | 8:809b1982b4ae | 695 | /** soften cliffs **/ |
Pokitto | 8:809b1982b4ae | 696 | level=7; int8_t edge = random(6)+1; |
Pokitto | 8:809b1982b4ae | 697 | while (level>edge) { |
Pokitto | 8:809b1982b4ae | 698 | i =0; |
Pokitto | 8:809b1982b4ae | 699 | for (int16_t y = 0; y<worldsize; y++) { |
Pokitto | 8:809b1982b4ae | 700 | for (int16_t x = 0; x<worldsize; x++, i++) { |
Pokitto | 8:809b1982b4ae | 701 | uint8_t a = getTileHeight(i); |
Pokitto | 8:809b1982b4ae | 702 | if (a==level) { |
Pokitto | 8:809b1982b4ae | 703 | if (a==0) setTileType(i,TER_WATER); // don't leave undefined tiles |
Pokitto | 8:809b1982b4ae | 704 | else { |
Pokitto | 8:809b1982b4ae | 705 | // set tile that is northwest(previous on x-axis) of this tile |
Pokitto | 8:809b1982b4ae | 706 | if ((x>0) && getTileType(i-1)==TER_UNDEFINED) {setTileType((i-1),TER_PLAIN); setTileHeight((i-1),level-1);} |
Pokitto | 8:809b1982b4ae | 707 | // set tile that is northeast (previous on logical y-axis) of this tile |
Pokitto | 8:809b1982b4ae | 708 | if ((y>0) && getTileType(i-worldsize)==TER_UNDEFINED) {setTileType((i-worldsize),TER_PLAIN); setTileHeight((i-worldsize),level-1);} |
Pokitto | 8:809b1982b4ae | 709 | // set tile that is southeast(next on x-axis) of this tile |
Pokitto | 8:809b1982b4ae | 710 | if ((x<worldsize-1) && getTileType(i+1)==TER_UNDEFINED) {setTileType((i+1),TER_PLAIN); setTileHeight((i+1),level-1);} |
Pokitto | 8:809b1982b4ae | 711 | // set tile that is southwest (next on logical y-axis) of this tile |
Pokitto | 8:809b1982b4ae | 712 | if ((y<worldsize-1) && getTileType(i+worldsize)==TER_UNDEFINED) {setTileType((i+worldsize),TER_PLAIN); setTileHeight((i+worldsize),level-1);} |
Pokitto | 8:809b1982b4ae | 713 | } |
Pokitto | 8:809b1982b4ae | 714 | } |
Pokitto | 8:809b1982b4ae | 715 | } |
Pokitto | 8:809b1982b4ae | 716 | } |
Pokitto | 8:809b1982b4ae | 717 | level--; |
Pokitto | 8:809b1982b4ae | 718 | } |
Pokitto | 8:809b1982b4ae | 719 | |
Pokitto | 8:809b1982b4ae | 720 | /** Generate small cliffs **/ |
Pokitto | 8:809b1982b4ae | 721 | for (uint16_t r=0; r<shards;r++) { |
Pokitto | 8:809b1982b4ae | 722 | uint16_t rx = random(worldsize-2)+1; |
Pokitto | 8:809b1982b4ae | 723 | uint16_t ry = random(worldsize-2)+1; |
Pokitto | 8:809b1982b4ae | 724 | uint8_t sx = random(1)+1; |
Pokitto | 8:809b1982b4ae | 725 | uint8_t sy = random(1)+1; |
Pokitto | 8:809b1982b4ae | 726 | uint8_t he = random(5)+2; |
Pokitto | 8:809b1982b4ae | 727 | for (uint8_t ty = 0; ty < sy; ty++) { |
Pokitto | 8:809b1982b4ae | 728 | for (uint8_t tx = 0; tx < sx; tx++) { |
Pokitto | 8:809b1982b4ae | 729 | uint32_t index = rx+tx+(ry+ty) * worldsize; |
Pokitto | 8:809b1982b4ae | 730 | if (index<worldsize*worldsize) { |
Pokitto | 8:809b1982b4ae | 731 | setTileHeight(index,he); |
Pokitto | 8:809b1982b4ae | 732 | setTileType(index,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 733 | } |
Pokitto | 8:809b1982b4ae | 734 | } |
Pokitto | 8:809b1982b4ae | 735 | } |
Pokitto | 8:809b1982b4ae | 736 | } |
Pokitto | 8:809b1982b4ae | 737 | |
Pokitto | 8:809b1982b4ae | 738 | /** Check the world **/ |
Pokitto | 8:809b1982b4ae | 739 | for (i=0;i<worldsize*worldsize;i++) { |
Pokitto | 8:809b1982b4ae | 740 | if (getTileType(i)==TER_UNDEFINED) { |
Pokitto | 8:809b1982b4ae | 741 | setTileHeight(i,0); |
Pokitto | 8:809b1982b4ae | 742 | setTileType(i,TER_WATER); |
Pokitto | 8:809b1982b4ae | 743 | } |
Pokitto | 8:809b1982b4ae | 744 | } |
Pokitto | 8:809b1982b4ae | 745 | /** SET TILE SLOPE TYPES **/ |
Pokitto | 8:809b1982b4ae | 746 | |
Pokitto | 8:809b1982b4ae | 747 | for (uint16_t j=0; j<worldsize*worldsize;j++) { |
Pokitto | 8:809b1982b4ae | 748 | if (getTileHeight(j)) setTileType(j,getTileSlope(j)); |
Pokitto | 8:809b1982b4ae | 749 | #if TREESNROCKS |
Pokitto | 8:809b1982b4ae | 750 | if (getTileType(j)!=TER_WATER) { |
Pokitto | 8:809b1982b4ae | 751 | uint8_t r=xorshift8(); |
Pokitto | 8:809b1982b4ae | 752 | if (r>0xff-HUTPROB) {setTileType(j,TER_HUT);setTileHeight(j,7);} |
Pokitto | 8:809b1982b4ae | 753 | else if (r>0xff-GOLDPROB && golds<MAXGOLD) { |
Pokitto | 8:809b1982b4ae | 754 | setTileType(j,TER_GOLD); |
Pokitto | 8:809b1982b4ae | 755 | //golds++; |
Pokitto | 8:809b1982b4ae | 756 | } |
Pokitto | 8:809b1982b4ae | 757 | else if (r>0xff-STONEPROB) setTileType(j,TER_STONE); |
Pokitto | 8:809b1982b4ae | 758 | else if (r>0xff-TREEPROB) setTileType(j,TER_TREE); |
Pokitto | 8:809b1982b4ae | 759 | } |
Pokitto | 8:809b1982b4ae | 760 | #endif |
Pokitto | 8:809b1982b4ae | 761 | } |
Pokitto | 8:809b1982b4ae | 762 | |
Pokitto | 8:809b1982b4ae | 763 | i=0; uint16_t huts[100],numhuts=0; |
Pokitto | 8:809b1982b4ae | 764 | |
Pokitto | 8:809b1982b4ae | 765 | /** BEACHES **/ |
Pokitto | 8:809b1982b4ae | 766 | |
Pokitto | 8:809b1982b4ae | 767 | for (uint16_t j = 0; j<worldsize*worldsize; j++) { |
Pokitto | 8:809b1982b4ae | 768 | if (world[j]==TER_WATER) { |
Pokitto | 8:809b1982b4ae | 769 | uint16_t y = j/worldsize+1; |
Pokitto | 8:809b1982b4ae | 770 | uint16_t x = j % worldsize; |
Pokitto | 8:809b1982b4ae | 771 | if ((j%worldsize)&&(world[j-1]!=TER_WATER)&&(world[j+1]==TER_WATER)&&x>0) world[j-1]=TER_BEACHSE; |
Pokitto | 8:809b1982b4ae | 772 | if (j>worldsize&&(world[j-worldsize]!= TER_WATER)&&(world[j+worldsize]==TER_WATER)&&y<worldsize-1) world[j-worldsize]=TER_BEACHSW; |
Pokitto | 8:809b1982b4ae | 773 | } |
Pokitto | 8:809b1982b4ae | 774 | } |
Pokitto | 8:809b1982b4ae | 775 | //setTileType(0,TER_HUT); |
Pokitto | 8:809b1982b4ae | 776 | //setTileType(5,TER_HUT); |
Pokitto | 8:809b1982b4ae | 777 | /** CLEAR FRONT PORCHES OF HUTS, count golds **/ |
Pokitto | 8:809b1982b4ae | 778 | |
Pokitto | 8:809b1982b4ae | 779 | for (uint16_t j = 0; j<worldsize*worldsize; j++) { |
Pokitto | 8:809b1982b4ae | 780 | //if (getTileType(j)==TER_GOLD) golds++; |
Pokitto | 8:809b1982b4ae | 781 | if (getTileType(j)==TER_HUT) { |
Pokitto | 8:809b1982b4ae | 782 | if (j < (worldsize-1)*worldsize) { |
Pokitto | 8:809b1982b4ae | 783 | setTileType(j+worldsize, TER_PLAIN);//clear porch |
Pokitto | 8:809b1982b4ae | 784 | huts[numhuts++]=j; |
Pokitto | 8:809b1982b4ae | 785 | } else {setTileType(j, TER_PLAIN);} // invalid dwelling |
Pokitto | 8:809b1982b4ae | 786 | } |
Pokitto | 8:809b1982b4ae | 787 | } |
Pokitto | 8:809b1982b4ae | 788 | /** MAKE SURE AT LEAST 1 HUT **/ |
Pokitto | 8:809b1982b4ae | 789 | if (numhuts==0) { |
Pokitto | 8:809b1982b4ae | 790 | uint16_t ut = worldsize*worldsize/2+worldsize/2; |
Pokitto | 8:809b1982b4ae | 791 | setTileType(ut,TER_HUT); |
Pokitto | 8:809b1982b4ae | 792 | setTileHeight(ut,4); |
Pokitto | 8:809b1982b4ae | 793 | setTileType(ut+worldsize,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 794 | } |
Pokitto | 8:809b1982b4ae | 795 | |
Pokitto | 8:809b1982b4ae | 796 | /** PUT GUY ON MAP **/ |
Pokitto | 8:809b1982b4ae | 797 | uint16_t t = random(numhuts); |
Pokitto | 8:809b1982b4ae | 798 | //if (numhuts<1) |
Pokitto | 8:809b1982b4ae | 799 | playertile = (uint16_t)random(worldsize*worldsize); |
Pokitto | 8:809b1982b4ae | 800 | //else playertile = huts[t]+2*worldsize;//110;//worldsize/2+worldsize/2*worldsize;//huts[t]; |
Pokitto | 8:809b1982b4ae | 801 | //playertile=202; |
Pokitto | 8:809b1982b4ae | 802 | //setTileHeight(102,7); |
Pokitto | 8:809b1982b4ae | 803 | //setTileType(102,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 804 | //if (playertile > worldsize*worldsize) playertile = random(worldsize); |
Pokitto | 8:809b1982b4ae | 805 | player.x = (playertile%worldsize)*SUBDIVS; |
Pokitto | 8:809b1982b4ae | 806 | player.y = playertile/worldsize*SUBDIVS; |
Pokitto | 8:809b1982b4ae | 807 | //focusOnTile(playertile); |
Pokitto | 8:809b1982b4ae | 808 | |
Pokitto | 8:809b1982b4ae | 809 | /** Count gold on map **/ |
Pokitto | 8:809b1982b4ae | 810 | golds=0; |
Pokitto | 8:809b1982b4ae | 811 | for (uint16_t i=0;i<worldsize*worldsize;i++) { |
Pokitto | 8:809b1982b4ae | 812 | if (getTileType(i)==TER_GOLD) golds++; |
Pokitto | 8:809b1982b4ae | 813 | } |
Pokitto | 8:809b1982b4ae | 814 | /** Make sure enough gold on the map **/ |
Pokitto | 8:809b1982b4ae | 815 | while (golds<gamelevel*GOLDMULT) { |
Pokitto | 8:809b1982b4ae | 816 | setTileType(random(worldsize*worldsize-1),TER_GOLD); |
Pokitto | 8:809b1982b4ae | 817 | golds++; |
Pokitto | 8:809b1982b4ae | 818 | } |
Pokitto | 8:809b1982b4ae | 819 | } |
Pokitto | 8:809b1982b4ae | 820 | |
Pokitto | 8:809b1982b4ae | 821 | void drawtile(int32_t x, int32_t y, const uint8_t* bmp) { |
Pokitto | 8:809b1982b4ae | 822 | gb.display.drawBitmap(x+cx, y+cy, bmp); |
Pokitto | 8:809b1982b4ae | 823 | } |
Pokitto | 8:809b1982b4ae | 824 | |
Pokitto | 8:809b1982b4ae | 825 | void drawhuman(uint8_t n) { |
Pokitto | 8:809b1982b4ae | 826 | switch (humans[n].facing) { |
Pokitto | 8:809b1982b4ae | 827 | case NE: |
Pokitto | 8:809b1982b4ae | 828 | if (!humans[n].animframe) drawToTile(playertile,pixelman_nw_1,player.x%SUBDIVS,player.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 829 | else drawToTile(playertile,pixelman_nw_2,player.x%SUBDIVS,player.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 830 | break; |
Pokitto | 8:809b1982b4ae | 831 | case SE: |
Pokitto | 8:809b1982b4ae | 832 | if (!humans[n].animframe) drawToTile(playertile,pixelman_se_1,player.x%SUBDIVS,player.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 833 | else drawToTile(playertile,pixelman_se_2,player.x%SUBDIVS,player.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 834 | break; |
Pokitto | 8:809b1982b4ae | 835 | case SW: |
Pokitto | 8:809b1982b4ae | 836 | if (!humans[n].animframe) drawToTile(playertile,pixelman_se_1,player.x%SUBDIVS,player.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 837 | else drawToTile(playertile,pixelman_se_2,player.x%SUBDIVS,player.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 838 | break; |
Pokitto | 8:809b1982b4ae | 839 | case NW: |
Pokitto | 8:809b1982b4ae | 840 | if (!humans[n].animframe) drawToTile(playertile,pixelman_nw_1,player.x%SUBDIVS,player.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 841 | else drawToTile(playertile,pixelman_nw_2,player.x%SUBDIVS,player.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 842 | break; |
Pokitto | 8:809b1982b4ae | 843 | default: |
Pokitto | 8:809b1982b4ae | 844 | while(1) { |
Pokitto | 8:809b1982b4ae | 845 | if(gb.update()) gb.display.print("ERROR!!!"); |
Pokitto | 8:809b1982b4ae | 846 | } |
Pokitto | 8:809b1982b4ae | 847 | } |
Pokitto | 8:809b1982b4ae | 848 | } |
Pokitto | 8:809b1982b4ae | 849 | |
Pokitto | 8:809b1982b4ae | 850 | void spawnGold(uint16_t t) { |
Pokitto | 8:809b1982b4ae | 851 | |
Pokitto | 8:809b1982b4ae | 852 | } |
Pokitto | 8:809b1982b4ae | 853 | |
Pokitto | 8:809b1982b4ae | 854 | void drawwolf(uint8_t n) { |
Pokitto | 8:809b1982b4ae | 855 | switch (werewolves[n].facing) { |
Pokitto | 8:809b1982b4ae | 856 | case NE: |
Pokitto | 8:809b1982b4ae | 857 | if (!werewolves[n].animframe) drawToTile(werewolves[n].tile,werewolf_nw_1,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 858 | else drawToTile(werewolves[n].tile,werewolf_nw_2,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 859 | break; |
Pokitto | 8:809b1982b4ae | 860 | case SE: |
Pokitto | 8:809b1982b4ae | 861 | if (!werewolves[n].animframe) drawToTile(werewolves[n].tile,werewolf_se_1,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 862 | else drawToTile(werewolves[n].tile,werewolf_se_2,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 863 | break; |
Pokitto | 8:809b1982b4ae | 864 | case SW: |
Pokitto | 8:809b1982b4ae | 865 | if (!werewolves[n].animframe) drawToTile(werewolves[n].tile,werewolf_se_1,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 866 | else drawToTile(werewolves[n].tile,werewolf_se_2,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,1); |
Pokitto | 8:809b1982b4ae | 867 | break; |
Pokitto | 8:809b1982b4ae | 868 | case NW: |
Pokitto | 8:809b1982b4ae | 869 | if (!werewolves[n].animframe) drawToTile(werewolves[n].tile,werewolf_nw_1,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 870 | else drawToTile(werewolves[n].tile,werewolf_nw_2,werewolves[n].position.x%SUBDIVS,werewolves[n].position.y%SUBDIVS,0); |
Pokitto | 8:809b1982b4ae | 871 | break; |
Pokitto | 8:809b1982b4ae | 872 | } |
Pokitto | 8:809b1982b4ae | 873 | } |
Pokitto | 8:809b1982b4ae | 874 | |
Pokitto | 8:809b1982b4ae | 875 | void drawworld(int32_t x, int32_t y) { |
Pokitto | 8:809b1982b4ae | 876 | |
Pokitto | 8:809b1982b4ae | 877 | uint16_t i=0; |
Pokitto | 8:809b1982b4ae | 878 | uint8_t tiletype; |
Pokitto | 8:809b1982b4ae | 879 | int8_t tileheight; |
Pokitto | 8:809b1982b4ae | 880 | int32_t scrx=x, scry=y; |
Pokitto | 8:809b1982b4ae | 881 | wave+=6; |
Pokitto | 8:809b1982b4ae | 882 | |
Pokitto | 8:809b1982b4ae | 883 | centertile =-1; |
Pokitto | 8:809b1982b4ae | 884 | int16_t rowx = 0; |
Pokitto | 8:809b1982b4ae | 885 | inHut = false; |
Pokitto | 8:809b1982b4ae | 886 | bool hdrawn=false; |
Pokitto | 8:809b1982b4ae | 887 | for (uint8_t iy=0;iy<worldsize;iy++,rowx-=8) { |
Pokitto | 8:809b1982b4ae | 888 | scry=y+iy*4; |
Pokitto | 8:809b1982b4ae | 889 | scrx=x; |
Pokitto | 8:809b1982b4ae | 890 | for (uint8_t ix=0;ix<worldsize;ix++, i++, scrx+=XOFF, scry+=YOFF) { |
Pokitto | 8:809b1982b4ae | 891 | if (scrx+rowx>-5*XOFF && scry>-5*YOFF && scrx+rowx < 110+XOFF && scry < 88+ YOFF ) { |
Pokitto | 8:809b1982b4ae | 892 | tiletype = getTileType(i); |
Pokitto | 8:809b1982b4ae | 893 | tileheight = getTileHeight(i); |
Pokitto | 8:809b1982b4ae | 894 | bool pillar=false; |
Pokitto | 8:809b1982b4ae | 895 | if (tileheight>PILLARHEIGHT) pillar=true; |
Pokitto | 8:809b1982b4ae | 896 | tileheight <<= GROUNDLIFT; |
Pokitto | 8:809b1982b4ae | 897 | |
Pokitto | 8:809b1982b4ae | 898 | switch (tiletype) { |
Pokitto | 8:809b1982b4ae | 899 | case TER_WATER: |
Pokitto | 8:809b1982b4ae | 900 | //wave=xorshift8(); |
Pokitto | 8:809b1982b4ae | 901 | if ((wave>0x7F)) { |
Pokitto | 8:809b1982b4ae | 902 | if (ix&1) drawtile(scrx+rowx,scry-tileheight,ter_water1); |
Pokitto | 8:809b1982b4ae | 903 | else drawtile(scrx+rowx,scry-tileheight,ter_water2); |
Pokitto | 8:809b1982b4ae | 904 | } else { |
Pokitto | 8:809b1982b4ae | 905 | if (ix&1) drawtile(scrx+rowx,scry-tileheight,ter_water2); |
Pokitto | 8:809b1982b4ae | 906 | else drawtile(scrx+rowx,scry-tileheight,ter_water1); |
Pokitto | 8:809b1982b4ae | 907 | } |
Pokitto | 8:809b1982b4ae | 908 | break; |
Pokitto | 8:809b1982b4ae | 909 | case TER_PLAIN: |
Pokitto | 8:809b1982b4ae | 910 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 911 | drawtile(scrx+rowx,scry-tileheight,ter_plain); |
Pokitto | 8:809b1982b4ae | 912 | break; |
Pokitto | 8:809b1982b4ae | 913 | case TER_GOLD: |
Pokitto | 8:809b1982b4ae | 914 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 915 | drawtile(scrx+rowx,scry-tileheight,ter_gold); |
Pokitto | 8:809b1982b4ae | 916 | break; |
Pokitto | 8:809b1982b4ae | 917 | case TER_HUT: |
Pokitto | 8:809b1982b4ae | 918 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 919 | drawtile(scrx+rowx,scry-10-tileheight,ter_hut); |
Pokitto | 8:809b1982b4ae | 920 | if (i==playertile) { |
Pokitto | 8:809b1982b4ae | 921 | drawToTile(i,anim_blue_flag1,2,0,0); |
Pokitto | 8:809b1982b4ae | 922 | inHut=true; |
Pokitto | 8:809b1982b4ae | 923 | } |
Pokitto | 8:809b1982b4ae | 924 | break; |
Pokitto | 8:809b1982b4ae | 925 | case TER_BEACHSE: |
Pokitto | 8:809b1982b4ae | 926 | drawtile(scrx+rowx,scry-tileheight-1,ter_beach_se); |
Pokitto | 8:809b1982b4ae | 927 | break; |
Pokitto | 8:809b1982b4ae | 928 | case TER_BEACHSW: |
Pokitto | 8:809b1982b4ae | 929 | drawtile(scrx+rowx,scry-tileheight-1,ter_beach_sw); |
Pokitto | 8:809b1982b4ae | 930 | break; |
Pokitto | 8:809b1982b4ae | 931 | case TER_TREE: |
Pokitto | 8:809b1982b4ae | 932 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 933 | drawtile(scrx+rowx,scry-10-tileheight,ter_tree); |
Pokitto | 8:809b1982b4ae | 934 | break; |
Pokitto | 8:809b1982b4ae | 935 | case TER_STONE: |
Pokitto | 8:809b1982b4ae | 936 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 937 | drawtile(scrx+rowx,scry-tileheight,ter_stone); |
Pokitto | 8:809b1982b4ae | 938 | break; |
Pokitto | 8:809b1982b4ae | 939 | case TER_BUMP_NW: |
Pokitto | 8:809b1982b4ae | 940 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 941 | drawtile(scrx+rowx,scry-tileheight,ter_bump_nw); |
Pokitto | 8:809b1982b4ae | 942 | break; |
Pokitto | 8:809b1982b4ae | 943 | case TER_BUMP_NE: |
Pokitto | 8:809b1982b4ae | 944 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 945 | drawtile(scrx+rowx,scry-tileheight,ter_bump_ne); |
Pokitto | 8:809b1982b4ae | 946 | break; |
Pokitto | 8:809b1982b4ae | 947 | case TER_BUMP_SE: |
Pokitto | 8:809b1982b4ae | 948 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 949 | drawtile(scrx+rowx,scry-tileheight,ter_bump_se); |
Pokitto | 8:809b1982b4ae | 950 | break; |
Pokitto | 8:809b1982b4ae | 951 | case TER_BUMP_SW: |
Pokitto | 8:809b1982b4ae | 952 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 953 | drawtile(scrx+rowx,scry-tileheight,ter_bump_sw); |
Pokitto | 8:809b1982b4ae | 954 | break; |
Pokitto | 8:809b1982b4ae | 955 | case TER_BUMP_W: |
Pokitto | 8:809b1982b4ae | 956 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 957 | drawtile(scrx+rowx,scry-tileheight,ter_bump_w); |
Pokitto | 8:809b1982b4ae | 958 | break; |
Pokitto | 8:809b1982b4ae | 959 | case TER_BUMP_E: |
Pokitto | 8:809b1982b4ae | 960 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 961 | drawtile(scrx+rowx,scry-tileheight,ter_bump_e); |
Pokitto | 8:809b1982b4ae | 962 | break; |
Pokitto | 8:809b1982b4ae | 963 | case TER_BUMP_S: |
Pokitto | 8:809b1982b4ae | 964 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 965 | drawtile(scrx+rowx,scry-tileheight-2,ter_bump_s); |
Pokitto | 8:809b1982b4ae | 966 | break; |
Pokitto | 8:809b1982b4ae | 967 | case TER_BUMP_N: |
Pokitto | 8:809b1982b4ae | 968 | if (pillar) drawtile(scrx+rowx,scry-tileheight,dirt_pillar); |
Pokitto | 8:809b1982b4ae | 969 | drawtile(scrx+rowx,scry-tileheight,ter_bump_n); |
Pokitto | 8:809b1982b4ae | 970 | break; |
Pokitto | 8:809b1982b4ae | 971 | default: |
Pokitto | 8:809b1982b4ae | 972 | break; |
Pokitto | 8:809b1982b4ae | 973 | } |
Pokitto | 8:809b1982b4ae | 974 | if (scrx+rowx<45 && scrx+rowx+2*XOFF>45) { |
Pokitto | 8:809b1982b4ae | 975 | if (scry-tileheight<40 && scry+tileheight+8>40) centertile=i; |
Pokitto | 8:809b1982b4ae | 976 | } |
Pokitto | 8:809b1982b4ae | 977 | if (i==playertile+wading && getTileType(i-wading) != TER_HUT) {drawhuman(0); hdrawn=true;} |
Pokitto | 8:809b1982b4ae | 978 | else if (i==playertile && getTileType(i) != TER_HUT && hdrawn == false) drawhuman(0); |
Pokitto | 8:809b1982b4ae | 979 | for (uint8_t r=0;r<(gamelevel-1)*WOLFMULT;r++) { |
Pokitto | 8:809b1982b4ae | 980 | if (werewolves[r].visible) { |
Pokitto | 8:809b1982b4ae | 981 | if (i==werewolves[r].tile) drawwolf(r); |
Pokitto | 8:809b1982b4ae | 982 | if (werewolves[r].tile == playertile) { |
Pokitto | 8:809b1982b4ae | 983 | caught=true; |
Pokitto | 8:809b1982b4ae | 984 | } |
Pokitto | 8:809b1982b4ae | 985 | } |
Pokitto | 8:809b1982b4ae | 986 | } |
Pokitto | 8:809b1982b4ae | 987 | if (caught) { |
Pokitto | 8:809b1982b4ae | 988 | if (xorshift8()>0xFD) drawToTile(playertile,splatter,random(3)-1,random(3)+1,random(1)); |
Pokitto | 8:809b1982b4ae | 989 | humans[0].energy--; |
Pokitto | 8:809b1982b4ae | 990 | if (humans[0].energy<0) gamestate = GAMEOVER; |
Pokitto | 8:809b1982b4ae | 991 | } |
Pokitto | 8:809b1982b4ae | 992 | } |
Pokitto | 8:809b1982b4ae | 993 | } |
Pokitto | 8:809b1982b4ae | 994 | } |
Pokitto | 8:809b1982b4ae | 995 | } |
Pokitto | 8:809b1982b4ae | 996 | |
Pokitto | 8:809b1982b4ae | 997 | int main () { |
Pokitto | 8:809b1982b4ae | 998 | gb.sound.playMusicStream("ZOMBIE.SND"); |
Pokitto | 8:809b1982b4ae | 999 | |
Pokitto | 8:809b1982b4ae | 1000 | gb.begin(); |
Pokitto | 8:809b1982b4ae | 1001 | gb.display.load565Palette(pokulous_pal); |
Pokitto | 8:809b1982b4ae | 1002 | //gb.display.load565Palette(night_palette); |
Pokitto | 8:809b1982b4ae | 1003 | //gb.display.bgcolor=10; |
Pokitto | 8:809b1982b4ae | 1004 | gb.display.clear(); |
Pokitto | 8:809b1982b4ae | 1005 | gb.display.setColorDepth(4); |
Pokitto | 8:809b1982b4ae | 1006 | gb.display.bgcolor = 13; |
Pokitto | 8:809b1982b4ae | 1007 | gb.display.invisiblecolor = 15; |
Pokitto | 8:809b1982b4ae | 1008 | gb.sound.ampEnable(true); |
Pokitto | 8:809b1982b4ae | 1009 | gb.sound.playMusicStream(); |
Pokitto | 8:809b1982b4ae | 1010 | uint8_t timeout=0xFF; |
Pokitto | 8:809b1982b4ae | 1011 | |
Pokitto | 8:809b1982b4ae | 1012 | /** veriables needed in gameloop (can't initialize inside a switch case) **/ |
Pokitto | 8:809b1982b4ae | 1013 | coords prev; |
Pokitto | 8:809b1982b4ae | 1014 | bool walking; |
Pokitto | 8:809b1982b4ae | 1015 | uint8_t newtime; |
Pokitto | 8:809b1982b4ae | 1016 | int tgold=0; |
Pokitto | 8:809b1982b4ae | 1017 | |
Pokitto | 8:809b1982b4ae | 1018 | |
Pokitto | 8:809b1982b4ae | 1019 | gb.display.bgcolor = 13; |
Pokitto | 8:809b1982b4ae | 1020 | while (gb.isRunning()) { |
Pokitto | 8:809b1982b4ae | 1021 | if (gb.update()) { |
Pokitto | 8:809b1982b4ae | 1022 | switch (gamestate) { |
Pokitto | 8:809b1982b4ae | 1023 | case TITLE: |
Pokitto | 8:809b1982b4ae | 1024 | while(!gb.buttons.pressed(BTN_A)&&gb.isRunning()) { |
Pokitto | 8:809b1982b4ae | 1025 | if (gb.update()) { |
Pokitto | 8:809b1982b4ae | 1026 | gb.display.drawBitmap(0,0,pixonia); |
Pokitto | 8:809b1982b4ae | 1027 | gb.display.setFont(fontRunes); |
Pokitto | 8:809b1982b4ae | 1028 | gb.display.color=9; |
Pokitto | 8:809b1982b4ae | 1029 | gb.display.bgcolor = gb.display.invisiblecolor; |
Pokitto | 8:809b1982b4ae | 1030 | gb.display.setCursor(9,74); |
Pokitto | 8:809b1982b4ae | 1031 | if (timeout>127) gb.display.print("Copyright 2017 Pokitto ltd"); |
Pokitto | 8:809b1982b4ae | 1032 | else gb.display.print(" Press 'A' to Start"); |
Pokitto | 8:809b1982b4ae | 1033 | timeout-=2; |
Pokitto | 8:809b1982b4ae | 1034 | srand(gb.getTime()); |
Pokitto | 8:809b1982b4ae | 1035 | } |
Pokitto | 8:809b1982b4ae | 1036 | } |
Pokitto | 8:809b1982b4ae | 1037 | gamestate = NEXTLEVEL; |
Pokitto | 8:809b1982b4ae | 1038 | initworld(); |
Pokitto | 8:809b1982b4ae | 1039 | break; |
Pokitto | 8:809b1982b4ae | 1040 | case NEXTLEVEL: |
Pokitto | 8:809b1982b4ae | 1041 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1042 | timeout = 0xf; |
Pokitto | 8:809b1982b4ae | 1043 | while (timeout--) gb.update(); |
Pokitto | 8:809b1982b4ae | 1044 | while(!gb.buttons.pressed(BTN_A)&&gb.isRunning()) { |
Pokitto | 8:809b1982b4ae | 1045 | if (gb.update()) { |
Pokitto | 8:809b1982b4ae | 1046 | gb.display.setFont(fontRunes); |
Pokitto | 8:809b1982b4ae | 1047 | gb.display.setCursor(0,30); |
Pokitto | 8:809b1982b4ae | 1048 | gb.display.print(" Level: "); |
Pokitto | 8:809b1982b4ae | 1049 | gb.display.palette[6]=65312; |
Pokitto | 8:809b1982b4ae | 1050 | gb.display.setColor(6,3); |
Pokitto | 8:809b1982b4ae | 1051 | gb.display.println(gamelevel); |
Pokitto | 8:809b1982b4ae | 1052 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1053 | gb.display.println(" "); |
Pokitto | 8:809b1982b4ae | 1054 | gb.display.print(" Find "); |
Pokitto | 8:809b1982b4ae | 1055 | gb.display.setColor(6,3); |
Pokitto | 8:809b1982b4ae | 1056 | gb.display.print(gamelevel*GOLDMULT); |
Pokitto | 8:809b1982b4ae | 1057 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1058 | gb.display.print(" Gold"); |
Pokitto | 8:809b1982b4ae | 1059 | |
Pokitto | 8:809b1982b4ae | 1060 | } |
Pokitto | 8:809b1982b4ae | 1061 | } |
Pokitto | 8:809b1982b4ae | 1062 | gamestate=GAME; |
Pokitto | 8:809b1982b4ae | 1063 | break; |
Pokitto | 8:809b1982b4ae | 1064 | case INTRO: |
Pokitto | 8:809b1982b4ae | 1065 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1066 | timeout = 0xf; |
Pokitto | 8:809b1982b4ae | 1067 | while (timeout--) gb.update(); |
Pokitto | 8:809b1982b4ae | 1068 | while(!gb.buttons.pressed(BTN_A)&&gb.isRunning()) { |
Pokitto | 8:809b1982b4ae | 1069 | if (gb.update()) { |
Pokitto | 8:809b1982b4ae | 1070 | gb.display.setFont(fontRunes); |
Pokitto | 8:809b1982b4ae | 1071 | gb.display.setCursor(0,30); |
Pokitto | 8:809b1982b4ae | 1072 | gb.display.print(" \"There's "); |
Pokitto | 8:809b1982b4ae | 1073 | gb.display.palette[6]=65312; |
Pokitto | 8:809b1982b4ae | 1074 | gb.display.setColor(6,3); |
Pokitto | 8:809b1982b4ae | 1075 | gb.display.println("gold"); |
Pokitto | 8:809b1982b4ae | 1076 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1077 | gb.display.println(" on the Islands"); |
Pokitto | 8:809b1982b4ae | 1078 | gb.display.println(" of Pixonia!\""); |
Pokitto | 8:809b1982b4ae | 1079 | } |
Pokitto | 8:809b1982b4ae | 1080 | } |
Pokitto | 8:809b1982b4ae | 1081 | gamestate=GAME; |
Pokitto | 8:809b1982b4ae | 1082 | break; |
Pokitto | 8:809b1982b4ae | 1083 | case LEVELPASSED: |
Pokitto | 8:809b1982b4ae | 1084 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1085 | timeout = 0xf; |
Pokitto | 8:809b1982b4ae | 1086 | while (timeout--) gb.update(); |
Pokitto | 8:809b1982b4ae | 1087 | while(!gb.buttons.pressed(BTN_A)&&gb.isRunning()) { |
Pokitto | 8:809b1982b4ae | 1088 | if (gb.update()) { |
Pokitto | 8:809b1982b4ae | 1089 | gb.display.setFont(fontRunes); |
Pokitto | 8:809b1982b4ae | 1090 | gb.display.setCursor(0,30); |
Pokitto | 8:809b1982b4ae | 1091 | gb.display.print(" You made it! "); |
Pokitto | 8:809b1982b4ae | 1092 | gb.display.palette[6]=65312; |
Pokitto | 8:809b1982b4ae | 1093 | gb.display.setColor(6,3); |
Pokitto | 8:809b1982b4ae | 1094 | //gb.display.println(gamelevel); |
Pokitto | 8:809b1982b4ae | 1095 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1096 | gb.display.println(" "); |
Pokitto | 8:809b1982b4ae | 1097 | gb.display.print(" Found "); |
Pokitto | 8:809b1982b4ae | 1098 | gb.display.setColor(6,3); |
Pokitto | 8:809b1982b4ae | 1099 | gb.display.print(gamelevel*GOLDMULT); |
Pokitto | 8:809b1982b4ae | 1100 | gb.display.setColor(9,3); |
Pokitto | 8:809b1982b4ae | 1101 | gb.display.print(" Gold"); |
Pokitto | 8:809b1982b4ae | 1102 | |
Pokitto | 8:809b1982b4ae | 1103 | } |
Pokitto | 8:809b1982b4ae | 1104 | } |
Pokitto | 8:809b1982b4ae | 1105 | gamelevel++; |
Pokitto | 8:809b1982b4ae | 1106 | gamestate=NEXTLEVEL; |
Pokitto | 8:809b1982b4ae | 1107 | initworld(); |
Pokitto | 8:809b1982b4ae | 1108 | break; |
Pokitto | 8:809b1982b4ae | 1109 | case GAME: |
Pokitto | 8:809b1982b4ae | 1110 | wolvestick = 1 - wolvestick; |
Pokitto | 8:809b1982b4ae | 1111 | watertick = 1 - watertick; |
Pokitto | 8:809b1982b4ae | 1112 | newtime = timeofday>>8; |
Pokitto | 8:809b1982b4ae | 1113 | if (oldtime > newtime) oldtime=0; // prevent "flash" when timer spins around |
Pokitto | 8:809b1982b4ae | 1114 | if (oldtime < newtime) { |
Pokitto | 8:809b1982b4ae | 1115 | oldtime = newtime; |
Pokitto | 8:809b1982b4ae | 1116 | if (oldtime<127) tweenfactor +=2; |
Pokitto | 8:809b1982b4ae | 1117 | else { |
Pokitto | 8:809b1982b4ae | 1118 | tweenfactor -= 2; |
Pokitto | 8:809b1982b4ae | 1119 | } |
Pokitto | 8:809b1982b4ae | 1120 | if (oldtime>150) { |
Pokitto | 8:809b1982b4ae | 1121 | monsterdrain =4; |
Pokitto | 8:809b1982b4ae | 1122 | } else monsterdrain =1; |
Pokitto | 8:809b1982b4ae | 1123 | if (tweenfactor>253) tweenfactor =253; |
Pokitto | 8:809b1982b4ae | 1124 | if (tweenfactor<2) tweenfactor = 2; |
Pokitto | 8:809b1982b4ae | 1125 | gb.display.tweenPalette(gb.display.palette,pokulous_pal,night_palette,tweenfactor); |
Pokitto | 8:809b1982b4ae | 1126 | if (oldtime>MONSTERMORNING && oldtime<MONSTERNIGHT && xorshift16()>0xFFFF-WOLFPROB) { |
Pokitto | 8:809b1982b4ae | 1127 | spawnWolf(); |
Pokitto | 8:809b1982b4ae | 1128 | gb.display.palette[6]=gb.display.palette[6]<<1; // make gold brighter during night |
Pokitto | 8:809b1982b4ae | 1129 | if (xorshift8()>0xff-GLIMMERPROB) gb.display.palette[6]=65312; //gold is shining |
Pokitto | 8:809b1982b4ae | 1130 | } |
Pokitto | 8:809b1982b4ae | 1131 | |
Pokitto | 8:809b1982b4ae | 1132 | #ifndef POK_SIM |
Pokitto | 8:809b1982b4ae | 1133 | //Pokitto::setBacklight(POK_BACKLIGHT_INITIALVALUE*tweenfactor/255); |
Pokitto | 8:809b1982b4ae | 1134 | #endif |
Pokitto | 8:809b1982b4ae | 1135 | |
Pokitto | 8:809b1982b4ae | 1136 | } |
Pokitto | 8:809b1982b4ae | 1137 | if (focusing) focusOnTile(focustile); |
Pokitto | 8:809b1982b4ae | 1138 | walking=false; |
Pokitto | 8:809b1982b4ae | 1139 | prev = player; |
Pokitto | 8:809b1982b4ae | 1140 | prevtile = playertile; |
Pokitto | 8:809b1982b4ae | 1141 | uint8_t inc; |
Pokitto | 8:809b1982b4ae | 1142 | if (wading!=1) inc=watertick; |
Pokitto | 8:809b1982b4ae | 1143 | else inc=1; |
Pokitto | 8:809b1982b4ae | 1144 | |
Pokitto | 8:809b1982b4ae | 1145 | if (caught) inc=0; // we got caught, can't move anymore |
Pokitto | 8:809b1982b4ae | 1146 | |
Pokitto | 8:809b1982b4ae | 1147 | if (gb.buttons.downBtn() && !gb.buttons.aBtn()) {if (player.x < (worldsize-1)*SUBDIVS) player.x+=inc;humans[0].animframe = 1 - humans[0].animframe;humans[0].facing = SE;} |
Pokitto | 8:809b1982b4ae | 1148 | if (gb.buttons.rightBtn() && !gb.buttons.aBtn()) {if (player.y>0) player.y-=inc;humans[0].animframe = 1 - humans[0].animframe;humans[0].facing = NE;} |
Pokitto | 8:809b1982b4ae | 1149 | if (gb.buttons.leftBtn() && !gb.buttons.aBtn()) {if (player.y < (worldsize-1)*SUBDIVS) player.y+=inc;humans[0].animframe = 1 - humans[0].animframe;humans[0].facing = SW;} |
Pokitto | 8:809b1982b4ae | 1150 | if (gb.buttons.upBtn() && !gb.buttons.aBtn()) {if (player.x>0) player.x-=inc; humans[0].animframe = 1 - humans[0].animframe; humans[0].facing = NW;} |
Pokitto | 8:809b1982b4ae | 1151 | |
Pokitto | 8:809b1982b4ae | 1152 | /** scroll world **/ |
Pokitto | 8:809b1982b4ae | 1153 | coords t; |
Pokitto | 8:809b1982b4ae | 1154 | t = worldToScreen(player.x,player.y); |
Pokitto | 8:809b1982b4ae | 1155 | if (!focusing) { |
Pokitto | 8:809b1982b4ae | 1156 | int16_t owx=wx, owy=wy; |
Pokitto | 8:809b1982b4ae | 1157 | uint8_t slower=0; |
Pokitto | 8:809b1982b4ae | 1158 | if (t.x>0 && t.x<110 && t.y>0 && t.y<88) { |
Pokitto | 8:809b1982b4ae | 1159 | slower = 4; |
Pokitto | 8:809b1982b4ae | 1160 | //if (t.x<20 || t.x>90 || t.y<15 || t.y>65) slower =3; |
Pokitto | 8:809b1982b4ae | 1161 | } |
Pokitto | 8:809b1982b4ae | 1162 | if (t.x<40) {wy-=0;wx+=XSPEED>>slower;} |
Pokitto | 8:809b1982b4ae | 1163 | if (t.y<40) {wy+=YSPEED>>slower;wx+=0;} |
Pokitto | 8:809b1982b4ae | 1164 | if (t.x>70) {wy+=0;wx-=XSPEED>>slower;} |
Pokitto | 8:809b1982b4ae | 1165 | if (t.y>50) {wy-=YSPEED>>slower;wx-=0;} |
Pokitto | 8:809b1982b4ae | 1166 | if (centertile > worldsize*worldsize) {centertile=oldcenter; wx=owx; wy=owy;} |
Pokitto | 8:809b1982b4ae | 1167 | } |
Pokitto | 8:809b1982b4ae | 1168 | playertile = worldToTile(player.x,player.y); |
Pokitto | 8:809b1982b4ae | 1169 | if (getTileType(playertile)==TER_WATER) wading=-1; |
Pokitto | 8:809b1982b4ae | 1170 | else wading = 1; |
Pokitto | 8:809b1982b4ae | 1171 | if (playertile != prevtile && canMoveTo(playertile)==false) { |
Pokitto | 8:809b1982b4ae | 1172 | player=prev; |
Pokitto | 8:809b1982b4ae | 1173 | playertile= prevtile; |
Pokitto | 8:809b1982b4ae | 1174 | } else { |
Pokitto | 8:809b1982b4ae | 1175 | oldplayercount++; |
Pokitto | 8:809b1982b4ae | 1176 | if (oldplayercount==6) { |
Pokitto | 8:809b1982b4ae | 1177 | /*for creature chase ai */ |
Pokitto | 8:809b1982b4ae | 1178 | oldplayer=player; |
Pokitto | 8:809b1982b4ae | 1179 | oldplayercount=0; |
Pokitto | 8:809b1982b4ae | 1180 | } |
Pokitto | 8:809b1982b4ae | 1181 | } |
Pokitto | 8:809b1982b4ae | 1182 | |
Pokitto | 8:809b1982b4ae | 1183 | //if (gb.buttons.pressed(BTN_C)) {initworld();} |
Pokitto | 8:809b1982b4ae | 1184 | if (gb.buttons.pressed(BTN_A)) { |
Pokitto | 8:809b1982b4ae | 1185 | uint16_t tile = getFacingTile(playertile, humans[0].facing); |
Pokitto | 8:809b1982b4ae | 1186 | int8_t z = getTileHeight(tile); |
Pokitto | 8:809b1982b4ae | 1187 | if (z==0) setTileType(tile,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 1188 | z++; |
Pokitto | 8:809b1982b4ae | 1189 | if (z>7) z=7; |
Pokitto | 8:809b1982b4ae | 1190 | setTileHeight(tile,z); |
Pokitto | 8:809b1982b4ae | 1191 | setSlopeAutomatic(tile); |
Pokitto | 8:809b1982b4ae | 1192 | } |
Pokitto | 8:809b1982b4ae | 1193 | if (gb.buttons.pressed(BTN_B)) { |
Pokitto | 8:809b1982b4ae | 1194 | uint16_t t = getFacingTile(playertile, humans[0].facing); |
Pokitto | 8:809b1982b4ae | 1195 | int8_t z = getTileHeight(t); |
Pokitto | 8:809b1982b4ae | 1196 | uint8_t k = getTileType(t); |
Pokitto | 8:809b1982b4ae | 1197 | |
Pokitto | 8:809b1982b4ae | 1198 | if (getTileType(t)!=TER_HUT) z--; |
Pokitto | 8:809b1982b4ae | 1199 | if (z<0) z=0; |
Pokitto | 8:809b1982b4ae | 1200 | |
Pokitto | 8:809b1982b4ae | 1201 | if (z<getTileHeight(playertile)-1 || z==0) { |
Pokitto | 8:809b1982b4ae | 1202 | if (k==TER_GOLD) { |
Pokitto | 8:809b1982b4ae | 1203 | playergolds++; |
Pokitto | 8:809b1982b4ae | 1204 | if (playergolds==gamelevel*GOLDMULT) { |
Pokitto | 8:809b1982b4ae | 1205 | gamestate=LEVELPASSED; |
Pokitto | 8:809b1982b4ae | 1206 | } |
Pokitto | 8:809b1982b4ae | 1207 | spawnGold(t); |
Pokitto | 8:809b1982b4ae | 1208 | } |
Pokitto | 8:809b1982b4ae | 1209 | if (k < 5 || k > 12) setTileType(t,TER_PLAIN); |
Pokitto | 8:809b1982b4ae | 1210 | |
Pokitto | 8:809b1982b4ae | 1211 | } |
Pokitto | 8:809b1982b4ae | 1212 | if (z==0) setTileType(t,TER_WATER); |
Pokitto | 8:809b1982b4ae | 1213 | setTileHeight(t,z); |
Pokitto | 8:809b1982b4ae | 1214 | setSlopeAutomatic(t); |
Pokitto | 8:809b1982b4ae | 1215 | } |
Pokitto | 8:809b1982b4ae | 1216 | |
Pokitto | 8:809b1982b4ae | 1217 | moveWolves(); |
Pokitto | 8:809b1982b4ae | 1218 | drawworld(wx,wy); |
Pokitto | 8:809b1982b4ae | 1219 | oldcenter = centertile; |
Pokitto | 8:809b1982b4ae | 1220 | gb.display.drawBitmap(1,1,gold); |
Pokitto | 8:809b1982b4ae | 1221 | gb.display.setCursor(10,0); |
Pokitto | 8:809b1982b4ae | 1222 | gb.display.setColor(9,gb.display.invisiblecolor); |
Pokitto | 8:809b1982b4ae | 1223 | timeofday += TIMEINCREMENT; |
Pokitto | 8:809b1982b4ae | 1224 | if (inHut) timeofday += 7*TIMEINCREMENT; |
Pokitto | 8:809b1982b4ae | 1225 | gb.display.print(playergolds); |
Pokitto | 8:809b1982b4ae | 1226 | gb.display.print("/"); |
Pokitto | 8:809b1982b4ae | 1227 | gb.display.println(gamelevel*GOLDMULT); |
Pokitto | 8:809b1982b4ae | 1228 | gb.display.bgcolor = 13; |
Pokitto | 8:809b1982b4ae | 1229 | |
Pokitto | 8:809b1982b4ae | 1230 | #ifdef DEBUG |
Pokitto | 8:809b1982b4ae | 1231 | gb.display.bgcolor = 9; |
Pokitto | 8:809b1982b4ae | 1232 | gb.display.println(numwolves); |
Pokitto | 8:809b1982b4ae | 1233 | gb.display.print(werewolves[0].position.x);gb.display.print(",");gb.display.println(werewolves[0].position.y); |
Pokitto | 8:809b1982b4ae | 1234 | gb.display.print(werewolves[0].tile); |
Pokitto | 8:809b1982b4ae | 1235 | switch (werewolves[0].ai) { |
Pokitto | 8:809b1982b4ae | 1236 | case HOMING: |
Pokitto | 8:809b1982b4ae | 1237 | gb.display.println("HOMING");break; |
Pokitto | 8:809b1982b4ae | 1238 | case CHASE: |
Pokitto | 8:809b1982b4ae | 1239 | gb.display.println("CHASE");break; |
Pokitto | 8:809b1982b4ae | 1240 | case INTERCEPT: |
Pokitto | 8:809b1982b4ae | 1241 | gb.display.println("ITERC");break; |
Pokitto | 8:809b1982b4ae | 1242 | case GOTO: |
Pokitto | 8:809b1982b4ae | 1243 | gb.display.println("GOTO");break; |
Pokitto | 8:809b1982b4ae | 1244 | case TURNCW: |
Pokitto | 8:809b1982b4ae | 1245 | gb.display.println("TURNCW");break; |
Pokitto | 8:809b1982b4ae | 1246 | } |
Pokitto | 8:809b1982b4ae | 1247 | |
Pokitto | 8:809b1982b4ae | 1248 | gb.display.print(player.x);gb.display.print(",");gb.display.println(player.y); |
Pokitto | 8:809b1982b4ae | 1249 | gb.display.println(timeofday>>8); |
Pokitto | 8:809b1982b4ae | 1250 | #endif // DEBUG |
Pokitto | 8:809b1982b4ae | 1251 | break; |
Pokitto | 8:809b1982b4ae | 1252 | case GAMEOVER: |
Pokitto | 8:809b1982b4ae | 1253 | |
Pokitto | 8:809b1982b4ae | 1254 | timeout = 0xf; |
Pokitto | 8:809b1982b4ae | 1255 | while (timeout--) gb.update(); |
Pokitto | 8:809b1982b4ae | 1256 | while(!gb.buttons.pressed(BTN_A)&&gb.isRunning()) { |
Pokitto | 8:809b1982b4ae | 1257 | if (gb.update()) { |
Pokitto | 8:809b1982b4ae | 1258 | gb.display.setFont(fontRunes); |
Pokitto | 8:809b1982b4ae | 1259 | gb.display.setCursor(0,20); |
Pokitto | 8:809b1982b4ae | 1260 | gb.display.setColor(14,0); |
Pokitto | 8:809b1982b4ae | 1261 | gb.display.println(" GAME OVER ");gb.display.println(""); |
Pokitto | 8:809b1982b4ae | 1262 | gb.display.setColor(9); |
Pokitto | 8:809b1982b4ae | 1263 | gb.display.println(" Player got eaten "); |
Pokitto | 8:809b1982b4ae | 1264 | gb.display.println(" by the Beast! "); |
Pokitto | 8:809b1982b4ae | 1265 | gb.display.println(""); |
Pokitto | 8:809b1982b4ae | 1266 | gb.display.setColor(6); |
Pokitto | 8:809b1982b4ae | 1267 | gb.display.palette[6]=65312; |
Pokitto | 8:809b1982b4ae | 1268 | gb.display.print(" Gold collected: "); |
Pokitto | 8:809b1982b4ae | 1269 | gb.display.print((uint16_t)float(playergolds*100/(gamelevel*GOLDMULT))); |
Pokitto | 8:809b1982b4ae | 1270 | gb.display.println("%"); |
Pokitto | 8:809b1982b4ae | 1271 | |
Pokitto | 8:809b1982b4ae | 1272 | } |
Pokitto | 8:809b1982b4ae | 1273 | } |
Pokitto | 8:809b1982b4ae | 1274 | despawnAllWolves(); |
Pokitto | 8:809b1982b4ae | 1275 | wx=-8,wy=0,cx=10,cy=10; |
Pokitto | 8:809b1982b4ae | 1276 | prevtile=0, playertile=0, focustile=0, centertile=0,oldcenter, tween_palette[16], golds=0, playergolds=0; |
Pokitto | 8:809b1982b4ae | 1277 | timeofday=0; //value>>8 0 is full daytime, 127 is full nightwave=0, oldtime=0,wolvestick=0,watertick=0; |
Pokitto | 8:809b1982b4ae | 1278 | wading=1; |
Pokitto | 8:809b1982b4ae | 1279 | focusing=false; |
Pokitto | 8:809b1982b4ae | 1280 | inHut = false; |
Pokitto | 8:809b1982b4ae | 1281 | tweenfactor=0,numwolves=0; |
Pokitto | 8:809b1982b4ae | 1282 | gamelevel=1; |
Pokitto | 8:809b1982b4ae | 1283 | initworld(); |
Pokitto | 8:809b1982b4ae | 1284 | gamestate=TITLE; |
Pokitto | 8:809b1982b4ae | 1285 | break; |
Pokitto | 8:809b1982b4ae | 1286 | } |
Pokitto | 8:809b1982b4ae | 1287 | } |
Pokitto | 8:809b1982b4ae | 1288 | } |
Pokitto | 8:809b1982b4ae | 1289 | } |
Pokitto | 8:809b1982b4ae | 1290 | |
Pokitto | 8:809b1982b4ae | 1291 | |
Pokitto | 8:809b1982b4ae | 1292 | |
Pokitto | 8:809b1982b4ae | 1293 |