/* Choose a place to start growing an island from */ static int place_island(int c, int *xp, int *yp) { int d, sx, sy; int ssy = roll0(WORLD_Y); int ssx = new_x(roll0(WORLD_X / 2) * 2 + ssy % 2); if (ssx > WORLD_X - 2) ssx = new_x(ssx + 2); for (d = di + id; d >= id; --d) { sx = ssx; sy = ssy; *xp = new_x(sx + 2); for (*yp = sy; *xp != sx || *yp != sy; *xp += 2) { if (*xp >= WORLD_X) { *yp = new_y(*yp + 1); *xp = *yp % 2; if (*xp == sx && *yp == sy) break; } if (own[*xp][*yp] == -1 && try_to_grow(c, *xp, *yp, d)) return 1; } } return 0; }
void Cursor::removeTabStop() { for (int i = 0; i < m_tab_stops.size(); i++) { if (new_x() == m_tab_stops.at(i)) { m_tab_stops.remove(i); return; } else if (new_x() < m_tab_stops.at(i)) { return; } } }
void Cursor::moveLeft(int positions) { if (!new_x() || !positions) return; if (positions < new_x()) { new_rx() -= positions; } else { new_rx() = 0; } notifyChanged(); }
void Cursor::moveRight(int positions) { int width = m_screen->width(); if (new_x() == width -1 || !positions) return; if (positions < width - new_x()) { new_rx() += positions; } else { new_rx() = width -1; } notifyChanged(); }
void Cursor::replaceAtCursor(const QByteArray &data) { //if (m_selection_valid ) { // if (current_cursor_new_y() >= m_selection_start.new_y() && current_cursor_new_y() <= m_selection_end.new_y()) // //don't need to schedule as event since it will only happen once // setSelectionEnabled(false); //} const QString text = m_gl_text_codec->toUnicode(data); if (new_x() + text.size() <= m_screen->width()) { Block *block = screen_data()->blockContainingLine(new_y()); block->replaceAtPos(new_x(), text, m_current_text_style); new_rx() += text.size(); } else if (m_wrap_around) { Block *block = screen_data()->blockContainingLine(new_y()); block->replaceAtPos(new_x(), text, m_current_text_style); int line_feeds = (new_x() + text.size()) / m_screen->width(); new_rx() = (new_x() + text.size()) % m_screen->width(); for (int i = 0; i < line_feeds; i++) { lineFeed(); } }else { const int size = m_document_width - new_x(); QString toBlock = text.mid(0,size); toBlock.replace(toBlock.size() - 1, 1, text.at(text.size()-1)); Block *block = screen_data()->blockContainingLine(new_y()); block->replaceAtPos(new_x(),toBlock, m_current_text_style); new_rx() += toBlock.size(); } if (new_x() >= m_document_width) new_rx() = m_document_width - 1; notifyChanged(); }
void Cursor::setTabStop() { int i; for (i = 0; i < m_tab_stops.size(); i++) { if (new_x() == m_tab_stops.at(i)) return; if (new_x() > m_tab_stops.at(i)) { continue; } else { break; } } m_tab_stops.insert(i,new_x()); }
/* Grow all the continents */ static void grow_continents(void) { int c; for (c = 0; c < nc; ++c) { sectx[c][0] = capx[c]; secty[c][0] = capy[c]; own[sectx[c][0]][secty[c][0]] = c; sectx[c][1] = new_x(capx[c] + 2); secty[c][1] = capy[c]; own[sectx[c][1]][secty[c][1]] = c; } for (secs = 2; secs < sc && !fl_status; ++secs) { for (c = 0; c < nc; ++c) { find_coast(c); grow_one_sector(c); } } for (c = 0; c < nc; ++c) find_coast(c); if (fl_status) qprint("Only managed to grow %d out of %d sectors.\n", secs, sc); ctot = nc; }
/* Test to see if we're allowed to grow there: the arguments di and id */ static int try_to_grow(int c, int newx, int newy, int d) { int i, j, px, py; for (i = 1; i <= d; ++i) { for (j = 0; j < i; ++j) vector[j] = 0; do { px = newx; py = newy; for (j = 0; j < i; ++j) { px = new_x(px + dirx[vector[j]]); py = new_y(py + diry[vector[j]]); } if (own[px][py] != -1 && own[px][py] != c && (DISTINCT_ISLANDS || own[px][py] < nc)) return 0; } while (next_vector(i)); } sectx[c][secs] = newx; secty[c][secs] = newy; own[newx][newy] = c; return 1; }
/* Generic function for finding the distance to the closest sea, land, or mountain */ static int distance_to_what(int x, int y, int flag) { int j, d, px, py; for (d = 1; d < 5; ++d) { for (j = 0; j < d; ++j) vector[j] = 0; do { px = x; py = y; for (j = 0; j < d; ++j) { px = new_x(px + dirx[vector[j]]); py = new_y(py + diry[vector[j]]); } switch (flag) { case 0: /* distance to sea */ if (own[px][py] == -1) return d; break; case 1: /* distance to land */ if (own[px][py] != -1) return d; break; case 2: /* distance to mountain */ if (elev[px][py] == INFINITY) return d; break; } } while (next_vector(d)); } return d; }
void Cursor::moveToNextTab() { for (int i = 0; i < m_tab_stops.size(); i++) { if (new_x() < m_tab_stops.at(i)) { moveToCharacter(std::min(m_tab_stops.at(i), m_screen_width -1)); return; } } moveToCharacter(m_screen_width - 1); }
static int grow_one_sector(int c) { int done, coast_search, try1, x, y, newx, newy, i, n, sx, sy; spike = roll0(100) < sp; if ((try1 = new_try(c)) == -1) return 0; x = sx = sectx[c][try1]; y = sy = secty[c][try1]; coast_search = 0; done = 0; do { if (spike) { for (i = roll0(6), n = 0; n < 12 && !done; i = (i + 1) % 6, ++n) { newx = new_x(x + dirx[i]); newy = new_y(y + diry[i]); if (own[newx][newy] == -1 && (n > 5 || (own[new_x(x+dirx[(i+5)%6])][new_y(y+diry[(i+5)%6])] == -1 && own[new_x(x+dirx[(i+1)%6])][new_y(y+diry[(i+1)%6])] == -1))) if (try_to_grow(c, newx, newy, c < nc ? di : id)) done = 1; } } else for (i = roll0(6), n = 0; n < 6 && !done; i = (i + 1) % 6, ++n) { newx = new_x(x + dirx[i]); newy = new_y(y + diry[i]); if (own[newx][newy] == -1) if (try_to_grow(c, newx, newy, c < nc ? di : id)) done = 1; } next_coast(c, x, y, &x, &y); ++coast_search; } while (!done && coast_search < COAST_SEARCH_MAX && (secs == 1 || x != sx || y != sy)); if (!done && c < nc) { qprint("fairland: error -- continent %c had no room to grow!\n", numletter[c % 62]); fl_status |= STATUS_NO_ROOM; } return done; }
void Cursor::insertAtCursor(const QByteArray &data, bool only_latin) { const QString text = m_gl_text_codec->toUnicode(data); auto diff = screen_data()->insert(m_new_position, text, m_current_text_style, only_latin); new_rx() += diff.character; new_ry() += diff.line; if (new_y() >= m_screen_height) new_ry() = m_screen_height - 1; if (new_x() >= m_screen_width) new_rx() = m_screen_width - 1; }
void Cursor::setScreenWidthAboutToChange(int width) { Q_UNUSED(width); auto it = m_screen->currentScreenData()->it_for_row(new_y()); if (m_screen->currentScreenData()->it_is_end(it)) return; m_resize_block = *it; int line_diff = new_y() - m_resize_block->screenIndex(); m_current_pos_in_block = (line_diff * m_screen_width) + new_x(); }
static void find_coast(int c) { int i, j; for (i = 0; i < secs; ++i) { sectc[c][i] = 0; for (j = 0; j < 6; ++j) if (own[new_x(sectx[c][i] + dirx[j])][new_y(secty[c][i] + diry[j])] == -1) sectc[c][i] = 1; } }
void Cursor::moveToCharacter(int character) { const int width = m_screen->width(); if (character < 0) { character = 1; } else if (character > width) { character = width; } if (character != new_x()) { new_rx() = character; notifyChanged(); } }
void Cursor::replaceAtCursor(const QByteArray &data, bool only_latin) { const QString text = m_gl_text_codec->toUnicode(data); if (!m_wrap_around && new_x() + text.size() > m_screen->width()) { const int size = m_screen_width - new_x(); QString toBlock = text.mid(0,size); toBlock.replace(toBlock.size() - 1, 1, text.at(text.size()-1)); screen_data()->replace(m_new_position, toBlock, m_current_text_style, only_latin); new_rx() += toBlock.size(); } else { auto diff = screen_data()->replace(m_new_position, text, m_current_text_style, only_latin); new_rx() += diff.character; new_ry() += diff.line; } if (new_y() >= m_screen_height) { new_ry() = m_screen_height - 1; } notifyChanged(); }
void Cursor::insertAtCursor(const QByteArray &data) { //if (m_selection_valid) { // if (current_cursor_new_y() >= m_selection_start.new_y() && current_cursor_new_y() <= m_selection_end.new_y()) // //don't need to schedule as event since it will only happen once // setSelectionEnabled(false); //} const QString text = m_gl_text_codec->toUnicode(data); Block *line = screen_data()->blockContainingLine(new_y()); line->insertAtPos(new_x(), text, m_screen->defaultTextStyle()); new_rx() += text.size(); }
static int map_symbol(int x, int y) { int c, iscap = 0; for (c = 0; c < nc; ++c) if ((x == capx[c] && y == capy[c]) || (x == new_x(capx[c] + 2) && y == capy[c])) iscap = 1; if ((elev[x][y] >= HILLMIN && elev[x][y] < PLATMIN) || elev[x][y] >= HIGHMIN) return '^'; return own[x][y] >= nc ? '%' : iscap ? '#' : numletter[own[x][y] % 62]; }
static void fl_move(int j) { int i, n, newx, newy; for (i = roll0(6), n = 0; n < 6; i = (i + 1) % 6, ++n) { newx = new_x(capx[j] + dirx[i]); newy = new_y(capy[j] + diry[i]); if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) { capx[j] = newx; capy[j] = newy; return; } } }
void Cursor::setDocumentWidth(int width) { if (width > m_document_width) { for (int i = m_document_width -1; i < width; i++) { if (i % 8 == 0) { m_tab_stops.append(i); } } } m_document_width = width; if (new_x() >= width) { new_rx() = width - 1; notifyChanged(); } }
static void next_coast(int c, int x, int y, int *xp, int *yp) { int i, nx, ny, wat = 0; if (secs == 1) { *xp = x; *yp = y; return; } for (i = 0; i < 12; ++i) { nx = new_x(x + dirx[i % 6]); ny = new_y(y + diry[i % 6]); if (own[nx][ny] == -1) wat = 1; if (wat && own[nx][ny] == c) { *xp = nx; *yp = ny; return; } } }
void next_x(){ new_x( x + 1 ); }
void Cursor::deleteCharacters(int characters) { screen_data()->deleteCharacters(m_new_position, new_x() + characters -1); }
void Cursor::clearToEndOfLine() { screen_data()->clearToEndOfLine(new_y(), new_x()); }
void Cursor::deleteCharacters(int characters) { screen_data()->deleteCharacters(new_y(), new_x(), new_x() + characters -1); }
void Cursor::clearToBeginningOfLine() { screen_data()->clearToBeginningOfLine(new_y(),new_x()); }
/* Decide where the mountains go */ static void elevate_land(void) { int i, mountain_search, k, c, total, ns, nm, highest, where, h, newk, r, dk; for (c = 0; c < ctot; ++c) { total = 0; ns = (c < nc) ? sc : isecs[c]; nm = (pm * ns) / 100; /* Place the mountains */ for (i = 0; i < ns; ++i) { dsea[i] = distance_to_sea(); weight[i] = (total += (dsea[i] * dsea[i])); } for (k = nm, mountain_search = 0; k && mountain_search < MOUNTAIN_SEARCH_MAX; ++mountain_search) { r = roll0(total); for (i = 0; i < ns; ++i) if (r < weight[i] && ELEV == -INFINITY && (c >= nc || ((!(capx[c] == sectx[c][i] && capy[c] == secty[c][i])) && (!(new_x(capx[c] + 2) == sectx[c][i] && capy[c] == secty[c][i]))))) { ELEV = INFINITY; break; } --k; } /* Elevate land that is not mountain and not capital */ for (i = 0; i < ns; ++i) dmoun[i] = distance_to_mountain(); dk = (ns - nm - ((c < nc) ? 3 : 1) > 0) ? (100 * (HIGHMIN - LANDMIN)) / (ns - nm - ((c < nc) ? 3 : 1)) : 100 * INFINITY; for (k = 100 * (HIGHMIN - 1);; k -= dk) { highest = -INFINITY; where = -1; for (i = 0; i < ns; ++i) { if (ELEV != INFINITY && (c >= nc || ((!(capx[c] == sectx[c][i] && capy[c] == secty[c][i])) && (!(new_x(capx[c] + 2) == sectx[c][i] && capy[c] == secty[c][i]))))) { h = 3 * (5 - dmoun[i]) + dsea[i]; if (h > highest) { highest = h; where = i; } } } if (where == -1) break; newk = k / 100; if (newk >= HILLMIN && newk < PLATMIN) newk = PLATMIN; if (newk < LANDMIN) newk = LANDMIN; elev[sectx[c][where]][secty[c][where]] = newk; dsea[where] = -INFINITY; dmoun[where] = INFINITY; } /* Elevate the mountains and capitals */ for (i = 0; i < ns; ++i) { if (ELEV == INFINITY) { if (dsea[i] == 1) ELEV = HILLMIN + roll0(PLATMIN - HILLMIN); else ELEV = HIGHMIN + roll0((256 - HIGHMIN) / 2) + roll0((256 - HIGHMIN) / 2); } else if (c < nc && (((capx[c] == sectx[c][i] && capy[c] == secty[c][i])) || ((new_x(capx[c] + 2) == sectx[c][i] && capy[c] == secty[c][i])))) ELEV = PLATMIN; } } }
void next_line(){ next_y(); new_x( left ); }
Vector* jacobi_method(Matrix A, Vector b, Vector y, int iterations) { unsigned int rows = mtl::mat::num_rows(A); unsigned int cols = mtl::mat::num_cols(A); if(rows != cols) throw std::string("Cannot perform Jacobi Method, Matrix is not square"); std::cout << "det(A) = " << det(A) << std::endl << std::endl; if(boost::numeric::zero_in(det(A)) == true) throw std::string("Cannot approximate bounding box as solution set is unbounded"); Matrix D(rows, cols); for(unsigned int i = 0; i < rows; i++) for(unsigned int j = 0; j < cols; j++) { if(i == j) D(i,j) = A(i,j); else D(i,j) = (Interval)0; } Matrix N(rows, cols); for(unsigned int i = 0; i < rows; i++) for(unsigned int j = 0; j < cols; j++) { if(i == j) N(i,j) = (Interval)0; else N(i,j) = A(i,j); } Matrix D_inverse(rows,cols); for(unsigned int i = 0; i < rows; i++) for(unsigned int j = 0; j < cols; j++) { if(i == j) D_inverse(i,j) = 1/D(i,j); else D_inverse(i,j) = (Interval)0; } Matrix G(rows, cols); Vector c(rows, (Interval)0); G = (-1 * D_inverse) * N; c = D_inverse * b; Vector *x = new Vector(rows, (Interval)0); *x = y; for(int i = 0; i < iterations; i++) { Vector new_x(rows, (Interval)0); Vector temp; temp = G * (*x); new_x = temp + c; *x = new_x; } return x; }