-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcivilization.py
166 lines (143 loc) · 6.45 KB
/
civilization.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import random
import star as stars
# from main import stafflist, civnum, dist
class civ(object):
def __init__(self, civid, starnum):
from main import civnum
self.live = 1 # 1存活0死亡
self.id = civid # 文明编号
self.state = random.random() / 2 # 科技水平,0为最低2为最高
self.speed = random.random() / 20 # 发展速度,遇到其他文明有可能增大
self.stars_in_sight = [starnum] # 观测的星系表
self.stars_not_seen = list(range(stars.starnum))
self.stars_not_seen.remove(starnum) # 没观测的星系表
self.stars_landed = [starnum] # 占领的星系表
self.stars_without_civ = [] # 未占领的无文明星系表
self.stars_with_civ = [] # 观测到有其他文明星系表
self.civ_without_communication = list(range(civnum))
self.civ_without_communication.remove(self.id) # 未接触的文明
self.civ_trap_target = self.civ_without_communication
self.civ_with_communication = [] # 建立友好交流的文明
# self.population = 0.2 # 人口,超过1会移民
# self.develop = 0.1*len(self.star) # 文明人口增长速度
self.character = random.randint(1, 3) # 1为好斗,2为和平,3为静默
self.staff = [] # 正在做的事项,格式['动作',对象,时间(距离),其他],如['immigrate',12,10],指正在移民12号星系,将在10百年后完成
stars.maplist[starnum]['owner'] = self.id
pass
pass
def move(self):
from main import stafflist, civs
if self.live == 1:
for i in self.staff:
# print(1, self.id, self.staff, i)
if i[2] <= 1:
self.event(i[0], i[1], i[2:-1])
self.staff.remove(i)
else:
# print(2, self.id, self.staff, i)
i[2] -= 1
pass
pass
if self.character == 1:
if random.random() > 0.7:
self.trap()
pass
pass
self.state += self.speed
if self.state > 2:
self.state = 2
self.search()
elif self.live == 0:
for i in civs:
for star in self.stars_landed:
stars.maplist[star]['owner'] = None
if star in civs[i].stars_with_civ:
civs[i].stars_with_civ.remove(star)
for civ_staff in civs[i].staff:
if civ_staff[1] == star:
civs[i].staff.remove(civ_staff)
if self.id in civs[i].civ_without_communication:
civs[i].civ_without_communication.remove(self.id)
if self.id in civs[i].civ_trap_target:
civs[i].civ_trap_target.remove(self.id)
if self.id in civs[i].civ_with_communication:
civs[i].civ_with_communication.remove(self.id)
for i in stafflist:
if i[2] == self.id or i[1] == self.id:
stafflist.remove(i)
pass
pass
pass
pass
# def immigrate(self):
#
# pass
def search(self):
distancelist = stars.distto(self.id, self.stars_not_seen)
for i in range(min(max(1, int(self.state * 10)), len(self.stars_not_seen))):
self.staff.append(['search', distancelist[i][0], distancelist[i][1]])
self.stars_not_seen.remove(distancelist[i][0])
pass
pass
def trap(self):
if self.civ_trap_target:
from main import dist, civiposlist
from main import civs
target_id = random.sample(self.civ_trap_target, 1)[-1]
self.staff.append(['trap', civiposlist[target_id], dist(self.id, target_id)])
self.civ_trap_target.remove(target_id)
# print('self:', self.id, 'target_id:', target_id, 'civ_without_communication:', self.civ_without_communication, civs.keys())
pass
def reply(self, movement, target_id):
from main import civs
from main import stafflist
if self.character == 2:
if movement == 'communicate':
stafflist.append(('re_communicate', self.id, target_id))
elif movement == 'trap':
times = 0
print('re_trap', self.stars_with_civ, civs[target_id].stars_landed)
for star in civs[target_id].stars_landed:
if star in self.stars_with_civ:
times += 1
pass
if times == 0:
stafflist.append(('re_trap', self.id, target_id))
pass
pass
pass
pass
def event(self, event, target_pos, others=None):
from main import stafflist
if event == 'search':
self.stars_in_sight.append(target_pos)
if stars.maplist[target_pos]['owner'] is None:
self.stars_without_civ.append(target_pos)
else:
self.stars_with_civ.append(target_pos)
# print(stars.maplist[target]['owner'], self.nocommunication)
if stars.maplist[target_pos]['owner'] in self.civ_without_communication:
self.civ_without_communication.remove(stars.maplist[target_pos]['owner'])
self.stars_with_civ.append(stars.maplist[target_pos]['owner'])
if stars.maplist[target_pos]['owner'] in self.civ_trap_target:
self.civ_trap_target.remove(stars.maplist[target_pos]['owner'])
if self.character == 1:
self.staff.append(['attack', target_pos, stars.distance(self.id, target_pos)])
pass
elif self.character == 2:
self.staff.append(['communicate', target_pos, stars.distance(self.id, target_pos)])
pass
pass
pass
pass
elif event == 'immigrate':
stars.maplist[target_pos]['owner'] = self.id
pass
elif event == 'attack':
stafflist.append(('attack', self.id, stars.maplist[target_pos]['owner'], self.state))
pass
elif event == 'trap':
stafflist.append(('trap', self.id, stars.maplist[target_pos]['owner'], self.state))
pass
pass
pass