Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac6e52a

Browse files
committed
Moved robot to c
1 parent 749667f commit ac6e52a

File tree

11 files changed

+467
-332
lines changed

11 files changed

+467
-332
lines changed

‎engine_test.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import random
44

55
import time
6-
# app = App()
76

87
class RandomRobot(Robot):
98
def run(self):
@@ -16,6 +15,8 @@ def run(self):
1615
robots = [RandomRobot((255, 0, 0)), RandomRobot((0, 255, 0))]
1716
eng = Engine(robots=robots)
1817

18+
19+
# app = App()
1920
# app.child = Battle(robots, (600, 400), eng=eng)
2021
# app.child.set_tick_rate(-1)
2122
# app.run()

‎robots/engine_c/bullet.cpp‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎robots/engine_c/bullet.h‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎robots/engine_c/core.cpp‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <core.h>
2+
#include <math.h>
3+
4+
void Bullet::step()
5+
{
6+
position += velocity;
7+
};
8+
9+
bool Bullet::operator>(const Bullet &other) const
10+
{
11+
return uid > other.uid;
12+
};
13+
14+
bool Bullet::operator<(const Bullet &other) const
15+
{
16+
return uid < other.uid;
17+
};
18+
19+
Bullet *Robot::fire()
20+
{
21+
heat = 1.0f + fire_power / 5.0f;
22+
energy = std::max(0.0f, energy - fire_power);
23+
should_fire = false;
24+
Vec2 turret_direction = Vec2::from_rads(turret_rotation);
25+
return new Bullet(
26+
uid,
27+
position + turret_direction * 30.0f,
28+
turret_direction * (20.0f - (3.0f * fire_power)),
29+
clip(fire_power, BULLET_MIN_POWER, BULLET_MAX_POWER));
30+
};
31+
32+
void Robot::step()
33+
{
34+
Vec2 direction = Vec2::from_rads(base_rotation);
35+
position += direction * velocity;
36+
37+
float base_rotation_velocity =
38+
std::max(0.0f, (BASE_ROTATION_VELOCITY_RADS - BASE_ROTATION_VELOCITY_DEC_RADS * std::abs(velocity))) * base_turning;
39+
base_rotation += base_rotation_velocity;
40+
float turret_rotation_velocity = TURRET_ROTATION_VELOCITY_RADS * turret_turning + base_rotation_velocity;
41+
turret_rotation += turret_rotation_velocity;
42+
float radar_rotation_velocity = RADAR_ROTATION_VELOCITY_RADS * radar_turning + turret_rotation_velocity;
43+
radar_rotation += radar_rotation_velocity;
44+
};
45+
46+
float Robot::acceleration()
47+
{
48+
if (velocity > 0.0f)
49+
{
50+
if (moving > 0)
51+
return 1.0f;
52+
else
53+
return -2.0f;
54+
}
55+
else if (velocity < 0.0f)
56+
{
57+
if (moving < 0)
58+
return -1.0f;
59+
else
60+
return 2.0f;
61+
}
62+
else if (std::abs(moving) > 0)
63+
return 1.0f;
64+
else
65+
return 0.0f;
66+
};

‎robots/engine_c/core.h‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef BULLET_H
2+
#define BULLET_H
3+
4+
#include <ostream>
5+
#include <vec2.h>
6+
#include <math.h>
7+
8+
unsigned long NUMBER_BULLETS = 0;
9+
unsigned long NUMBER_ROBOTS = 0;
10+
11+
const float BASE_ROTATION_VELOCITY_RADS = 5/180 * M_PI;
12+
const float BASE_ROTATION_VELOCITY_DEC_RADS = 0.75/180 * M_PI;
13+
const float TURRET_ROTATION_VELOCITY_RADS = 5/180 * M_PI;
14+
const float RADAR_ROTATION_VELOCITY_RADS = 5/180 * M_PI;
15+
16+
const float BULLET_MAX_POWER = 3.0;
17+
const float BULLET_MIN_POWER = 0.1;
18+
const float ROBOT_RADIUS = 24;
19+
20+
21+
struct Bullet
22+
{
23+
unsigned long uid;
24+
long owner_uid;
25+
Vec2 position;
26+
Vec2 velocity;
27+
float power;
28+
29+
Bullet()
30+
: uid(NUMBER_BULLETS += 1),
31+
owner_uid(NULL),
32+
position(Vec2(0.0, 0.0)),
33+
velocity(Vec2(0.0, 0.0)),
34+
power(0){};
35+
Bullet(long owner_uid, Vec2 position, Vec2 velocity, float power)
36+
: uid(NUMBER_BULLETS += 1),
37+
owner_uid(owner_uid),
38+
position(position),
39+
velocity(velocity),
40+
power(power){};
41+
void step();
42+
43+
bool operator>(const Bullet &other) const;
44+
bool operator<(const Bullet &other) const;
45+
};
46+
47+
struct Robot
48+
{
49+
unsigned long uid;
50+
float energy;
51+
float velocity;
52+
Vec2 position;
53+
float base_rotation;
54+
float turret_rotation;
55+
float radar_rotation;
56+
float heat;
57+
58+
public:
59+
int moving;
60+
int base_turning;
61+
int turret_turning;
62+
int radar_turning;
63+
bool should_fire;
64+
int fire_power;
65+
66+
Robot()
67+
: uid(NUMBER_ROBOTS += 1),
68+
energy(100.0),
69+
velocity(0.0),
70+
position(Vec2(0.0, 0.0)),
71+
base_rotation(0.0),
72+
turret_rotation(0.0),
73+
radar_rotation(0.0),
74+
heat(0.0),
75+
76+
moving(0),
77+
base_turning(0),
78+
turret_turning(0),
79+
radar_turning(0),
80+
should_fire(false),
81+
fire_power(0)
82+
{};
83+
84+
void step();
85+
float acceleration();
86+
Bullet *fire();
87+
};
88+
89+
#endif

‎robots/engine_c/core.pxd‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,26 @@ cdef extern from "vec2.cpp":
2828
pass
2929

3030

31-
cdef extern from "bullet.h" nogil:
31+
cdef extern from "core.h" nogil:
3232
cdef cppclass Bullet:
3333
long owner_uid
34-
unsigned long longuid
34+
unsigned long uid
3535
Vec2 position, velocity
3636
float power
3737
Bullet()
3838
Bullet(long, Vec2, Vec2, float)
39-
void update()
39+
void step()
4040
bint operator>(Vec2)
41+
42+
cdef cppclass Robot:
43+
unsigned long uid
44+
Vec2 position
45+
float power, velocity
46+
Robot()
47+
void step()
48+
Bullet* fire()
4149

4250

43-
cdef extern from "bullet.cpp":
51+
cdef extern from "core.cpp":
4452
pass
4553

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /