Passed
Pull Request — master (#291)
by Marek
02:00
created

osci.res.getNA()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import bisect
22
import glob
23
import math
24
import os
25
import re
26
27
import gdata, client
28
import pygame, pygame.image
29
from osci import gdata
30
import ige.ospace.Const as Const
31
from ige.ospace import Rules
32
from ige import log
33
34
import resources
35
36
whiteShift = 80000
37
redShift = 90000
38
39
smallStarImgs = None
40
techImgs = None
41
bigStarImgs = None
42
planetImgs = None
43
planetImgCnt = None
44
buttonImgs = None
45
cmdInProgressImg = None
46
loginLogoImg = None
47
structProblemImg = None
48
structOffImg = None
49
icons = {}
50
ui_icons = {}
51
52
def initialize():
53
    # needed for progress dlg
54
    global loginLogoImg
55
    loginLogoImg = pygame.image.load(resources.get('logo-login.png')).convert_alpha()
56
57
def updateProgress(curr, progress_dlg):
58
    if not progress_dlg: return
59
    if curr % 30 == 0:
60
        periods = '.' * (curr / 30 % 4)
61
        progress_dlg.setProgress(_('Loading resources' + periods), curr)
62
63
64
65
def loadResources(progress_dlg=None):
66
    curr = 0
67
    max = len(glob.glob(resources.get('galaxy/*.png'))) + len(glob.glob(resources.get('techs/*.png'))) + \
68
        len(glob.glob(resources.get('system/*.png'))) + len(glob.glob(resources.get('icons/*.png'))) + len(glob.glob(resources.get('buttons/*.png')))
69
    if progress_dlg:
70
        progress_dlg.display(_('Loading resources'), 0, max)
71
    # load star imgs
72
    global smallStarImgs
73
    smallStarImgs = {}
74
    for filename in glob.glob(resources.get('galaxy/star_*.png')):
75
        name = re.search("star_([^.]+).png", filename).group(1)
76
        smallStarImgs[name] = pygame.image.load(filename).convert_alpha()
77
        curr += 1
78
        updateProgress(curr, progress_dlg)
79
    # load tech imgs
80
    global techImgs
81
    techImgs = {}
82
    white = pygame.Surface((37,37))
83
    white.fill((255, 255, 255))
84
    white.set_alpha(64)
85
    red = pygame.Surface((37,37))
86
    red.fill((255, 0, 0))
87
    red.set_alpha(64)
88
    for filename in glob.glob(resources.get('techs/????.png')):
89
        name = os.path.splitext(os.path.basename(filename))[0]
90
        imgID = int(name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable int does not seem to be defined.
Loading history...
91
        techImgs[imgID] = pygame.image.load(filename).convert_alpha()
92
        copyImg = techImgs[imgID].convert_alpha()
93
        copyImg.blit(white, (0,0))
94
        techImgs[imgID + whiteShift] = copyImg
95
        copyImg = techImgs[imgID].convert_alpha()
96
        copyImg.blit(red, (0,0))
97
        techImgs[imgID + redShift] = copyImg
98
        curr += 1
99
        updateProgress(curr, progress_dlg)
100
    # load big star imgs
101
    global bigStarImgs
102
    bigStarImgs = {}
103
    for filename in glob.glob(resources.get('system/star_*.png')):
104
        name = re.search("star_([^.]+).png", filename).group(1)
105
        bigStarImgs[name] = pygame.image.load(filename).convert_alpha()
106
        curr += 1
107
        updateProgress(curr, progress_dlg)
108
    # load planet images
109
    global planetImgs
110
    global planetImgCnt
111
    planetImgs = {}
112
    planetImgCnt = {}
113
    for filename in glob.glob(resources.get('system/planet_*.png')):
114
        matchobj = re.search("planet_((.)[^.]+).png", filename)
115
        name = matchobj.group(1)
116
        pltype = matchobj.group(2)
117
        if pltype in planetImgCnt:
118
            planetImgCnt[pltype] += 1
119
        else:
120
            planetImgCnt[pltype] = 1
121
        planetImgs[name] = pygame.image.load(filename).convert_alpha()
122
        curr += 1
123
        updateProgress(curr, progress_dlg)
124
    # load ship imgs
125
    global shipImgs
126
    shipImgs = {}
127
    for filename in glob.glob(resources.get('ships/??.png')):
128
        name = os.path.splitext(os.path.basename(filename))[0]
129
        shipImgs[int(name)] = pygame.image.load(filename).convert_alpha()
130
        curr += 1
131
        updateProgress(curr, progress_dlg)
132
    # load star imgs
133
    global icons
134
    icons = {}
135
    for filename in glob.glob(resources.get('icons/[!ui_]*.png')):
136
        name = os.path.splitext(os.path.basename(filename))[0]
137
        icons[name] = pygame.image.load(filename).convert_alpha()
138
        curr += 1
139
        updateProgress(curr, progress_dlg)
140
    # load UI icons
141
    global ui_icons
142
    ui_icons = {}
143
    for filename in glob.glob(resources.get('icons/ui_*.png')):
144
        name = os.path.splitext(os.path.basename(filename))[0]
145
        ui_icons[name] = pygame.image.load(filename).convert_alpha()
146
        curr += 1
147
        updateProgress(curr, progress_dlg)
148
    # load buttons
149
    global buttonImgs
150
    buttonImgs = {}
151
    for filename in glob.glob(resources.get('buttons/*.png')):
152
        name = os.path.splitext(os.path.basename(filename))[0]
153
        buttonImgs[name] = pygame.image.load(filename).convert_alpha()
154
        curr += 1
155
        updateProgress(curr, progress_dlg)
156
    # other icons
157
    global cmdInProgressImg
158
    cmdInProgressImg = pygame.image.load(resources.get('cmdInProgress.png')).convert_alpha()
159
    global structProblemImg
160
    structProblemImg = pygame.image.load(resources.get('struct_problem.png')).convert_alpha()
161
    global structOffImg
162
    structOffImg = pygame.image.load(resources.get('struct_off.png')).convert_alpha()
163
164
def prepareUIIcons(color):
165
    for image in ui_icons.values():
166
        image.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)
167
        image.fill(color[0:3] + (0,), None, pygame.BLEND_RGBA_ADD)
168
169
def getUIIcon(icon_name):
170
    return ui_icons["ui_" + str(icon_name)]
171
172
def getTechImg(techID):
173
    return techImgs.get(techID, techImgs[0])
174
175
def getShipImg(combatClass, isMilitary):
176
    return shipImgs.get(int(combatClass) * 10 + int(isMilitary), shipImgs[99])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable shipImgs does not seem to be defined.
Loading history...
177
178
def getSmallStarImg(name):
179
    return smallStarImgs[name]
180
181
def getBigStarImg(name):
182
    return bigStarImgs[name]
183
184
def getPlanetImg(pltype,plid):
185
    global planetImgCnt
186
    name = '%s%d' % (pltype,plid % planetImgCnt[pltype])
187
    return planetImgs[name]
188
189
def getButton(name,state):
190
    if state:
191
        name = "%s_%s" % (name,'active')
192
    else:
193
        name = "%s_%s" % (name,'inactive')
194
    return buttonImgs[name]
195
196
def getUnknownName():
197
    return _('[Unknown]')
198
199
def getNA():
200
    return _('N/A')
201
202
def getSystemOverviewProblemColor(owner,problem):
203
    if problem:
204
        return gdata.sevColors[gdata.CRI]
205
    else:
206
        return getPlayerColor(owner)
207
208
def getFFColorCode(relationship):
209
    if relationship == Const.REL_UNDEF:
210
        return (0xc0, 0xc0, 0xc0)
211
    if relationship == Const.REL_UNITY:
212
        return (0x00, 0xff, 0x00)
213
    relColors = [(0xff, 0x80, 0x80),
214
                 (0xff, 0x90, 0x01),
215
                 (0xff, 0xff, 0x00),
216
                 (0xb0, 0xb0, 0xff),
217
                 (0x80, 0xff, 0xff)]
218
    return relColors[bisect.bisect(Const.REL_BOUNDARIES, relationship)]
219
220
def getHabitabilityColorCode(bio):
221
    if bio < 0:
222
        return (0xff, 0x00, 0xff)
223
    colorCodes = [(255, 5*bio, 0), # end 255, 125, 0
224
                  (255-2*(bio-25), 125+2*(bio-25), 0), # end 155, 225, 0
225
                  (155-3*(bio-75), 225, 0), #end 5, 225, 0
226
                  (0, 255, 2*(bio-125)), #end 0, 225, 250
227
                  (0, 255, 255)]
228
    boundaries = [25,75,125,200]
229
    return colorCodes[bisect.bisect(boundaries, bio)]
230
231
def getPirateColonyColorCode(pirate):
232
    if pirate is False:
233
        return (0xc0, 0xc0, 0xc0)
234
    if pirate <= 0:
235
        return (0xff, 0x00, 0xff)
236
    colorCodes = [(255, 5*pirate, 0), # end 255, 125, 0
237
                  (255-2*(pirate-25), 125+2*(pirate-25), 0), # end 155, 225, 0
238
                  (155-3*(pirate-75), 225, 0), #end 5, 225, 0
239
                  (0, 255, 2*(pirate-125)), #end 0, 225, 250
240
                  (0, 255, 255)]
241
    boundaries = [25,75,125,200]
242
    return colorCodes[bisect.bisect(boundaries, pirate)]
243
244
def getFameColorCode(fame):
245
    if fame > 0 and fame < 200:
246
        #log.debug(fame,(0xff,255 - int(255*(fame/200)),0x00))
247
        return (0xff,255 - int(255*(fame/200.0)),0x00)
248
    if fame == 200:
249
        return (0xff,0x00,0xff) #pirate colony
250
    return (0xc0, 0xc0, 0xc0)
251
252
def getMineralColorCode(minerals):
253
    if minerals >= 0:
254
        return (0xff,max(0,min(255,255 - int(255*(minerals/200.0)))),0x0) #use min, since it we occasionally get 201+ mineral levels, but it is so rare that we can ignore it for colors.
255
    return (0xc0, 0xc0, 0xc0)
256
257
def getSlotColorCode(slots):
258
    if slots > 20:
259
        return (0x0,0xFF,min(255,(slots-20)*10)) #in case sometime in the future we have larger worlds available
260
    if slots > 0:
261
        return (int(255*(slots/20.0)),255 - int(255*(slots/20.0)),0x0)
262
    return (0xc0, 0xc0, 0xc0)
263
264
def getSlotSystemColorCode(slots,planets):
265
    if planets > 0:
266
        slots = int(float(slots)/planets)
267
        if slots > 20:
268
            return (0x0,0xFF,min(255,(slots-20)*10)) #in case sometime in the future we have larger worlds available
269
        if slots > 0:
270
            return (int(255*(slots/20.0)),255 - int(255*(slots/20.0)),0x0)
271
        return (0xc0, 0xc0, 0xc0)
272
    else:
273
        return (0xc0, 0xc0, 0xc0)
274
275
276
def getStargateColorCode(accel):
277
    accel = accel * 100 - 100
278
    if accel < 1:
279
        return (0xc0, 0xc0, 0xc0)
280
    if accel < 50:
281
        return (0xff, 0xff, 0x00)
282
    if accel < 150:
283
        return (0xff, 0xc0, 0x00)
284
    if accel < 250:
285
        return (0xff, 0x60, 0x00)
286
    if accel < 350:
287
        return (0xff, 0x00, 0x00)
288
    if accel < 450:
289
        return (0xff, 0x00, 0x80)
290
    if accel > 449:
291
        return (0xff, 0x00, 0xff)
292
    return (0xc0, 0xc0, 0xc0)
293
294
def getDockColorCode(refuel,upgrades): #refuel is based on best dock; upgrades are based on sum of all docks
295
    #refuel range: 0...6
296
    #upgrade range: 0...100 (cap 100)
297
    if (refuel > 1 and upgrades > 0) or (refuel > 2): #refuel > 2 for other player docks since they always read upgrades of 0; this probably should be corrected for allies
298
        refuelScale = max(0,min(1,(refuel-1)/5.0))
299
        upgradeScale = max(0,min(1,upgrades/50))
300
        return (0xFF,int(refuelScale*(1-upgradeScale)*255),int(refuelScale*(upgradeScale)*255))
301
    if refuel > 0:
302
        refuelScale = max(0,min(1,refuel/2.0))
303
        return (0x00,100+100*int(refuelScale),100+100*int(refuelScale)) # cyan
304
    return (0xc0, 0xc0, 0xc0)
305
306
def getMoraleColors(morale):
307
    if morale >= 0:
308
        return (255-int(255*(morale/100.0)),int(255*(morale/100.0)),0x0)
309
    return (0xc0, 0xc0, 0xc0)
310
311
def getPlayerColor(owner, onlyDiplo = False):
312
    if owner == Const.OID_NONE:
313
        return getFFColorCode(Const.REL_UNDEF)
314
    if not onlyDiplo:
315
        if gdata.config.defaults.highlights == 'yes':
316
            if gdata.playersHighlightColors.has_key(owner):
317
                return gdata.playersHighlightColors[owner]
318
    rel = min(Const.REL_UNDEF,client.getRelationTo(owner))
319
    return getFFColorCode(rel)
320
321
def getControlColor(owner, onlyDiplo = False):
322
    if owner == Const.OID_NONE:
323
        return False
324
    if not onlyDiplo:
325
        if gdata.config.defaults.highlights == 'yes':
326
            if gdata.playersHighlightColors.has_key(owner):
327
                return fadeDarkColor(gdata.playersHighlightColors[owner])
328
    rel = min(Const.REL_UNDEF,client.getRelationTo(owner))
329
    return fadeDarkColor(getFFColorCode(rel))
330
331
def getGateLineWidth(owner):
332
    if owner == Const.OID_NONE:
333
        return 1
334
    rel = min(Const.REL_UNDEF,client.getRelationTo(owner))
335
    if rel == 1250:
336
        return 2
337
    return 1
338
339
def getStarmapWidgetPlanetColor(ownerid,bio,mineral,slot,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
340
    colors = {}
341
    for datatype in gdata.OVERLAY_TYPES:
342
        colors[datatype] = getStarmapWidgetPlanetColorPerDatatype(datatype,ownerid,bio,mineral,slot,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate)
343
    return colors
344
345
def getStarmapWidgetSystemColor(ownerid,bio,mineral,slot,num_planets,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
346
    colors = {}
347
    for datatype in gdata.OVERLAY_TYPES:
348
        colors[datatype] = getStarmapWidgetSystemColorPerDatatype(datatype,ownerid,bio,mineral,slot,num_planets,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate)
349
    return colors
350
351
352 View Code Duplication
def getStarmapWidgetPlanetColorPerDatatype(datatype,ownerid,bio,mineral,slot,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
353
    if (datatype == gdata.OVERLAY_OWNER):
354
        return getPlayerColor(ownerid)
355
    if (datatype == gdata.OVERLAY_DIPLO):
356
        return getPlayerColor(ownerid,True)
357
    if (datatype == gdata.OVERLAY_BIO):
358
        return getHabitabilityColorCode(bio)
359
    if (datatype == gdata.OVERLAY_MIN):
360
        return getMineralColorCode(mineral)
361
    if (datatype == gdata.OVERLAY_SLOT):
362
        return getSlotColorCode(slot)
363
    if (datatype == gdata.OVERLAY_STARGATE):
364
        return getStargateColorCode(stargate)
365
    if (datatype == gdata.OVERLAY_DOCK):
366
        return getDockColorCode(dockfuel,dockupgrade)
367
    if (datatype == gdata.OVERLAY_FAME):
368
        return getFameColorCode(fame)
369
    if (datatype == gdata.OVERLAY_PIRATECOLONYCOST):
370
        return getPirateColonyColorCode(pirate)
371
#    if (datatype == "stratres"):
372
#        return getMoraleColors(stratres)
373
    if (datatype == gdata.OVERLAY_MORALE):
374
        return getMoraleColors(morale)
375
    return getPlayerColor(ownerid) #default
376
377
378 View Code Duplication
def getStarmapWidgetSystemColorPerDatatype(datatype,ownerid,bio,mineral,slot,num_planets,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
379
    if (datatype == gdata.OVERLAY_OWNER):
380
        return getPlayerColor(ownerid)
381
    if (datatype == gdata.OVERLAY_DIPLO):
382
        return getPlayerColor(ownerid,True)
383
    if (datatype == gdata.OVERLAY_BIO):
384
        return getHabitabilityColorCode(bio)
385
    if (datatype == gdata.OVERLAY_MIN):
386
        return getMineralColorCode(mineral)
387
    if (datatype == gdata.OVERLAY_SLOT):
388
        return getSlotSystemColorCode(slot,num_planets)
389
    if (datatype == gdata.OVERLAY_STARGATE):
390
        return getStargateColorCode(stargate)
391
    if (datatype == gdata.OVERLAY_DOCK):
392
        return getDockColorCode(dockfuel,dockupgrade)
393
    if (datatype == gdata.OVERLAY_FAME):
394
        return getFameColorCode(fame)
395
    if (datatype == gdata.OVERLAY_PIRATECOLONYCOST):
396
        return getPirateColonyColorCode(pirate)
397
#    if (datatype == "stratres"):
398
#        return getMoraleColors(stratres)
399
    if (datatype == gdata.OVERLAY_MORALE):
400
        return getMoraleColors(morale)
401
    return getPlayerColor(ownerid) #default
402
403
def fadeColor(triplet):
404
    return ((triplet[0]+0xc0)/2,(triplet[1]+0xc0)/2,(triplet[2]+0xc0)/2)
405
406
def fadeDarkColor(triplet):
407
    return ((triplet[0]+0x00*2)/3,(triplet[1]+0x00*2)/3,(triplet[2]+0x00*2)/3)
408
409
def formatTime(time,separator=':'):
410
    time = int(math.ceil(time))
411
    sign = ''
412
    if time < 0:
413
        time = - time
414
        sign = '-'
415
    days = time / Rules.turnsPerDay
416
    hours = time % Rules.turnsPerDay
417
    return '%s%d%s%02d' % (sign, days, separator, hours)
418
419
def formatBE(b, e):
420
    return '%d / %d' % (b, e)
421
422
def globalQueueName(index):
423
    return ['Default', 'Red', 'Blue', 'Yellow', 'Violet'][index]
424