// Sets the value for a string in the map // Returns 1 if this was an update, 0 if // the string is new. int mapSet (StringMap *map, char *string, int value) { unsigned shash, index; MapItem *item = findMapItem(map, string); // Replace old value, if exists if (item) { item->value = value; return 1; } // Check if array needs to be expanded if ((float) map->size / map->arrSize > MAX_LOAD_FACTOR) { expand(map); } // Add the item shash = hash(string); index = shash % map->arrSize; map->array[index] = makeMapItem( string, value, shash, map->array[index]); map->size++; return 0; }
void QMapViewWidget::mousePressEvent(QMouseEvent *event) { switch (pMode) { case 0: { QRect rect = pBackBuffer.rect(); rect.moveCenter(QPoint(width()/2-1, height()/2-1)); if (rect.contains(event->pos())) { QPoint scenePos = event->pos() - rect.topLeft(); scenePos.setX(scenePos.x() / pRectSize); scenePos.setY(scenePos.y() / pRectSize); QPoint pos; switch (pSelectedItem) { case -4: // red token pos = findMapItem('r'); pNewTokenType = 'r'; break; case -5: // green token pos = findMapItem('g'); pNewTokenType = 'g'; break; case -6: // blue token pos = findMapItem('b'); pNewTokenType = 'b'; break; case -7: // red dest pos = findMapItem('R'); pNewTokenType = 'R'; break; case -8: // green dest pos = findMapItem('G'); pNewTokenType = 'G'; break; case -9: // blue dest pos = findMapItem('B'); pNewTokenType = 'B'; break; default: pos = QPoint(-1, -1); pNewTokenType = 0; break; } if ((pos.x() != -1) && (pos.y() != -1)) pMap[pos.y()][pos.x()] = '.'; redrawMap(); update(); } mouseMoveEvent(event); } break; case 1: { if (pIsAnimationRun) return; QRect rect = pBackBuffer.rect(); rect.moveCenter(QPoint(width()/2-1, height()/2-1)); if (rect.contains(event->pos())) { QPoint scenePos = event->pos() - rect.topLeft(); scenePos.setX(scenePos.x() / pRectSize); scenePos.setY(scenePos.y() / pRectSize); char c = getTokenType(pMap[scenePos.y()][scenePos.x()]); if ( (c == 'r') || (c == 'g') || (c == 'b') ) { pSelToken = scenePos; pSelTokenType = c; pNewToken = scenePos; pNewTokenType = c; pSelDir = 0; update(); } else pSelTokenType = 0; } } break; default: break; } }
// Checks if a string is in the array int mapIn (StringMap *map, char *string) { return findMapItem(map, string) != NULL; }
// Gets an int value from the map // (Returns 0 if the string does not exist in the map) int mapGet (StringMap *map, char *string) { MapItem *item = findMapItem(map, string); if (item) return item->value; return 0; }