Dependencies:   XBeeLib mbed HvZAlphaNumLib HvZServerLib

Committer:
etherealflaim
Date:
Sun Dec 12 19:35:00 2010 +0000
Revision:
0:9cdba0589ba2

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
etherealflaim 0:9cdba0589ba2 1 #ifndef _iHvZ
etherealflaim 0:9cdba0589ba2 2 #define _iHvZ
etherealflaim 0:9cdba0589ba2 3
etherealflaim 0:9cdba0589ba2 4 #include "mbed.h"
etherealflaim 0:9cdba0589ba2 5
etherealflaim 0:9cdba0589ba2 6
etherealflaim 0:9cdba0589ba2 7 #include <map>
etherealflaim 0:9cdba0589ba2 8 #include <string>
etherealflaim 0:9cdba0589ba2 9 #include <sstream>
etherealflaim 0:9cdba0589ba2 10 #include <fstream>
etherealflaim 0:9cdba0589ba2 11 class iHvZ;
etherealflaim 0:9cdba0589ba2 12
etherealflaim 0:9cdba0589ba2 13 #define STUN_STATUS_LED LED4
etherealflaim 0:9cdba0589ba2 14 #define STUN_DEFAULT_DURATION (15)
etherealflaim 0:9cdba0589ba2 15 #define STUN_INCUBATION_TIME (30)
etherealflaim 0:9cdba0589ba2 16 #include "Stun.hpp"
etherealflaim 0:9cdba0589ba2 17
etherealflaim 0:9cdba0589ba2 18 #define _XBEE_DEBUG 0
etherealflaim 0:9cdba0589ba2 19 #define XBEE_PINS_DIN_DOUT_RST_ON p28,p27,p26,p20
etherealflaim 0:9cdba0589ba2 20 #include "XBee.hpp"
etherealflaim 0:9cdba0589ba2 21
etherealflaim 0:9cdba0589ba2 22 #define TAG_TIMEOUT_TIME 5
etherealflaim 0:9cdba0589ba2 23 #define TAG_INPUT p23
etherealflaim 0:9cdba0589ba2 24 #include "Tag.hpp"
etherealflaim 0:9cdba0589ba2 25
etherealflaim 0:9cdba0589ba2 26 #define ALPHA_NUM_DISPLAY_PINS p13, p10, p8, p11, p9, p15, p6, p14, p5, p12, p7
etherealflaim 0:9cdba0589ba2 27 #include "AlphaNumDisplay.hpp"
etherealflaim 0:9cdba0589ba2 28
etherealflaim 0:9cdba0589ba2 29 #define ETH_PIN p21
etherealflaim 0:9cdba0589ba2 30 #include "udp.hpp"
etherealflaim 0:9cdba0589ba2 31
etherealflaim 0:9cdba0589ba2 32 typedef enum
etherealflaim 0:9cdba0589ba2 33 {
etherealflaim 0:9cdba0589ba2 34 STATUS_HUMAN = 0,
etherealflaim 0:9cdba0589ba2 35 STATUS_ZOMBIE
etherealflaim 0:9cdba0589ba2 36 } Status;
etherealflaim 0:9cdba0589ba2 37
etherealflaim 0:9cdba0589ba2 38 class iHvZ {
etherealflaim 0:9cdba0589ba2 39 private:
etherealflaim 0:9cdba0589ba2 40 // Device configuration
etherealflaim 0:9cdba0589ba2 41 string m_id; //< The ID (8-byte DeviceID or 16-bit MAC Address) of this iHvZ device
etherealflaim 0:9cdba0589ba2 42 Stun m_stun; //< The stun status class
etherealflaim 0:9cdba0589ba2 43 XBee m_xb; //< The XBee device
etherealflaim 0:9cdba0589ba2 44 Tag m_tag; //< The device-to-device tagging
etherealflaim 0:9cdba0589ba2 45 AlphaNumDisplay m_alphanum; //< The alphanumeric display
etherealflaim 0:9cdba0589ba2 46 UDP m_server; //< The UDP server class
etherealflaim 0:9cdba0589ba2 47
etherealflaim 0:9cdba0589ba2 48 // Game configuration
etherealflaim 0:9cdba0589ba2 49 unsigned m_stun_duration; //< The current stun duration time
etherealflaim 0:9cdba0589ba2 50 unsigned m_incubation_time; //< The zombie incubation time
etherealflaim 0:9cdba0589ba2 51
etherealflaim 0:9cdba0589ba2 52 // Tag IDs
etherealflaim 0:9cdba0589ba2 53 vector<string> m_tagids; //< The list of IDs (8-byte TagIDs) given out when stunned or tagged (antidotes, etc can add to this list)
etherealflaim 0:9cdba0589ba2 54
etherealflaim 0:9cdba0589ba2 55 // List of TagIDs of victims
etherealflaim 0:9cdba0589ba2 56 vector<string> m_victimids;
etherealflaim 0:9cdba0589ba2 57
etherealflaim 0:9cdba0589ba2 58 public:
etherealflaim 0:9cdba0589ba2 59 /// Start an iHvZ game with the given ID (should start out as the MAC address)
etherealflaim 0:9cdba0589ba2 60 inline iHvZ(string id)
etherealflaim 0:9cdba0589ba2 61 : m_id(id),
etherealflaim 0:9cdba0589ba2 62 m_stun(STUN_STATUS_LED),
etherealflaim 0:9cdba0589ba2 63 m_xb(id, XBEE_PINS_DIN_DOUT_RST_ON),
etherealflaim 0:9cdba0589ba2 64 m_tag(this, TAG_INPUT),
etherealflaim 0:9cdba0589ba2 65 m_server(this, ETH_PIN),
etherealflaim 0:9cdba0589ba2 66 m_alphanum(ALPHA_NUM_DISPLAY_PINS),
etherealflaim 0:9cdba0589ba2 67 m_stun_duration(STUN_DEFAULT_DURATION),
etherealflaim 0:9cdba0589ba2 68 m_incubation_time(STUN_INCUBATION_TIME)
etherealflaim 0:9cdba0589ba2 69 {
etherealflaim 0:9cdba0589ba2 70 srand(time(NULL));
etherealflaim 0:9cdba0589ba2 71 }
etherealflaim 0:9cdba0589ba2 72
etherealflaim 0:9cdba0589ba2 73 inline string itoa(int i)
etherealflaim 0:9cdba0589ba2 74 {
etherealflaim 0:9cdba0589ba2 75 static char buf[12];
etherealflaim 0:9cdba0589ba2 76 sprintf(buf, "%d", i);
etherealflaim 0:9cdba0589ba2 77 return string(buf);
etherealflaim 0:9cdba0589ba2 78 }
etherealflaim 0:9cdba0589ba2 79
etherealflaim 0:9cdba0589ba2 80 inline int atoi(string s)
etherealflaim 0:9cdba0589ba2 81 {
etherealflaim 0:9cdba0589ba2 82 int i;
etherealflaim 0:9cdba0589ba2 83 sscanf(s.c_str(), "%d", &i);
etherealflaim 0:9cdba0589ba2 84 return i;
etherealflaim 0:9cdba0589ba2 85 }
etherealflaim 0:9cdba0589ba2 86
etherealflaim 0:9cdba0589ba2 87 /* Filesystem save and load */
etherealflaim 0:9cdba0589ba2 88 /** Save the iHvZ state to iHvZ.cfg on the filesystem */
etherealflaim 0:9cdba0589ba2 89 inline bool save()
etherealflaim 0:9cdba0589ba2 90 {
etherealflaim 0:9cdba0589ba2 91 LocalFileSystem lfs("usb");
etherealflaim 0:9cdba0589ba2 92 Serial usb(USBTX,USBRX);
etherealflaim 0:9cdba0589ba2 93 ofstream statefile("/usb/iHvZ.cfg");
etherealflaim 0:9cdba0589ba2 94 map<string,string> params;
etherealflaim 0:9cdba0589ba2 95 int writecount = 0;
etherealflaim 0:9cdba0589ba2 96
etherealflaim 0:9cdba0589ba2 97 // Make sure the file opened
etherealflaim 0:9cdba0589ba2 98 if (!statefile) return false;
etherealflaim 0:9cdba0589ba2 99
etherealflaim 0:9cdba0589ba2 100 // Set the parameters
etherealflaim 0:9cdba0589ba2 101 //params["mode"] = (m_status==STATUS_HUMAN)?"HUMAN":"ZOMBIE";
etherealflaim 0:9cdba0589ba2 102 params["deviceid"] = m_id;
etherealflaim 0:9cdba0589ba2 103 params["stundur"] = itoa(m_stun_duration);
etherealflaim 0:9cdba0589ba2 104 params["inctime"] = itoa(m_incubation_time);
etherealflaim 0:9cdba0589ba2 105 params["tagcount"] = itoa(m_tagids.size());
etherealflaim 0:9cdba0589ba2 106 for (int i = 0; i < m_tagids.size(); ++i)
etherealflaim 0:9cdba0589ba2 107 {
etherealflaim 0:9cdba0589ba2 108 params["tagid[" + itoa(i) + "]"] = m_tagids[i];
etherealflaim 0:9cdba0589ba2 109 }
etherealflaim 0:9cdba0589ba2 110 params["victimcount"] = itoa(m_victimids.size());
etherealflaim 0:9cdba0589ba2 111 for (int i = 0; i < m_victimids.size(); ++i)
etherealflaim 0:9cdba0589ba2 112 {
etherealflaim 0:9cdba0589ba2 113 params["victim[" + itoa(i) + "]"] = m_victimids[i];
etherealflaim 0:9cdba0589ba2 114 }
etherealflaim 0:9cdba0589ba2 115
etherealflaim 0:9cdba0589ba2 116
etherealflaim 0:9cdba0589ba2 117 // Write to file
etherealflaim 0:9cdba0589ba2 118 for (map<string,string>::iterator iter = params.begin(); iter != params.end(); ++iter)
etherealflaim 0:9cdba0589ba2 119 {
etherealflaim 0:9cdba0589ba2 120 statefile << iter->first << "=" << iter->second << endl;
etherealflaim 0:9cdba0589ba2 121 ++writecount;
etherealflaim 0:9cdba0589ba2 122 }
etherealflaim 0:9cdba0589ba2 123
etherealflaim 0:9cdba0589ba2 124 // Write status to USB
etherealflaim 0:9cdba0589ba2 125 usb.printf("Successfully wrote %d parameters to iHvZ.cfg\r\n", writecount);
etherealflaim 0:9cdba0589ba2 126
etherealflaim 0:9cdba0589ba2 127 // Update the display (in case stuff changed)
etherealflaim 0:9cdba0589ba2 128 m_alphanum.display(m_tagids.size()==0?'Z':'H');
etherealflaim 0:9cdba0589ba2 129
etherealflaim 0:9cdba0589ba2 130 // Success
etherealflaim 0:9cdba0589ba2 131 return true;
etherealflaim 0:9cdba0589ba2 132 }
etherealflaim 0:9cdba0589ba2 133
etherealflaim 0:9cdba0589ba2 134 /* Load the iHvZ state from iHvZ.cfg on the filesystem */
etherealflaim 0:9cdba0589ba2 135 inline bool load()
etherealflaim 0:9cdba0589ba2 136 {
etherealflaim 0:9cdba0589ba2 137 LocalFileSystem lfs("usb");
etherealflaim 0:9cdba0589ba2 138 Serial usb(USBTX,USBRX);
etherealflaim 0:9cdba0589ba2 139 ifstream statefile("/usb/iHvZ.cfg");
etherealflaim 0:9cdba0589ba2 140 map<string,string> params;
etherealflaim 0:9cdba0589ba2 141 int readcount = 0;
etherealflaim 0:9cdba0589ba2 142
etherealflaim 0:9cdba0589ba2 143 // Make sure the file opened
etherealflaim 0:9cdba0589ba2 144 if (statefile)
etherealflaim 0:9cdba0589ba2 145 {
etherealflaim 0:9cdba0589ba2 146 // Read in the lines of the file
etherealflaim 0:9cdba0589ba2 147 string line;
etherealflaim 0:9cdba0589ba2 148 while (getline(statefile, line))
etherealflaim 0:9cdba0589ba2 149 {
etherealflaim 0:9cdba0589ba2 150 int eqsign = line.find('=');
etherealflaim 0:9cdba0589ba2 151 if (eqsign == string::npos) continue;
etherealflaim 0:9cdba0589ba2 152 string param = line.substr(0,eqsign);
etherealflaim 0:9cdba0589ba2 153 string value = line.substr(eqsign+1);
etherealflaim 0:9cdba0589ba2 154 params[param] = value;
etherealflaim 0:9cdba0589ba2 155 ++readcount;
etherealflaim 0:9cdba0589ba2 156 }
etherealflaim 0:9cdba0589ba2 157
etherealflaim 0:9cdba0589ba2 158 // Read static parameters
etherealflaim 0:9cdba0589ba2 159 m_id = params["deviceid"];
etherealflaim 0:9cdba0589ba2 160 m_stun_duration = atoi(params["stundur"]);
etherealflaim 0:9cdba0589ba2 161 m_incubation_time = atoi(params["inctime"]);
etherealflaim 0:9cdba0589ba2 162
etherealflaim 0:9cdba0589ba2 163 // Read lives
etherealflaim 0:9cdba0589ba2 164 int tagcnt = atoi(params["tagcount"]);
etherealflaim 0:9cdba0589ba2 165 m_tagids.clear();
etherealflaim 0:9cdba0589ba2 166 m_tagids.reserve(tagcnt);
etherealflaim 0:9cdba0589ba2 167 for (int i = 0; i < tagcnt; ++i)
etherealflaim 0:9cdba0589ba2 168 {
etherealflaim 0:9cdba0589ba2 169 m_tagids.push_back( params["tagid[" + itoa(i) + "]"] );
etherealflaim 0:9cdba0589ba2 170 }
etherealflaim 0:9cdba0589ba2 171
etherealflaim 0:9cdba0589ba2 172 // Read victims
etherealflaim 0:9cdba0589ba2 173 int victimcnt = atoi(params["victimcount"]);
etherealflaim 0:9cdba0589ba2 174 m_victimids.clear();
etherealflaim 0:9cdba0589ba2 175 m_victimids.reserve(victimcnt);
etherealflaim 0:9cdba0589ba2 176 for (int i = 0; i < victimcnt; ++i)
etherealflaim 0:9cdba0589ba2 177 {
etherealflaim 0:9cdba0589ba2 178 m_victimids.push_back( params["victim[" + itoa(i) + "]"] );
etherealflaim 0:9cdba0589ba2 179 }
etherealflaim 0:9cdba0589ba2 180
etherealflaim 0:9cdba0589ba2 181 usb.printf("Successfully read %d parameters from /usb/iHvZ.cfg\r\n", readcount);
etherealflaim 0:9cdba0589ba2 182 }
etherealflaim 0:9cdba0589ba2 183 else
etherealflaim 0:9cdba0589ba2 184 {
etherealflaim 0:9cdba0589ba2 185 usb.printf("Unable to read /usb/iHvZ.cfg\r\n");
etherealflaim 0:9cdba0589ba2 186 }
etherealflaim 0:9cdba0589ba2 187 m_alphanum.display(m_tagids.size()==0?'Z':'H');
etherealflaim 0:9cdba0589ba2 188
etherealflaim 0:9cdba0589ba2 189 // Success or failure
etherealflaim 0:9cdba0589ba2 190 return readcount > 0;
etherealflaim 0:9cdba0589ba2 191 }
etherealflaim 0:9cdba0589ba2 192
etherealflaim 0:9cdba0589ba2 193
etherealflaim 0:9cdba0589ba2 194 /* Getters */
etherealflaim 0:9cdba0589ba2 195 /// Get the status
etherealflaim 0:9cdba0589ba2 196 inline bool status() { return m_tagids.size() ? STATUS_HUMAN : STATUS_ZOMBIE; }
etherealflaim 0:9cdba0589ba2 197 /// Get the id
etherealflaim 0:9cdba0589ba2 198 inline string id() { return m_id; }
etherealflaim 0:9cdba0589ba2 199 /// Get the next tagid
etherealflaim 0:9cdba0589ba2 200 inline string life()
etherealflaim 0:9cdba0589ba2 201 {
etherealflaim 0:9cdba0589ba2 202 if (m_tagids.size() == 0) return m_id;
etherealflaim 0:9cdba0589ba2 203 return m_tagids.back();
etherealflaim 0:9cdba0589ba2 204 }
etherealflaim 0:9cdba0589ba2 205 /// Get the victims vector
etherealflaim 0:9cdba0589ba2 206 inline vector<string> &get_victims() { return m_victimids; }
etherealflaim 0:9cdba0589ba2 207
etherealflaim 0:9cdba0589ba2 208 /// Get stun time
etherealflaim 0:9cdba0589ba2 209 inline unsigned stun_duration() { return m_stun_duration; }
etherealflaim 0:9cdba0589ba2 210 /// Get incubation time
etherealflaim 0:9cdba0589ba2 211 inline unsigned incubation_time() { return m_incubation_time; }
etherealflaim 0:9cdba0589ba2 212 /// Get the stun tracker
etherealflaim 0:9cdba0589ba2 213 inline Stun &stun_tracker() { return m_stun; }
etherealflaim 0:9cdba0589ba2 214 /// Get the tag tracker
etherealflaim 0:9cdba0589ba2 215 inline Tag &tag_tracker() { return m_tag; }
etherealflaim 0:9cdba0589ba2 216 /// Get the xbee device
etherealflaim 0:9cdba0589ba2 217 inline XBee &xbee_device() { return m_xb; }
etherealflaim 0:9cdba0589ba2 218 /// Get the UDP device
etherealflaim 0:9cdba0589ba2 219 //inline UDP &udp_device() { return m_server; }
etherealflaim 0:9cdba0589ba2 220 /// Get the alphanum device
etherealflaim 0:9cdba0589ba2 221 inline AlphaNumDisplay &alphanumdisplay_device() { return m_alphanum; }
etherealflaim 0:9cdba0589ba2 222
etherealflaim 0:9cdba0589ba2 223
etherealflaim 0:9cdba0589ba2 224 /* Setters */
etherealflaim 0:9cdba0589ba2 225 /// Set the id
etherealflaim 0:9cdba0589ba2 226 inline void id(string newid) { m_id = newid; m_xb.uid(newid); }
etherealflaim 0:9cdba0589ba2 227 /// Add a tagid
etherealflaim 0:9cdba0589ba2 228 inline void life(string newtagid) {
etherealflaim 0:9cdba0589ba2 229 m_tagids.push_back(newtagid);
etherealflaim 0:9cdba0589ba2 230
etherealflaim 0:9cdba0589ba2 231 m_alphanum.display('H');
etherealflaim 0:9cdba0589ba2 232 }
etherealflaim 0:9cdba0589ba2 233 /// Set stun time
etherealflaim 0:9cdba0589ba2 234 inline void stun_duration(unsigned stun) { m_stun_duration = stun; }
etherealflaim 0:9cdba0589ba2 235 /// Set incubation time
etherealflaim 0:9cdba0589ba2 236 inline void incubation_time(unsigned incubation) { m_incubation_time = incubation; }
etherealflaim 0:9cdba0589ba2 237
etherealflaim 0:9cdba0589ba2 238 /* Meta-actions */
etherealflaim 0:9cdba0589ba2 239 inline void tagged()
etherealflaim 0:9cdba0589ba2 240 {
etherealflaim 0:9cdba0589ba2 241 if (status() == STATUS_ZOMBIE) return;
etherealflaim 0:9cdba0589ba2 242 // Should we have an admin flag?
etherealflaim 0:9cdba0589ba2 243
etherealflaim 0:9cdba0589ba2 244 // Use up the next player life or antidote
etherealflaim 0:9cdba0589ba2 245 if (m_tagids.size() > 0) m_tagids.pop_back();
etherealflaim 0:9cdba0589ba2 246
etherealflaim 0:9cdba0589ba2 247 // If the player still has lives, they are fine
etherealflaim 0:9cdba0589ba2 248 if (m_tagids.size() > 0) return;
etherealflaim 0:9cdba0589ba2 249
etherealflaim 0:9cdba0589ba2 250 // This player is now a zombie!
etherealflaim 0:9cdba0589ba2 251 m_stun.stun(m_incubation_time);
etherealflaim 0:9cdba0589ba2 252
etherealflaim 0:9cdba0589ba2 253 // Save the stun to prevent power cycle cheating
etherealflaim 0:9cdba0589ba2 254 save();
etherealflaim 0:9cdba0589ba2 255 wait(1);
etherealflaim 0:9cdba0589ba2 256 m_alphanum.display('Z');
etherealflaim 0:9cdba0589ba2 257 }
etherealflaim 0:9cdba0589ba2 258
etherealflaim 0:9cdba0589ba2 259 // Register the tagging of the given tagID by this device
etherealflaim 0:9cdba0589ba2 260 inline void register_tag(string tagid) { m_victimids.push_back(tagid); }
etherealflaim 0:9cdba0589ba2 261
etherealflaim 0:9cdba0589ba2 262 // Clear the victim list
etherealflaim 0:9cdba0589ba2 263 inline void clear_victims() { m_victimids.clear(); }
etherealflaim 0:9cdba0589ba2 264 inline void clear_lives() { m_tagids.clear(); }
etherealflaim 0:9cdba0589ba2 265
etherealflaim 0:9cdba0589ba2 266 inline void stunned()
etherealflaim 0:9cdba0589ba2 267 {
etherealflaim 0:9cdba0589ba2 268 if (status() == STATUS_HUMAN) return;
etherealflaim 0:9cdba0589ba2 269
etherealflaim 0:9cdba0589ba2 270 // This zombie is now stunned
etherealflaim 0:9cdba0589ba2 271 m_stun.stun(m_stun_duration);
etherealflaim 0:9cdba0589ba2 272
etherealflaim 0:9cdba0589ba2 273 // Save the stun to prevent power cycle cheating
etherealflaim 0:9cdba0589ba2 274 save();
etherealflaim 0:9cdba0589ba2 275 }
etherealflaim 0:9cdba0589ba2 276
etherealflaim 0:9cdba0589ba2 277 inline void register_stun(string tagid) {}
etherealflaim 0:9cdba0589ba2 278 };
etherealflaim 0:9cdba0589ba2 279
etherealflaim 0:9cdba0589ba2 280
etherealflaim 0:9cdba0589ba2 281 #endif