src/subsector.cpp

00001 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
00002 /*
00003  * frpuniverse
00004  * Copyright (C) Peter L. Berghold 2008 <Peter@Berghold.net>
00005  * 
00006  * frpuniverse is free software.
00007  * 
00008  * You may redistribute it and/or modify it under the terms of the
00009  * GNU General Public License, as published by the Free Software
00010  * Foundation; either version 2 of the License, or (at your option)
00011  * any later version.
00012  * 
00013  * frpuniverse is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with frpuniverse.  If not, write to:
00020  *      The Free Software Foundation, Inc.,
00021  *      51 Franklin Street, Fifth Floor
00022  *      Boston, MA  02110-1301, USA.
00023  */
00024 #include <string.h>
00025 #include <malloc.h>
00026 #include <stdio.h>
00027 
00028 #include "subsector.hpp"
00029 
00030 #include "dice.hpp"
00031 
00032 static parsecCoordinate markers[16]={
00033          {1,1},{1,9},{1,17},{1,25},
00034          {11,1},{11,9},{11,17},{11,25},
00035      {21,1},{21,9},{21,17},{21,25},
00036      {31,1},{31,9},{31,17},{31,25}
00037 };
00038 
00043 subsector::subsector(){
00044         density=DENSITY_NORMAL; // This is the default from book 3
00045         
00046         parsecStart.row=1;
00047         parsecStart.column=1;
00048         parsecEnd.row=10;
00049         parsecEnd.column=8;
00050         
00051         this->populateGrid();
00052         
00053 }
00054 
00055 subsector::subsector(int position) : sparseArray() {
00056         int row;
00057         int column;
00058         
00059         density=DENSITY_NORMAL;
00060         
00061         parsecStart = markers[position-1];
00062         parsecEnd.row=parsecStart.row+9;
00063         parsecEnd.column=parsecStart.column+7;
00064         
00065         this->populateGrid();
00066         
00067         
00068 }
00069 
00070 
00071 int subsector::getDensity() {
00072         return density;
00073 }
00074 
00075 void subsector::setDensity(int d){
00076         density=d;
00077         this->populateGrid();
00078         
00079 }
00080 
00081 void subsector::addParsec(parsec* p) {
00082         int row = p->getRow();
00083         int column = p->getColumn();
00084         
00085         this->addAt(row,column,p);
00086         
00087 }
00088 
00089 void subsector::addParsec(int row,int column){
00090         parsec *p = new parsec();
00091         
00092         this->addAt(row,column,p);
00093 }
00094 
00095 void subsector::addParsec(int row,int column,parsec *p){
00096         this->addAt(row,column,p);
00097 }
00098 
00099 int subsector::parsecPresenceCheck(){
00100         return (d1d6()>3 ? 1 : 0 ); // For now... until we do density checking. 
00101 }
00102 
00103 void subsector::populateGrid(){
00104         
00105         int row;
00106         int column;
00107         
00108         for (row=parsecStart.row;row<=parsecEnd.row;row++){
00109                 for (column=parsecStart.column;column<=parsecEnd.column;column++){
00110                         if ( this->parsecPresenceCheck() != 0 )
00111 #ifdef DEBUG_ON
00112                                 printf ("Row: %0.2d Column: %0.2d\n",row,column);
00113 #endif
00114                                 this->addParsec(row,column,new parsec());
00115                 }
00116         }
00117         
00118 }
00119 
00120 char* subsector::asText(){
00121         int row;
00122         int column;
00123         char *buffer;
00124         
00125         buffer = (char*) malloc(128);
00126         buffer[0]='\0';
00127         sprintf(buffer,"%s\n\n","********** Subsector Listing ***************");
00128         
00129         
00130         for(row=parsecStart.row;row<=parsecEnd.row;row++){
00131                 for(column=parsecStart.column;column<=parsecEnd.column;column++){
00132                         parsec* p = (parsec *)this->findAt(row,column);
00133                         if ( p!= 0 ) {
00134                                 char *newtext = p->asText();
00135                                 char *newBuffer = (char*) malloc(strlen(newtext)+strlen(buffer)+10);
00136                                 newBuffer[0]='\0';
00137                                 sprintf(newBuffer,"%s%s\n",buffer,newtext);
00138                                 free(buffer);
00139                                 buffer=newBuffer;
00140                         }
00141                 }
00142         }
00143         return buffer;
00144 }

Generated on Fri Mar 7 16:40:53 2008 for frpuniverse by  doxygen 1.4.7