File: Factory.cpp

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (131 lines) | stat: -rw-r--r-- 3,330 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "Factory.h"

#include "../CGroup.h"
#include "../CWishList.h"
#include "../CUnit.h"
#include "../CUnitTable.h"
#include "../CConfigParser.h"

FactoryTask::FactoryTask(AIClasses *_ai, CGroup& group): ATask(_ai) {
	t = TASK_FACTORY;
	// NOTE: currently if factories are joined into one group then assisters
	// will assist the first factory only
	//factoryTask->pos = group.pos();
	pos = group.firstUnit()->pos();
	validateInterval = 10 * 30;
	addGroup(group);
}

bool FactoryTask::assistable(CGroup& assister) {
	CGroup *group = firstGroup();

	if (!group->firstUnit()->def->canBeAssisted)
		return false; // there is no physical ability
	if ((assister.firstUnit()->type->cats&COMMANDER).any())
		return true; // commander must stay at the base

	int maxAssisters;

	switch(ai->difficulty) {
		case DIFFICULTY_EASY:
			maxAssisters = FACTORY_ASSISTERS / 3;
			break;
		case DIFFICULTY_NORMAL:
			maxAssisters = FACTORY_ASSISTERS / 2;
			break;
	 	case DIFFICULTY_HARD:
	 		maxAssisters = FACTORY_ASSISTERS;
	 		break;
	}

	if (assisters.size() >= std::min(ai->cfgparser->getState() * 2, maxAssisters)) {
		if ((assister.cats&AIR).any()) {
			// try replacing existing assisters (except commander) with
			// aircraft assisters to free factory exit...
			std::list<ATask*>::iterator it;
			for (it = assisters.begin(); it != assisters.end(); ++it) {
				ATask *task = *it;
				if ((task->firstGroup()->cats&(AIR|COMMANDER)).none()) {
					task->remove();
					return true;
				}
			}
		}
		return false;
	}

	return true;
}

bool FactoryTask::onValidate() {
	int numUnits = ai->cb->GetFriendlyUnits(&ai->unitIDs[0], pos, 16.0f);
	if (numUnits > 0) {
		int factoryID = firstGroup()->firstUnit()->key;
		for (int i = 0; i < numUnits; i++) {
    		int uid = ai->unitIDs[i];

    		if (factoryID == uid)
    			continue;

    		if (!ai->cb->UnitBeingBuilt(uid)) {
    			CUnit *unit = ai->unittable->getUnit(uid);
    			if (unit) {
    				if (unit->canPerformTasks())
    					// our unit stalled a factory
    					return false;
    			}
    			else
    				// some stupid allied unit stalled out factory
    				return false;
    		}
		}
	}
	return true;
}

void FactoryTask::onUpdate() {
	std::map<int, CUnit*>::iterator i;
	CGroup *group = firstGroup();
	CUnit *factory;

	for(i = group->units.begin(); i != group->units.end(); ++i) {
		factory = i->second;
		if (ai->unittable->idle[factory->key] && !ai->wishlist->empty(factory->key)) {
			Wish w = ai->wishlist->top(factory->key);
			ai->wishlist->pop(factory->key);
			if (factory->factoryBuild(w.ut)) {
				ai->unittable->unitsBuilding[factory->key] = w;
			}
		}
	}
}

void FactoryTask::setWait(bool on) {
	std::map<int, CUnit*>::iterator itUnit;
	std::list<ATask*>::iterator itTask;
	CGroup *group = firstGroup();
	CUnit *factory;

	for (itUnit = group->units.begin(); itUnit != group->units.end(); ++itUnit) {
		factory = itUnit->second;
		if(on)
			factory->wait();
		else
			factory->unwait();
	}

	for (itTask = assisters.begin(); itTask != assisters.end(); ++itTask) {
		if ((*itTask)->isMoving) continue;
		if(on)
			(*itTask)->firstGroup()->wait();
		else
			(*itTask)->firstGroup()->unwait();
	}
}

void FactoryTask::toStream(std::ostream& out) const {
	out << "FactoryTask(" << key << ") ";
	CGroup *group = firstGroup();
	if (group)
		out << (*group);
}