Skip to content

Commit

Permalink
Repel allies (#34)
Browse files Browse the repository at this point in the history
Add mailing list to readme
  • Loading branch information
cxong committed Sep 11, 2013
1 parent 1263219 commit 0ce7131
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 15 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ For the latest release notes please see https://github.com/cxong/cdogs-sdl/wiki/

For more information about the original C-Dogs read 'original\_readme.txt'.

Join the C-Dogs SDL mailing list here: https://groups.google.com/forum/#!forum/c-dogs-sdl


2.Tested Environments
---------------------
Expand Down Expand Up @@ -103,7 +105,10 @@ Which will make the game try to run fullscreen at 400x300 resolution.
----------

If you have an questions, comments, bug reports (yes please), patches (even
better) or anything else related to C-Dogs SDL email:
better) or anything else related to C-Dogs SDL, check out the mailing list:

> Cong <congusbongus@gmail.com>
> https://groups.google.com/forum/#!forum/c-dogs-sdl
Or email:

> Cong <[email protected]>
9 changes: 7 additions & 2 deletions README.md.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ For the latest release notes please see https://github.com/cxong/cdogs-sdl/wiki/

For more information about the original C-Dogs read 'original\_readme.txt'.

Join the C-Dogs SDL mailing list here: https://groups.google.com/forum/#!forum/c-dogs-sdl


2.Tested Environments
---------------------
Expand Down Expand Up @@ -103,7 +105,10 @@ Which will make the game try to run fullscreen at 400x300 resolution.
----------

If you have an questions, comments, bug reports (yes please), patches (even
better) or anything else related to C-Dogs SDL email:
better) or anything else related to C-Dogs SDL, check out the mailing list:

> Cong <congusbongus@gmail.com>
> https://groups.google.com/forum/#!forum/c-dogs-sdl

Or email:

> Cong <[email protected]>
41 changes: 37 additions & 4 deletions src/cdogs/actors.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

#define SOUND_LOCK_FOOTSTEP 4
#define FOOTSTEP_DISTANCE_PLUS 380
#define REPEL_STRENGTH 20


TActor *gPlayer1 = NULL;
Expand Down Expand Up @@ -327,6 +328,7 @@ TActor *AddActor(int character)
actor->tileItem.w = 7;
actor->tileItem.h = 5;
actor->tileItem.flags = TILEITEM_IMPASSABLE | TILEITEM_CAN_BE_SHOT;
actor->tileItem.actor = actor;
actor->next = actorList;
actorList = actor;
actor->flags = FLAGS_SLEEPING | gCharacterDesc[character].flags;
Expand Down Expand Up @@ -515,7 +517,8 @@ int MoveActor(TActor * actor, int x, int y)

realPos = Vec2iScaleDiv(Vec2iNew(x, y), 256);
target = GetItemOnTileInCollision(
&actor->tileItem, realPos, TILEITEM_IMPASSABLE);
&actor->tileItem, realPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor->flags));
if (target)
{
Vec2i realXPos, realYPos;
Expand Down Expand Up @@ -556,13 +559,15 @@ int MoveActor(TActor * actor, int x, int y)

realYPos = Vec2iScaleDiv(Vec2iNew(actor->x, y), 256);
if (GetItemOnTileInCollision(
&actor->tileItem, realYPos, TILEITEM_IMPASSABLE))
&actor->tileItem, realYPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor->flags)))
{
y = actor->y;
}
realXPos = Vec2iScaleDiv(Vec2iNew(x, actor->y), 256);
if (GetItemOnTileInCollision(
&actor->tileItem, realXPos, TILEITEM_IMPASSABLE))
&actor->tileItem, realXPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor->flags)))
{
x = actor->x;
}
Expand All @@ -581,7 +586,8 @@ int MoveActor(TActor * actor, int x, int y)
{
realPos = Vec2iScaleDiv(Vec2iNew(x, y), 256);
target = GetItemOnTileInCollision(
&actor->tileItem, realPos, TILEITEM_CAN_BE_TAKEN);
&actor->tileItem, realPos, TILEITEM_CAN_BE_TAKEN,
CalcCollisionTeam(1, actor->flags));
if (target && target->kind == KIND_OBJECT)
{
PickupObject(actor, target->data);
Expand Down Expand Up @@ -870,6 +876,33 @@ void UpdateAllActors(int ticks)
}
else
{
// Find actors that are on the same team and colliding,
// and repel them
Vec2i realPos = Vec2iScaleDiv(
Vec2iNew(actor->x, actor->y), 256);
TTileItem *collidingItem = GetItemOnTileInCollision(
&actor->tileItem, realPos, TILEITEM_IMPASSABLE,
COLLISIONTEAM_NONE);
if (collidingItem && collidingItem->kind == KIND_CHARACTER)
{
TActor *collidingActor = collidingItem->actor;
if (CalcCollisionTeam(1, collidingActor->flags) ==
CalcCollisionTeam(1, actor->flags))
{
Vec2i v = Vec2iNew(
actor->x - collidingActor->x,
actor->y - collidingActor->y);
if (Vec2iEqual(v, Vec2iZero()))
{
v = Vec2iNew(1, 0);
}
v = Vec2iScale(Vec2iNorm(v), REPEL_STRENGTH);
actor->dx += v.x;
actor->dy += v.y;
collidingActor->dx -= v.x;
collidingActor->dy -= v.y;
}
}
actor = actor->next;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/ai.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ static int PositionOK(TActor * actor, int x, int y)
return 0;
}
if (GetItemOnTileInCollision(
&actor->tileItem, realPos, TILEITEM_IMPASSABLE))
&actor->tileItem, realPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor->flags)))
{
return 0;
}
Expand Down
38 changes: 34 additions & 4 deletions src/cdogs/collision.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@
*/
#include "collision.h"

