void Entity::updateEntity(vector <int*>* IdleShips)
{
	if (rFuel <= 0) { Alive = false; };
	if (Alive == false)
	{
		//delete[] m_nodes->at(m_Home);
		Alive = true;
	}

	turn -= 1;
	if (turn == 0)
	{
	

		CollectIdleShips(IdleShips, m_ship);

		for (int i = 0; i < IdleShips->size(); i++)
		{
		
			while (	(*m_ship)[ *(*IdleShips)[i] ]->Hull != 100) 
			{
				if ((*m_ship)[*(*IdleShips)[i]]->m_rtype == Metal)
				{
					rMetal += 5;
				}
				if ((*m_ship)[*(*IdleShips)[i]]->m_rtype == Fuel)
				{
					rFuel += 5;
				}
				(*m_ship)[*(*IdleShips)[i]]->m_cargo = 0;
				(*m_ship)[*(*IdleShips)[i]]->m_rtype = Home;
				(*m_ship)[ *(*IdleShips)[i] ]->Hull++;
				rMetal--;
			}
		}

		FindRec();
		CalculatePriotity(IdleShips);



		CalculateFuelUse(IdleShips);
		turn = 100;
		rFuel -= FuelUse;

		
		
		cout << "Fuel  " << rFuel << endl;
		cout << "Fuel Use " << FuelUse << endl;
		cout << "Total Ships " << TotalShips << endl;
		cout << "Metal " << rMetal << endl;
		cout << "Metal Threshold " << MetalThreshold  << endl << endl;
	}

	//cout << turn << endl;


}
Esempio n. 2
0
UpdateRec* UpdateDlg::GetRecFromListView()
{
    wxListCtrl* lst = XRCCTRL(*this, "lvFiles", wxListCtrl);
    int index = lst->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
    if (index == -1)
        return 0;
    wxString title = lst->GetItemText(index);
    wxString version = GetListColumnText(index, 1);
    wxString revision = GetListColumnText(index, 4);
    return FindRec(title, version, revision, m_Recs, m_RecsCount);
}
Esempio n. 3
0
int Join(int argc, char **argv) {
    if (g_DBOpenFlag != OK) {
        return ErrorMsgs(DB_NOT_OPEN, g_PrintFlag);
    }
    int relNum1, relNum2, count, attrFoundFlag = 0, i, retVal, recResultLength;
    int numAttrsRel1, numAttrsRel2, numAttrsTotal;
    int offset1, offset2, attrSize1, attrSize2, newRelNum;
    datatype type1, type2;
    struct attrCatalog *head;
    char **argumentList, *recPtr1, *recPtr2, *recResult;
    Rid startRidRel1 = { 1, 0 }, startRidRel2 = { 1, 0 }, *foundRidRel1, *foundRidRel2;

    if (argc < 6)
        return ErrorMsgs(ARGC_INSUFFICIENT, g_PrintFlag);

    if ((relNum1 = OpenRel(argv[2])) == NOTOK || (relNum2 = OpenRel(argv[4])) == NOTOK)
        return ErrorMsgs(RELNOEXIST, g_PrintFlag);

    numAttrsRel1 = g_CatCache[relNum1].numAttrs;
    numAttrsRel2 = g_CatCache[relNum2].numAttrs;
    numAttrsTotal = numAttrsRel1 + numAttrsRel2;

    /* Preparing Argument list which should be passed to Create() */
    argumentList = malloc(sizeof(char*) * (numAttrsTotal * 2));
    argumentList[0] = malloc(sizeof(char) * RELNAME);
    argumentList[1] = malloc(sizeof(char) * RELNAME);

    for (count = 2; count < numAttrsTotal * 2; count++) {
        argumentList[count] = malloc(sizeof(char) * RELNAME);
        argumentList[count + 1] = malloc(sizeof(char) * 4);
    }

    strcpy(argumentList[0], "create");
    strcpy(argumentList[1], argv[1]);

    count = 2;
    head = g_CatCache[relNum1].attrList;

    while (head != NULL) {
        strcpy(argumentList[count], head->attrName);
        switch (head->type) {
            case INTEGER:
                strcpy(argumentList[count + 1], "i");
                break;
            case STRING:
                sprintf(argumentList[count + 1], "s%d", head->length);
                break;
            case FLOAT:
                strcpy(argumentList[count + 1], "f");
                break;
        }
        if (strcmp(head->attrName, argv[3]) == 0) {
            attrFoundFlag = 1;
            offset1 = head->offset;
            type1 = head->type;
            attrSize1 = head->length;
        }
        head = head->next;
        count = count + 2;
    }
    if (attrFoundFlag == 0)
        return ErrorMsgs(ATTRNOEXIST, g_PrintFlag);

    /* Next Relation's attribute list */
    attrFoundFlag = 0;
    head = g_CatCache[relNum2].attrList;

    while (head != NULL) {
        if (strcmp(head->attrName, argv[5]) == 0) {
            attrFoundFlag = 1;
            offset2 = head->offset;
            type2 = head->type;
            attrSize2 = head->length;
        }
        /* Skipping the join attribute in 2nd relation */
        if (strcmp(head->attrName, argv[5]) == 0) {
            head = head->next;
            continue;
        }
        strcpy(argumentList[count], head->attrName);
        /* This is to give different name for same attribute names in two Relations */
        for (i = 2; i < numAttrsRel1 + 2; i = i + 2)
            if (strcmp(argumentList[i], head->attrName) == 0) {
                strcat(argumentList[count], "_2");
            }

        switch (head->type) {
            case INTEGER:
                strcpy(argumentList[count + 1], "i");
                break;
            case STRING:
                sprintf(argumentList[count + 1], "s%d", head->length);
                break;
            case FLOAT:
                strcpy(argumentList[count + 1], "f");
                break;
        }
        head = head->next;
        count = count + 2;
    }
    if (attrFoundFlag == 0)
        return ErrorMsgs(ATTRNOEXIST, g_PrintFlag);

    retVal = Create(numAttrsTotal * 2, argumentList);
    if (retVal == NOTOK)
        return NOTOK;

    OpenRel(argv[1]);
    newRelNum = FindRelNum(argv[1]);

    for (i = 0; i < numAttrsTotal * 2; i++)
        free(argumentList[i]);
    free(argumentList);

    if (type1 != type2)
        return ErrorMsgs(TYPE_MISMATCH, g_PrintFlag);

    recResultLength = g_CatCache[relNum1].recLength + g_CatCache[relNum2].recLength - attrSize2;
    recResult = malloc(sizeof(char) * (recResultLength));

    while (GetNextRec(relNum1, &startRidRel1, &foundRidRel1, &recPtr1) == OK) {
        startRidRel2.pid = 1;
        startRidRel2.slotnum = 0;

        while (FindRec(relNum2, &startRidRel2, &foundRidRel2, &recPtr2, type2, attrSize2, offset2,
                recPtr1 + offset1, EQ) == OK) {

            copyBinaryArray(recResult, recPtr1, g_CatCache[relNum1].recLength);
            copyBinaryArray(recResult + g_CatCache[relNum1].recLength, recPtr2, offset2);
            copyBinaryArray(recResult + g_CatCache[relNum1].recLength + offset2,
                    recPtr2 + offset2 + attrSize2,
                    g_CatCache[relNum2].recLength - offset2 - attrSize2);

            InsertRec(newRelNum, recResult);

            startRidRel2 = *foundRidRel2;
            free(foundRidRel2);
        }
        startRidRel1 = *foundRidRel1;
        free(foundRidRel1);
    }

    return OK;
}
Esempio n. 4
0
int Select(int argc, char **argv) {
    if (g_DBOpenFlag != OK) {
        return ErrorMsgs(DB_NOT_OPEN, g_PrintFlag);
    }
    int relNum, newRelNum, numAttrs, count, i, retVal, offset, attrFoundFlag = 0;
    int attrSize, intVal;
    float floatVal;
    struct attrCatalog* head;
    datatype type;
    Rid startRid = { 1, 0 }, *foundRid;
    char **createArgumentList, *recPtr;

    if (argc < 6)
        return ErrorMsgs(ARGC_INSUFFICIENT, g_PrintFlag);

    if (OpenRel(argv[2]) == NOTOK)
        return ErrorMsgs(RELNOEXIST, g_PrintFlag);
    /* Finding the relNum of Source Relation */
    relNum = FindRelNum(argv[2]);

    head = g_CatCache[relNum].attrList;
    numAttrs = g_CatCache[relNum].numAttrs;

    /* Preparing Argument list which should be passed to Create() */
    createArgumentList = malloc(sizeof(char*) * (numAttrs + 1) * 2);
    createArgumentList[0] = malloc(sizeof(char) * RELNAME);
    createArgumentList[1] = malloc(sizeof(char) * RELNAME);

    for (count = 2; count < (numAttrs + 1) * 2; count++) {
        createArgumentList[count] = malloc(sizeof(char) * RELNAME);
        createArgumentList[count + 1] = malloc(sizeof(char) * 4);
    }

    strcpy(createArgumentList[0], "create");
    strcpy(createArgumentList[1], argv[1]);
    count = 2;
    while (head != NULL) {
        strcpy(createArgumentList[count], head->attrName);
        switch (head->type) {
            case INTEGER:
                strcpy(createArgumentList[count + 1], "i");
                break;
            case STRING:
                sprintf(createArgumentList[count + 1], "s%d", head->length);
                break;
            case FLOAT:
                strcpy(createArgumentList[count + 1], "f");
                break;
        }
        /* This is to catch the desired attribute's specifications from Source relation. 
         Expected to happen only once in this loop */
        if (strcmp(head->attrName, argv[3]) == 0) {
            attrFoundFlag = 1;
            offset = head->offset;
            type = head->type;
            attrSize = head->length;
        }
        head = head->next;
        count = count + 2;
    }
    /* Given attribute name never appeared in attr linkedlist */
    if (attrFoundFlag == 0)
        return ErrorMsgs(ATTRNOEXIST, g_PrintFlag);

    retVal = Create((numAttrs + 1) * 2, createArgumentList);

    for (i = 0; i < (numAttrs + 1) * 2; i++)
        free(createArgumentList[i]);
    free(createArgumentList);

    if (retVal == NOTOK)
        return NOTOK;

    OpenRel(argv[1]);
    newRelNum = FindRelNum(argv[1]);

    switch (type) {
        case STRING:
            break;
        case INTEGER:
            intVal = atoi(argv[5]);
            convertIntToByteArray(intVal, argv[5]);
            break;
        case FLOAT:
            floatVal = atof(argv[5]);
            convertFloatToByteArray(floatVal, argv[5]);
            break;
    }
    /* Finding record from Source, which satisfying given condition, and Adding to Result Relation*/
    while (FindRec(relNum, &startRid, &foundRid, &recPtr, type, attrSize, offset, argv[5],
            readIntFromByteArray(argv[4], 0)) == OK) {
        InsertRec(newRelNum, recPtr);
        startRid = (*foundRid);
        free(foundRid);
    }
    return OK;
}