示例#1
0
void RetinueAddWarrior(struct Retinue* _Retinue, const struct Person* _Warrior) {
	if(RetinueIsWarrior(_Retinue, _Warrior) != 0 || _Retinue->Leader->Person == _Warrior)
		return;
	ArrayInsertSort_S(&_Retinue->Warriors, (void*)_Warrior, ObjectCmp);	
	_Retinue->FamilySz += FamilySize(_Warrior->Family);
	PushEvent(EVENT_NEWRECRUIT, _Retinue->Leader, (void*)_Warrior);
}
示例#2
0
struct Array* AnimalFoodDep(const struct HashTable* _Table) {
    int i;
    int _SetSize = 0;
    struct Array* _Array = CreateArray(_Table->Size);
    struct Population* _Pop = NULL;
    struct HashItrCons* _Itr = HashCreateItrCons(_Table);
    struct FoodBase** _Temp = NULL;
    struct AnimalDep* _Dep = NULL;
    struct AnimalDep* _Search = NULL;
    struct FoodBase*** _Set = NULL;

    while(_Itr != NULL) {
        _Pop = _Itr->Node->Pair;
        for(i = 0; i < _Pop->EatsSize; ++i) {
            if((_Dep = BinarySearch(_Pop->Eats[i], _Array->Table, _Array->Size, AnDepArrayCmp)) == NULL) {
                _Dep = (struct AnimalDep*) malloc(sizeof(struct AnimalDep));
                _Temp = calloc(2, sizeof(struct FoodBase*));
                _Temp[0] = _Pop->Eats[i];
                _Temp[1] = NULL;
                _Dep->Tbl = _Temp;
                _Dep->Animals = CreateArray(4);
                _Dep->Nutrition = 0;
                ArrayInsertSort_S(_Array, _Dep, AnDepArrayArrayCmp);
            }
            ArrayInsertSort_S(_Dep->Animals, _Pop, PopulationCmp);
        }
        if(_Pop->EatsSize == 0)
            goto loopend;
        _Temp = calloc(_Pop->EatsSize + 1, sizeof(struct FoodBase*));
        _Dep = (struct AnimalDep*) malloc(sizeof(struct AnimalDep));
        for(i = 0; i < _Pop->EatsSize; ++i) {
            _Temp[i] = _Pop->Eats[i];
        }
        _Temp[i] = NULL;
        _Dep->Nutrition = _Pop->Nutrition;
        _Dep->Tbl = _Temp;

        _Set = PowerSet(_Temp,_Pop->EatsSize);
        _SetSize = pow(2, _Pop->EatsSize);
        for(i = 1; i < _SetSize; ++i) {
            if((_Search = BinarySearch(_Set[i], _Array->Table, _Array->Size, FoodArrayAnDepArray)) != NULL)
                _Search->Nutrition += _Pop->Nutrition;
            free(_Set[i]);
        }
        free(_Set[0]);
        free(_Set[_SetSize]);
        free(_Set);
        if((_Search = BinarySearch(_Dep, _Array->Table, _Array->Size, AnDepArrayArrayCmp)) != NULL) {
            ArrayInsertSort_S(_Search->Animals, _Pop, PopulationCmp);
            free(_Dep);
            free(_Temp);
            goto loopend;
        }
        _Dep->Animals = CreateArray(4);
        ArrayInsertSort_S(_Dep->Animals, _Pop, PopulationCmp);
        ArrayInsertSort_S(_Array, _Dep, AnDepArrayArrayCmp);
loopend:
        _Itr = HashNextCons(_Table, _Itr);
    }
    HashDeleteItrCons(_Itr);
    if(_Array->Size > 0) {
        _Array->Table = realloc(_Array->Table, _Array->Size * sizeof(struct FoodBase*));
        _Array->TblSize = _Array->Size;
    }
    return _Array;
}