File indexing completed on 2025-03-09 03:49:43
0001 /* 0002 SPDX-FileCopyrightText: 1998 Anders Widell <d95-awi@nada.kth.se> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "LevelMap.h" 0007 0008 #include "LevelCollection.h" 0009 0010 #include <KConfig> 0011 0012 #include <unistd.h> 0013 0014 #include <cassert> 0015 #include <cctype> 0016 #include <cstdio> 0017 #include <cstdlib> 0018 #include <cstring> 0019 0020 const QString &LevelMap::collectionName() const 0021 { 0022 return collection_->name(); 0023 } 0024 0025 LevelMap::LevelMap() = default; 0026 0027 LevelMap::~LevelMap() = default; 0028 0029 void LevelMap::changeCollection(LevelCollection *_collection) 0030 { 0031 collection_ = _collection; 0032 goodLevel_ = collection_->loadLevel(&map_); 0033 totalMoves_ = totalPushes_ = 0; 0034 } 0035 0036 int LevelMap::level() const 0037 { 0038 if (collection_ == nullptr) 0039 return 0; 0040 return collection_->level(); 0041 } 0042 0043 void LevelMap::level(int _level) 0044 { 0045 assert(collection_ != nullptr); 0046 0047 collection_->level(_level); 0048 goodLevel_ = collection_->loadLevel(&map_); 0049 0050 totalMoves_ = totalPushes_ = 0; 0051 } 0052 0053 int LevelMap::noOfLevels() const 0054 { 0055 assert(collection_ != nullptr); 0056 return collection_->noOfLevels(); 0057 } 0058 0059 int LevelMap::completedLevels() const 0060 { 0061 assert(collection_ != nullptr); 0062 return collection_->completedLevels(); 0063 } 0064 0065 int LevelMap::distance(int x1, int y1, int x2, int y2) 0066 { 0067 int d; 0068 0069 if (x2 > x1) 0070 d = x2 - x1; 0071 else 0072 d = x1 - x2; 0073 0074 if (y2 > y1) 0075 d += y2 - y1; 0076 else 0077 d += y1 - y2; 0078 0079 return d; 0080 } 0081 0082 bool LevelMap::step(int _x, int _y) 0083 { 0084 const int oldX = map_.xpos(); 0085 const int oldY = map_.ypos(); 0086 0087 bool success = map_.step(_x, _y); 0088 0089 const int d = distance(oldX, oldY, map_.xpos(), map_.ypos()); 0090 totalMoves_ += d; 0091 0092 return success; 0093 } 0094 0095 bool LevelMap::push(int _x, int _y) 0096 { 0097 const int oldX = map_.xpos(); 0098 const int oldY = map_.ypos(); 0099 0100 bool success = map_.push(_x, _y); 0101 0102 const int d = distance(oldX, oldY, map_.xpos(), map_.ypos()); 0103 totalMoves_ += d; 0104 totalPushes_ += d; 0105 0106 if (map_.completed()) 0107 collection_->levelCompleted(); 0108 0109 return success; 0110 } 0111 0112 bool LevelMap::unstep(int _x, int _y) 0113 { 0114 const int oldX = map_.xpos(); 0115 const int oldY = map_.ypos(); 0116 0117 bool success = map_.unstep(_x, _y); 0118 0119 const int d = distance(oldX, oldY, map_.xpos(), map_.ypos()); 0120 totalMoves_ -= d; 0121 0122 return success; 0123 } 0124 0125 bool LevelMap::unpush(int _x, int _y) 0126 { 0127 const int oldX = map_.xpos(); 0128 const int oldY = map_.ypos(); 0129 0130 bool success = map_.unpush(_x, _y); 0131 0132 const int d = distance(oldX, oldY, map_.xpos(), map_.ypos()); 0133 totalMoves_ -= d; 0134 totalPushes_ -= d; 0135 0136 return success; 0137 } 0138 0139 #if 0 0140 void 0141 LevelMap::random (void) { 0142 printf ("start!\n"); 0143 0144 minX_ = 0; 0145 minY_ = 0; 0146 maxX_ = MAX_X; 0147 maxY_ = MAX_Y; 0148 totalMoves_ = totalPushes_ = 0; 0149 clearMap (); 0150 0151 xpos_ = 13; 0152 ypos_ = 9; 0153 0154 KRandomSequence random(0); 0155 0156 for (int i=0; i<200; i++) { 0157 map (xpos_, ypos_, FLOOR); 0158 0159 switch (random.getLong(4)) { 0160 case 0: 0161 if (ypos_ > 1) ypos_--; else i--; 0162 break; 0163 0164 case 1: 0165 if (ypos_ < MAX_Y-1) ypos_++; else i--; 0166 break; 0167 0168 case 2: 0169 if (xpos_ > 1) xpos_--; else i--; 0170 break; 0171 0172 case 3: 0173 if (xpos_ < MAX_X-1) xpos_++; else i--; 0174 break; 0175 } 0176 } 0177 0178 for (int y=1; y<MAX_Y; y++) { 0179 for (int x=1; x<MAX_X; x++) { 0180 if (map (x, y) & FLOOR) { 0181 if (!(map (x, y-1) & FLOOR)) map (x, y-1, WALL); 0182 if (!(map (x, y+1) & FLOOR)) map (x, y+1, WALL); 0183 if (!(map (x-1, y) & FLOOR)) map (x-1, y, WALL); 0184 if (!(map (x+1, y) & FLOOR)) map (x+1, y, WALL); 0185 } 0186 } 0187 } 0188 0189 printf ("klar!\n"); 0190 printMap (); 0191 } 0192 #endif