#ifndef _RAILWAY_H_ #define _RAILWAY_H_ #include "modelbahn.h" #include "server.h" enum { RAILWAY_NOTHING = 0, RAILWAY_NORMAL, RAILWAY_CROSSING, RAILWAY_TURNOUT, RAILWAY_SENSOR, RAILWAY_CONNECTOR, RAILWAY_BUTTON, // just a simple button on the track to press RAILWAY_TEXT, RAILWAY_BLOCK, RAILWAY_MAX }; #define RAILWAYS_MIN_WIDTH 40 #define RAILWAYS_MIN_HEIGHT 25 #define RAILWAYS_MAX_WIDTH 500 #define RAILWAYS_MAX_HEIGHT 500 // direktion // // +---+ +---+ +---+ +---+ // | | | | | | | |/ | // 0| | 1| | | 2|---| 3| | // | | | | | | | | | // +---+ +---+ +---+ +---+ // // +---+ +---+ +---+ +---+ // | \| | | | | | | // 4| | 5| | 6| | 7| | // | | | /| |\ | | | // +---+ +---+ +---+ +---+ // // Enter from // 0 // +---+ // | | // 3| |1 // | | // +---+ // 2 enum { EF_NORTH = 0, EF_EAST, EF_SOUTH, EF_WEST }; struct s_Railway { int type; int x; int y; int dir; int altdir; // turnout or crossing int maxspeed; int flags; // not defined yet char name[REFERENCENAME_LEN]; // reference name char lockedby[REFERENCENAME_LEN]; // element locked by locreference (only set by server, not by JSON/Webinterface) } typedef Railway; struct s_findway_data { int x; int y; int enterfrom; // enter field from clock view (0 = North, 3 = East... 6 ... 9) int oldx; int oldy; int oldenterfrom; int dist; string way; }; struct s_findway_map { int e; // save entryfrom as (1 << entryfrom) int score; }; class Railways { private: Railway *railways; int width; int height; int changed; pthread_mutex_t mtx; int Lock(); int UnLock(); int GetRIdx(int x, int y); JSONParse _GetJSONRailway(int x, int y); void _New (int w, int h); void DebugPrintFindWay(struct s_findway_map *fw); int _FindReference(int *x, int *y, string name, int cnt); int _SplitBlockIsFreeAndAllowed(string loconame, int locoflags, string block); int _NextPosIsValid(string loconame, int locoflags, struct s_findway_data *fwd); string GetDestBlock(int locoflags, string blockend); public: Railways(); ~Railways(); void New (int w, int h); void SetSize (int w, int h); Railway Get(int x, int y); Railways* GetBlock(int x, int y, int w, int h); int Set(Railway *rw); int SetBlock(Railway *rw, int cnt); int GetHeight () {return height;}; int GetWidth () {return width;}; int IsChanged() { return changed; }; void ClearChanged() { changed = 0; }; int Change(Railway *rw); void ClearLockedby(string name); Railway RailwayGet(int x, int y) {return railways[GetRIdx(x, y)];}; // no lock JSONParse GetJSONRailway(int x, int y); JSONParse GetJSONTrack(); void GetJSONAll(JSONParse *json); Railway GetRailwayFromJSON(JSONParse *j); int FindReference(int *x, int *y, string name, int cnt); int FindWay(string blockstart, string blockend, string lockedfor, string *next); int FindWayCheckIfNoDoubleTurnouts (string *next); int FindRandomWay(string blockstart, string lockedfor, string *next); int LockWay (string way, string lockedby, int lockonoff); int LockWay (string way, string lockedby) { return LockWay(way, lockedby, 1); }; int UnLockWay (string way, string lockedby) { return LockWay(way, lockedby, 0); }; int SetLockedby (int x, int y, string name, int locked); struct s_findway_data NextPos(struct s_findway_data pos, int dirtype); }; #endif