00001 /* position.h 00002 * 00003 * Micropolis, Unix Version. This game was released for the Unix platform 00004 * in or about 1990 and has been modified for inclusion in the One Laptop 00005 * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If 00006 * you need assistance with this program, you may contact: 00007 * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org. 00008 * 00009 * This program is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or (at 00012 * your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, but 00015 * WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * General Public License for more details. You should have received a 00018 * copy of the GNU General Public License along with this program. If 00019 * not, see <http://www.gnu.org/licenses/>. 00020 * 00021 * ADDITIONAL TERMS per GNU GPL Section 7 00022 * 00023 * No trademark or publicity rights are granted. This license does NOT 00024 * give you any right, title or interest in the trademark SimCity or any 00025 * other Electronic Arts trademark. You may not distribute any 00026 * modification of this program using the trademark SimCity or claim any 00027 * affliation or association with Electronic Arts Inc. or its employees. 00028 * 00029 * Any propagation or conveyance of this program must include this 00030 * copyright notice and these terms. 00031 * 00032 * If you convey this program (or any modifications of it) and assume 00033 * contractual liability for the program to recipients of it, you agree 00034 * to indemnify Electronic Arts for any liability that those contractual 00035 * assumptions impose on Electronic Arts. 00036 * 00037 * You may not misrepresent the origins of this program; modified 00038 * versions of the program must be marked as such and not identified as 00039 * the original program. 00040 * 00041 * This disclaimer supplements the one included in the General Public 00042 * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS 00043 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY 00044 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF 00045 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS 00046 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES, 00047 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY, 00048 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY 00049 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING, 00050 * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST 00051 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL 00052 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE 00053 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE 00054 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE 00055 * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR 00056 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME 00057 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED 00058 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A 00059 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY 00060 * NOT APPLY TO YOU. 00061 */ 00062 00065 #ifndef H_POSITION 00066 #define H_POSITION 00067 00072 enum Direction2 { 00073 DIR2_INVALID, 00074 DIR2_NORTH, 00075 DIR2_NORTH_EAST, 00076 DIR2_EAST, 00077 DIR2_SOUTH_EAST, 00078 DIR2_SOUTH, 00079 DIR2_SOUTH_WEST, 00080 DIR2_WEST, 00081 DIR2_NORTH_WEST, 00082 00083 DIR2_BEGIN = DIR2_NORTH, 00084 DIR2_END = DIR2_NORTH_WEST + 1, 00085 }; 00086 00092 static inline Direction2 increment45(Direction2 dir, int count = 1) 00093 { 00094 return (Direction2)(dir + count); 00095 } 00096 00097 00103 static inline Direction2 increment90(Direction2 dir) 00104 { 00105 return increment45(dir, 2); 00106 } 00107 00113 static inline Direction2 rotate45(Direction2 dir, int count = 1) 00114 { 00115 return (Direction2)(((dir - DIR2_NORTH + count) & 7) + DIR2_NORTH); 00116 } 00117 00123 static inline Direction2 rotate90(Direction2 dir) 00124 { 00125 return rotate45(dir, 2); 00126 } 00127 00133 static inline Direction2 rotate180(Direction2 dir) 00134 { 00135 return rotate45(dir, 4); 00136 } 00137 00138 00140 class Position { 00141 00142 public: 00143 00144 Position(); 00145 Position(int x, int y); 00146 Position(const Position &pos); 00147 Position(const Position &pos, Direction2 dir); 00148 Position(const Position &pos, int dx, int dy); 00149 Position &operator=(const Position &pos); 00150 00151 bool move(Direction2 dir); 00152 inline bool testBounds(); 00153 00154 int posX; 00155 int posY; 00156 }; 00157 00158 00163 inline bool Position::testBounds() 00164 { 00165 return (this->posX >= 0 && this->posX < WORLD_W 00166 && this->posY >= 0 && this->posY < WORLD_H); 00167 } 00168 00176 inline bool operator<(const Position &pos1, const Position &pos2) 00177 { 00178 if (pos1.posX != pos2.posX) return pos1.posX < pos2.posX; 00179 return pos1.posY < pos2.posY; 00180 } 00181 00182 #endif 00183
1.5.6