Beispiel #1
0
Float BuildTower(Script * script)
{
	if (script->VerifyArguments(2) == true)
	{
		Word * targetWord = script->GetNextWord();
		Word * posWord = script->GetNextWord();

		Point2D position(0, 0);
		Point2D * pos = (Point2D *)(uint32)posWord->value;
		if (pos != NULL)
		{
			position.SetValues(pos->GetX(), pos->GetY());
		}
		else
		{
			return false;
		}

		if ((IntervalCheck(position.GetX(), 1, GRID_SIZE - 2) == true) && (IntervalCheck(position.GetY(), 1, GRID_SIZE - 2) == true))
		{
			if (targetWord->type == POINTER)
			{
				Object * target = script->GetTarget(targetWord);
				if ((target != NULL) && (target->CheckType(OBJ_TYPE_TOWER) == true))
				{
					Grid * grid = WorldGame.GetGrid();
					Tower * tower = (Tower *)target;

					//To bypass grid auto-filling
					tower->SetState(false);

					Point2D towerCoords = tower->GetPosition();
					towerCoords.SetValues((towerCoords.GetX() - 80)/32, (towerCoords.GetY() - 80)/32);
					grid->SetPath(towerCoords, 0);	

					if (BuildTower(tower->GetTowerType(), position, true) == true)
					{
						Tower * newTower = grid->GetData(Point2D(position.GetX(), position.GetY()));
						tower->SetState(true);
						grid->FindPath(Origin);
						newTower->Copy(*tower);
						newTower->SetUInt32Value(TOWER_VAR_BUILD_TIME, GetGameTime());
						return true;
					}
					else
					{
						tower->SetState(true);
						grid->FindPath(Origin);
					}
				}
			}
			else if ((targetWord->type == NUMERIC) && (IntervalCheck(targetWord->value, 1, GetTowerDataSize()) == true))
			{
				return (BuildTower(targetWord->value, position, true));
			}
		}
	}
	return false;
}
Float SpellAreaTowers(Script * script)
{
	if (script->VerifyArguments(1) == true)
	{
		Word * spellWord = script->GetNextWord();

		GameObject * source = (GameObject *)script->GetSource();
		if ((source != NULL) && (source->CheckType(OBJ_TYPE_GAME_OBJECT) == true))
		{
			const Spell * spell = GetSpell(spellWord->value);
			if (spell != NULL)
			{
				Shape * shape = source->GetShape();
				Shape * newShape = shape->CreateInstance();
				newShape->Translate(source->GetPosition());

				Grid * grid = WorldGame.GetGrid();
				Tower ** TowerData = grid->GetData();

				for (uint32 i = 1; i < GRID_SIZE - 1; i++)
				{
					for (uint32 j = 1; j < GRID_SIZE - 1; j++)
					{
						Tower * tower = TowerData[(i*GRID_SIZE)+j];
						if ((tower != NULL) && (tower->GetState() == true) && (tower->ActiveSpell(spell, source) == false))
						{
							Point2D towerPos = tower->GetPosition();
							Rect towerRect(towerPos.GetX(), towerPos.GetY(), towerPos.GetX() + 32, towerPos.GetY() + 32);

							if (Intersect(newShape, &towerRect) == true)
							{
								if (tower->ActiveSpell(spell, source) == false)
								{
									tower->ApplySpell(spell, source);
								}
							}
							else
							{
								tower->RemoveSpell(spell, source);
							}
						}
					}
				}
				delete newShape;
				return true;
			}
		}
	}
	return false;
}
Beispiel #3
0
/*! We construct a horizontal line that starts at point pt and goes into any
    direction. Then we count how many times the line crosses the
    polygon. Odd number of crossings means that the pt is inside
    of polygon.

    \retval true pt is inside the polygon.
    \retval false pt is outside the polygon. 

 */
bool Polygon2D::IsInside(
   const Point2D& pt, //!< point to be tested.
   bool /*bBorder*/       //!< true if polygon border is inside the polygon.
) const
{
	// (1) http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
	const qreal y = pt.GetY();
	const qreal x = pt.GetX();

	const int npol = GetCount()+1;
	int i, j;
	bool bInside = false;

	for (i = 0, j = npol-1; i < npol; j = i++) {
		qreal ypi = m_vecV[i].GetY();
		qreal ypj = m_vecV[j].GetY();
		qreal xpi = m_vecV[i].GetX();
		qreal xpj = m_vecV[j].GetX();

		if (((ypi <= y) && (ypj > y)) || ((ypj <= y) && (ypi > y)))
			if((x < (xpj - xpi) * (y - ypi) / (ypj - ypi) + xpi))
				bInside = !bInside;
   }
	return bInside;
}
Float SpellAreaProjectiles(Script * script)
{
	if (script->VerifyArguments(1) == true)
	{
		Word * spellWord = script->GetNextWord();

		GameObject * source = (GameObject *)script->GetSource();
		if ((source != NULL) && (source->CheckType(OBJ_TYPE_GAME_OBJECT) == true))
		{
			const Spell * spell = GetSpell(spellWord->value);
			if (spell != NULL)
			{
				Shape * shape = source->GetShape();
				Shape * newShape = shape->CreateInstance();
				newShape->Translate(source->GetPosition());

				List<Projectile *> * ActiveProjectiles = GetActiveProjectiles();

				ActiveProjectiles->ToStart();
				for (uint32 i = 0; i < ActiveProjectiles->GetSize(); i++)
				{
					Projectile * projectile = ActiveProjectiles->GetCurrent();
					ActiveProjectiles->ToNext();

					uint32 baseSize = projectile->GetUInt32Value(PROJ_VAR_BASE_SIZE);
					Point2D position = projectile->GetPosition();
					Rect bounds(position.GetX(), position.GetY(), position.GetX() + baseSize, position.GetY() + baseSize);
					
					if (Intersect(newShape, &bounds) == true)
					{
						if (projectile->ActiveSpell(spell, source) == false)
						{
							projectile->ApplySpell(spell, source);
						}
					}
					else
					{
						projectile->RemoveSpell(spell, source);
					}
				}
				delete newShape;
				return true;
			}
		}
	}
	return false;
}
Float SpellAreaCreeps(Script * script)
{
	if (script->VerifyArguments(1) == true)
	{
		Word * spellWord = script->GetNextWord();

		GameObject * source = (GameObject *)script->GetSource();
		if ((source != NULL) && (source->CheckType(OBJ_TYPE_GAME_OBJECT) == true))
		{
			const Spell * spell = GetSpell(spellWord->value);
			if (spell != NULL)
			{
				Shape * shape = source->GetShape();
				Shape * newShape = shape->CreateInstance();
				newShape->Translate(source->GetPosition());

				List<Creep *> * ActiveCreeps = GetActiveCreeps();

				ActiveCreeps->ToStart();
				for (uint32 i = 0; i < ActiveCreeps->GetSize(); i++)
				{
					Creep * creep = ActiveCreeps->GetCurrent();
					ActiveCreeps->ToNext();

					Point2D position = creep->GetPosition();
					Rect bounds(position.GetX(), position.GetY(), position.GetX() + 32, position.GetY() + 32);
					
					if (Intersect(newShape, &bounds) == true)
					{
						if (creep->ActiveSpell(spell, source) == false)
						{
							creep->ApplySpell(spell, source);
						}
					}
					else
					{
						creep->RemoveSpell(spell, source);
					}
				}
				delete newShape;
				return true;
			}
		}
	}
	return false;
}
Beispiel #6
0
/*! Find a minimal rectangle that encapsulates the polygon. The
    rectangle is defined by ptMin and ptMax Point2D objects.
 */
