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 749667f

Browse files
committed
Added Cython implementation of engine
1 parent 5775e52 commit 749667f

File tree

13 files changed

+9596
-15
lines changed

13 files changed

+9596
-15
lines changed

‎engine_test.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from robots.engine_c.engine import *
2+
from robots.app import App, Battle
3+
import random
4+
5+
import time
6+
# app = App()
7+
8+
class RandomRobot(Robot):
9+
def run(self):
10+
self.moving = 1
11+
self.base_turning = 1
12+
self.turret_turning = -1
13+
if random.randint(0, 1):
14+
self.fire(random.randint(1, 3))
15+
16+
robots = [RandomRobot((255, 0, 0)), RandomRobot((0, 255, 0))]
17+
eng = Engine(robots=robots)
18+
19+
# app.child = Battle(robots, (600, 400), eng=eng)
20+
# app.child.set_tick_rate(-1)
21+
# app.run()
22+
23+
24+
print(eng)
25+
eng.step()
26+
print([(b.uid, b.owner_uid) for b in eng.bullets])
27+
28+
29+
start = time.perf_counter_ns()
30+
for i in range(int(1e6)):
31+
eng.step()
32+
33+
duration = (time.perf_counter_ns() - start)/1e9
34+
print(100000/(duration), duration)
35+
print(eng.get_bullets())
36+

‎main.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from robots.app import App, Battle
22
from robots.robot import Robot
33
from robots.robot.utils import *
4+
from robots.engine import engine
45
import random
56

7+
8+
engine.Timer.should_print = True
69
app = App()
710

811

‎robots/engine/engine.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def energy_on_hit(bullet):
9292
class Timer(object):
9393
times = defaultdict(lambda : deque(maxlen=1000))
9494
next_print = time.perf_counter() + 1
95+
should_print = False
96+
9597
@classmethod
9698
def wrap(cls, func):
9799
def inner(*args, **kwargs):
@@ -105,7 +107,7 @@ def inner(*args, **kwargs):
105107

106108
@classmethod
107109
def print(cls, name):
108-
if time.perf_counter() > cls.next_print:
110+
if cls.should_print& (time.perf_counter() > cls.next_print):
109111
t = np.mean(cls.times[name])/10e9
110112
print(f"\rAVG Time: {t:9.9f}, FPS: {1/t:010.2f}, Len: {len(cls.times[name]):3.0f}",end='', flush=True)
111113
cls.next_print = time.perf_counter() + 1

‎robots/engine_c/__init__.py‎

Whitespace-only changes.

‎robots/engine_c/bullet.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <bullet.h>
2+
3+
void Bullet::update()
4+
{
5+
position += velocity;
6+
};
7+
8+
bool Bullet::operator>(const Bullet &other) const
9+
{
10+
return uid > other.uid;
11+
};
12+
13+
bool Bullet::operator<(const Bullet &other) const
14+
{
15+
return uid < other.uid;
16+
};

‎robots/engine_c/bullet.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef BULLET_H
2+
#define BULLET_H
3+
4+
#include <ostream>
5+
#include <vec2.h>
6+
7+
unsigned long long NUMBER_BULLETS = 0;
8+
9+
struct Bullet
10+
{
11+
unsigned long long uid;
12+
long owner_uid;
13+
Vec2 position;
14+
Vec2 velocity;
15+
float power;
16+
17+
Bullet()
18+
: uid(NUMBER_BULLETS += 1),
19+
owner_uid(NULL),
20+
position(Vec2(0.0, 0.0)),
21+
velocity(Vec2(0.0, 0.0)),
22+
power(0){};
23+
Bullet(long owner_uid, Vec2 position, Vec2 velocity, float power)
24+
: uid(NUMBER_BULLETS += 1),
25+
owner_uid(owner_uid),
26+
position(position),
27+
velocity(velocity),
28+
power(power){};
29+
void update();
30+
31+
bool operator>(const Bullet &other) const;
32+
bool operator<(const Bullet &other) const;
33+
};
34+
35+
#endif

‎robots/engine_c/core.pxd‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# distutils: language = c++
2+
3+
cimport cython
4+
from libc.math cimport sin, cos, abs, pi, pow
5+
from libcpp.set cimport set
6+
from libc.stdlib cimport malloc, free
7+
from uuid import uuid4
8+
9+
10+
cdef extern from "vec2.h" nogil:
11+
cdef cppclass Vec2:
12+
float x,y
13+
Vec2() except +
14+
Vec2(float, float) except +
15+
@staticmethod
16+
Vec2 random(float, float)
17+
Vec2 pow(float)
18+
float sum()
19+
void clip(Vec2, Vec2)
20+
void clip(float,float,float,float)
21+
Vec2 operator+(Vec2)
22+
Vec2 operator-(Vec2)
23+
Vec2 operator*(float)
24+
# Vec2 &operator+=(Vec2)
25+
26+
27+
cdef extern from "vec2.cpp":
28+
pass
29+
30+
31+
cdef extern from "bullet.h" nogil:
32+
cdef cppclass Bullet:
33+
long owner_uid
34+
unsigned long long uid
35+
Vec2 position, velocity
36+
float power
37+
Bullet()
38+
Bullet(long, Vec2, Vec2, float)
39+
void update()
40+
bint operator>(Vec2)
41+
42+
43+
cdef extern from "bullet.cpp":
44+
pass
45+

0 commit comments

Comments
(0)

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