#include "actors.h"

CollisionTeam CalcCollisionTeam(int isActor, int actorFlags)
{
// Need to have prisoners collide with everything otherwise they will not
// be "rescued"
if (!isActor || (actorFlags & FLAGS_PRISONER))
{
return COLLISIONTEAM_NONE;
}
if (actorFlags & (FLAGS_PLAYERS | FLAGS_GOOD_GUY))
{
return COLLISIONTEAM_GOOD;
}
return COLLISIONTEAM_BAD;
}

int IsCollisionWithWall(Vec2i pos, Vec2i size)
{
if (HitWall(pos.x - size.x, pos.y - size.y) ||
Expand Down Expand Up @@ -84,7 +101,8 @@ int ItemsCollide(TTileItem *item1, TTileItem *item2, Vec2i pos)
return 0;
}

TTileItem *GetItemOnTileInCollision(TTileItem *item, Vec2i pos, int mask)
TTileItem *GetItemOnTileInCollision(
TTileItem *item, Vec2i pos, int mask, CollisionTeam team)
{
int dy;
int tx = pos.x / TILE_WIDTH;
Expand All @@ -103,11 +121,23 @@ TTileItem *GetItemOnTileInCollision(TTileItem *item, Vec2i pos, int mask)
TTileItem *i = Map(tx + dx, ty + dy).things;
while (i)
{
if (item != i &&
// Don't collide if items are on the same team
CollisionTeam itemTeam = COLLISIONTEAM_NONE;
if (i->kind == KIND_CHARACTER)
{
TActor *a = i->actor;
itemTeam = CalcCollisionTeam(1, a->flags);
}
if (team == COLLISIONTEAM_NONE ||
itemTeam == COLLISIONTEAM_NONE ||
team != itemTeam)
{
if (item != i &&
(i->flags & mask) &&
ItemsCollide(item, i, pos))
{
return i;
{
return i;
}
}
i = i->next;
}
Expand Down
14 changes: 13 additions & 1 deletion src/cdogs/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@

#define HitWall(x, y) (gMap[(y)/TILE_HEIGHT][(x)/TILE_WIDTH].flags & MAPTILE_NO_WALK)

// Which "team" the actor's on, for collision
// Actors on the same team don't have to collide
typedef enum
{
COLLISIONTEAM_NONE,
COLLISIONTEAM_GOOD,
COLLISIONTEAM_BAD
} CollisionTeam;

CollisionTeam CalcCollisionTeam(int isActor, int actorFlags);

int IsCollisionWithWall(Vec2i pos, Vec2i size);
TTileItem *GetItemOnTileInCollision(TTileItem *item, Vec2i pos, int mask);
TTileItem *GetItemOnTileInCollision(
TTileItem *item, Vec2i pos, int mask, CollisionTeam team);

#endif
1 change: 1 addition & 0 deletions src/cdogs/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ struct TileItem {
int flags;
void *data;
TileItemDrawFunc drawFunc;
void *actor;
struct TileItem *next;
struct TileItem *nextToDisplay;
};
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/objs.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ int HitItem(TMobileObject * obj, int x, int y, special_damage_e special)
}

item = GetItemOnTileInCollision(
&obj->tileItem, realPos, TILEITEM_CAN_BE_SHOT);
&obj->tileItem, realPos, TILEITEM_CAN_BE_SHOT, COLLISIONTEAM_NONE);
hasHit = DamageSomething(
Vec2iNew(obj->dx, obj->dy),
obj->power,
Expand Down Expand Up @@ -1323,6 +1323,7 @@ void InternalAddObject(
o->tileItem.drawFunc = (TileItemDrawFunc)DrawObject;
o->tileItem.w = w;
o->tileItem.h = h;
o->tileItem.actor = NULL;
MoveTileItem(&o->tileItem, x >> 8, y >> 8);
o->next = objList;
objList = o;
Expand Down
13 changes: 13 additions & 0 deletions src/cdogs/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ Vec2i Vec2iScaleDiv(Vec2i v, int scaleDiv)
return v;
}

Vec2i Vec2iNorm(Vec2i v)
{
double magnitude;
if (Vec2iEqual(v, Vec2iZero()))
{
return v;
}
magnitude = sqrt(v.x*v.x + v.y*v.y);
v.x = (int)floor(v.x / magnitude + 0.5);
v.y = (int)floor(v.y / magnitude + 0.5);
return v;
}

int Vec2iEqual(Vec2i a, Vec2i b)
{
return a.x == b.x && a.y == b.y;
Expand Down
1 change: 1 addition & 0 deletions src/cdogs/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Vec2i Vec2iZero(void);
Vec2i Vec2iAdd(Vec2i a, Vec2i b);
Vec2i Vec2iScale(Vec2i v, int scalar);
Vec2i Vec2iScaleDiv(Vec2i v, int scaleDiv);
Vec2i Vec2iNorm(Vec2i v);
int Vec2iEqual(Vec2i a, Vec2i b);

int DistanceSquared(Vec2i a, Vec2i b);
Expand Down

0 comments on commit 0ce7131

Please sign in to comment.