void Polygon2D::GetSurroundingRectangle(
   Point2D& ptMin,   //!< Result - stores minimal x and y value. 
   Point2D& ptMax    //!< Result . stores maximal x and y value.
) const
{
   if(m_vecV.size()) {
      VecVertex::const_iterator i=m_vecV.begin();
      ptMin = ptMax = *i;
      // loop over remaining points
      for(i++; i!=m_vecV.end(); i++) {
         ptMin.SetX(GetMin(ptMin.GetX(), i->GetX()));
         ptMin.SetY(GetMin(ptMin.GetY(), i->GetY()));
         ptMax.SetX(GetMax(ptMax.GetX(), i->GetX()));
         ptMax.SetY(GetMax(ptMax.GetY(), i->GetY()));
      }
   }
}
Float RotateGameObject(Script * script)
{
	if (script->VerifyArguments(3) == true)
	{
		Word * rotatePosWord = script->GetNextWord();
		Point2D rotatePosition(0, 0);
		Point2D * rotatePos = (Point2D *)(uint32)rotatePosWord->value;
		if (rotatePos != NULL)
		{
			rotatePosition.SetValues(rotatePos->GetX(), rotatePos->GetY());
		}

		Word * angleWord = script->GetNextWord();

		Word * directionWord = script->GetNextWord();
		Vector3D direction(0, 0, 0);
		Point2D * dirVector = (Point2D *)(uint32)directionWord->value;
		if (dirVector != NULL)
		{
			direction.SetValues(dirVector->GetX(), dirVector->GetY(), 0);
		}

		GameObject * source = (GameObject *)script->GetSource();
		Shape * shape = source->GetShape();
		
		if ((rotatePos != NULL) && (source != NULL) && (source->CheckType(OBJ_TYPE_GAME_OBJECT) == true) && (shape != NULL))
		{
			Float distance = CalculateDistance(rotatePosition, source->GetPosition());
			if (angleWord->value != NULL)
			{
				Vector3D sourceDirection(rotatePosition, source->GetPosition());
				Float angle = sourceDirection.GetZeroAngleD() + angleWord->value;
				
				source->SetPosition(rotatePosition.GetX() + (distance * CosD(angle)),
									rotatePosition.GetY() + (distance * SinD(angle)));

				direction.SetValues(rotatePosition, source->GetPosition());
				direction.SetUnitVector();
			}
			else if (dirVector != NULL)
			{
				direction -= rotatePosition;
				direction.SetUnitVector();

				source->SetPosition(rotatePosition.GetX() + (direction.GetX() * distance),
									rotatePosition.GetY() + (direction.GetY() * distance));
			}
			else
			{
				return false;
			}

			if ((shape->GetType() == SHAPE_TYPE_LINE) || (shape->GetType() == SHAPE_TYPE_LINE_SEGMENT))
			{
				Line * line = (Line *)shape;
				Line * newLine = (Line *)line->CreateInstance();
				newLine->Translate(source->GetPosition());	

				Vector3D v1(rotatePosition, newLine->GetPoint1());
				Vector3D v2(rotatePosition, newLine->GetPoint2());

				Float baseAngle = direction.GetZeroAngleD();

				Float v1Length = v1.GetLength();
				Float v2Length = v2.GetLength();
				Float angle1 = v1.GetZeroAngleD() - baseAngle;
				Float angle2 = v2.GetZeroAngleD() - baseAngle;

				Point2D p1((v1Length * CosD(angle1)),
						   (v1Length * SinD(angle1)));
				Point2D p2((v2Length * CosD(angle2)),
						   (v2Length * SinD(angle2)));

				line->SetValues(p1 + rotatePosition, p2 + rotatePosition);

				line->Translate(!source->GetPosition());

				delete newLine;
			}
			else if (shape->GetType() == SHAPE_TYPE_CONE)
			{
				Cone * cone = (Cone *)shape;
				cone->SetValues(cone->GetVertex(), Vector3D(dirVector->GetX(), dirVector->GetY(), 0), cone->GetHeight(), cone->GetAngle());
			}
			return true;
		}
	}
	return false;
}
Float DamageArea(Script * script)
{
	if (script->VerifyArguments(4) == true)
	{
		Word * damageType = script->GetNextWord();
		Word * timeWord = script->PeekWord(2);
		Word * cycles = script->PeekWord(3);

		Object * originalSource = script->GetOriginalSource();
		GameObject * target = (GameObject *)script->GetSource();
		if ((target != NULL) && (target->CheckType(OBJ_TYPE_GAME_OBJECT) == true))
		{
			uint32 currentTime = GetGameTime();
			uint32 startTime = target->GetUInt32Value(GOBJECT_VAR_START_TIME);

			if (((startTime + (timeWord->value * cycles->value)) > currentTime) || (cycles->value <= 0))
			{
				List<Creep *> * ActiveCreeps = GetActiveCreeps();

				Shape * shape = target->GetShape();
				Shape * newShape = shape->CreateInstance();
				newShape->Translate(target->GetPosition());

				ActiveCreeps->ToStart();
				for (uint32 i = 0; i < ActiveCreeps->GetSize(); i++)
				{
					Creep * creep = ActiveCreeps->GetCurrent();
					ActiveCreeps->ToNext();

					if ((creep != NULL) && (creep->GetState() == true))
					{
						Point2D creepPos = creep->GetPosition();
						Rect creepRect(creepPos.GetX(), creepPos.GetY(), creepPos.GetX() + 32, creepPos.GetY() + 32);

						if (Intersect(newShape, &creepRect) == true)
						{
							script->SetSource(creep);

							Word * damageWord = script->PeekWord(1);
							Float damage = (timeWord->value == 0) ? damageWord->value : (damageWord->value * target->GetUInt32Value(GOBJECT_VAR_TIME_DIFFERENCE))/timeWord->value;

							if (damageType->value == CREEP_VAR_HITPOINTS)
							{
								if ((creep->GetInt32Value(CREEP_VAR_SPELL_IMMUNE) == false) && (creep->GetInt32Value(CREEP_VAR_SPLASH_IMMUNE) == false))
								{
									creep->InflictDamage(damage, creep->GetArmor());
								}
							}
							else
							{
								creep->ModValue(damageType->type, damageType->value, -damage);
							}
							if ((creep->GetState() == false) || (creep->GetFloatValue(CREEP_VAR_MAX_HITPOINTS) <= 0))
							{
								creep->SetState(false);
								AddTowerKill(target);
							}
						}
					}
				}

				delete newShape;
				script->SetSource(originalSource);

				//Properly offset the words list
				for (uint32 i = 0; i < 3; i++)
				{
					script->GetNextOriginalWord();
				}
				return true;
			}	
		}
		script->SetSource(originalSource);
	}
	return false;
}
Beispiel #9
0
Rect::Rect(const Point2D& topLeft, const Point2D& bottomRight)
{
	SetType(SHAPE_TYPE_RECT);
	SetValues(topLeft.GetX(), topLeft.GetY(), bottomRight.GetX(), bottomRight.GetY());
}
Beispiel #10
0
void Entity::Move(Point2D new_path)
{
	m_position.SetX(m_position.GetX()+new_path.GetX());
	m_position.SetY(m_position.GetY()+new_path.GetY());
}
Beispiel #11
0
void Font::PrintText(const char * text, const Point2D& pos, const Rect& bounds, uint8 format) const
{
	Point2D offset;

	glPushMatrix();
	glLoadIdentity();
	glColor(Black);
	glTranslated(pos.GetX(), pos.GetY(), 0);

	uint8 justify = format & 0x03;

	Float lineOffset = (int32)(CalculateLineLength(text, bounds.GetWidth(), 0)/2.0 * justify);
	offset.SetValues(offset.GetX() + lineOffset, offset.GetY());
	glTranslated(lineOffset, 0.0, 0);

	uint32 textPos = 0;
	while (textPos < strlen(text))
	{
		bool wordBegin = true;
		uint32 wordLength = GetWordLength(text, textPos);
		uint32 wordSpace = GetWordSpace(text, textPos);

		for (uint32 i = textPos; i < textPos + wordLength; i++)
		{
			if (text[i] == '&')
			{
				ParseSpecial(text, i, true);
				continue;
			}
			else if (((text[i] == '\n') || ((text[i] == '\\') && (text[i+1] == 'n')) || (offset.GetX() > bounds.GetWidth())) || ((wordSpace + offset.GetX() >= bounds.GetWidth()) && (wordSpace < bounds.GetWidth()) && (wordBegin == true)))
			{
				glTranslated(-offset.GetX(), baseSize, 0);
				offset.SetValues(0, offset.GetY() + baseSize);
				if (offset.GetY() > bounds.GetHeight())
				{
					glPopMatrix();
					return;
				}
				lineOffset = (int32)(CalculateLineLength(text, bounds.GetWidth(), i + 1)/2.0 * justify);
				offset.SetValues(offset.GetX() + lineOffset, offset.GetY());
				glTranslated(lineOffset, 0.0, 0);

				if (text[i] == '\n')
				{
					continue;
				}
				else if ((text[i] == '\\') && (text[i+1] == 'n'))
				{
					i++;
					continue;
				}
			}
			glCallList(text[i] + displayLists);
			offset.SetValues(offset.GetX() + width[text[i]], offset.GetY());
			wordBegin = false;
		}

		offset.SetValues(offset.GetX() + baseSize, offset.GetY());
		glTranslated(baseSize, 0, 0);

		textPos += wordLength + 1;
	}

	glPopMatrix();
}