| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: signpost.cpp ( POWDER Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * Tracks signposts and their values. Creates a heap of signposts |
| 14 | * which can be queried to get a sign at a position. O(N) to |
| 15 | * access so be wary! |
| 16 | */ |
| 17 | |
| 18 | #include "signpost.h" |
| 19 | #include "sramstream.h" |
| 20 | |
| 21 | void |
| 22 | SIGNPOSTDATA::init(SIGNPOST_NAMES sign, int x, int y) |
| 23 | { |
| 24 | myPos = y & 31; |
| 25 | myPos <<= 5; |
| 26 | myPos |= x & 31; |
| 27 | mySign = sign; |
| 28 | } |
| 29 | |
| 30 | bool |
| 31 | SIGNPOSTDATA::isPos(int x, int y) const |
| 32 | { |
| 33 | int x1, y1; |
| 34 | getPos(x1, y1); |
| 35 | return (x1 == x) && (y1 == y); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | SIGNPOSTDATA::getPos(int &x, int &y) const |
| 40 | { |
| 41 | x = myPos & 31; |
| 42 | y = myPos >> 5; |
| 43 | } |
| 44 | |
| 45 | SIGNPOST_NAMES |
| 46 | SIGNPOSTDATA::getSign() const |
| 47 | { |
| 48 | return (SIGNPOST_NAMES) mySign; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | SIGNPOSTDATA::save(SRAMSTREAM &os) const |
| 53 | { |
| 54 | int collapse; |
| 55 | |
| 56 | collapse = mySign << 10 | myPos; |
| 57 | os.write(collapse, 16); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | SIGNPOSTDATA::load(SRAMSTREAM &is) |
| 62 | { |
| 63 | int val; |
| 64 | |
| 65 | is.uread(val, 16); |
| 66 | myPos= val & 1023; |
| 67 | mySign= val >> 10; |
| 68 | } |
| 69 | |
| 70 | SIGNLIST::SIGNLIST() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | SIGNLIST::~SIGNLIST() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | SIGNPOST_NAMES |
| 79 | SIGNLIST::find(int x, int y) const |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < myPosts.entries(); i++) |
| 84 | { |
| 85 | if (myPosts(i).isPos(x, y)) |
| 86 | return myPosts(i).getSign(); |
| 87 | } |
| 88 | return SIGNPOST_NONE; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | SIGNLIST::append(SIGNPOST_NAMES sign, int x, int y) |
| 93 | { |
| 94 | SIGNPOSTDATA post; |
| 95 | |
| 96 | post.init(sign, x, y); |
| 97 | myPosts.append(post); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | SIGNLIST::save(SRAMSTREAM &os) const |
| 102 | { |
| 103 | int i; |
| 104 | |
| 105 | os.write(myPosts.entries(), 8); |
| 106 | for (i = 0; i < myPosts.entries(); i++) |
| 107 | { |
| 108 | myPosts(i).save(os); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | SIGNLIST::load(SRAMSTREAM &is) |
| 114 | { |
| 115 | int i; |
| 116 | int val; |
| 117 | |
| 118 | is.uread(val, 8); |
| 119 | for (i = 0; i < val; i++) |
| 120 | { |
| 121 | SIGNPOSTDATA post; |
| 122 | post.load(is); |
| 123 | |
| 124 | myPosts.append(post); |
| 125 | } |
| 126 | } |